29#define UI_TREEVIEW_INDENT short(0.7f * UI_UNIT_X)
45 std::unique_ptr<AbstractTreeViewItem> item)
50 if (
root_ ==
nullptr) {
83 child->foreach_item_recursive(iter_fn,
options);
97 [](
const std::unique_ptr<AbstractTreeViewItem> &a,
98 const std::unique_ptr<AbstractTreeViewItem> &
b) {
104 for (std::unique_ptr<AbstractTreeViewItem> &item :
children_) {
114 child->foreach_sort_invert(order);
148 if (!first_visible_item) {
149 first_visible_item = &item;
154 return active_item ? active_item : first_visible_item;
160 if (!tree_item->is_collapsible() || tree_item->is_collapsed()) {
161 return tree_item->get_parent();
163 tree_item->set_collapsed(
true);
170 if (!active_item->is_collapsible() || active_item->is_collapsed()) {
171 active_item->set_collapsed(
false);
174 return active_item->get_child();
180 bool found_active =
false;
183 found_active |= item.is_active();
190 return found_active ? next_item : from;
196 bool found_active =
false;
202 found_active =
false;
204 found_active = item.is_active();
207 return next_item ? next_item : from;
216 const int visible_rows = this->tot_visible_row_count().value_or(0);
220 *this->scroll_value_ = std::max(0, *this->scroll_value_ - visible_rows);
223 *this->scroll_value_ = std::min(this->last_tot_items_, *this->scroll_value_ + visible_rows);
226 *this->scroll_value_ = 0;
229 *this->scroll_value_ = this->last_tot_items_;
237 "Default value is smaller than the minimum rows. Limit is required to prevent "
238 "resizing below specific height.");
251 if (!custom_height_ && !scroll_value_) {
255 if (custom_height_) {
259 state.scroll_offset = *scroll_value_;
267 if (
state.custom_height) {
271 if (
state.scroll_offset) {
272 scroll_value_ = std::make_shared<int>(
state.scroll_offset);
288 for (
const auto &item : parent.
children_) {
289 if (!item->is_filtered_visible()) {
293 count += count_visible_descendants(*item);
299void AbstractTreeView::get_hierarchy_lines(
const TreeViewOrItem &parent,
301 Vector<std::pair<int2, int2>> &lines,
302 int &visible_item_index)
const
304 const int scroll_ofs = scroll_value_ ? *scroll_value_ : 0;
305 const int max_visible_row_count = tot_visible_row_count().value_or(
306 std::numeric_limits<int>::max());
308 for (
const auto &item : parent.children_) {
309 if (!item->is_filtered_visible()) {
313 const int item_index = visible_item_index;
314 visible_item_index++;
316 if (!item->is_collapsible() || item->is_collapsed()) {
319 if (item->children_.is_empty()) {
327 const int descendant_count = count_visible_descendants(*item);
329 const int first_descendant_index = item_index + 1;
330 const int last_descendant_index = item_index + descendant_count;
333 const bool line_ends_above_visible = last_descendant_index < scroll_ofs;
334 if (line_ends_above_visible) {
337 visible_item_index = last_descendant_index + 1;
341 const bool line_starts_below_visible = first_descendant_index >
342 (scroll_ofs + long(max_visible_row_count));
344 if (line_starts_below_visible) {
352 const int ymax = std::max(0, first_descendant_index - scroll_ofs) *
padded_item_height() /
354 const int ymin = std::min(max_visible_row_count, last_descendant_index + 1 - scroll_ofs) *
358 this->get_hierarchy_lines(*item, aspect, lines, visible_item_index);
362void AbstractTreeView::sort_inverted()
372 return *invert_sort_type_;
381 if (&view_item_but->view_item->get_view() == &view) {
382 return view_item_but;
388void AbstractTreeView::draw_hierarchy_lines(
const ARegion ®ion,
const Block &block)
const
396 if (!first_item_but) {
400 Vector<std::pair<int2, int2>> lines;
402 get_hierarchy_lines(*
this, aspect, lines,
index);
403 if (lines.is_empty()) {
415 rcti first_item_but_pixel_rect;
417 int2 top_left{first_item_but_pixel_rect.xmin, first_item_but_pixel_rect.ymax};
419 for (
const auto &line : lines) {
421 immVertex2f(pos, top_left.
x + line.first.x, top_left.
y - line.first.y);
422 immVertex2f(pos, top_left.
x + line.second.x, top_left.
y - line.second.y);
432 this->draw_hierarchy_lines(region, block);
435void AbstractTreeView::update_children_from_old(
const AbstractView &old_view)
439 custom_height_ = old_tree_view.custom_height_;
440 scroll_value_ = old_tree_view.scroll_value_;
441 search_string_ = old_tree_view.search_string_;
442 show_display_options_ = old_tree_view.show_display_options_;
443 sort_alpha_ = old_tree_view.sort_alpha_;
444 invert_sort_type_ = old_tree_view.invert_sort_type_;
445 invert_search_filter_ = old_tree_view.invert_search_filter_;
446 update_children_from_old_recursive(*
this, old_tree_view);
449void AbstractTreeView::update_children_from_old_recursive(
const TreeViewOrItem &new_items,
455 for (
const auto &old_item : old_items.children_) {
456 old_children_by_label.
add(old_item->label_, old_item.get());
459 for (
const auto &new_item : new_items.children_) {
463 possible_old_children);
464 if (!matching_old_item) {
468 new_item->update_from_old(*matching_old_item);
471 update_children_from_old_recursive(*new_item, *matching_old_item);
478 for (
auto *iter_item : possible_items) {
479 if (lookup_item.matches(*iter_item)) {
488std::optional<int> AbstractTreeView::tot_visible_row_count()
const
490 if (!custom_height_) {
500 return custom_height_ && scroll_value_;
505 return this->tot_visible_row_count().value_or(0) >= last_tot_items_;
510 if (!supports_scrolling()) {
520 const std::optional<int> visible_row_count = tot_visible_row_count();
522 if (!custom_height_) {
526 if (!visible_row_count.has_value()) {
530 if (!scroll_value_) {
531 scroll_value_ = std::make_unique<int>(0);
536 if (scroll_active_to_center) {
537 *scroll_value_ = std::clamp(
538 index - (*visible_row_count - 1) / 2, 0, (last_tot_items_ - *visible_row_count));
541 if (
index < *scroll_value_) {
542 *scroll_value_ =
index;
545 if (
index > (*scroll_value_ + *visible_row_count - 1)) {
546 *scroll_value_ = std::max(0,
index - *visible_row_count + 1);
579 const int segment_count =
585 const float segment_height = item_height / segment_count;
587 if (event.
xy[1] < win_rect->ymin) {
590 if (event.
xy[1] - win_rect->ymin > (item_height - segment_height)) {
593 if (event.
xy[1] - win_rect->ymin <= segment_height) {
615void AbstractTreeViewItem::add_treerow_button(
Block &block)
633int AbstractTreeViewItem::indent_width()
const
638void AbstractTreeViewItem::add_indent(Layout &row)
const
640 Block *block = row.block();
641 Layout &subrow = row.row(
true);
642 subrow.fixed_size_set(
true);
644 uiDefBut(block,
ButtonType::Sepr,
"", 0, 0, this->indent_width(), 0,
nullptr, 0.0, 0.0,
"");
650 uiDefBut(block,
ButtonType::Sepr,
"", 0, 0,
UI_TREEVIEW_INDENT, 0,
nullptr, 0.0, 0.0,
"");
657void AbstractTreeViewItem::collapse_chevron_click_fn(bContext *
C,
668 *region, win->runtime->eventstate->xy);
673 hovered_item->toggle_collapsed_from_view(*
C);
676 if (hovered_item->has_active_child()) {
677 hovered_item->activate(*
C);
681void AbstractTreeViewItem::add_collapse_chevron(
Block &block)
const
689 &block,
ButtonType::ButToggle, icon, 0, 0,
UI_TREEVIEW_INDENT,
UI_UNIT_Y,
nullptr, 0, 0,
"");
696void AbstractTreeViewItem::add_rename_button(
Layout &row)
698 Block *block = row.block();
711bool AbstractTreeViewItem::has_active_child()
const
715 if (item.is_active()) {
746 is_open_ = old_tree_item.is_open_;
810 if (child->is_filtered_visible()) {
831 "State cannot be queried until reconstruction is completed");
852 if (collapsed == !is_open_) {
856 is_open_ = !collapsed;
873 "Default state should only be set while building the tree");
882 "State can't be queried until reconstruction is completed");
894 const bool prev_active_state =
is_active();
898 this->
get_tree_view().scroll_active_into_view_on_draw_ =
true;
912 parent->set_collapsed(
false);
928 parent && other_parent;
931 if (!parent->matches_single(*other_parent)) {
958class TreeViewLayoutBuilder {
960 bool add_box_ =
true;
962 friend TreeViewBuilder;
973 TreeViewLayoutBuilder(
Layout &layout);
976TreeViewLayoutBuilder::TreeViewLayoutBuilder(
Layout &layout) : block_(*layout.block()) {}
998 Layout &col = (add_box_ ? parent_layout.
box() : parent_layout).
column(
true);
1003 const std::optional<int> visible_row_count = tree_view.tot_visible_row_count();
1005 tree_view.last_tot_items_ = tot_items;
1011 if (tree_view.scroll_value_ && visible_row_count) {
1012 *tree_view.scroll_value_ = std::clamp(
1013 *tree_view.scroll_value_, 0, tot_items - *visible_row_count);
1016 if (tree_view.scroll_active_into_view_on_draw_) {
1020 const int first_visible_index = tree_view.scroll_value_ ? *tree_view.scroll_value_ : 0;
1021 const int max_visible_index = visible_row_count ? first_visible_index + *visible_row_count - 1 :
1022 std::numeric_limits<int>::max();
1026 if ((
index >= first_visible_index) && (
index <= max_visible_index)) {
1035 if (tree_view.custom_height_) {
1038 if (!tree_view.scroll_value_) {
1039 tree_view.scroll_value_ = std::make_unique<int>(0);
1043 if (visible_row_count && (tot_items > *visible_row_count)) {
1051 *tree_view.custom_height_,
1052 tree_view.scroll_value_.get(),
1054 tot_items - *visible_row_count,
1068 ICON_DISCLOSURE_TRI_DOWN,
1073 tree_view.show_display_options_.get(),
1088 tree_view.custom_height_.get(),
1093 if (*tree_view.show_display_options_) {
1103 tree_view.search_string_.get(),
1116 ICON_ARROW_LEFTRIGHT,
1121 tree_view.invert_search_filter_.get(),
1124 TIP_(
"Invert search results (Show items that don't match the search string)"));
1136 tree_view.sort_alpha_.get(),
1139 TIP_(
"Sort items alphabetically"));
1142 int icon = ICON_SORT_DESC;
1143 switch (*tree_view.invert_sort_type_) {
1145 icon = ICON_DOWNARROW_HLT;
1148 icon = ICON_SORT_ASC;
1164 TIP_(
"Reverse the order of items"));
1182 const int width = prev_layout.
width();
1201 item.add_treerow_button(block_);
1209 if (margin_top > 0) {
1210 uiDefBut(&block_,
ButtonType::Label,
"", 0, 0,
UI_UNIT_X, margin_top,
nullptr, 0, 0,
"");
1212 row = &content_col.
row(
true);
1215 item.add_indent(*row);
1216 item.add_collapse_chevron(block_);
1219 item.add_rename_button(*row);
1241 return *
block().curlayout;
1248 const std::optional<int> visible_rows = tree_view.tot_visible_row_count();
1249 if (!visible_rows) {
1253 int tot_visible_items = 0;
1255 [&tot_visible_items](AbstractTreeViewItem & ) { tot_visible_items++; },
1258 if (tot_visible_items >= *visible_rows) {
1262 for (
int i = 0; i < (*visible_rows - tot_visible_items); i++) {
1263 BasicTreeViewItem &new_item = tree_view.
add_tree_item<BasicTreeViewItem>(
"");
1264 new_item.disable_interaction();
1284 if (*tree_view.sort_alpha_) {
1297 tree_view.sort_inverted();
1303 tree_view.
filter(tree_view.search_string_ ? std::optional{string} : std::nullopt);
1305 ensure_min_rows_items(tree_view);
1311 builder.add_box_ = add_box;
1335void BasicTreeViewItem::on_activate(
bContext &
C)
1357 return std::nullopt;
#define BLI_assert_unreachable()
#define SET_FLAG_FROM_TEST(value, test, flag)
#define BLI_assert_msg(a, msg)
Span< Value > lookup(const Key &key) const
void add(const Key &key, const Value &value)
constexpr const char * c_str() const
constexpr StringRefNull()
constexpr bool is_empty() const
Abstract base class for defining a customizable tree-view item.
std::optional< rctf > get_win_rect(const ARegion ®ion) const
void update_from_old(const AbstractViewItem &old) override
bool is_collapsed() const
AbstractTreeViewItem * get_child()
void change_state_delayed() override
int count_parents() const
AbstractTreeView & get_tree_view() const
std::unique_ptr< DropTargetInterface > create_item_drop_target() final
bool is_collapsible() const
StringRef get_rename_string() const override
bool matches(const AbstractViewItem &other) const override
void toggle_collapsed_from_view(bContext &C)
virtual void build_row(Layout &row)=0
virtual std::unique_ptr< TreeViewItemDropTarget > create_drop_target()
virtual void on_collapse_change(bContext &C, bool is_collapsed)
virtual std::optional< bool > should_be_collapsed() const
AbstractTreeViewItem * get_parent()
virtual bool set_collapsed(bool collapsed)
virtual bool matches_single(const AbstractTreeViewItem &other) const
bool should_be_filtered_visible(StringRefNull filter_string) const override
std::optional< std::string > debug_name() const override
friend class AbstractTreeView
virtual bool supports_collapsing() const
void uncollapse_by_default()
bool rename(const bContext &C, StringRefNull new_name) override
StringRefNull label() const
void ensure_parents_uncollapsed()
void on_filter() override
void scroll(ViewScrollDirection direction) override
void foreach_root_item(ItemIterFn iter_fn) const
friend class AbstractTreeViewItem
bool is_fully_visible() const override
void scroll_active_into_view(bContext *C, bool scroll_active_to_center=false) override
void persistent_state_apply(const uiViewState &state) override
void draw_overlays(const ARegion ®ion, const Block &block) const override
TreeViewSortOrder invert_sort_type_get() const
virtual void build_tree()=0
void foreach_item(ItemIterFn iter_fn, IterOptions options=IterOptions::None) const
std::optional< uiViewState > persistent_state() const override
void set_default_rows(int default_rows)
virtual void change_state_delayed()
virtual void update_from_old(const AbstractViewItem &old)
bool is_always_collapsible_
virtual bool should_be_filtered_visible(StringRefNull filter_string) const
virtual bool set_state_active()
AbstractView & get_view() const
AbstractViewItem()=default
bool is_filtered_visible() const
void add_rename_button(Block &block)
virtual std::optional< bool > should_be_active() const
bool is_filtered_visible_
ui::ButtonViewItem * view_item_button() const
ui::ButtonViewItem * view_item_but_
bool activate_for_context_menu_
virtual bool supports_scrolling() const
virtual void change_state_delayed()
void register_item(AbstractViewItem &item)
void update_from_old(Block &new_block)
friend class AbstractViewItem
virtual AbstractViewItem * find_active_or_visible_item() const =0
void filter(std::optional< StringRef > filter_str)
void add_label(Layout &layout, StringRefNull label_override="")
BasicTreeViewItem(StringRef label, BIFIconID icon=ICON_NONE)
std::function< bool()> IsActiveFn
std::function< void(bContext &C, BasicTreeViewItem &new_active)> ActivateFn
void set_is_active_fn(IsActiveFn is_active_fn)
void set_on_activate_fn(ActivateFn fn)
void build_row(Layout &row) override
static void build_tree_view(const bContext &C, AbstractTreeView &tree_view, Layout &layout, bool add_box=true)
ItemT & add_tree_item(Args &&...args)
friend class AbstractTreeViewItem
AbstractTreeViewItem * parent_
TreeViewItemContainer * root_
void foreach_item_recursive(ItemIterFn iter_fn, IterOptions options=IterOptions::None) const
void foreach_sort_invert(TreeViewSortOrder order)
Vector< std::unique_ptr< AbstractTreeViewItem > > children_
void foreach_parent(ItemIterFn iter_fn) const
friend class AbstractTreeView
FunctionRef< void(AbstractTreeViewItem &)> ItemIterFn
AbstractTreeViewItem & view_item_
TreeViewItemDropTarget(AbstractTreeViewItem &view_item, DropBehavior behavior=DropBehavior::Insert)
const DropBehavior behavior_
std::optional< DropLocation > choose_drop_location(const ARegion ®ion, const wmEvent &event) const override
Layout & current_layout() const
void build_from_tree(AbstractTreeView &tree_view)
void build_row(AbstractTreeViewItem &item) const
CCL_NAMESPACE_BEGIN struct Options options
T max(const T &a, const T &b)
static int unpadded_item_height()
AbstractViewItem * region_views_find_item_at(const ARegion ®ion, const int xy[2])
void button_func_set(Button *but, std::function< void(bContext &)> func)
int uiLayoutListItemPaddingWidth()
void button_func_pushed_state_set(Button *but, std::function< bool(const Button &)> func)
void block_flag_enable(Block *block, int flag)
static int count_visible_items(AbstractTreeView &tree_view)
Button * uiDefIconButV(Block *block, ButtonType type, int icon, int x, int y, short width, short height, T *poin, float min, float max, std::optional< StringRef > tip)
const uiStyle * style_get_dpi()
static int padded_item_height()
EmbossType block_emboss_get(Block *block)
static void set_sort_order_fn(bContext *, void *, void *arg2)
void button_flag_enable(Button *but, int64_t flag)
Button * uiDefBut(Block *block, ButtonTypeWithPointerType but_and_ptr_type, StringRef str, int x, int y, short width, short height, void *poin, float min, float max, std::optional< StringRef > tip)
void block_to_window_rctf(const ARegion *region, const Block *block, rctf *rct_dst, const rctf *rct_src)
void button_placeholder_set(Button *but, StringRef placeholder_text)
void button_to_pixelrect(rcti *rect, const ARegion *region, const Block *block, const Button *but)
TreeViewItemContainer TreeViewOrItem
void block_emboss_set(Block *block, EmbossType emboss)
void block_flag_disable(Block *block, int flag)
void layout_list_set_labels_active(Layout *layout)
void button_flag_disable(Button *but, int64_t flag)
static ButtonViewItem * find_first_view_item_but(const Block &block, const AbstractTreeView &view)
void def_but_icon(Button *but, const int icon, const int64_t flag)
Button * uiDefIconBut(Block *block, ButtonTypeWithPointerType but_and_ptr_type, int icon, int x, int y, short width, short height, void *poin, float min, float max, std::optional< StringRef > tip)
Button * uiDefIconButBit(Block *block, ButtonType type, int bit, int icon, int x, int y, short width, short height, T *poin, float min, float max, std::optional< StringRef > tip)
Button * uiDefButV(Block *block, ButtonType type, StringRef str, int x, int y, short width, short height, T *poin, float min, float max, std::optional< StringRef > tip)
void uiLayoutListItemAddPadding(Layout *layout)
void block_layout_set_current(Block *block, Layout *layout)
void block_view_persistent_state_restore(const ARegion ®ion, const Block &block, AbstractView &view)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
char * STRNCPY(char(&dst)[N], const char *src)
int char char int int int BLI_strcasecmp_natural(const char *s1, const char *s2) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
MINLINE int round_fl_to_int(float a)
GPUVertFormat * immVertexFormat()
void immBegin(GPUPrimType, uint vertex_len)
wmWindow * CTX_wm_window(const bContext *C)
void immUniformThemeColorAlpha(int color_id, float a)
char char * BLI_strncpy_ensure_pad(char *__restrict dst, const char *__restrict src, char pad, size_t dst_maxncpy) ATTR_NONNULL(1
void GPU_line_width(float width)
void immBindBuiltinProgram(GPUBuiltinShader shader_id)
BLI_INLINE int BLI_rcti_size_y(const struct rcti *rct)
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
VecBase< int32_t, 2 > int2
void immVertex2f(uint attr_id, float x, float y)
@ GPU_SHADER_3D_UNIFORM_COLOR
BLI_INLINE float BLI_rctf_size_y(const struct rctf *rct)
@ UI_VIEW_SHOW_FILTER_OPTIONS
uint GPU_vertformat_attr_add(GPUVertFormat *format, StringRef name, gpu::VertAttrType type)
ARegion * CTX_wm_region_popup(const bContext *C)
void GPU_blend(GPUBlend blend)
ARegion * CTX_wm_region(const bContext *C)
std::string format(const std::string &fmt, Args &&...args)
PipelineCompute lines(lines_main)
std::ranges::transform_view< std::ranges::ref_view< const Vector< std::unique_ptr< Button > > >, Button &(*)(const std::unique_ptr< Button > &)> buttons() const
void label(StringRef name, int icon)
void active_set(bool active)
Layout & column(bool align)
void separator(float factor=1.0f, LayoutSeparatorType type=LayoutSeparatorType::Auto)
void emboss_set(EmbossType emboss)
#define UI_TREEVIEW_INDENT