40 explicit TreeItem(std::string label =
"",
41 std::string value =
"",
42 bool expanded =
false);
44 const std::string &getLabel()
const noexcept {
return labelText_; }
45 void setLabel(std::string label);
47 const std::string &getValue()
const noexcept {
return value_; }
48 void setValue(std::string value) { value_ = std::move(value); }
50 bool isExpanded()
const noexcept {
return expanded_; }
51 void setExpanded(
bool expanded,
bool notify =
false);
52 void toggleExpanded(
bool notify =
false) { setExpanded(!expanded_, notify); }
54 bool isSelected()
const noexcept {
return selected_; }
60 std::shared_ptr<TreeItem>
addItem(std::string label,
61 std::string value =
"",
62 bool expanded =
false);
74 std::vector<std::shared_ptr<TreeItem>>
childItems()
const;
85 bool isForceExpandable() const noexcept {
return forceExpandable_; }
91 std::shared_ptr<TreeItem>
parentItem()
const {
return parentItem_.lock(); }
94 int depth() const noexcept {
return depth_; }
96 std::shared_ptr<Layout> row()
const {
return row_; }
97 std::shared_ptr<Button> toggleButton()
const {
return toggleBtn_; }
98 std::shared_ptr<Text> labelNode()
const {
return labelNode_; }
99 std::shared_ptr<Layout> childrenHost()
const {
return childrenHost_; }
107 friend class TreeView;
109 void syncVisuals(TreeView *owner,
116 std::string labelText_;
118 bool expanded_{
false};
119 bool selected_{
false};
120 bool initialSelected_{
false};
121 bool forceExpandable_{
false};
122 bool syncedExpandable_{
false};
125 std::shared_ptr<Layout> row_;
126 std::shared_ptr<Button> toggleBtn_;
127 std::shared_ptr<Text> labelNode_;
128 std::shared_ptr<Layout> childrenHost_;
130 std::weak_ptr<TreeView> owner_;
131 std::weak_ptr<TreeItem> parentItem_;
134 TreeView *boundOwner_{
nullptr};
135 int syncedDepth_{-1};
136 float syncedIndent_{-1.0f};
137 bool syncedHasChildren_{
false};
138 bool syncedExpanded_{
false};
139 bool syncedSelected_{
false};
140 bool syncedVisualsOnce_{
false};
156 class TreeView :
public TLayout<TreeView, ScrollView>
159 explicit TreeView(
float indent = 18.0f);
162 std::shared_ptr<TreeItem>
addItem(std::string label,
163 std::string value =
"",
164 bool expanded =
false);
176 std::vector<std::shared_ptr<TreeItem>>
rootItems()
const;
181 void setIndent(
float px);
182 float getIndent() const noexcept {
return indent_; }
185 void selectItem(
const std::shared_ptr<TreeItem> &item,
bool notify =
false);
214 Vec2 measure(
float parent_w,
float parent_h)
override;
217 void onUpdate(
float dt)
override;
220 friend class TreeItem;
223 void syncSubtree(
const std::shared_ptr<TreeItem> &item,
224 const std::shared_ptr<TreeItem> &parentItem,
226 bool ancestorExpanded,
227 std::vector<std::shared_ptr<TreeItem>> &outVisible);
228 void handleRowClick(
const std::shared_ptr<TreeItem> &item);
229 void handleToggleClick(
const std::shared_ptr<TreeItem> &item);
230 void notifyToggled(
const std::shared_ptr<TreeItem> &item,
bool expanded);
231 void setExpandedRecursive(
const std::shared_ptr<TreeItem> &item,
bool expanded);
232 static std::shared_ptr<TreeItem> findByValueRecursive(
const std::shared_ptr<TreeItem> &item,
233 std::string_view value);
235 float indent_{18.0f};
236 float basePadLeft_{8.0f};
237 std::weak_ptr<TreeItem> selected_;
238 std::weak_ptr<TreeItem> lastClicked_;
239 double lastClickTime_{-10.0};
Centralized standard library includes used throughout ZenitUI.
The node class: tree structure, layout pass, style resolution, input dispatch, render orchestration,...
A read-only text node backed by UIContext::textEngine.
Layout(LayoutType type=LayoutType::Stack)
Construct a node with the given layout strategy.
Definition Layout.hpp:163
TLayout(Args &&...args)
Definition Layout.hpp:1401
bool isExpandable() const
true if this item has visible child items or isForceExpandable() is enabled.
Definition TreeView.hpp:88
int depth() const noexcept
Nesting depth (0 for root items).
Definition TreeView.hpp:94
void setForceExpandable(bool force)
Force this item to display as an expandable branch even with zero mounted children.
std::shared_ptr< TreeItem > addItem(std::string label, std::string value="", bool expanded=false)
Create, attach, and return a nested TreeItem.
void addChildItem(std::shared_ptr< TreeItem > child)
Attach an existing TreeItem as a child.
void requestInitialSelection(bool s=true) noexcept
Mark this item to become initially selected when attached to a TreeView.
Definition TreeView.hpp:57
void onBuild() override
Hook: called once after create() finishes constructing the object.
Layout * contentTarget() override
Return the node that receives children when built from markup.
Definition TreeView.hpp:101
void clearChildItems()
Remove all direct child TreeItems.
std::shared_ptr< TreeItem > parentItem() const
Parent TreeItem, or nullptr if this is a root item.
Definition TreeView.hpp:91
bool removeChildItem(const TreeItem *child)
Detach a direct child TreeItem.
bool hasChildItems() const
true if this item has at least one visible child TreeItem.
std::vector< std::shared_ptr< TreeItem > > childItems() const
Direct child TreeItems in order.
Scrollable tree container with selection, expand/collapse, and keyboard navigation.
Definition TreeView.hpp:157
void collapseAll()
Collapse every branch item in the tree.
bool removeRootItem(const TreeItem *item)
Detach a root-level TreeItem.
void expandAll()
Expand every branch item in the tree.
std::shared_ptr< TreeItem > getSelectedItem() const
Currently selected item, or nullptr if none.
Definition TreeView.hpp:191
void addRootItem(std::shared_ptr< TreeItem > item)
Attach an existing TreeItem at root level.
std::shared_ptr< TreeItem > addItem(std::string label, std::string value="", bool expanded=false)
Create, attach, and return a root-level TreeItem.
std::function< void(std::shared_ptr< TreeItem >)> onSelectionChanged
Fired when the selected item changes (argument may be nullptr on clear).
Definition TreeView.hpp:206
void clearItems()
Remove every item and clear selection.
void clearSelection(bool notify=false)
Clear the current selection.
Definition TreeView.hpp:188
bool toggleOnDoubleClick
Toggle branch expansion on row double-click (default: true).
Definition TreeView.hpp:203
std::vector< std::shared_ptr< TreeItem > > visibleItems() const
Currently visible TreeItems in depth-first display order.
void selectItem(const std::shared_ptr< TreeItem > &item, bool notify=false)
Select item (or clear selection if null).
std::shared_ptr< TreeItem > findItemByValue(std::string_view value) const
Depth-first search for the first item whose getValue() == value.
std::function< void(std::shared_ptr< TreeItem >, bool)> onItemToggled
Fired when a branch item is expanded or collapsed.
Definition TreeView.hpp:212
std::function< void(std::shared_ptr< TreeItem >)> onItemActivated
Fired on Enter or double-click on an item.
Definition TreeView.hpp:209
std::vector< std::shared_ptr< TreeItem > > rootItems() const
Direct root-level TreeItems in order.
2D vector with component-wise arithmetic.
Definition CoreTypes.hpp:25