Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
layout.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3#pragma once
4
5#include <limits>
6#include <portals/widget.hpp>
7#include <span>
8
9namespace portals {
10
18
20inline constexpr std::int32_t INTRINSIC_EXTENT = -1;
21
23struct AxisSize {
24 std::int32_t minimum{};
26 std::int32_t maximum{std::numeric_limits<std::int32_t>::max()};
28 std::uint32_t weight{1};
29
31 constexpr bool operator==(const AxisSize &other) const = default;
32};
33
35struct SizePolicy {
38
40 constexpr bool operator==(const SizePolicy &other) const = default;
41};
42
44inline constexpr SizePolicy DEFAULT_CONTENT_POLICY{.width = {.mode = SizeMode::CONTENT},
45 .height = {.mode = SizeMode::CONTENT}};
46
47inline constexpr SizePolicy DEFAULT_FLEX_WIDTH_POLICY{.width = {.preferred = 0, .mode = SizeMode::FLEX},
48 .height = {.mode = SizeMode::CONTENT}};
49
50inline constexpr SizePolicy DEFAULT_EDITOR_POLICY{.width = {.minimum = 1, .preferred = 0, .mode = SizeMode::FLEX},
51 .height = {.mode = SizeMode::CONTENT}};
52
53inline constexpr SizePolicy DEFAULT_VIEWPORT_POLICY{.width = {.preferred = 0, .mode = SizeMode::FLEX},
54 .height = {.preferred = 0, .mode = SizeMode::FLEX}};
55
58public:
59 virtual ~LayoutItem() = default;
60
64 virtual SizePolicy sizePolicy() const = 0;
65};
66
68class SizedWidget : public Widget, public LayoutItem {
69public:
73 explicit SizedWidget(SizePolicy policy = {});
74
78 SizePolicy sizePolicy() const override;
79
80private:
81 friend std::expected<void, TreeError> setSizePolicy(WidgetTree &, Node<SizedWidget>, SizePolicy);
82 SizePolicy policy_;
83};
84
88class Spacer final : public Widget {
89public:
91 explicit Spacer(std::int32_t weight = 1) : weight_(weight) {
92 }
93
95 std::int32_t weight() const {
96 return weight_;
97 }
98
99private:
100 std::int32_t weight_;
101};
102
104class Spacing final : public Widget {
105public:
107 explicit Spacing(std::int32_t cells) : cells_(cells) {
108 }
109
111 std::int32_t cells() const {
112 return cells_;
113 }
114
115private:
116 std::int32_t cells_;
117};
118
125
130class Layout {
131public:
132 virtual ~Layout() = default;
133
138 virtual std::expected<CellSize, TreeError> childAvailable(CellSize available) const = 0;
139
146 virtual std::expected<CellRect, TreeError> childClip(CellSize available) const;
147
154 virtual std::expected<LayoutChildPolicy, TreeError> childLayoutFor(NodeId child, CellSize available) const;
155
160 virtual std::expected<CellSize, TreeError> measureChildren(std::span<const MeasuredChild> children) const = 0;
161
167 virtual std::expected<std::vector<CellRect>, TreeError> arrangeChildren(std::span<const MeasuredChild> children,
168 CellSize available) const = 0;
169};
170
178
180enum class LayoutOverflow {
182};
183
186 std::int32_t left{};
187 std::int32_t top{};
188 std::int32_t right{};
189 std::int32_t bottom{};
190};
191
200
202class LinearLayout : public SizedWidget, public Layout {
203public:
208 std::expected<CellSize, TreeError> childAvailable(CellSize available) const override;
209
214 std::expected<CellSize, TreeError> measureChildren(std::span<const MeasuredChild> children) const override;
215
222 std::expected<std::vector<CellRect>, TreeError> arrangeChildren(std::span<const MeasuredChild> children,
223 CellSize available) const override;
224
229
231 bool horizontal() const {
232 return horizontal_;
233 }
234
235protected:
242
243private:
244 friend std::expected<void, TreeError> setLinearOptions(WidgetTree &, Node<LinearLayout>, LinearOptions);
245 bool horizontal_;
246 LinearOptions options_;
247};
248
250class Row : public LinearLayout {
251public:
256 explicit Row(LinearOptions options = {}, SizePolicy policy = {});
257};
258
260class Column : public LinearLayout {
261public:
266 explicit Column(LinearOptions options = {}, SizePolicy policy = {});
267};
268
277std::expected<void, TreeError> setSizePolicy(WidgetTree &tree, Node<SizedWidget> node, SizePolicy policy);
278
285std::expected<void, TreeError> setLinearOptions(WidgetTree &tree, Node<LinearLayout> node, LinearOptions options);
286
315std::expected<void, TreeError> layoutTree(WidgetTree &tree, Node<Widget> node, CellRect local, CellRect viewport);
316
317} // namespace portals
An owning, invariant-preserving half-open cell rectangle.
Definition geometry.hpp:81
Column(LinearOptions options={}, SizePolicy policy={})
Own settings and policies.
Optional const sizing interface, read only within tree.read.
Definition layout.hpp:57
virtual SizePolicy sizePolicy() const =0
Return an owning policy; no tree/model mutation or I/O.
virtual ~LayoutItem()=default
Destroy with the implementing widget.
Pure container interface; callbacks run inside tree.read and an enclosing tree.frame....
Definition layout.hpp:130
virtual std::expected< CellSize, TreeError > childAvailable(CellSize available) const =0
Measurement bounds.
virtual std::expected< std::vector< CellRect >, TreeError > arrangeChildren(std::span< const MeasuredChild > children, CellSize available) const =0
Own parent-local rectangles in child order.
virtual std::expected< CellSize, TreeError > measureChildren(std::span< const MeasuredChild > children) const =0
Own aggregate.
virtual std::expected< LayoutChildPolicy, TreeError > childLayoutFor(NodeId child, CellSize available) const
Describe one child's clip and reveal policy after arrangeChildren.
virtual ~Layout()=default
Destroy with the implementing widget.
virtual std::expected< CellRect, TreeError > childClip(CellSize available) const
Clip descendants within this container without changing allocation.
std::expected< CellSize, TreeError > childAvailable(CellSize available) const override
Checked inset extent.
bool horizontal() const
Definition layout.hpp:231
LinearOptions options() const
Owning copy; no invalidation.
std::expected< std::vector< CellRect >, TreeError > arrangeChildren(std::span< const MeasuredChild > children, CellSize available) const override
Deterministic bounded solve.
LinearLayout(bool horizontal, LinearOptions options, SizePolicy policy)
Initialize axis and owning settings.
std::expected< CellSize, TreeError > measureChildren(std::span< const MeasuredChild > children) const override
Checked sum/max.
friend std::expected< void, TreeError > setLinearOptions(WidgetTree &, Node< LinearLayout >, LinearOptions)
Validate, edit and invalidate layout between frames on the tree's UI owner; tree errors propagate.
Copyable typed observer; never owns or exposes an unchecked widget pointer.
Definition widget.hpp:92
Row(LinearOptions options={}, SizePolicy policy={})
Own settings and policies.
friend std::expected< void, TreeError > setSizePolicy(WidgetTree &, Node< SizedWidget >, SizePolicy)
Validate and edit policies between frames on the tree's UI owner; tree errors propagate....
SizedWidget(SizePolicy policy={})
Own the supplied policies; checked by layout.
SizePolicy sizePolicy() const override
Copy current policy without borrowing storage.
Spacer(std::int32_t weight=1)
Definition layout.hpp:91
std::int32_t weight() const
Definition layout.hpp:95
std::int32_t cells() const
Definition layout.hpp:111
Spacing(std::int32_t cells)
Definition layout.hpp:107
Exclusive parent/child ownership on Scheduler's UI thread. Scheduler must outlive this object....
Definition widget.hpp:179
UI-owned extension object. Hooks borrow their arguments for one call. Models are separate explicit ow...
Definition widget.hpp:32
Public Portals API.
Definition action.hpp:8
std::expected< void, TreeError > layoutTree(WidgetTree &tree, Node< Widget > node, CellRect local, CellRect viewport)
Measure and arrange a subtree inside an active tree frame.
constexpr SizePolicy DEFAULT_EDITOR_POLICY
Single-line editors retain one cell even during shortage; text length is not a flex basis.
Definition layout.hpp:50
SizeMode
Independent axis allocation policy, in integer terminal cells.
Definition layout.hpp:12
@ 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
std::expected< void, TreeError > setSizePolicy(WidgetTree &tree, Node< SizedWidget > node, SizePolicy policy)
Validate and edit policies between frames on the tree's UI owner; tree errors propagate....
constexpr SizePolicy DEFAULT_CONTENT_POLICY
Intrinsic content on both axes; explicit policies remain authoritative.
Definition layout.hpp:44
constexpr SizePolicy DEFAULT_VIEWPORT_POLICY
A bounded data viewport fills both axes without requesting the whole model extent.
Definition layout.hpp:53
constexpr std::int32_t INTRINSIC_EXTENT
Sentinel requesting the widget's intrinsic measurement.
Definition layout.hpp:20
std::expected< void, TreeError > setLinearOptions(WidgetTree &tree, Node< LinearLayout > node, LinearOptions options)
Validate, edit and invalidate layout between frames on the tree's UI owner; tree errors propagate.
LayoutOverflow
Minimum/fixed sizes survive shortage and are clipped by ancestor geometry.
Definition layout.hpp:180
@ CLIP
Preserve hard minima and fixed extents; clip content that does not fit the viewport.
Definition layout.hpp:181
LayoutAlign
Alignment; center assigns floor(free/2) before the child/group.
Definition layout.hpp:172
@ STRETCH
Fill available cross-axis space within size bounds; acts as START on the linear main axis.
Definition layout.hpp:176
@ START
Place at the leading edge of the available space.
Definition layout.hpp:173
@ CENTER
Center in the usable viewport, rounding down.
Definition dialog.hpp:22
constexpr SizePolicy DEFAULT_FLEX_WIDTH_POLICY
Fill available width from a zero flex basis while retaining content height.
Definition layout.hpp:47
@ START
Immediately after the leading corner.
Definition dialog.hpp:35
TreeError
Checked tree failures; rejected operations never start the requested callback.
Definition widget_types.hpp:26
Owning per-axis bounds; preferred=-1 or CONTENT uses intrinsic measurement.
Definition layout.hpp:23
std::int32_t minimum
Nonnegative hard minimum.
Definition layout.hpp:24
SizeMode mode
Fixed does not shrink; flex shares spare cells.
Definition layout.hpp:27
constexpr bool operator==(const AxisSize &other) const =default
std::uint32_t weight
Positive flex weight.
Definition layout.hpp:28
std::int32_t maximum
Inclusive cap.
Definition layout.hpp:26
std::int32_t preferred
Intrinsic when -1, otherwise requested size.
Definition layout.hpp:25
An owning available extent in terminal columns/rows.
Definition geometry.hpp:44
Owning nonnegative padding in terminal cells.
Definition layout.hpp:185
std::int32_t bottom
Trailing rows.
Definition layout.hpp:189
std::int32_t left
Leading columns.
Definition layout.hpp:186
std::int32_t right
Trailing columns.
Definition layout.hpp:188
std::int32_t top
Leading rows.
Definition layout.hpp:187
Owning linear container options.
Definition layout.hpp:193
LayoutPadding padding
Insets, retained during shortage.
Definition layout.hpp:195
std::int32_t gap
Cells between visible children only.
Definition layout.hpp:194
LayoutAlign cross_align
Child alignment across the linear direction.
Definition layout.hpp:197
LayoutAlign main_align
Group alignment after allocation; stretch is start on this axis.
Definition layout.hpp:196
LayoutOverflow overflow
Explicit shortage policy.
Definition layout.hpp:198
Owning child input for a pure layout computation.
Definition layout.hpp:120
CellSize desired
Intrinsic or aggregated nonnegative measurement.
Definition layout.hpp:121
SizePolicy policy
Copied policies; no widget references.
Definition layout.hpp:122
NodeId node
Stable owning identity, including when siblings are hidden.
Definition layout.hpp:123
Owning identity, independent of addresses; zero is never a live identity.
Definition widget_types.hpp:15
Owning independent policies for width and height.
Definition layout.hpp:35
AxisSize width
Horizontal policy.
Definition layout.hpp:36
constexpr bool operator==(const SizePolicy &other) const =default
AxisSize height
Vertical policy.
Definition layout.hpp:37