ZenitUI
Retained-mode C++20 UI framework
Loading...
Searching...
No Matches
TreeView.hpp
Go to the documentation of this file.
1#pragma once
2
7
8#include "Common.hpp"
9#include "Layout.hpp"
10#include "ScrollView.hpp"
11#include "Button.hpp"
12#include "Text.hpp"
13
14#include <functional>
15#include <string>
16#include <string_view>
17#include <vector>
18
19namespace ZenitUI::UI
20{
24
25 class TreeView;
26
37 class TreeItem : public TLayout<TreeItem>
38 {
39 public:
40 explicit TreeItem(std::string label = "",
41 std::string value = "",
42 bool expanded = false);
43
44 const std::string &getLabel() const noexcept { return labelText_; }
45 void setLabel(std::string label);
46
47 const std::string &getValue() const noexcept { return value_; }
48 void setValue(std::string value) { value_ = std::move(value); }
49
50 bool isExpanded() const noexcept { return expanded_; }
51 void setExpanded(bool expanded, bool notify = false);
52 void toggleExpanded(bool notify = false) { setExpanded(!expanded_, notify); }
53
54 bool isSelected() const noexcept { return selected_; }
55
57 void requestInitialSelection(bool s = true) noexcept { initialSelected_ = s; }
58
60 std::shared_ptr<TreeItem> addItem(std::string label,
61 std::string value = "",
62 bool expanded = false);
63
65 void addChildItem(std::shared_ptr<TreeItem> child);
66
68 bool removeChildItem(const TreeItem *child);
69
72
74 std::vector<std::shared_ptr<TreeItem>> childItems() const;
75
77 bool hasChildItems() const;
78
84 void setForceExpandable(bool force);
85 bool isForceExpandable() const noexcept { return forceExpandable_; }
86
88 bool isExpandable() const { return forceExpandable_ || hasChildItems(); }
89
91 std::shared_ptr<TreeItem> parentItem() const { return parentItem_.lock(); }
92
94 int depth() const noexcept { return depth_; }
95
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_; }
100
101 Layout *contentTarget() override { return childrenHost_ ? childrenHost_.get() : this; }
102
103 protected:
104 void onBuild() override;
105
106 private:
107 friend class TreeView;
108
109 void syncVisuals(TreeView *owner,
110 const std::shared_ptr<TreeItem> &parentItem,
111 int depth,
112 float indentPx,
113 float basePadLeftPx,
114 bool isSelected);
115
116 std::string labelText_;
117 std::string value_;
118 bool expanded_{false};
119 bool selected_{false};
120 bool initialSelected_{false};
121 bool forceExpandable_{false};
122 bool syncedExpandable_{false};
123 int depth_{0};
124
125 std::shared_ptr<Layout> row_;
126 std::shared_ptr<Button> toggleBtn_;
127 std::shared_ptr<Text> labelNode_;
128 std::shared_ptr<Layout> childrenHost_;
129
130 std::weak_ptr<TreeView> owner_;
131 std::weak_ptr<TreeItem> parentItem_;
132
133 // Sync cache to avoid redundant style invalidation on unchanged frames
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};
141 };
142
156 class TreeView : public TLayout<TreeView, ScrollView>
157 {
158 public:
159 explicit TreeView(float indent = 18.0f);
160
162 std::shared_ptr<TreeItem> addItem(std::string label,
163 std::string value = "",
164 bool expanded = false);
165
167 void addRootItem(std::shared_ptr<TreeItem> item);
168
170 bool removeRootItem(const TreeItem *item);
171
174
176 std::vector<std::shared_ptr<TreeItem>> rootItems() const;
177
179 std::vector<std::shared_ptr<TreeItem>> visibleItems() const;
180
181 void setIndent(float px);
182 float getIndent() const noexcept { return indent_; }
183
185 void selectItem(const std::shared_ptr<TreeItem> &item, bool notify = false);
186
188 void clearSelection(bool notify = false) { selectItem(nullptr, notify); }
189
191 std::shared_ptr<TreeItem> getSelectedItem() const { return selected_.lock(); }
192
194 void expandAll();
195
198
200 std::shared_ptr<TreeItem> findItemByValue(std::string_view value) const;
201
204
206 std::function<void(std::shared_ptr<TreeItem>)> onSelectionChanged;
207
209 std::function<void(std::shared_ptr<TreeItem>)> onItemActivated;
210
212 std::function<void(std::shared_ptr<TreeItem>, bool)> onItemToggled;
213
214 Vec2 measure(float parent_w, float parent_h) override;
215
216 protected:
217 void onUpdate(float dt) override;
218
219 private:
220 friend class TreeItem;
221
222 void syncTree();
223 void syncSubtree(const std::shared_ptr<TreeItem> &item,
224 const std::shared_ptr<TreeItem> &parentItem,
225 int depth,
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);
234
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};
240 };
241
243} // namespace ZenitUI::UI
A clickable button with an optional text label.
Centralized standard library includes used throughout ZenitUI.
The node class: tree structure, layout pass, style resolution, input dispatch, render orchestration,...
A generic scrolling container.
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