ZenitUI
Retained-mode C++20 UI framework
Loading...
Searching...
No Matches
Table.hpp
Go to the documentation of this file.
1#pragma once
2
7
8#include "Common.hpp"
9#include "Layout.hpp"
10#include "Layouts.hpp"
11#include "Button.hpp"
12#include "Text.hpp"
13#include "VirtualView.hpp"
14
15#include <functional>
16#include <string>
17#include <string_view>
18#include <vector>
19
20namespace ZenitUI::UI
21{
25
27 enum class SortDirection
28 {
29 None,
30 Ascending,
31 Descending
32 };
33
36 {
37 std::string title;
38 std::string id;
39 float width{140.0f};
40 float minWidth{48.0f};
41 Align align{Align::Start};
42 bool sortable{false};
43 bool resizable{true};
44 };
45
50 class TableColumn : public TLayout<TableColumn>
51 {
52 public:
53 explicit TableColumn(TableColumnSpec spec = {})
54 : TLayout<TableColumn>(LayoutType::Stack), spec_(std::move(spec))
55 {
56 setVisible(false);
57 setInteractive(false);
58 setStyleTag("TableColumn");
59 }
60
61 const TableColumnSpec &spec() const noexcept { return spec_; }
62 TableColumnSpec &spec() noexcept { return spec_; }
63
64 private:
65 TableColumnSpec spec_;
66 };
67
79 class Table : public TLayout<Table>
80 {
81 public:
82 using CellTextProvider = std::function<std::string(int row, int col)>;
83 using CellFactory = std::function<std::shared_ptr<Layout>(int row, int col)>;
84
85 explicit Table(float rowHeight = 32.0f, float headerHeight = 34.0f);
86
87 // ---- Columns ----
88
89 int addColumn(TableColumnSpec spec);
90 int addColumn(std::string title,
91 float width = 140.0f,
92 bool sortable = false,
93 bool resizable = true,
94 Align align = Align::Start,
95 float minWidth = 48.0f,
96 std::string id = "");
97 void setColumns(std::vector<TableColumnSpec> cols);
98 void clearColumns();
99
100 int columnCount() const noexcept { return static_cast<int>(columns_.size()); }
101 const std::vector<TableColumnSpec> &columns() const noexcept { return columns_; }
102
103 void setColumnWidth(int col, float width, bool notify = false);
104 float getColumnWidth(int col) const;
105 float totalColumnsWidth() const noexcept;
106
107 // ---- Rows & data providers ----
108
110 void setRowCount(int count);
111 int getRowCount() const noexcept { return rowCount_; }
112
114 void setRows(std::vector<std::vector<std::string>> rows);
115 void addRow(std::vector<std::string> row);
116 void clearRows();
117
118 void setCellTextProvider(CellTextProvider provider);
119 void setCellFactory(CellFactory factory);
120
123
124 void setRowHeight(float px);
125 float getRowHeight() const noexcept { return rowHeight_; }
126
127 void setHeaderHeight(float px);
128 float getHeaderHeight() const noexcept { return headerHeight_; }
129
130 // ---- Sorting ----
131
132 void setSort(int col, SortDirection dir, bool notify = false);
133 void clearSort(bool notify = false) { setSort(-1, SortDirection::None, notify); }
134 int getSortColumn() const noexcept { return sortColumn_; }
135 SortDirection getSortDirection() const noexcept { return sortDirection_; }
136
137 // ---- Selection & scrolling ----
138
139 void selectRow(int row, bool notify = false);
140 void clearSelection(bool notify = false) { selectRow(-1, notify); }
141 int getSelectedRow() const noexcept { return selectedRow_; }
142
143 void scrollToRow(int row);
144
145 // ---- Sub-node accessors ----
146
147 std::shared_ptr<Layout> headerBar() const { return headerViewport_; }
148 std::shared_ptr<Layout> headerRow() const { return headerRow_; }
149 std::shared_ptr<Button> headerButton(int col);
150 std::shared_ptr<Layout> resizeHandle(int col);
151 std::shared_ptr<VirtualView> body() const { return body_; }
152
153 // ---- Callbacks ----
154
155 std::function<void(int row)> onSelectionChanged;
156 std::function<void(int row)> onRowActivated;
157 std::function<void(int col, SortDirection dir)> onSortChanged;
158 std::function<void(int col, float width)> onColumnResized;
159
161 static bool parseAlign(std::string_view s, Align &out) noexcept;
162
163 Layout *contentTarget() override { return columnsStaging_ ? columnsStaging_.get() : this; }
164 Vec2 measure(float parent_w, float parent_h) override;
165
166 protected:
167 void onBuild() override;
168 void onUpdate(float dt) override;
169
170 private:
171 struct HeaderColNodes
172 {
173 std::shared_ptr<Layout> cell;
174 std::shared_ptr<Button> button;
175 std::shared_ptr<Layout> handle;
176 };
177
178 void ingestDeclarativeColumns();
179 void rebuildHeader();
180 void updateHeaderLabels();
181 void updateHeaderGeometry();
182 void syncHeaderScroll();
183 void flushBodyIfDirty();
184 void applySelectionVisuals();
185 void handleHeaderClick(int col);
186 void handleRowClick(int row);
187 void setDraggingCursor(bool active);
188 std::shared_ptr<Layout> buildRow(int row);
189 std::string resolveCellText(int row, int col) const;
190
191 std::shared_ptr<Layout> headerViewport_;
192 std::shared_ptr<Layout> headerRow_;
193 std::shared_ptr<VirtualView> body_;
194 std::shared_ptr<Layout> columnsStaging_;
195
196 std::vector<TableColumnSpec> columns_;
197 std::vector<HeaderColNodes> headerNodes_;
198 std::vector<std::vector<std::string>> inlineRows_;
199 std::vector<std::pair<int, std::weak_ptr<Layout>>> materializedRows_;
200
201 CellTextProvider textProvider_;
202 CellFactory cellFactory_;
203
204 float rowHeight_{32.0f};
205 float headerHeight_{34.0f};
206 int rowCount_{0};
207 int selectedRow_{-1};
208 int sortColumn_{-1};
209 SortDirection sortDirection_{SortDirection::None};
210
211 bool headerDirty_{true};
212 bool bodyDirty_{false};
213 float lastHeaderScrollX_{0.0f};
214
215 int resizingCol_{-1};
216 float resizeStartX_{0.0f};
217 float resizeStartWidth_{0.0f};
218
219 int lastClickedRow_{-1};
220 double lastClickTime_{-10.0};
221 };
222
224} // 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,...
Basic layout containers: VStack, HStack, Panel.
A read-only text node backed by UIContext::textEngine.
A virtualized scrolling list with lazy materialization.
Layout(LayoutType type=LayoutType::Stack)
Construct a node with the given layout strategy.
Definition Layout.hpp:163
void setStyleTag(const std::string &t)
Set the node's style tag.
void setVisible(bool v)
Show or hide the node (display: none semantics).
TLayout(Args &&...args)
Definition Layout.hpp:1401
void onUpdate(float dt) override
Called every frame from update, after input dispatch.
void setRowCount(int count)
Set row count when driving the table via setCellTextProvider / setCellFactory.
void onBuild() override
Hook: called once after create() finishes constructing the object.
static bool parseAlign(std::string_view s, Align &out) noexcept
Parse "start", "center", or "end".
void refreshRows()
Force visible virtualized rows to be rebuilt on the next frame.
void setRows(std::vector< std::vector< std::string > > rows)
Convenience in-memory row storage (used when no custom provider/factory is set).
Layout * contentTarget() override
Return the node that receives children when built from markup.
Definition Table.hpp:163
Vec2 measure(float parent_w, float parent_h) override
Compute the intrinsic size given available space.
Align
Cross-axis alignment for flex items and content.
Definition CoreTypes.hpp:452
@ Start
Pack at the leading edge.
Definition CoreTypes.hpp:454
@ Stack
Children overlap each other; Z-order controls paint order.
Definition UIEnums.hpp:30
SortDirection
Active sort order of a Table column.
Definition Table.hpp:28
@ None
Draw at 1:1 source pixels, centered and cropped if larger than the box.
Definition ImageContainer.hpp:33
Configuration of a single column inside a Table.
Definition Table.hpp:36
2D vector with component-wise arithmetic.
Definition CoreTypes.hpp:25