TachyonUI 1.1.0
High-performance UI for building tools.
Loading...
Searching...
No Matches
gui_tree_utils.h
Go to the documentation of this file.
1#pragma once
2
3// Flat-list tree helpers shared by VirtualTreeView / tree-table views: rows
4// are a flat vector in depth-first order, each row carrying its depth.
5// RebuildTreeIndex derives skipCount / ancestorLineMask / isLastSibling;
6// ReorderTreeRow moves a subtree (with reparenting) for drag-drop.
7
8
9#include <gui_controls/gui_widget_state_types.h> // TreeDropPosition
10
11#include <algorithm>
12#include <concepts>
13#include <iterator>
14#include <stdint.h>
15#include <vector>
16
18{
21
24 template <typename Row>
25 concept TreeRowLike = requires(Row r)
26 {
27 { r.depth } -> std::convertible_to<int>;
28 { r.skipCount } -> std::convertible_to<int>;
29 { r.ancestorLineMask } -> std::convertible_to<uint32_t>;
30 { r.isLastSibling } -> std::convertible_to<bool>;
31 };
32
36 template <TreeRowLike Row>
37 void RebuildTreeIndex(std::vector<Row>& rows)
38 {
39 const int n = static_cast<int>(rows.size());
40 int maxDepth = 0;
41 for (const Row& row : rows)
42 maxDepth = std::max(maxDepth, static_cast<int>(row.depth));
43 // later[t]: a row below this one is at depth t with none shallower in between, so a
44 // line at depth t runs on past this row. Only entries up to `highest` can be set.
45 std::vector<uint8_t> later(static_cast<size_t>(maxDepth) + 1, 0);
46 int highest = -1;
47 // The rows below this one that no row below them is at or above: the first one at or
48 // above this row's depth ends its subtree.
49 std::vector<int> open;
50 for (int i = n - 1; i >= 0; --i)
51 {
52 const int D = std::max(0, static_cast<int>(rows[i].depth));
53 while (!open.empty() && rows[open.back()].depth > D)
54 open.pop_back();
55 rows[i].skipCount = (open.empty() ? n : open.back()) - i - 1;
56 open.push_back(i);
57
58 uint32_t mask = 0;
59 for (int d = 0; d <= D - 2 && d < 32; d++) // the mask is 32 bits wide
60 if (d + 1 <= highest && later[size_t(d + 1)])
61 mask |= (1u << d);
62 rows[i].ancestorLineMask = mask;
63 rows[i].isLastSibling = !(D <= highest && later[size_t(D)]);
64
65 // For the rows above: lines deeper than this row end at it, its own runs on.
66 for (int t = D + 1; t <= highest; t++)
67 later[size_t(t)] = 0;
68 later[size_t(D)] = 1;
69 highest = D;
70 }
71 }
72
78 template <TreeRowLike Row>
79 int ReorderTreeRow(std::vector<Row>& rows, int srcDs, int dstDs,
81 {
82 const int n = static_cast<int>(rows.size());
83 if (srcDs < 0 || srcDs >= n) return -1;
84 if (dstDs < 0 || dstDs >= n) return -1;
85 if (srcDs == dstDs) return -1;
86
87 const int srcStart = srcDs;
88 const int srcSliceCount = 1 + rows[srcStart].skipCount;
89 const int srcEnd = srcStart + srcSliceCount;
90
91 // Cycle guard: dropping into the source's own subtree is invalid.
92 if (dstDs >= srcStart && dstDs < srcEnd) return -1;
93
94 int targetBaseDepth = rows[dstDs].depth;
95 int insertAt = dstDs;
96 auto openTarget = [&] {
97 if constexpr (requires { rows[dstDs].isOpen = true; })
98 rows[dstDs].isOpen = true;
99 };
100 switch (pos)
101 {
103 insertAt = dstDs;
104 targetBaseDepth = rows[dstDs].depth;
105 break;
107 // First child: splice in right after the parent row.
108 insertAt = dstDs + 1;
109 targetBaseDepth = rows[dstDs].depth + 1;
110 openTarget();
111 break;
113 // Last child: splice in past the parent's whole subtree.
114 insertAt = dstDs + 1 + rows[dstDs].skipCount;
115 targetBaseDepth = rows[dstDs].depth + 1;
116 openTarget();
117 break;
119 insertAt = dstDs + 1 + rows[dstDs].skipCount;
120 targetBaseDepth = rows[dstDs].depth;
121 break;
122 default: return -1;
123 }
124
125 // Lift the source slice into a temp, adjust depth on each lifted row.
126 const int srcBaseDepth = rows[srcStart].depth;
127 const int deltaDepth = targetBaseDepth - srcBaseDepth;
128 std::vector<Row> lifted;
129 lifted.reserve(srcSliceCount);
130 lifted.insert(lifted.end(),
131 std::make_move_iterator(rows.begin() + srcStart),
132 std::make_move_iterator(rows.begin() + srcEnd));
133 if (deltaDepth != 0)
134 for (auto& r : lifted) r.depth += deltaDepth;
135
136 rows.erase(rows.begin() + srcStart, rows.begin() + srcEnd);
137 if (insertAt > srcStart) insertAt -= srcSliceCount;
138 rows.insert(rows.begin() + insertAt,
139 std::make_move_iterator(lifted.begin()),
140 std::make_move_iterator(lifted.end()));
141 return insertAt; // new flat index of the moved row's root
142 }
143
144}
A flat tree row: the caller sets depth; RebuildTreeIndex derives the rest.
Definition gui_tree_utils.h:25
int ReorderTreeRow(std::vector< Row > &rows, int srcDs, int dstDs, tachyon::TreeDropPosition pos)
Moves the subtree rooted at flat index srcDs to pos relative to row dstDs, shifting depths when the t...
Definition gui_tree_utils.h:79
void RebuildTreeIndex(std::vector< Row > &rows)
Recomputes the derived fields (skipCount, ancestorLineMask, isLastSibling) from each row's depth.
Definition gui_tree_utils.h:37
TreeDropPosition
Where a tree reorder drop lands relative to the target row.
Definition gui_widget_state_types.h:46
@ InsideLast
last child of the target (rectangle around the row)
Definition gui_widget_state_types.h:50
@ After
next sibling of the target (line at the row bottom)
Definition gui_widget_state_types.h:51
@ Before
previous sibling of the target (line at the row top)
Definition gui_widget_state_types.h:48
@ Inside
first child of an open target branch (indented line at the row bottom)
Definition gui_widget_state_types.h:49
@ Row
Horizontal, left to right.
Definition gui_style.h:165
Definition gui_tree_utils.h:18