UI: Allow displaying grid views in popups
The optimization to only add grid view items to the layout that are actually in view relied on View2D information on scrolling. Popups don't use View2D, so with this change the optimization will be skipped when View2D is not used. Necessary for the brush assets project which adds a popup version of the asset shelf, see #116337.
This commit is contained in:
@@ -205,12 +205,12 @@ GridViewItemDropTarget::GridViewItemDropTarget(AbstractGridView &view) : view_(v
|
||||
* side(s) as well.
|
||||
*/
|
||||
class BuildOnlyVisibleButtonsHelper {
|
||||
const View2D &v2d_;
|
||||
const AbstractGridView &grid_view_;
|
||||
const GridViewStyle &style_;
|
||||
const int cols_per_row_ = 0;
|
||||
/* Indices of items within the view. Calculated by constructor */
|
||||
IndexRange visible_items_range_{};
|
||||
/* Indices of items within the view. Calculated by constructor. If this is unset it means all
|
||||
* items/buttons should be drawn. */
|
||||
std::optional<IndexRange> visible_items_range_;
|
||||
|
||||
public:
|
||||
BuildOnlyVisibleButtonsHelper(const View2D &v2d,
|
||||
@@ -222,30 +222,34 @@ class BuildOnlyVisibleButtonsHelper {
|
||||
void fill_layout_after_visible(uiBlock &block) const;
|
||||
|
||||
private:
|
||||
IndexRange get_visible_range() const;
|
||||
IndexRange get_visible_range(const View2D &v2d) const;
|
||||
void add_spacer_button(uiBlock &block, int row_count) const;
|
||||
};
|
||||
|
||||
BuildOnlyVisibleButtonsHelper::BuildOnlyVisibleButtonsHelper(const View2D &v2d,
|
||||
const AbstractGridView &grid_view,
|
||||
const int cols_per_row)
|
||||
: v2d_(v2d), grid_view_(grid_view), style_(grid_view.get_style()), cols_per_row_(cols_per_row)
|
||||
: grid_view_(grid_view), style_(grid_view.get_style()), cols_per_row_(cols_per_row)
|
||||
{
|
||||
visible_items_range_ = this->get_visible_range();
|
||||
if ((v2d.flag & V2D_IS_INIT) && grid_view.get_item_count_filtered()) {
|
||||
visible_items_range_ = this->get_visible_range(v2d);
|
||||
}
|
||||
}
|
||||
|
||||
IndexRange BuildOnlyVisibleButtonsHelper::get_visible_range() const
|
||||
IndexRange BuildOnlyVisibleButtonsHelper::get_visible_range(const View2D &v2d) const
|
||||
{
|
||||
BLI_assert(v2d.flag & V2D_IS_INIT);
|
||||
|
||||
int first_idx_in_view = 0;
|
||||
|
||||
const float scroll_ofs_y = std::abs(v2d_.cur.ymax - v2d_.tot.ymax);
|
||||
const float scroll_ofs_y = std::abs(v2d.cur.ymax - v2d.tot.ymax);
|
||||
if (!IS_EQF(scroll_ofs_y, 0)) {
|
||||
const int scrolled_away_rows = int(scroll_ofs_y) / style_.tile_height;
|
||||
|
||||
first_idx_in_view = scrolled_away_rows * cols_per_row_;
|
||||
}
|
||||
|
||||
const int view_height = BLI_rcti_size_y(&v2d_.mask);
|
||||
const int view_height = BLI_rcti_size_y(&v2d.mask);
|
||||
const int count_rows_in_view = std::max(view_height / style_.tile_height, 1);
|
||||
const int max_items_in_view = (count_rows_in_view + 1) * cols_per_row_;
|
||||
|
||||
@@ -255,12 +259,15 @@ IndexRange BuildOnlyVisibleButtonsHelper::get_visible_range() const
|
||||
|
||||
bool BuildOnlyVisibleButtonsHelper::is_item_visible(const int item_idx) const
|
||||
{
|
||||
return visible_items_range_.contains(item_idx);
|
||||
return !visible_items_range_ || visible_items_range_->contains(item_idx);
|
||||
}
|
||||
|
||||
void BuildOnlyVisibleButtonsHelper::fill_layout_before_visible(uiBlock &block) const
|
||||
{
|
||||
const int first_idx_in_view = visible_items_range_.first();
|
||||
if (!visible_items_range_ || visible_items_range_->is_empty()) {
|
||||
return;
|
||||
}
|
||||
const int first_idx_in_view = visible_items_range_->first();
|
||||
if (first_idx_in_view < 1) {
|
||||
return;
|
||||
}
|
||||
@@ -271,8 +278,11 @@ void BuildOnlyVisibleButtonsHelper::fill_layout_before_visible(uiBlock &block) c
|
||||
|
||||
void BuildOnlyVisibleButtonsHelper::fill_layout_after_visible(uiBlock &block) const
|
||||
{
|
||||
if (!visible_items_range_ || visible_items_range_->is_empty()) {
|
||||
return;
|
||||
}
|
||||
const int last_item_idx = grid_view_.get_item_count_filtered() - 1;
|
||||
const int last_visible_idx = visible_items_range_.last();
|
||||
const int last_visible_idx = visible_items_range_->last();
|
||||
|
||||
if (last_item_idx > last_visible_idx) {
|
||||
const int remaining_rows = (cols_per_row_ > 0) ? ceilf((last_item_idx - last_visible_idx) /
|
||||
|
||||
Reference in New Issue
Block a user