5#include <portals/scheduler.hpp>
6#include <portals/style_types.hpp>
7#include <portals/widget_types.hpp>
84 friend class StyleEngine;
86 std::uint64_t styleRevision_{};
92template <
class T = W
idget>
class Node {
102 requires std::derived_from<U, T>
120 friend class WidgetTree;
163 std::vector<Node<Widget>>
children = std::vector<Node<Widget>>(0);
191 static std::expected<std::unique_ptr<WidgetTree>,
TreeError>
195 WidgetTree(
const WidgetTree &) =
delete;
196 WidgetTree &operator=(
const WidgetTree &) =
delete;
222 std::expected<void, TreeError>
close();
234 template <
class T,
class Callback> std::expected<void, TreeError>
read(
Node<T> node, Callback &&callback) {
235 static_assert(std::is_void_v<std::invoke_result_t<Callback, const T &>>);
237 auto value = access(node.
id(),
false);
240 return std::unexpected(value.error());
246 std::invoke(std::forward<Callback>(callback),
static_cast<const T &
>(**value));
267 template <
class T,
class... Args>
268 requires std::derived_from<T, Widget>
270 return emplaceImpl<T>(parent,
false, std::forward<Args>(args)...);
283 template <
class T,
class... Args>
284 requires std::derived_from<T, Widget>
286 auto node = emplaceImpl<T>(parent,
true, std::forward<Args>(args)...);
293 auto live = access(node->id(),
false);
296 return std::unexpected(live.error());
310 template <
class T,
class Callback> std::expected<void, TreeError>
edit(
Node<T> node, Callback &&callback) {
311 static_assert(std::is_void_v<std::invoke_result_t<Callback, T &>>);
313 auto value = access(node.
id(),
true);
316 return std::unexpected(value.error());
322 std::invoke(std::forward<Callback>(callback),
static_cast<T &
>(**value));
341 auto value = access(node.
id(),
true);
344 return std::unexpected(value.error());
350 std::invoke(std::forward<Callback>(callback), scope(node.
id()));
375 template <
class Value,
class T,
class Callback>
379 auto value = access(node.
id(),
true);
382 return std::unexpected(value.error());
388 auto result = scheduler().connect<Value>(
390 [
this, node, function = std::forward<Callback>(callback)](std::shared_ptr<const Value> item)
mutable {
391 auto status =
edit(node, [&](T &widget) {
392 std::invoke(function, widget, std::move(item));
402 return std::unexpected(fromScheduler(result.error()));
405 return std::move(*result);
406 }
catch (
const std::bad_alloc &) {
421 template <
class T,
class Callback>
423 auto value = access(node.
id(),
true);
426 return std::unexpected(value.error());
433 scheduler().at(scope(node.
id()), deadline, [
this, node, function = std::forward<Callback>(callback)]()
mutable {
434 auto status = edit(node, [&](T &widget) {
435 std::invoke(function, widget);
444 return std::unexpected(fromScheduler(result.error()));
447 return std::move(*result);
448 }
catch (
const std::bad_alloc &) {
538 template <class Callback> std::expected<
void,
TreeError>
frame(Callback &&callback) {
539 static_assert(std::is_void_v<std::invoke_result_t<Callback>>);
541 auto allowed = beginFrame();
547 Borrow guard(*
this,
true);
551 std::invoke(std::forward<Callback>(callback));
563 friend struct detail::RowTreeAccess;
570 std::expected<bool, TreeError> revealVisible(
Node<Widget> node)
const;
571 std::expected<std::uint64_t, TreeError> inputRevision(
Node<Widget> node)
const;
579 struct GeometryObservation {
581 std::uint64_t visibilityRevision{};
585 std::expected<GeometryObservation, TreeError> observeGeometry(
Node<Widget> node)
const;
588 std::expected<NodeGeometry, TreeError> paintGeometry(
Node<Widget> node)
const;
589 std::expected<std::uint64_t, TreeError> geometryRevision()
const;
590 std::uint64_t inputGeneration() const noexcept;
596 explicit Borrow(
WidgetTree &tree,
bool frame =
false);
602 explicit WidgetTree(std::unique_ptr<State> state);
603 std::expected<Widget *, TreeError> access(
NodeId id,
bool mutation)
const;
604 std::expected<void, TreeError> canInsert(
NodeId parent)
const;
605 std::expected<NodeId, TreeError> attach(
NodeId parent, std::unique_ptr<Widget> widget,
bool checked);
607 template <
class T,
class... Args>
609 auto allowed = canInsert(parent.id());
612 return std::unexpected(allowed.error());
618 auto value = std::make_unique<T>(std::forward<Args>(args)...);
619 auto id = attach(parent.id(), std::move(value), checked);
622 return std::unexpected(
id.error());
626 }
catch (
const std::bad_alloc &) {
638 std::optional<CellRect> contentClip,
640 std::expected<void, TreeError> beginFrame()
const;
645 void reclaim() noexcept;
646 std::unique_ptr<State> state_;
An owning, invariant-preserving half-open cell rectangle.
Definition geometry.hpp:81
Stack-only causal context on one thread; restores the previous scope on every exit....
Definition diagnostics.hpp:210
Explicit, thread-safe metadata recorder. Disabled by default; no clocks or I/O. enable/disable/snapsh...
Definition diagnostics.hpp:141
UI-owned focus and synchronous bubbling over an existing WidgetTree. Tree and Scheduler must outlive ...
Definition action.hpp:169
Owning immutable identities and local sizes from one completed layout pass. Copies share immutable va...
Definition layout_snapshot.hpp:43
Additive lifecycle owner over one existing Application subtree. UI affine and nonmovable....
Definition managed.hpp:530
Copyable typed observer; never owns or exposes an unchecked widget pointer.
Definition widget.hpp:92
Node(Node< U > other)
Safe observing upcast; downcasts are unavailable.
Definition widget.hpp:103
Node()=default
Empty observer; checked access fails.
bool operator==(const Node &other) const noexcept=default
Compare identity.
NodeId id() const noexcept
Copy the stable diagnostic identity.
Definition widget.hpp:109
Borrowed synchronous command sink supplied by the frame owner. submit must consume commands immediate...
Definition widget_types.hpp:66
Ordinary one-focus-stop, model-owned resident radio group with clipped row painting.
Definition radio_group.hpp:86
One UI executor; call dispatch only between frames, never during layout/paint. create captures its ca...
Definition scheduler.hpp:238
UI-owned style transaction coordinator; tree must outlive this nonmovable object. Bindings own metada...
Definition style.hpp:383
Immutable resident presentation with independently sparse extension blocks. Widget bindings share equ...
Definition style_types.hpp:231
UI-owned scope disconnecting all its subscriptions and timers, in any token order....
Definition scheduler.hpp:154
Public Portals API.
Definition action.hpp:8
SchedulerError
Observable operation failures; no callback is started on rejection.
Definition scheduler.hpp:20
SchedulerOverflow
Replace only this sender's pending display snapshot; never raw domain events.
Definition scheduler.hpp:37
@ REJECT
Reject publication when the lane has no free queue slot.
Definition scheduler.hpp:38
SchedulerLane
Independent finite queues; control is serviced before data each cycle.
Definition scheduler.hpp:32
@ DATA
Ordinary immutable data publications.
Definition scheduler.hpp:33
RevealAxes
Axes an ancestor layout may move when revealing this child.
Definition widget.hpp:130
@ HORIZONTAL_ONLY
Keep a sticky region's vertical position.
Definition widget.hpp:132
@ BOTH
Reveal horizontally and vertically.
Definition widget.hpp:131
@ CLOSED
Owner, node scope, scheduler or completion endpoint is closed.
Definition async.hpp:28
RegionalInput
Proposed input admission for a layout region, independent of visibility.
Definition widget.hpp:136
@ UNCHANGED
Use ordinary visible/enabled ancestry.
Definition widget.hpp:137
std::uint64_t SchedulerTime
Monotonic milliseconds; the clock must not move backwards.
Definition scheduler.hpp:18
@ UNCHANGED
An equal set succeeded, or a clean editor lost focus without attempting publication.
Definition line_edit.hpp:130
Invalidation
Accumulated work; layout always includes painting.
Definition widget_types.hpp:41
@ OPEN
Enter a run and initialize its transport and runner state.
Definition application.hpp:17
TreeError
Checked tree failures; rejected operations never start the requested callback.
Definition widget_types.hpp:26
@ CALLBACK_FAILED
A user extension or callback threw.
Definition widget_types.hpp:37
@ 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
Owning resolved presentation; no actions or model state are encoded.
Definition style_types.hpp:169
Fixed-size causal token; zero fields mean unobserved, never a reused identity.
Definition diagnostics.hpp:92
Owning pure layout result for one direct child.
Definition widget.hpp:143
CellRect localClip
Clip in the parent's local cell coordinates.
Definition widget.hpp:144
RevealAxes revealAxes
Allowed movement of this particular ancestor.
Definition widget.hpp:145
RegionalInput proposedInput
Admission to stage outside the layout frame.
Definition widget.hpp:146
Owning arranged geometry; invalid geometry must never be used for hit-testing.
Definition widget_types.hpp:48
Owning identity, independent of addresses; zero is never a live identity.
Definition widget_types.hpp:15
Owning snapshot of a live node; handles remain checked after subsequent mutations.
Definition widget.hpp:160
bool visible
Hidden subtrees do not participate in layout, paint or input.
Definition widget.hpp:165
NodeGeometry geometry
Owning geometry snapshot.
Definition widget.hpp:164
std::vector< Node< Widget > > children
Live children in insertion order.
Definition widget.hpp:163
bool regionalInputOpen
Committed direct-edge admission, independent of user flags.
Definition widget.hpp:169
std::optional< std::uint64_t > geometryEpoch
Successful arrangement stamp, absent after invalidation.
Definition widget.hpp:168
Node< Widget > parent
Empty only for the root.
Definition widget.hpp:161
bool enabled
Disabled subtrees retain layout/paint but do not receive input.
Definition widget.hpp:166
std::optional< ParentLayoutEdge > parentLayoutEdge
Optional specialized parent policy, copied by value.
Definition widget.hpp:167
Owning arranged edge before intersection with any ancestor's clip.
Definition widget.hpp:150
CellRect surfaceClip
Owner clip in surface coordinates, before intersection.
Definition widget.hpp:153
RevealAxes revealAxes
Reveal axes for this edge only.
Definition widget.hpp:154
std::uint64_t geometryEpoch
Child's nonreused successful arrangement stamp.
Definition widget.hpp:156
NodeId parent
Exact owner identity.
Definition widget.hpp:151
NodeId child
Exact direct child identity.
Definition widget.hpp:152
RegionalInput proposedInput
Pure proposal, not a mutation of input state.
Definition widget.hpp:155
Internal checked bridge for owning layout edges and regional input. Public arrange retains its ordina...
Definition layout_traversal.hpp:137
Internal checked named publication bridge; no widget or scene ownership escapes.
Definition ui_ref.hpp:2399