Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
virtual_list.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3#pragma once
4#include <portals/data_presentation.hpp>
5#include <portals/detail/list_text.hpp>
6
7#include <portals/action.hpp>
8#include <portals/layout.hpp>
9#include <portals/model.hpp>
10#include <portals/scroll.hpp>
11
12namespace portals {
13namespace ui::detail {
14struct Access;
15}
16
19 std::int32_t row_height{1};
20 std::size_t overscan_rows{2};
23 CellStyle selected{{}, {}, true, true};
24 CellStyle highlight{.bold = true, .underline = true};
25};
26
28namespace detail {
29struct ListWindow {
30 RowRange visible;
31 RowRange requested;
32};
33
34std::expected<ListWindow, TreeError> listWindow(std::uint64_t count, std::uint64_t first, CellSize viewport,
35 VirtualListOptions options);
36
37struct ListRow {
38 std::optional<RowId> id;
39 std::optional<GraphemeText> text;
40 std::shared_ptr<const PreparedListText> prepared;
41};
42
43std::expected<void, TreeError> paintList(PaintContext &context, CellSize viewport, VirtualListOptions options,
44 std::span<const ListRow> rows, std::optional<RowId> selected);
45std::expected<bool, TreeError> listShown(WidgetTree &tree, Node<Widget> node);
46} // namespace detail
47
49
50template <class T> class VirtualList;
66template <class T>
67std::expected<std::optional<ModelRequest<T>>, TreeError>
68prepareVirtualList(WidgetTree &tree, Node<VirtualList<T>> node, const ModelFrame<T> &frame, CellSize viewport);
77template <class T>
78std::expected<void, TreeError> setVirtualListFirst(WidgetTree &tree, Node<VirtualList<T>> node, std::uint64_t first);
87template <class T>
88std::expected<void, TreeError> setVirtualListSelection(WidgetTree &tree, Node<VirtualList<T>> node,
89 std::optional<RowId> selected);
98template <class T>
99std::expected<void, TreeError> setVirtualListOptions(WidgetTree &tree, Node<VirtualList<T>> node,
100 VirtualListOptions options);
108template <class T> std::expected<void, TreeError> retryVirtualList(WidgetTree &tree, Node<VirtualList<T>> node);
118template <class T>
119std::expected<ActionBinding, TreeError> bindVirtualList(EventRouter &router, WidgetTree &tree,
120 Node<VirtualList<T>> node);
121
133template <class T> class VirtualList : public SizedWidget {
134public:
142 VirtualList(ModelConsumer<T> consumer, std::function<GraphemeText(const ModelRow<T> &)> delegate,
144 : SizedWidget(policy), consumer_(std::move(consumer)), delegate_(std::move(delegate)), options_(options) {
145 }
146
147 ~VirtualList() override {
148 request_.cancel();
149 }
150
152 std::uint64_t firstRow() const noexcept {
153 return scroll_.rasterOffset();
154 }
155
157 ScrollState scrollState() const noexcept {
158 return scroll_;
159 }
160
162 std::optional<RowId> anchorRow() const noexcept {
163 return anchor_;
164 }
165
167 std::optional<RowId> selection() const noexcept {
168 return selected_;
169 }
170
172 RowRange visibleRange() const noexcept {
173 return prepared_ ? prepared_->window.visible : RowRange{};
174 }
175
177 RowRange preparedRange() const noexcept {
178 return prepared_ ? prepared_->window.requested : RowRange{};
179 }
180
182 RangeState rangeState() const noexcept {
183 return prepared_ ? prepared_->range.state() : RangeState::PENDING;
184 }
185
189 DataRangeStatus dataStatus() const noexcept {
190 return prepared_ ? DataRangeStatus{true, prepared_->range.state(), prepared_->frame.rowCount(),
191 prepared_->frame.dataRevision(), !request_.stopRequested()}
192 : DataRangeStatus{};
193 }
194
198 std::expected<CellSize, TreeError> measure(CellSize available) override {
199 return CellSize{std::max(0, available.width), std::max(0, available.height)};
200 }
201
205 std::expected<void, TreeError> arrange(CellRect local) override {
206 arranged_ = local.size();
207
208 return {};
209 }
210
216 std::expected<void, TreeError> paint(PaintContext &context) override {
217 if (preparedSuppressed_) {
218 auto bounds = CellRect::from({}, arranged_);
219
220 if (!bounds) {
221 return std::unexpected(TreeError::INVALID_GEOMETRY);
222 }
223 const auto style = computedStyle() ? computedStyle()->normal : options_.normal;
224 const std::array<RenderCommand, 1> commands{FillCommand{*bounds, *bounds, style}};
225
226 return context.submit(commands);
227 }
228 if (!prepared_ || dirty_) {
229 return std::unexpected(TreeError::WOULD_BLOCK);
230 }
231
232 if (arranged_ != prepared_->viewport) {
233 return std::unexpected(TreeError::INVALID_GEOMETRY);
234 }
235
236 auto options = options_;
237
238 if (const auto &style = computedStyle()) {
239 options.normal = style->normal;
240 options.selected = style->selected;
241 }
242
243 return detail::paintList(context, arranged_, options, prepared_->rows, selected_);
244 }
245
247 ComputedStyle styleDefaults() const override {
248 ComputedStyle result;
249
250 result.normal = options_.normal;
251 result.selected = options_.selected;
252
253 return result;
254 }
255
256private:
257 friend class ManagedScene;
258 friend struct ui::detail::Access;
259 bool clearMissingSelection_{};
260 bool detachedCollection_{};
262 template <class U>
263 friend std::expected<std::optional<ModelRequest<U>>, TreeError> prepareVirtualList(WidgetTree &, Node<VirtualList<U>>,
264 const ModelFrame<U> &, CellSize);
265 template <class U>
266 friend std::expected<void, TreeError> setVirtualListFirst(WidgetTree &, Node<VirtualList<U>>, std::uint64_t);
267 template <class U>
268 friend std::expected<void, TreeError> setVirtualListSelection(WidgetTree &, Node<VirtualList<U>>,
269 std::optional<RowId>);
270 template <class U>
271 friend std::expected<void, TreeError> setVirtualListOptions(WidgetTree &, Node<VirtualList<U>>, VirtualListOptions);
272 template <class U> friend std::expected<void, TreeError> retryVirtualList(WidgetTree &, Node<VirtualList<U>>);
273 template <class U>
274 friend std::expected<ActionBinding, TreeError> bindVirtualList(EventRouter &, WidgetTree &, Node<VirtualList<U>>);
275
276 template <class U>
277 friend std::expected<void, TreeError> scrollVirtualList(WidgetTree &, Node<VirtualList<U>>, double);
278 template <class U>
279 friend std::expected<RowLookup, TreeError> ensureVirtualListVisible(WidgetTree &, Node<VirtualList<U>>,
280 const ModelFrame<U> &, RowId);
281
283
284 struct Prepared {
285 Prepared(const ModelFrame<T> &owner, const ModelRange<T> &data, CellSize size, detail::ListWindow interval)
286 : frame(owner), range(data), viewport(size), window(interval) {
287 }
288
289 ModelFrame<T> frame;
290 ModelRange<T> range;
291 CellSize viewport;
292 detail::ListWindow window;
293 std::vector<detail::ListRow> rows = std::vector<detail::ListRow>(std::initializer_list<detail::ListRow>{});
294 };
295
296 std::expected<std::optional<ModelRequest<T>>, TreeError> prepare(const ModelFrame<T> &frame, CellSize viewport,
297 bool &changed) {
298 auto consumer_range =
299 detachedCollection_ ? std::expected<ModelRange<T>, TreeError>(ModelRange<T>{}) : frame.range(consumer_.id());
300
301 if (!consumer_range) {
302 return std::unexpected(consumer_range.error());
303 }
304
305 auto nextScroll = scroll_;
306
307 if (anchor_ && anchorRevision_ != frame.dataRevision() && !intent_) {
308 auto location = frame.locateRow(*anchor_);
309
310 if (!location) {
311 return std::unexpected(location.error());
312 }
313
314 if (location->state == RowLookupState::FOUND) {
315 (void)nextScroll.setOffset(location->index, nextScroll.fraction());
316 }
317 // Removed anchors and legacy providers retain the old ordinal, clamped below.
318 }
319
320 nextScroll.clamp(frame.rowCount());
321
322 auto window = detail::listWindow(frame.rowCount(), nextScroll.rasterOffset(), viewport, options_);
323
324 if (!window || !delegate_) {
325 return std::unexpected(window ? TreeError::INVALID_ARGUMENT : window.error());
326 }
327
328 if (prepared_ && !dirty_ && !retry_ && prepared_->frame.revision() == frame.revision() &&
329 prepared_->viewport == viewport && prepared_->window.visible == window->visible &&
330 (window->requested.count == 0 || prepared_->range.state() != RangeState::PENDING ||
331 !request_.stopRequested())) {
332 return std::optional<ModelRequest<T>>{};
333 }
334
335 try {
336 auto range = consumer_range;
337 const bool waitingRetry = retryPending_ && !request_.stopRequested() && request_.range() == window->requested &&
338 request_.dataRevision() == frame.dataRevision() &&
339 (frame.revision() == retryFrameRevision_ || range->state() == RangeState::PENDING);
340 // Retain the current explicit lease independently from the provider's base-read policy.
341 const bool waitingFailedBase = waitingRetry && retryFailedBase_;
342
343 if (window->requested.count == 0) {
344 range = ModelRange<T>{};
345 } else if (!waitingFailedBase &&
346 (retry_ || range->range() != window->requested || range->state() == RangeState::PENDING)) {
347 range = frame.read(window->requested);
348 }
349
350 if (!range) {
351 return std::unexpected(range.error());
352 }
353
354 if (!retry_ && !waitingFailedBase && range->state() == RangeState::PENDING && window->requested.count != 0) {
355 auto cached = frame.readCached(*consumer_range, prepared_ ? &prepared_->range : nullptr, window->requested);
356
357 if (!cached) {
358 return std::unexpected(cached.error());
359 }
360
361 if (cached->dataRevision() != 0) {
362 range = std::move(cached);
363 }
364 }
365
366 // Stage all owning data before touching the visible state or issuing provider work.
367 // A delegate may fail halfway through: frame, row identities and text still commit together.
368 // Construct in place: MSVC Debug vector move is noexcept but may allocate an iterator proxy.
369 auto next = std::make_unique<Prepared>(frame, *range, viewport, *window);
370 auto selection = selected_;
371 if (clearMissingSelection_ && selection) {
372 auto found = frame.locateRow(*selection);
373 if (!found) {
374 return std::unexpected(found.error());
375 }
376 if (found->state == RowLookupState::NOT_FOUND) {
377 selection.reset();
378 }
379 }
380 const bool apply_intent = intent_ && intent_revision_ == frame.dataRevision();
381
382 next->rows.reserve(window->visible.count);
383
384 for (std::size_t i = 0; i < window->visible.count; ++i) {
385 const auto index = window->visible.first + i;
386
387 if (range->state() == RangeState::READY) {
388 const auto &row = range->rows()[static_cast<std::size_t>(index - window->requested.first)];
389 if (row.id.value == 0 || row.id.generation == 0) {
390 return std::unexpected(TreeError::INVALID_ARGUMENT);
391 }
392 if (preparedDelegate_) {
393 auto text = preparedDelegate_(row);
394
395 if (!text || text->text.policy() != options_.width_policy) {
396 return std::unexpected(TreeError::INVALID_ARGUMENT);
397 }
398 next->rows.push_back({row.id, {}, std::move(text)});
399 } else {
400 auto text = delegate_(row);
401
402 if (text.policy() != options_.width_policy) {
403 return std::unexpected(TreeError::INVALID_ARGUMENT);
404 }
405 next->rows.push_back({row.id, std::move(text), {}});
406 }
407
408 if (apply_intent && index == *intent_) {
409 selection = row.id;
410 }
411 } else {
412 auto text = GraphemeText::from(range->state() == RangeState::FAILED ? "!" : "...", options_.width_policy);
413
414 if (!text) {
415 return std::unexpected(TreeError::INVALID_ARGUMENT);
416 }
417
418 next->rows.push_back({{}, std::move(*text)});
419 }
420 }
421
422 std::optional<ModelRequest<T>> dispatch;
423 // A frozen complete collection consumes retry as local preparation, without a live provider endpoint.
424 const bool needs_request = !detachedCollection_ && window->requested.count != 0 &&
425 (retry_ || range->state() == RangeState::PENDING) &&
426 (retry_ || request_.stopRequested() || request_.range() != window->requested ||
427 request_.dataRevision() != frame.dataRevision());
428
429 // Work is requested only after every fallible formatting/allocation step succeeded.
430 // Drop our cancelled lease before requesting, so a one-slot budget can recover after caller release.
431 const bool explicitRetry = retry_;
432 if (needs_request) {
433 request_.cancel();
434 request_ = {};
435
436 auto request = consumer_.request(window->requested);
437
438 if (!request) {
439 return std::unexpected(request.error());
440 }
441
442 dispatch.emplace(*request);
443 request_ = std::move(*request);
444 } else if ((!waitingRetry && range->state() != RangeState::PENDING) || window->requested.count == 0) {
445 request_.cancel();
446 request_ = {};
447 }
448
449 // From this point the presentation commit is nonallocating. Old owners survive until all fields agree.
450 prepared_.swap(next);
451 changed = true;
452 selected_ = selection;
453 scroll_ = nextScroll;
454
455 if (!prepared_->rows.empty() && prepared_->rows.front().id) {
456 anchor_ = prepared_->rows.front().id;
457 anchorRevision_ = frame.dataRevision();
458 } else if (frame.rowCount() == 0) {
459 anchor_.reset();
460 }
461
462 retryPending_ =
463 (explicitRetry && needs_request) || (waitingRetry && window->requested.count && !request_.stopRequested());
464 if (explicitRetry && needs_request) {
465 retryFrameRevision_ = frame.revision();
466 retryFailedBase_ = range->state() == RangeState::FAILED;
467 }
468 dirty_ = false;
469 retry_ = false;
470
471 if (intent_ && (!apply_intent || range->state() == RangeState::READY)) {
472 intent_.reset();
473 }
474
475 return dispatch;
476 } catch (const std::bad_alloc &) {
477 return std::unexpected(TreeError::NO_MEMORY);
478 } catch (const std::length_error &) {
479 return std::unexpected(TreeError::INVALID_ARGUMENT);
480 } catch (...) {
481 return std::unexpected(TreeError::CALLBACK_FAILED);
482 }
483 }
484
485 void choose(std::uint64_t position) {
486 // Intent is positional only within one data revision. Publication/reorder never rewrites a stable ID.
487 const auto count = prepared_->window.visible.count;
488
489 intent_ = position;
490 intent_revision_ = prepared_->frame.dataRevision();
491
492 (void)scroll_.ensureVisible(position, count, prepared_->frame.rowCount());
493 anchor_.reset();
494 dirty_ = true;
495 }
496
497 std::expected<bool, TreeError> scrollBy(double rows) {
498 if (!prepared_) {
499 return std::unexpected(TreeError::WOULD_BLOCK);
500 }
501
502 auto changed = scroll_.moveBy(rows, prepared_->frame.rowCount());
503
504 if (changed && *changed) {
505 anchor_.reset();
506 dirty_ = true;
507 intent_.reset();
508 request_.cancel();
509 }
510
511 return changed;
512 }
513
514 std::expected<RowLookup, TreeError> ensureVisible(const ModelFrame<T> &frame, RowId id) {
515 auto range =
516 detachedCollection_ ? std::expected<ModelRange<T>, TreeError>(ModelRange<T>{}) : frame.range(consumer_.id());
517
518 if (!range) {
519 return std::unexpected(range.error());
520 }
521
522 if (!prepared_) {
523 return std::unexpected(TreeError::WOULD_BLOCK);
524 }
525
526 auto location = frame.locateRow(id);
527
528 if (!location) {
529 return std::unexpected(location.error());
530 }
531
532 if (location->state != RowLookupState::FOUND) {
533 return *location;
534 }
535
536 const auto height = prepared_->viewport.height;
537 const auto capacity = static_cast<std::uint64_t>(height == 0 ? 0 : std::max(1, height / options_.row_height));
538
539 (void)scroll_.ensureVisible(location->index, capacity, frame.rowCount());
540 anchor_.reset();
541 dirty_ = true;
542 intent_.reset();
543 request_.cancel();
544
545 return *location;
546 }
547
548 EventReply action(Action action) {
549 if (!prepared_ || prepared_->frame.rowCount() == 0) {
550 return EventReply::IGNORED;
551 }
552
553 auto position = intent_.value_or(scroll_.rasterOffset());
554 bool located = intent_.has_value();
555
556 // Locate identities only in bounded visible delegates; global index lookup belongs to the provider.
557 if (!located && selected_) {
558 for (std::size_t i = 0; i < prepared_->rows.size(); ++i) {
559 if (prepared_->rows[i].id == selected_) {
560 position = prepared_->window.visible.first + i;
561 located = true;
562
563 break;
564 }
565 }
566 }
567
568 const auto last = prepared_->frame.rowCount() - 1;
569
570 position = std::min(position, last);
571
572 switch (action) {
573 case Action::MOVE_UP:
574 position -= located && position > 0 ? 1 : 0;
575
576 break;
577 case Action::MOVE_DOWN:
578 position += located && position < last ? 1 : 0;
579
580 break;
581 case Action::MOVE_HOME:
582 position = 0;
583
584 break;
585 case Action::MOVE_END:
586 position = last;
587
588 break;
589 default:
590 return EventReply::IGNORED;
591 }
592
593 choose(position);
594
595 return EventReply::HANDLED;
596 }
597
598 ModelConsumer<T> consumer_;
599 std::function<GraphemeText(const ModelRow<T> &)> delegate_;
600 std::function<std::shared_ptr<const detail::PreparedListText>(const ModelRow<T> &)> preparedDelegate_;
601 std::function<void(RowId)> activation_;
602 bool clickActivates_{};
603 std::shared_ptr<void> service_;
604 bool preparedSuppressed_{};
605 VirtualListOptions options_;
606 std::unique_ptr<Prepared> prepared_;
607 ModelRequest<T> request_;
608 std::optional<RowId> selected_;
609 std::optional<std::uint64_t> intent_;
610 std::uint64_t intent_revision_{};
611 ScrollState scroll_;
612 std::optional<RowId> anchor_;
613 std::uint64_t anchorRevision_{};
614 CellSize arranged_;
615 bool dirty_{true};
616 bool retry_{};
617 bool retryPending_{};
618 bool retryFailedBase_{};
619 std::uint64_t retryFrameRevision_{};
620};
621
622template <class T>
623std::expected<std::optional<ModelRequest<T>>, TreeError>
625 std::expected<std::optional<ModelRequest<T>>, TreeError> result;
626 bool changed{};
627 auto shown = detail::listShown(tree, node);
628
629 if (!shown) {
630 return std::unexpected(shown.error());
631 }
632
633 auto edited = tree.edit(node, [&](VirtualList<T> &view) {
634 if (!*shown) {
635 view.request_.cancel();
636 view.request_ = {};
637 view.prepared_.reset();
638 view.dirty_ = true;
639 view.intent_.reset();
640
641 return;
642 }
643
644 result = view.prepare(frame, viewport, changed);
645 });
646
647 if (!edited) {
648 return std::unexpected(edited.error());
649 }
650
651 if (result && changed) {
652 auto invalidated = tree.invalidate(node, Invalidation::PAINT);
653
654 if (!invalidated) {
655 return std::unexpected(invalidated.error());
656 }
657 }
658
659 return result;
660}
661
662template <class T>
663std::expected<void, TreeError> setVirtualListFirst(WidgetTree &tree, Node<VirtualList<T>> node, std::uint64_t first) {
664 auto edited = tree.edit(node, [&](VirtualList<T> &view) {
665 if (view.scroll_.rasterOffset() != first || view.scroll_.fraction() != 0 || view.anchor_) {
666 (void)view.scroll_.setOffset(first);
667 view.anchor_.reset();
668 view.dirty_ = true;
669 view.intent_.reset();
670 view.request_.cancel();
671 }
672 });
673
674 return edited ? tree.invalidate(node, Invalidation::PAINT) : edited;
675}
676
677template <class T>
678std::expected<void, TreeError> setVirtualListSelection(WidgetTree &tree, Node<VirtualList<T>> node,
679 std::optional<RowId> selected) {
680 if (selected && (selected->value == 0 || selected->generation == 0)) {
681 return std::unexpected(TreeError::INVALID_ARGUMENT);
682 }
683
684 auto edited = tree.edit(node, [&](VirtualList<T> &view) {
685 view.selected_ = selected;
686 view.intent_.reset();
687 });
688
689 return edited ? tree.invalidate(node, Invalidation::PAINT) : edited;
690}
691
692template <class T>
693std::expected<void, TreeError> setVirtualListOptions(WidgetTree &tree, Node<VirtualList<T>> node,
694 VirtualListOptions options) {
695 if (options.row_height <= 0) {
696 return std::unexpected(TreeError::INVALID_ARGUMENT);
697 }
698
699 auto edited = tree.edit(node, [&](VirtualList<T> &view) {
700 view.options_ = options;
701 view.dirty_ = true;
702 view.request_.cancel();
703 });
704
705 return edited ? tree.invalidate(node, Invalidation::LAYOUT) : edited;
706}
707
708template <class T> std::expected<void, TreeError> retryVirtualList(WidgetTree &tree, Node<VirtualList<T>> node) {
709 auto edited = tree.edit(node, [&](VirtualList<T> &view) {
710 view.retry_ = true;
711 view.request_.cancel();
712 });
713
714 return edited ? tree.invalidate(node, Invalidation::PAINT) : edited;
715}
716
717template <class T>
718std::expected<ActionBinding, TreeError> bindVirtualList(EventRouter &router, WidgetTree &tree,
719 Node<VirtualList<T>> node) {
720 return router.bind(node, [&tree, node](VirtualList<T> &view, const RoutedEvent &event) {
722
723 if (const auto *action = std::get_if<ActionEvent>(&event)) {
724 reply = view.action(action->action);
725 if (action->action == Action::ACTIVATE && view.activation_ && view.selected_ && !view.preparedSuppressed_) {
726 view.activation_(*view.selected_);
727 reply = EventReply::HANDLED;
728 }
729 } else if (const auto *mouse = std::get_if<MouseInputEvent>(&event); mouse && view.prepared_) {
730 auto snapshot = tree.inspect(node);
731
732 if (!snapshot || !snapshot->geometry.valid || !snapshot->geometry.clip.contains({mouse->column, mouse->row})) {
733 return EventReply::IGNORED;
734 }
735
736 if (mouse->action == MouseAction::PRESS && mouse->button == MouseButton::LEFT && !view.dirty_) {
737 // Router coordinates are surface cells; translate using the current arranged and clipped geometry.
738 const auto y = static_cast<std::int64_t>(mouse->row) - snapshot->geometry.surface.origin().y;
739 const auto offset = static_cast<std::uint64_t>(y / view.options_.row_height);
740
741 if (offset < view.prepared_->window.visible.count) {
742 const auto clicked = view.prepared_->rows[static_cast<std::size_t>(offset)].id;
743 view.choose(view.prepared_->window.visible.first + offset);
744 if (view.clickActivates_ && view.activation_ && clicked) {
745 view.selected_ = clicked;
746 view.activation_(*clicked);
747 }
748 reply = EventReply::HANDLED;
749 }
750 } else if (mouse->action == MouseAction::SCROLL &&
751 (mouse->button == MouseButton::WHEEL_UP || mouse->button == MouseButton::WHEEL_DOWN)) {
752 const auto count = view.prepared_->frame.rowCount();
753
754 const double movement = mouse->button == MouseButton::WHEEL_UP ? -1.0 : 1.0;
755
756 (void)view.scroll_.moveBy(movement, count);
757 view.anchor_.reset();
758 view.dirty_ = true;
759 view.intent_.reset();
760 view.request_.cancel();
761 reply = EventReply::HANDLED;
762 }
763 }
764
765 if (reply == EventReply::HANDLED) {
766 (void)tree.invalidate(node, Invalidation::PAINT);
767 }
768
769 return reply;
770 });
771}
772
781template <class T>
782std::expected<void, TreeError> scrollVirtualList(WidgetTree &tree, Node<VirtualList<T>> node, double rows) {
783 std::expected<bool, TreeError> result;
784 auto edited = tree.edit(node, [&](VirtualList<T> &view) {
785 result = view.scrollBy(rows);
786 });
787
788 if (!edited) {
789 return edited;
790 }
791
792 if (!result) {
793 return std::unexpected(result.error());
794 }
795
796 return *result ? tree.invalidate(node, Invalidation::PAINT) : std::expected<void, TreeError>{};
797}
798
810template <class T>
811std::expected<RowLookup, TreeError> ensureVirtualListVisible(WidgetTree &tree, Node<VirtualList<T>> node,
812 const ModelFrame<T> &frame, RowId id) {
813 std::expected<RowLookup, TreeError> result;
814 auto edited = tree.edit(node, [&](VirtualList<T> &view) {
815 result = view.ensureVisible(frame, id);
816 });
817
818 if (!edited) {
819 return std::unexpected(edited.error());
820 }
821
822 if (result && result->state == RowLookupState::FOUND) {
823 auto invalidated = tree.invalidate(node, Invalidation::PAINT);
824
825 if (!invalidated) {
826 return std::unexpected(invalidated.error());
827 }
828 }
829
830 return result;
831}
832
833} // 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.
UI-owned focus and synchronous bubbling over an existing WidgetTree. Tree and Scheduler must outlive ...
Definition action.hpp:169
std::expected< ActionBinding, TreeError > bind(Node< T > node, Callback &&callback, bool focusable=true)
Register one typed handler; the scoped token owns disconnect responsibility. Callback must be copy-co...
Definition action.hpp:234
static std::expected< GraphemeText, TextError > from(std::string_view bytes, WidthPolicy policy={}, TextLimits limits={})
Validate, segment and measure a single logical UTF-8 buffer.
GraphemeText(const GraphemeText &other)=default
Copy bytes and metadata into independent storage; may allocate.
Additive lifecycle owner over one existing Application subtree. UI affine and nonmovable....
Definition managed.hpp:530
Move-only UI-owned range slot. Equal/overlapping consumers are independent. Close/destruction retires...
Definition model.hpp:698
One immutable model revision retained by the frame owner for all its views. Obtain once before Widget...
Definition model.hpp:400
Independent owner for borrowed rows and provider error text.
Definition model.hpp:175
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.
Owning one-dimensional logical position with floor quantization to integral rows. Integral and fracti...
Definition scroll.hpp:15
std::expected< void, TreeError > setOffset(std::uint64_t row, double fraction=0) noexcept
Replace the unbounded intended position; call clamp when row count is known.
double fraction() const noexcept
std::uint64_t rasterOffset() const noexcept
std::expected< bool, TreeError > moveBy(double rows, std::uint64_t count) noexcept
Accumulate signed fractional wheel movement, saturating at the first/last row.
Widget with owning policies, changed only through setSizePolicy on UI.
Definition layout.hpp:68
SizedWidget(SizePolicy policy={})
Own the supplied policies; checked by layout.
One UI-owned view with bounded owning row text and one independent model consumer....
Definition virtual_list.hpp:133
VirtualList(ModelConsumer< T > consumer, std::function< GraphemeText(const ModelRow< T > &)> delegate, VirtualListOptions options={}, SizePolicy policy=DEFAULT_VIEWPORT_POLICY)
Own consumer, delegate and options; validation happens on preparation.
Definition virtual_list.hpp:142
DataRangeStatus dataStatus() const noexcept
Definition virtual_list.hpp:189
std::optional< RowId > selection() const noexcept
Stable identity, retained across model revisions and hidden views.
Definition virtual_list.hpp:167
std::expected< CellSize, TreeError > measure(CellSize available) override
Viewport-sized intrinsic extent, independent of logical row count.
Definition virtual_list.hpp:198
std::uint64_t firstRow() const noexcept
Integral scroll position, clamped at preparation.
Definition virtual_list.hpp:152
std::expected< void, TreeError > paint(PaintContext &context) override
Emit widget-local commands; caller applies surface translation and ancestor clip.
Definition virtual_list.hpp:216
std::expected< void, TreeError > arrange(CellRect local) override
Remember local bounds without reading/loading/formatting rows.
Definition virtual_list.hpp:205
~VirtualList() override
Cancel work; worker ownership may outlive this view.
Definition virtual_list.hpp:147
RowRange visibleRange() const noexcept
Last successfully prepared visible rows.
Definition virtual_list.hpp:172
RowRange preparedRange() const noexcept
Last successfully prepared viewport plus overscan.
Definition virtual_list.hpp:177
ComputedStyle styleDefaults() const override
Definition virtual_list.hpp:247
std::optional< RowId > anchorRow() const noexcept
Definition virtual_list.hpp:162
RangeState rangeState() const noexcept
Last prepared model readiness; error text is owned by its retained model range.
Definition virtual_list.hpp:182
ScrollState scrollState() const noexcept
Definition virtual_list.hpp:157
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
std::expected< NodeSnapshot, TreeError > inspect(Node< Widget > node) const
Copy live structure; may allocate.
std::expected< void, TreeError > invalidate(Node< Widget > node, Invalidation kind)
Coalesce one frame request.
const std::shared_ptr< const StyleSnapshot > & computedStyle() const noexcept
Inspect the last atomically installed theme inside tree.read/frame.
consteval auto id(StyleState state=StyleState::ANY, bool readOnly=false)
Definition style_static.hpp:865
Public Portals API.
Definition action.hpp:8
std::expected< void, TreeError > setVirtualListFirst(WidgetTree &tree, Node< VirtualList< T > > node, std::uint64_t first)
Change the integral first row on UI and invalidate paint; prepare before painting again.
Definition virtual_list.hpp:663
ui::ValueText text(std::format_string< const ui::detail::TextArgumentValue< Args > &... > pattern, Args &&...args)
Declare live formatted text over explicit scalar roots, fields or value properties.
Definition ui_value_text.hpp:567
@ ACTIVATE
Activate the target's primary command.
Definition action.hpp:11
std::expected< ActionBinding, TreeError > bindVirtualList(EventRouter &router, WidgetTree &tree, Node< VirtualList< T > > node)
Bind arrows/Home/End, left press and integral wheel scrolling to the existing router....
Definition virtual_list.hpp:718
std::expected< void, TreeError > retryVirtualList(WidgetTree &tree, Node< VirtualList< T > > node)
Explicitly retry a provider error/pending range at the next preparation; no worker is started.
Definition virtual_list.hpp:708
std::expected< RowLookup, TreeError > ensureVirtualListVisible(WidgetTree &tree, Node< VirtualList< T > > node, const ModelFrame< T > &frame, RowId id)
Reveal a stable row using a caller-supplied shared pin and the last prepared viewport.
Definition virtual_list.hpp:811
@ SCROLL
Wheel movement in the direction given by MouseInputEvent::button.
Definition input.hpp:253
@ PRESS
Mouse button press.
Definition input.hpp:250
RangeState
Observable asynchronous range readiness.
Definition model.hpp:45
@ READY
Exactly the requested immutable rows are available.
Definition model.hpp:47
@ FAILED
Provider failure is represented by snapshot-owned text and no rows.
Definition model.hpp:48
@ PENDING
Data is not ready; reading must not block or start work.
Definition model.hpp:46
std::expected< void, TreeError > scrollVirtualList(WidgetTree &tree, Node< VirtualList< T > > node, double rows)
Accumulate fractional wheel input on UI using the last prepared model count.
Definition virtual_list.hpp:782
EventReply
Explicit bubbling decision; ignored continues toward the original root.
Definition action.hpp:49
@ IGNORED
Continue bubbling along the original ancestry.
Definition action.hpp:50
@ HANDLED
Stop bubbling; this registration consumed the event.
Definition action.hpp:51
std::expected< std::optional< ModelRequest< T > >, TreeError > prepareVirtualList(WidgetTree &tree, Node< VirtualList< T > > node, const ModelFrame< T > &frame, CellSize viewport)
Prepare a local final viewport between frames, using the same pin as every sibling view....
Definition virtual_list.hpp:624
constexpr SizePolicy DEFAULT_VIEWPORT_POLICY
A bounded data viewport fills both axes without requesting the whole model extent.
Definition layout.hpp:53
std::expected< void, TreeError > setVirtualListOptions(WidgetTree &tree, Node< VirtualList< T > > node, VirtualListOptions options)
Validate settings on UI, invalidate layout and require preparation before the next paint.
Definition virtual_list.hpp:693
std::variant< ActionEvent, KeyEvent, TextInputEvent, PasteInputEvent, MouseInputEvent > RoutedEvent
Synchronous input value; callback borrows it only until return.
Definition action.hpp:47
std::expected< void, TreeError > setVirtualListSelection(WidgetTree &tree, Node< VirtualList< T > > node, std::optional< RowId > selected)
Set stable selection on UI, clear pending navigation, and invalidate paint.
Definition virtual_list.hpp:678
@ FOUND
The index identifies this RowId in the same snapshot.
Definition model.hpp:74
@ NOT_FOUND
Provider supports lookup, but this full RowId is absent.
Definition model.hpp:73
@ WHEEL_UP
Vertical wheel movement upward.
Definition input.hpp:242
@ WHEEL_DOWN
Vertical wheel movement downward.
Definition input.hpp:243
@ LEFT
Left mouse button.
Definition input.hpp:238
@ LAYOUT
Layout and painting are required; old arranged geometry becomes invalid.
Definition widget_types.hpp:44
@ PAINT
Painting is required; arranged geometry remains usable.
Definition widget_types.hpp:43
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
@ CALLBACK_FAILED
A user extension or callback threw.
Definition widget_types.hpp:37
@ WOULD_BLOCK
The current frame/reentry boundary or finite queue/work budget rejects this operation.
Definition widget_types.hpp:32
@ 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
Owning resolved presentation; no actions or model state are encoded.
Definition style_types.hpp:169
CellStyle normal
Ordinary text and fill.
Definition style_types.hpp:170
CellStyle selected
Selected list/table row or editor range.
Definition style_types.hpp:173
Owning facts from the same successful virtual-view preparation, without another model read.
Definition data_presentation.hpp:29
Fill an owning half-open area with styled one-cell ASCII spaces.
Definition render.hpp:42
A typed domain row and its stable identity; a table uses a typed record as T.
Definition model.hpp:55
Provider-assigned row identity, independent of position; increment generation on reuse.
Definition row_id.hpp:9
Half-open logical row interval; arithmetic is checked before provider access.
Definition model.hpp:35
Owning independent policies for width and height.
Definition layout.hpp:35
Owning fixed-row presentation settings; invalid heights are rejected at preparation.
Definition virtual_list.hpp:18
CellStyle highlight
Prepared match emphasis over normal/selected appearance.
Definition virtual_list.hpp:24
WidthPolicy width_policy
Required policy for delegate and placeholder text.
Definition virtual_list.hpp:21
CellStyle selected
Selected row fill and text.
Definition virtual_list.hpp:23
std::size_t overscan_rows
Extra requested rows on each side, never formatted while invisible.
Definition virtual_list.hpp:20
std::int32_t row_height
Positive cell rows per logical row.
Definition virtual_list.hpp:19
CellStyle normal
Unselected row fill and text.
Definition virtual_list.hpp:22
Owning deterministic width policy; no font or terminal matching promise.
Definition text.hpp:42
Internal bridge for checked access to managed scene and recipe state.
Definition ui_recipe_access.hpp:28