Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
layout_traversal.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3#pragma once
4
5#include <algorithm>
6#include <portals/layout.hpp>
7
8namespace portals {
10}
11
12namespace portals::detail {
20inline std::expected<SizePolicy, TreeError> effectiveSizePolicy(SizePolicy policy, std::int32_t styleMinWidth,
21 const StyleSizing *sizing) {
22 const auto valid = [](AxisSize axis) {
23 return axis.minimum >= 0 && axis.maximum >= axis.minimum && axis.preferred >= INTRINSIC_EXTENT && axis.weight > 0 &&
24 (axis.mode == SizeMode::AUTOMATIC || axis.mode == SizeMode::FIXED || axis.mode == SizeMode::FLEX ||
25 axis.mode == SizeMode::CONTENT);
26 };
27
28 if (styleMinWidth < 0 || !valid(policy.width) || !valid(policy.height)) {
29 return std::unexpected(TreeError::INVALID_ARGUMENT);
30 }
31
32 policy.width.minimum = std::max(policy.width.minimum, styleMinWidth);
33
34 const auto intersect = [](AxisSize &axis, const std::optional<StyleAxisSize> &request,
35 std::optional<std::int32_t> minimum, std::optional<std::int32_t> maximum) {
36 if ((minimum && *minimum < 0) || (maximum && *maximum < 0)) {
37 return false;
38 }
39
40 if (minimum) {
41 axis.minimum = std::max(axis.minimum, *minimum);
42 }
43
44 if (maximum) {
45 axis.maximum = std::min(axis.maximum, *maximum);
46 }
47
48 if (request) {
49 if (request->weight == 0 || request->extent < 0) {
50 return false;
51 }
52
53 axis.preferred = INTRINSIC_EXTENT;
54 axis.weight = 1;
55
56 switch (request->mode) {
58 axis.minimum = std::max(axis.minimum, request->extent);
59 axis.maximum = std::min(axis.maximum, request->extent);
60 axis.preferred = request->extent;
61 axis.mode = SizeMode::FIXED;
62
63 break;
65 axis.mode = SizeMode::AUTOMATIC;
66
67 break;
69 axis.mode = SizeMode::CONTENT;
70
71 break;
73 axis.mode = SizeMode::FLEX;
74 axis.preferred = 0;
75 axis.weight = request->weight;
76
77 break;
78 default:
79 return false;
80 }
81 }
82
83 return axis.maximum >= axis.minimum;
84 };
85
86 if (sizing && (!intersect(policy.width, sizing->width, {}, sizing->maxWidth) ||
87 !intersect(policy.height, sizing->height, sizing->minHeight, sizing->maxHeight))) {
88 return std::unexpected(TreeError::INVALID_ARGUMENT);
89 }
90
91 if (policy.width.maximum < policy.width.minimum) {
92 return std::unexpected(TreeError::INVALID_ARGUMENT);
93 }
94
95 return policy;
96}
97
103inline std::expected<SizePolicy, TreeError> effectiveSizePolicy(const Widget &widget, std::int32_t styleMinWidth,
104 const StyleSizing *sizing = nullptr) {
105 const auto *item = dynamic_cast<const LayoutItem *>(&widget);
106
107 return effectiveSizePolicy(item ? item->sizePolicy() : SizePolicy{}, styleMinWidth, sizing);
108}
109
113inline std::expected<SizePolicy, TreeError> effectiveSizePolicy(const Widget &widget) {
114 const auto &style = widget.computedStyle();
115
116 return effectiveSizePolicy(widget, style ? style->minWidth : 0, style ? style->sizing() : nullptr);
117}
118
124 bool open{};
125 std::uint64_t geometryEpoch{};
126};
127
131 std::span<const RegionalInputProposal> children;
132};
133
145 static std::expected<void, TreeError> arrange(WidgetTree &tree, Node<Widget> node, CellRect local, CellRect viewport,
146 const ParentLayoutEdge &edge) {
147 return tree.arrangeImpl(node, local, viewport, edge.surfaceClip, &edge);
148 }
149
157 static std::expected<bool, TreeError> adoptRegionalInput(WidgetTree &tree, const LayoutSnapshot &snapshot,
158 std::span<const RegionalInputOwnerProposal> owners);
159};
160} // namespace portals::detail
An owning, invariant-preserving half-open cell rectangle.
Definition geometry.hpp:81
Owning immutable identities and local sizes from one completed layout pass. Copies share immutable va...
Definition layout_snapshot.hpp:43
Copyable typed observer; never owns or exposes an unchecked widget pointer.
Definition widget.hpp:92
Exclusive parent/child ownership on Scheduler's UI thread. Scheduler must outlive this object....
Definition widget.hpp:179
Public Portals API.
Definition action.hpp:8
@ AUTOMATIC
Use the ordinary preferred/intrinsic allocation.
Definition style_types.hpp:140
@ CONTENT
Prefer intrinsic content without consuming spare cells.
Definition style_types.hpp:143
@ FIXED
Intersect an exact cell extent with all hard bounds.
Definition style_types.hpp:141
@ FLEX
Share spare cells using the positive weight.
Definition style_types.hpp:142
@ AUTOMATIC
Use the preferred/intrinsic extent; shrink towards minimum when space is scarce.
Definition layout.hpp:13
@ CONTENT
Prefer intrinsic size, shrink towards minimum, and never grow to consume spare cells.
Definition layout.hpp:16
@ FIXED
Keep the bounded preferred/intrinsic extent even when it overflows the available space.
Definition layout.hpp:14
@ FLEX
Share spare main-axis cells by weight; allow shrinking towards minimum.
Definition layout.hpp:15
constexpr std::int32_t INTRINSIC_EXTENT
Sentinel requesting the widget's intrinsic measurement.
Definition layout.hpp:20
@ INVALID_ARGUMENT
An option, enum value or operation argument is invalid.
Definition widget_types.hpp:27
Owning identity, independent of addresses; zero is never a live identity.
Definition widget_types.hpp:15
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
Internal checked bridge for owning layout edges and regional input. Public arrange retains its ordina...
Definition layout_traversal.hpp:137
static std::expected< bool, TreeError > adoptRegionalInput(WidgetTree &tree, const LayoutSnapshot &snapshot, std::span< const RegionalInputOwnerProposal > owners)
Validate and allocate the whole batch before publishing any gate.
static std::expected< void, TreeError > arrange(WidgetTree &tree, Node< Widget > node, CellRect local, CellRect viewport, const ParentLayoutEdge &edge)
Definition layout_traversal.hpp:145
Complete borrowed proposals for one owner, consumed synchronously.
Definition layout_traversal.hpp:129
Node< Widget > owner
Checked identity whose direct children are governed.
Definition layout_traversal.hpp:130
std::span< const RegionalInputProposal > children
Borrow ends when adoption returns.
Definition layout_traversal.hpp:131
One fresh edge proposal in an owner's complete desired set. An absent child clears only that owner's ...
Definition layout_traversal.hpp:122
bool open
Whether the current region admits input.
Definition layout_traversal.hpp:124
std::uint64_t geometryEpoch
Nonreused arrangement stamp.
Definition layout_traversal.hpp:125
NodeId child
Full direct-child identity.
Definition layout_traversal.hpp:123