25 std::vector<std::unique_ptr<TreeNode>>
children;
29 children.push_back(std::make_unique<TreeNode>());
30 children.back()->label = std::move(child_label);
92 float indent()
const {
return indent_; }
104 return find_by_user_data(root_, data);
140 canvas.clip_rect(
b.x,
b.y,
b.width,
b.height);
142 float y =
b.y - scroll_offset_;
143 paint_node(
canvas, root_, -1,
b.x,
y,
b.width);
157 float y =
event.position.y + scroll_offset_;
160 TreeNode* hit = find_node_at_y(root_, -1, current_y,
y, &hit_depth);
167 float indent_x =
static_cast<float>(hit_depth) * indent_;
168 float triangle_end = indent_x + indent_;
181 if (!event.
is_down)
return false;
184 selected_->expanded =
true;
189 if (selected_->expanded && selected_->has_children()) {
190 selected_->expanded =
false;
204 float row_height_ = 22.0f;
205 float indent_ = 18.0f;
206 float scroll_offset_ = 0.0f;
212 static constexpr float kTriangleWidth = 18.0f;
215 static constexpr float kIconWidth = 18.0f;
218 static constexpr float kTriangleFontSize = 9.0f;
221 float x,
float&
y,
float width) {
223 float indent_x =
x +
static_cast<float>(depth) * indent_;
225 const bool is_sel = (&node == selected_);
238 canvas.fill_rect(
x,
y, 3.0f, row_height_);
249 const float row_mid =
y + row_height_ * 0.5f;
258 canvas.
set_font(
"system", kTriangleFontSize);
261 float tx = indent_x + 2;
273 float label_x = indent_x + kTriangleWidth;
282 if (!node.
icon.empty()) {
288 label_x += kIconWidth;
305 paint_node(canvas, *child, depth + 1,
x,
y,
width);
310 int count_visible(
const TreeNode& node,
int depth)
const {
311 int n = (depth >= 0) ? 1 : 0;
312 if (depth < 0 || node.expanded) {
313 for (
const auto& child : node.children) {
314 n += count_visible(*child, depth + 1);
320 TreeNode* find_by_user_data(TreeNode& node,
void* data) {
321 if (node.user_data == data && data !=
nullptr)
return &node;
322 for (
auto& child : node.children) {
323 if (
auto* found = find_by_user_data(*child, data))
return found;
328 TreeNode* find_node_at_y(TreeNode& node,
int depth,
float& current_y,
float target_y,
329 int* out_depth =
nullptr) {
331 if (target_y >= current_y && target_y < current_y + row_height_) {
332 if (out_depth) *out_depth = depth;
335 current_y += row_height_;
338 if (depth < 0 || node.expanded) {
339 for (
auto& child : node.children) {
340 auto* result = find_node_at_y(*child, depth + 1, current_y, target_y, out_depth);
341 if (result)
return result;
Definition canvas.hpp:240
@ GlyphCenter
Definition canvas.hpp:1122
virtual void set_text_align(TextAlign align)=0
virtual void set_font(const std::string &family, float size)=0
virtual void set_fill_color(Color c)=0
virtual void fill_text_anchored(const std::string &text, float x, float y, TextAnchor anchor)
Definition canvas.hpp:1125
virtual void fill_text(const std::string &text, float x, float y)=0
void on_mouse_event(const MouseEvent &event) override
Mouse down with full event context.
Definition tree_view.hpp:148
bool wants_mouse_input() const override
True if this widget handles mouse down/drag through virtual methods even when it does not expose JS p...
Definition tree_view.hpp:198
void set_selection_style(SelectionStyle s)
Select the selection treatment (see SelectionStyle).
Definition tree_view.hpp:75
float scroll_offset() const
Current vertical scroll offset in pixels (0 = first row flush at top).
Definition tree_view.hpp:121
float max_scroll_offset() const
Largest valid scroll offset: 0 when the content fits, else the overflow past the viewport.
Definition tree_view.hpp:125
float indent() const
Indent per level in pixels.
Definition tree_view.hpp:92
std::function< void(TreeNode &node)> on_select
Called when a node is selected (clicked).
Definition tree_view.hpp:79
std::function< void(TreeNode &node)> on_activate
Called when a node is double-clicked (activated).
Definition tree_view.hpp:82
const TreeNode & root() const
Definition tree_view.hpp:71
void set_scroll_offset(float v)
Scroll to an absolute offset, clamped to [0, max_scroll_offset()] so the view can neither overscroll ...
Definition tree_view.hpp:132
TreeView()
Definition tree_view.hpp:67
TreeNode & root()
The invisible root node (its children are the top-level items).
Definition tree_view.hpp:70
bool on_key_event(const KeyEvent &event) override
Key event with modifiers and up/down state.
Definition tree_view.hpp:180
SelectionStyle
Selection treatment.
Definition tree_view.hpp:65
@ accent
Definition tree_view.hpp:65
@ standard
Definition tree_view.hpp:65
int visible_node_count() const
Number of rows currently painted (every node under an expanded ancestor; collapsed subtrees are exclu...
Definition tree_view.hpp:110
float content_height() const
Intrinsic content height in pixels: visible rows * row height.
Definition tree_view.hpp:116
void set_indent(float px)
Definition tree_view.hpp:93
std::function< void(TreeNode &node, bool expanded)> on_toggle
Called when a node is expanded or collapsed.
Definition tree_view.hpp:85
void set_row_height(float h)
Definition tree_view.hpp:89
void set_selected_node(TreeNode *node)
Programmatically select a node (without firing on_select).
Definition tree_view.hpp:99
TreeNode * find_node_by_user_data(void *data)
Find a node whose user_data matches the given pointer.
Definition tree_view.hpp:103
void paint(canvas::Canvas &canvas) override
Definition tree_view.hpp:136
TreeNode * selected_node() const
Get the currently selected node (nullptr if none).
Definition tree_view.hpp:96
SelectionStyle selection_style() const
Definition tree_view.hpp:76
float row_height() const
Row height in pixels.
Definition tree_view.hpp:88
Color resolve_color(const std::string &name, Color fallback={}) const
void request_repaint()
Request that this view's host invalidate and schedule a repaint.
void set_focusable(bool f)
Definition view.hpp:669
Rect local_bounds() const
Definition view.hpp:88
Definition affine_transform.hpp:24
@ left
Definition canvas.hpp:221
@ glyph
GlyphAtlas — SDF / custom-text glyph cache.
Definition atlas_inventory.hpp:40
Hover-cursor tracking for platform window hosts.
Definition fwd.hpp:27
@ px
Absolute pixels.
Definition geometry.hpp:393
@ s
Definition input_events.hpp:41
@ h
Definition input_events.hpp:39
@ y
Definition input_events.hpp:42
@ n
Definition input_events.hpp:40
@ right
Definition input_events.hpp:53
@ left
Definition input_events.hpp:53
@ b
Definition input_events.hpp:39
@ x
Definition input_events.hpp:41
@ v
Definition input_events.hpp:41
@ height
Definition css_animation.hpp:128
@ width
Definition css_animation.hpp:127
static Color hex(uint32_t rgb)
Construct from hex 0xRRGGBB (alpha = 1.0).
Definition canvas.hpp:66
static constexpr Color rgba8(uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
Construct from 8-bit channels [0,255] — convenience for legacy/hex code.
Definition canvas.hpp:61
Rich key event with key code, modifiers, and repeat state.
Definition input_events.hpp:193
KeyCode key
Definition input_events.hpp:194
bool is_down
True for key-down, false for key-up.
Definition input_events.hpp:196
Rich mouse/pointer event with position, modifiers, button state, pointer ID, and stylus properties.
Definition input_events.hpp:104
bool is_wheel
True for scroll wheel events.
Definition input_events.hpp:114
bool is_down
True for mouse-down / touch-began events.
Definition input_events.hpp:111
float scroll_delta_y
Vertical scroll delta (positive = down).
Definition input_events.hpp:116
int click_count
1=single, 2=double, 3=triple click
Definition input_events.hpp:110
Point position
Position in local view coordinates.
Definition input_events.hpp:105
float x
Definition geometry.hpp:21
A node in the tree hierarchy.
Definition tree_view.hpp:18
void toggle()
Toggle expanded state.
Definition tree_view.hpp:38
void * user_data
Arbitrary payload.
Definition tree_view.hpp:23
bool expanded
Whether children are visible.
Definition tree_view.hpp:21
std::string icon
Optional icon name.
Definition tree_view.hpp:20
TreeNode & add_child(std::string child_label)
Add a child node and return a reference to it.
Definition tree_view.hpp:28
std::string label
Definition tree_view.hpp:19
std::vector< std::unique_ptr< TreeNode > > children
Definition tree_view.hpp:25
bool selectable
Definition tree_view.hpp:22
bool has_children() const
Check if this node has children.
Definition tree_view.hpp:35