Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
virtual_rows.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3#pragma once
4
5#include <portals/detail/virtual_rows_tree.hpp>
6#include <portals/layout.hpp>
7#include <portals/managed_navigation.hpp>
8#include <portals/model.hpp>
9#include <portals/model_edit.hpp>
10
11namespace portals {
12class ScrollBar;
13
14namespace ui {
15template <class> class Model;
16template <class> struct WindowRowsModel;
17} // namespace ui
18
19namespace detail {
21template <class T> struct VirtualRowsModelBinding {
22 virtual ~VirtualRowsModelBinding() = default;
23 virtual std::expected<void, TreeError> replaceModel(ui::Model<T>, ModelRebindOptions) = 0;
24
25 virtual std::expected<void, TreeError> replaceWindowModel(const ui::WindowRowsModel<T> &, ModelRebindOptions) {
26 return std::unexpected(TreeError::INVALID_ARGUMENT);
27 }
28};
29
31} // namespace detail
32
38public:
39 virtual ~VirtualRowEditParticipant() = default;
41 virtual bool unfinished() const noexcept = 0;
45 virtual std::expected<void, TreeError> beforeLeave() = 0;
46
48 virtual void retire() noexcept {
49 }
50};
51
55 std::optional<RowId> current;
56 std::optional<std::uint64_t> currentIndex;
57 std::optional<std::uint64_t> firstIndex;
58 std::size_t visible{};
59 std::optional<std::uint64_t> count;
60 std::optional<bool> hasBefore;
61 std::optional<bool> hasAfter;
62 std::optional<WindowStatus> windowStatus;
63 bool editing{};
65 bool operator==(const VirtualRowsPosition &other) const = default;
66};
67
72 std::int32_t rowHeight{1};
73 std::size_t maxRows{256};
74 std::size_t overscanRows{2};
75 std::int64_t maxPendingRows{4096};
76 bool scrollbar{true};
77 bool currentIndicator{true};
79};
80
81namespace ui::detail {
83struct RowPositionState;
84
85struct RowPlacement {
86 Node<Widget> node;
87 std::int32_t y{};
88 std::int32_t height{1};
89};
90
91struct VirtualRowsState {
92 virtual ~VirtualRowsState() = default;
93 VirtualRowsOptions options;
94 std::vector<RowPlacement> placements = std::vector<RowPlacement>(0);
95 std::optional<TreeError> failure;
96 std::optional<SourceError> sourceFailure;
97 std::optional<WindowStatus> sourceStatus;
98 std::optional<SourceCursor> sourceRecovery;
99 std::optional<SourceCapabilities> sourceCapabilities;
100 std::optional<std::uint64_t> exactCount;
101 std::uint64_t first{};
102 std::int32_t firstCellOffset{};
103 VirtualRowsPosition position;
104 std::shared_ptr<RowPositionState> positionState;
105 Node<ScrollBar> scrollbar;
106 bool scrollbarVisible{};
107 bool hasFocus{};
108 std::size_t currentSlot{};
109 std::optional<std::size_t> visibleCurrentSlot;
110 std::optional<GraphemeText> currentGlyph;
111
112 virtual std::expected<void, TreeError> autoHeight(bool) {
113 return std::unexpected(TreeError::INVALID_ARGUMENT);
114 }
115
116 virtual std::expected<void, TreeError> requestFirst(std::uint64_t) = 0;
117 virtual std::expected<void, TreeError> requestMove(std::int64_t) = 0;
118 virtual std::expected<void, TreeError> requestPage(bool) = 0;
119
120 virtual std::expected<void, TreeError> requestAnchor(SourceAnchor, SourceDirection) {
121 return std::unexpected(TreeError::INVALID_ARGUMENT);
122 }
123
124 virtual std::expected<void, TreeError> notifyReady(std::uint64_t) {
125 return std::unexpected(TreeError::INVALID_ARGUMENT);
126 }
127};
128
130} // namespace ui::detail
131
136template <class T> class VirtualRows final : public SizedWidget, public Layout {
137public:
138 using VirtualRow = T;
139
141 explicit VirtualRows(std::shared_ptr<ui::detail::VirtualRowsState> state, SizePolicy policy = DEFAULT_VIEWPORT_POLICY)
142 : SizedWidget(policy), state_(std::move(state)) {
143 }
144
145 const std::shared_ptr<ui::detail::VirtualRowsState> &state() const noexcept {
146 return state_;
147 }
148
150
151 std::uint64_t firstRow() const noexcept {
152 return state_->first;
153 }
154
156 std::optional<TreeError> lastError() const noexcept {
157 return state_->failure;
158 }
159
161 std::optional<std::uint64_t> rowCount() const noexcept {
162 return state_->exactCount;
163 }
164
166 std::optional<WindowStatus> windowStatus() const noexcept {
167 return state_->sourceStatus;
168 }
169
171 std::optional<SourceError> sourceError() const noexcept {
172 return state_->sourceFailure;
173 }
174
176 std::optional<SourceCursor> recoveryCursor() const noexcept {
177 return state_->sourceRecovery;
178 }
179
181 std::optional<SourceCapabilities> capabilities() const noexcept {
182 return state_->sourceCapabilities;
183 }
184
186 std::size_t mountedRows() const noexcept {
187 return state_->placements.size();
188 }
189
191 VirtualRowsPosition position() const noexcept {
192 return state_->position;
193 }
194
196 std::expected<CellSize, TreeError> childAvailable(CellSize available) const override {
197 const auto gutters =
198 static_cast<std::int32_t>(state_->options.currentIndicator) + static_cast<std::int32_t>(state_->scrollbarVisible);
199 return CellSize{std::max(0, available.width - gutters),
200 state_->options.autoRowHeight ? available.height : state_->options.rowHeight};
201 }
202
204 std::expected<CellSize, TreeError> measureChildren(std::span<const MeasuredChild>) const override {
205 return CellSize{};
206 }
207
211 std::expected<std::vector<CellRect>, TreeError> arrangeChildren(std::span<const MeasuredChild> children,
212 CellSize available) const override try {
213 std::vector<CellRect> result(0);
214 result.reserve(children.size());
215 for (const auto &child : children) {
216 if (child.node == state_->scrollbar.id()) {
217 auto rect =
218 CellRect::from({std::max(0, available.width - 1), 0}, {std::min(1, available.width), available.height});
219 if (!rect) {
220 return std::unexpected(TreeError::INVALID_GEOMETRY);
221 }
222 result.push_back(*rect);
223 continue;
224 }
225 if (!state_->options.autoRowHeight && child.desired.height > state_->options.rowHeight) {
226 return std::unexpected(TreeError::INVALID_GEOMETRY);
227 }
228 const auto found = std::ranges::find(state_->placements, child.node, [](const auto &entry) {
229 return entry.node.id();
230 });
231 if (found == state_->placements.end()) {
232 return std::unexpected(TreeError::INVALID_ARGUMENT);
233 }
234 const auto leading = std::min(available.width, static_cast<std::int32_t>(state_->options.currentIndicator));
235 const auto width = std::max(0, available.width - leading - static_cast<std::int32_t>(state_->scrollbarVisible));
236 auto rect = CellRect::from({leading, found->y}, {width, found->height});
237 if (!rect) {
238 return std::unexpected(TreeError::INVALID_GEOMETRY);
239 }
240 result.push_back(*rect);
241 }
242 // Copy into the elided expected result: MSVC checked vector moves can allocate in noexcept.
243 return std::expected<std::vector<CellRect>, TreeError>(std::in_place, result);
244 } catch (const std::bad_alloc &) {
245 return std::unexpected(TreeError::NO_MEMORY);
246 }
247
249 std::expected<void, TreeError> arrange(CellRect local) override {
250 auto bounds = CellRect::from({}, local.size());
251 if (!bounds) {
252 return std::unexpected(TreeError::INVALID_GEOMETRY);
253 }
254 bounds_ = *bounds;
255 return {};
256 }
257
259 std::expected<void, TreeError> paint(PaintContext &context) override {
260 if (!state_->options.currentIndicator || !state_->currentGlyph || !state_->position.current ||
261 !state_->visibleCurrentSlot) {
262 return {};
263 }
264 const auto index = *state_->visibleCurrentSlot;
265 if (index >= state_->placements.size()) {
266 return {};
267 }
268 const std::array<RenderCommand, 1> commands{
269 TextCommand{{0, std::max(0, state_->placements[index].y)},
270 *state_->currentGlyph,
271 bounds_,
272 CellStyle{.bold = state_->hasFocus, .underline = state_->hasFocus}}};
273 return context.submit(commands);
274 }
275
276private:
277 std::shared_ptr<ui::detail::VirtualRowsState> state_;
278 CellRect bounds_;
279};
280
286template <class T>
287std::expected<void, TreeError> setVirtualRowsAutoRowHeight(WidgetTree &tree, Node<VirtualRows<T>> node, bool enabled) {
288 std::expected<void, TreeError> result;
289 auto changed = tree.edit(node, [&](auto &host) {
290 result = host.state()->autoHeight(enabled);
291 });
292 return changed ? result : changed;
293}
294
300template <class T>
301std::expected<void, TreeError> setVirtualRowsFirst(WidgetTree &tree, Node<VirtualRows<T>> node, std::uint64_t first) {
302 std::expected<void, TreeError> result;
303 const auto changed = tree.edit(node, [&](auto &host) {
304 result = host.state()->requestFirst(first);
305 });
306 return changed ? result : changed;
307}
308
313template <class T>
314std::expected<void, TreeError> scrollVirtualRows(WidgetTree &tree, Node<VirtualRows<T>> node, std::int64_t rows) {
315 std::expected<void, TreeError> result;
316 auto edited = tree.edit(node, [&](auto &host) {
317 result = host.state()->requestMove(rows);
318 });
319 return edited ? result : edited;
320}
321
326template <class T>
327std::expected<void, TreeError> pageVirtualRows(WidgetTree &tree, Node<VirtualRows<T>> node, bool backwards = false) {
328 std::expected<void, TreeError> result;
329 auto changed = tree.edit(node, [&](auto &host) {
330 result = host.state()->requestPage(backwards);
331 });
332 return changed ? result : changed;
333}
334
340template <class T>
341std::expected<void, TreeError> navigateVirtualRows(WidgetTree &tree, Node<VirtualRows<T>> node, SourceAnchor anchor,
343 std::expected<void, TreeError> result;
344 auto changed = tree.edit(node, [&](auto &host) {
345 result = host.state()->requestAnchor(anchor, direction);
346 });
347 return changed ? result : changed;
348}
349
354template <class T>
355std::expected<void, TreeError> notifyVirtualRowsReady(WidgetTree &tree, Node<VirtualRows<T>> node,
356 std::uint64_t revision) {
357 std::expected<void, TreeError> result;
358 auto changed = tree.edit(node, [&](auto &host) {
359 result = host.state()->notifyReady(revision);
360 });
361 return changed ? result : changed;
362}
363} // namespace portals
An owning, invariant-preserving half-open cell rectangle.
Definition geometry.hpp:81
CellSize size() const noexcept
Return the normalized cell extents by value; no errors.
static std::expected< CellRect, GeometryError > from(CellPoint origin, CellSize available) noexcept
Create a rectangle after normalizing negative available extents to zero.
Pure container interface; callbacks run inside tree.read and an enclosing tree.frame....
Definition layout.hpp:130
Copyable typed observer; never owns or exposes an unchecked widget pointer.
Definition widget.hpp:92
Borrowed synchronous command sink supplied by the frame owner. submit must consume commands immediate...
Definition widget_types.hpp:66
virtual std::expected< void, TreeError > submit(std::span< const RenderCommand > commands)=0
Borrow commands.
Independent one-axis projection of an owning controller, without a content offset....
Definition scroll_controls.hpp:127
SizedWidget(SizePolicy policy={})
Own the supplied policies; checked by layout.
Typed opt-in for custom editor state owned by a delegated row. Custom controls call RowContext::admit...
Definition virtual_rows.hpp:37
virtual void retire() noexcept
Retire the participant after its row identity has closed; never commit/cancel backend work.
Definition virtual_rows.hpp:48
virtual std::expected< void, TreeError > beforeLeave()=0
virtual ~VirtualRowEditParticipant()=default
Release only the participant's own state.
virtual bool unfinished() const noexcept=0
Bounded real widget subtrees, independent of the text-only VirtualList.
Definition virtual_rows.hpp:136
std::size_t mountedRows() const noexcept
Definition virtual_rows.hpp:186
std::expected< void, TreeError > arrange(CellRect local) override
Definition virtual_rows.hpp:249
VirtualRowsPosition position() const noexcept
Definition virtual_rows.hpp:191
std::optional< WindowStatus > windowStatus() const noexcept
Definition virtual_rows.hpp:166
std::expected< void, TreeError > paint(PaintContext &context) override
Definition virtual_rows.hpp:259
std::optional< SourceError > sourceError() const noexcept
Definition virtual_rows.hpp:171
std::optional< TreeError > lastError() const noexcept
Definition virtual_rows.hpp:156
std::expected< std::vector< CellRect >, TreeError > arrangeChildren(std::span< const MeasuredChild > children, CellSize available) const override
Definition virtual_rows.hpp:211
std::uint64_t firstRow() const noexcept
Definition virtual_rows.hpp:151
std::expected< CellSize, TreeError > measureChildren(std::span< const MeasuredChild >) const override
Ignore child measurements.
Definition virtual_rows.hpp:204
std::optional< SourceCapabilities > capabilities() const noexcept
Definition virtual_rows.hpp:181
T VirtualRow
Domain row used by the host's typed observing facade.
Definition virtual_rows.hpp:138
std::optional< SourceCursor > recoveryCursor() const noexcept
Definition virtual_rows.hpp:176
std::expected< CellSize, TreeError > childAvailable(CellSize available) const override
Definition virtual_rows.hpp:196
std::optional< std::uint64_t > rowCount() const noexcept
Definition virtual_rows.hpp:161
Exclusive parent/child ownership on Scheduler's UI thread. Scheduler must outlive this object....
Definition widget.hpp:179
std::expected< void, TreeError > edit(Node< T > node, Callback &&callback)
Borrow T only during a void callback; nested edit/removal is supported. Forbidden during measure/arra...
Definition widget.hpp:310
Owning cold model declaration; copies share one single-scene registration. Construction does not read...
Definition ui_model.hpp:129
Public Portals API.
Definition action.hpp:8
std::expected< void, TreeError > navigateVirtualRows(WidgetTree &tree, Node< VirtualRows< T > > node, SourceAnchor anchor, SourceDirection direction=SourceDirection::FORWARD)
Request a capability-gated native row/tail/cursor operation.
Definition virtual_rows.hpp:341
std::expected< void, TreeError > setVirtualRowsFirst(WidgetTree &tree, Node< VirtualRows< T > > node, std::uint64_t first)
Request a finite ordinal; mounting occurs at the existing managed service boundary.
Definition virtual_rows.hpp:301
std::expected< void, ManagedError > setVirtualRowsAutoRowHeight(ManagedScene &scene, Node< VirtualRows< T > > node, bool enabled)
Checked managed facade for natural row sizing.
Definition ui_virtual_rows.hpp:943
std::expected< void, TreeError > scrollVirtualRows(WidgetTree &tree, Node< VirtualRows< T > > node, std::int64_t rows)
Accumulate a relative row intent without replacing it with a stale committed position.
Definition virtual_rows.hpp:314
constexpr SizePolicy DEFAULT_VIEWPORT_POLICY
A bounded data viewport fills both axes without requesting the whole model extent.
Definition layout.hpp:53
SourceDirection
Requested traversal; returned rows remain in display order.
Definition window_source.hpp:47
@ FORWARD
Read after the requested gap.
Definition window_source.hpp:48
std::expected< void, TreeError > notifyVirtualRowsReady(WidgetTree &tree, Node< VirtualRows< T > > node, std::uint64_t revision)
Publish a native provider readiness revision through the existing source notifier.
Definition virtual_rows.hpp:355
std::expected< void, TreeError > pageVirtualRows(WidgetTree &tree, Node< VirtualRows< T > > node, bool backwards=false)
Navigate one bounded delivered page, preserving unknown source extent.
Definition virtual_rows.hpp:327
TreeError
Checked tree failures; rejected operations never start the requested callback.
Definition widget_types.hpp:26
@ INVALID_GEOMETRY
Required arranged geometry is absent or translation overflowed.
Definition widget_types.hpp:38
@ INVALID_ARGUMENT
An option, enum value or operation argument is invalid.
Definition widget_types.hpp:27
@ NO_MEMORY
Allocation failed inside the operation boundary.
Definition widget_types.hpp:33
An owning available extent in terminal columns/rows.
Definition geometry.hpp:44
std::int32_t height
Available number of rows, possibly negative before normalization.
Definition geometry.hpp:48
std::int32_t width
Available number of columns, possibly negative before normalization.
Definition geometry.hpp:46
Small owning paint value, independent of themes and text metrics.
Definition cell_buffer.hpp:35
Bounded retained editing, independent of collection extent.
Definition model_edit.hpp:93
Bounded history options, independent of model row count.
Definition managed_navigation.hpp:38
Logical resource namespace, distinct from its current delivery session. Reuse an existing SourceStamp...
Definition model_position.hpp:21
Owning independent policies for width and height.
Definition layout.hpp:35
Owning navigation intent, including optional capability-gated operations.
Definition window_source.hpp:66
One horizontal text run; controls display placeholders, never line breaks.
Definition render.hpp:24
Bounded real-child row host settings; fixed-height defaults with opt-in natural sizing.
Definition virtual_rows.hpp:69
std::int32_t rowHeight
Positive terminal cell rows per domain row.
Definition virtual_rows.hpp:72
ModelCheckpointOptions checkpoints
Existing bounded source-position history budget.
Definition virtual_rows.hpp:70
std::size_t maxRows
Positive maximum visible assignments; excessive viewport demand is rejected.
Definition virtual_rows.hpp:73
EditOptions editing
Existing common field editing mode and retained field/byte limits.
Definition virtual_rows.hpp:71
bool currentIndicator
Reserve one leading cell for the current-row marker.
Definition virtual_rows.hpp:77
bool scrollbar
Show the existing styled vertical bar when a finite source can scroll.
Definition virtual_rows.hpp:76
std::int64_t maxPendingRows
Positive cap on queued native row moves or natural-mode cell intents.
Definition virtual_rows.hpp:75
bool autoRowHeight
Measure bounded real row compositions at their actual offered width.
Definition virtual_rows.hpp:78
std::size_t overscanRows
Additional native delivered rows for smooth bounded row-step overlap.
Definition virtual_rows.hpp:74
Owning committed row-host orientation; native cursor sources never invent ordinals.
Definition virtual_rows.hpp:53
std::optional< bool > hasAfter
Known forward availability, absent when unknown.
Definition virtual_rows.hpp:61
std::optional< std::uint64_t > count
Exact finite extent; never an estimate.
Definition virtual_rows.hpp:59
bool operator==(const VirtualRowsPosition &other) const =default
std::optional< bool > hasBefore
Known backward availability, absent when unknown.
Definition virtual_rows.hpp:60
std::optional< std::uint64_t > currentIndex
Zero-based finite ordinal, absent for native cursors.
Definition virtual_rows.hpp:56
std::size_t visible
Ready visible row assignments, excluding retained edit state.
Definition virtual_rows.hpp:58
std::optional< std::uint64_t > firstIndex
First visible finite ordinal, absent for native cursors.
Definition virtual_rows.hpp:57
std::optional< RowId > current
Stable current record, independent of keyboard focus.
Definition virtual_rows.hpp:55
ModelResource resource
Exact accepted source identity.
Definition virtual_rows.hpp:54
std::optional< WindowStatus > windowStatus
Native delivery state, absent for finite sources.
Definition virtual_rows.hpp:62
bool editing
Focus is inside the current row's real controls.
Definition virtual_rows.hpp:63
Cold native window delivery capability, with no invented finite extent/current.
Definition ui_virtual_rows.hpp:364