Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
grid.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3#pragma once
4
5#include <optional>
6#include <portals/layout.hpp>
7#include <portals/scroll_controller.hpp>
8#include <portals/scroll_options.hpp>
9
10namespace portals {
11namespace detail {
12struct ScrollWidgetState;
13struct ScrollWidgetAccess;
14struct GridLayoutState;
18std::expected<void, TreeError> validateGridTrackSizes(std::span<const AxisSize> policies) noexcept;
19} // namespace detail
20
22enum class GridRegion {
25};
26
29 std::optional<CellStyle> even;
30 std::optional<CellStyle> odd;
31 bool operator==(const GridRowStyles &) const = default;
32};
33
36 std::optional<CellStyle> header;
37 std::optional<CellStyle> body;
39 std::size_t maxPaintCells{16384};
40 bool operator==(const GridPresentation &) const = default;
41};
42
47 value.focusable = false;
48 return value;
49 }();
50 std::shared_ptr<ScrollController> controller;
51};
52
57
63
66 std::optional<std::size_t> row;
67 std::optional<std::size_t> column;
68 std::size_t rowSpan{1};
69 std::size_t columnSpan{1};
73};
74
77 std::optional<std::size_t> columns;
78 std::optional<std::size_t> rows;
80 std::int32_t columnGap{};
81 std::int32_t rowGap{};
83 std::size_t maxTracksPerAxis{4096};
84 std::size_t maxPlacements{4096};
85 std::size_t maxPlacementWork{65536};
86 bool regions{};
87 bool stickyHeader{};
88};
89
92 std::size_t row{};
93 std::size_t column{};
94};
95
98 ~GridPlacementPlan() = default;
99 std::size_t columns{};
100 std::size_t rows{};
101 std::vector<GridPosition> positions = std::vector<GridPosition>(0);
102 std::size_t headerRows{};
103};
104
111std::expected<GridPlacementPlan, TreeError> resolveGridPlacements(const DeclarativeGridOptions &options,
112 std::span<const GridItemOptions> items);
113
125
128 std::vector<AxisSize> columns{AxisSize{}};
129 std::vector<AxisSize> rows{AxisSize{}};
130 std::int32_t column_gap{};
131 std::int32_t row_gap{};
133 std::vector<GridCell> cells = std::vector<GridCell>(0);
134 std::size_t max_tracks_per_axis{4096};
135 std::size_t max_placements{4096};
136 std::vector<AxisSize> header_rows = std::vector<AxisSize>(0);
138 bool regions{};
140};
141
149class Grid : public SizedWidget, public Layout {
150public:
156 explicit Grid(const GridOptions &options = {}, SizePolicy policy = {});
166 std::optional<GridScrollOptions> scroll = {}, WidthPolicy width = {});
168 ~Grid() override;
170 void unmount() override;
174 std::expected<void, TreeError> arrange(CellRect local) override;
178 std::expected<CellRect, TreeError> childClip(CellSize available) const override;
182 std::expected<LayoutChildPolicy, TreeError> childLayoutFor(NodeId child, CellSize available) const override;
186 std::expected<void, TreeError> paint(PaintContext &context) override;
188 ComputedStyle styleDefaults() const override;
195 bool usesRegions() const noexcept;
200 std::expected<CellSize, TreeError> childAvailable(CellSize available) const override;
205 std::expected<CellSize, TreeError> measureChildren(std::span<const MeasuredChild> children) const override;
212 std::expected<std::vector<CellRect>, TreeError> arrangeChildren(std::span<const MeasuredChild> children,
213 CellSize available) const override;
214
215private:
216 friend std::expected<void, TreeError> setGridOptions(WidgetTree &, Node<Grid>, const GridOptions &);
217 friend std::expected<void, TreeError> setDeclarativeGridOptions(WidgetTree &, Node<Grid>, const GridOptions &);
218 friend std::expected<void, TreeError> setGridPresentation(WidgetTree &, Node<Grid>, const GridPresentation &);
219 friend struct detail::ScrollWidgetAccess;
220 GridOptions options_;
222 std::unique_ptr<detail::ScrollWidgetState> scroll_;
223 mutable std::unique_ptr<detail::GridLayoutState> layout_;
224};
225
232
238class Overlay : public SizedWidget, public Layout {
239public:
244 explicit Overlay(OverlayOptions options = {}, SizePolicy policy = {});
253 std::expected<CellSize, TreeError> childAvailable(CellSize available) const override;
258 std::expected<CellSize, TreeError> measureChildren(std::span<const MeasuredChild> children) const override;
265 std::expected<std::vector<CellRect>, TreeError> arrangeChildren(std::span<const MeasuredChild> children,
266 CellSize available) const override;
267
268private:
269 friend std::expected<void, TreeError> setOverlayOptions(WidgetTree &, Node<Overlay>, OverlayOptions);
270 OverlayOptions options_;
271};
272
281std::expected<void, TreeError> setGridOptions(WidgetTree &tree, Node<Grid> node, const GridOptions &options);
289std::expected<void, TreeError> setDeclarativeGridOptions(WidgetTree &tree, Node<Grid> node, const GridOptions &options);
295std::expected<void, TreeError> setGridPresentation(WidgetTree &tree, Node<Grid> node,
296 const GridPresentation &presentation);
303std::expected<void, TreeError> setOverlayOptions(WidgetTree &tree, Node<Overlay> node, OverlayOptions options);
304} // namespace portals
An owning, invariant-preserving half-open cell rectangle.
Definition geometry.hpp:81
void unmount() override
Retire an optional scroll lease, pointer capture and intent before destruction.
friend std::expected< void, TreeError > setDeclarativeGridOptions(WidgetTree &, Node< Grid >, const GridOptions &)
Atomically replace resolved declarative settings between frames on the UI owner.
std::expected< std::vector< CellRect >, TreeError > arrangeChildren(std::span< const MeasuredChild > children, CellSize available) const override
Child-order local rectangles.
std::expected< CellSize, TreeError > childAvailable(CellSize available) const override
Checked inset bounds.
std::expected< CellRect, TreeError > childClip(CellSize available) const override
~Grid() override
Release owning caches without callbacks or terminal operations.
friend std::expected< void, TreeError > setGridOptions(WidgetTree &, Node< Grid >, const GridOptions &)
Validate and atomically copy settings on the UI owner between frames, then invalidate layout....
std::expected< void, TreeError > arrange(CellRect local) override
friend std::expected< void, TreeError > setGridPresentation(WidgetTree &, Node< Grid >, const GridPresentation &)
Replace owning backgrounds between frames on the UI owner.
GridOptions options() const
Owning UI-thread copy; allocation may throw bad_alloc.
Grid(const GridOptions &options={}, SizePolicy policy={})
Own settings and size policies.
bool usesRegions() const noexcept
std::expected< LayoutChildPolicy, TreeError > childLayoutFor(NodeId child, CellSize available) const override
std::expected< CellSize, TreeError > measureChildren(std::span< const MeasuredChild > children) const override
Aggregate tracks.
Grid(const GridOptions &options, GridPlacementMode mode, SizePolicy policy={}, std::optional< GridScrollOptions > scroll={}, WidthPolicy width={})
Own resolved settings in a fixed construction-time placement mode.
ComputedStyle styleDefaults() const override
std::expected< void, TreeError > paint(PaintContext &context) override
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
OverlayOptions options() const
Owning UI-thread copy.
Overlay(OverlayOptions options={}, SizePolicy policy={})
Own settings and size policies.
friend std::expected< void, TreeError > setOverlayOptions(WidgetTree &, Node< Overlay >, OverlayOptions)
Validate and replace settings on the UI owner between frames, then invalidate layout.
std::expected< std::vector< CellRect >, TreeError > arrangeChildren(std::span< const MeasuredChild > children, CellSize available) const override
Child-order local rectangles.
std::expected< CellSize, TreeError > measureChildren(std::span< const MeasuredChild > children) const override
Maximum child extent on each axis.
std::expected< CellSize, TreeError > childAvailable(CellSize available) const override
Checked inset bounds.
Borrowed synchronous command sink supplied by the frame owner. submit must consume commands immediate...
Definition widget_types.hpp:66
SizedWidget(SizePolicy policy={})
Own the supplied policies; checked by layout.
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
std::expected< void, TreeError > setOverlayOptions(WidgetTree &tree, Node< Overlay > node, OverlayOptions options)
Validate and replace settings on the UI owner between frames, then invalidate layout.
GridFlow
Direction in which declarative placement searches for the next free origin.
Definition grid.hpp:53
@ ROW_MAJOR
Advance through columns before moving to the next body row.
Definition grid.hpp:54
@ COLUMN_MAJOR
Advance through body rows before moving to the next column.
Definition grid.hpp:55
GridPlacementMode
Construction-time placement mode; explicit grids retain their overlap semantics.
Definition grid.hpp:59
@ EXPLICIT
Preserve low-level overlapping cells in sibling paint/input order.
Definition grid.hpp:60
@ DECLARATIVE
Reject overlapping cells within each independent region.
Definition grid.hpp:61
std::expected< void, TreeError > setGridOptions(WidgetTree &tree, Node< Grid > node, const GridOptions &options)
Validate and atomically copy settings on the UI owner between frames, then invalidate layout....
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
std::expected< void, TreeError > setDeclarativeGridOptions(WidgetTree &tree, Node< Grid > node, const GridOptions &options)
Atomically replace resolved declarative settings between frames on the UI owner.
std::expected< GridPlacementPlan, TreeError > resolveGridPlacements(const DeclarativeGridOptions &options, std::span< const GridItemOptions > items)
Resolve sparse placement without factories, tree access or retained input views.
std::expected< void, TreeError > setGridPresentation(WidgetTree &tree, Node< Grid > node, const GridPresentation &presentation)
Replace owning backgrounds between frames on the UI owner.
GridRegion
Independent local row coordinate space sharing the grid's columns.
Definition grid.hpp:22
@ BODY
Ordinary content rows, optionally below a header.
Definition grid.hpp:23
@ HEADER
Independent heading rows sharing the horizontal tracks.
Definition grid.hpp:24
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
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
Owning automatic-placement settings; counts are distinct from GridOptions track policies.
Definition grid.hpp:76
LayoutPadding padding
Nonnegative content insets.
Definition grid.hpp:82
std::int32_t columnGap
Nonnegative terminal cells between columns.
Definition grid.hpp:80
std::optional< std::size_t > columns
Required for row-major flow; otherwise inferred within the budget.
Definition grid.hpp:77
GridFlow flow
Forward-only search; explicit rectangles are reserved first.
Definition grid.hpp:79
std::optional< std::size_t > rows
Required for column-major flow; otherwise inferred within the budget.
Definition grid.hpp:78
bool stickyHeader
Header remains fixed vertically when scrolling is enabled.
Definition grid.hpp:87
std::size_t maxPlacements
Positive bound on structural children, including hidden children.
Definition grid.hpp:84
std::size_t maxPlacementWork
Positive total bound on candidates, occupancy probes and reservations.
Definition grid.hpp:85
std::size_t maxTracksPerAxis
Positive bound on either resolved axis, including empty tracks.
Definition grid.hpp:83
std::int32_t rowGap
Nonnegative terminal cells between rows.
Definition grid.hpp:81
bool regions
Header/body have separate row occupancy and require explicit columns.
Definition grid.hpp:86
Owning explicit placement, keyed by full identity rather than visible sibling index.
Definition grid.hpp:115
LayoutAlign vertical
Child alignment within its rows.
Definition grid.hpp:122
GridRegion region
Region-local row index; columns remain shared.
Definition grid.hpp:123
LayoutAlign horizontal
Child alignment within its columns.
Definition grid.hpp:121
std::size_t column
Zero-based first column.
Definition grid.hpp:118
NodeId node
Owning identity; hidden/removed entries never bind a replacement node.
Definition grid.hpp:116
std::size_t row
Zero-based first row.
Definition grid.hpp:117
std::size_t column_span
Positive span wholly within the explicit columns.
Definition grid.hpp:120
std::size_t row_span
Positive span wholly within the explicit rows.
Definition grid.hpp:119
Owning parent-child relationship; omitted coordinates request automatic placement.
Definition grid.hpp:65
LayoutAlign vertical
Vertical alignment inside the complete span.
Definition grid.hpp:71
std::optional< std::size_t > row
Local zero-based row; must be supplied together with column.
Definition grid.hpp:66
std::size_t rowSpan
Positive number of occupied rows.
Definition grid.hpp:68
LayoutAlign horizontal
Horizontal alignment inside the complete span.
Definition grid.hpp:70
GridRegion region
Independent region; HEADER requires declarative regions.
Definition grid.hpp:72
std::optional< std::size_t > column
Local zero-based column; must be supplied together with row.
Definition grid.hpp:67
std::size_t columnSpan
Positive number of occupied columns.
Definition grid.hpp:69
Owning sparse grid configuration; no implicit tracks or automatic placement.
Definition grid.hpp:127
bool regions
Header/body presence fixed before mount, including empty regions.
Definition grid.hpp:138
std::int32_t row_gap
Nonnegative gap, included inside a spanning cell.
Definition grid.hpp:131
std::vector< AxisSize > columns
Explicit horizontal tracks using shared size policies.
Definition grid.hpp:128
std::size_t max_placements
Positive work budget, including inactive placements.
Definition grid.hpp:135
std::size_t max_tracks_per_axis
Positive work budget for each explicit axis.
Definition grid.hpp:134
GridPresentation presentation
Owning backgrounds; does not alter child styles.
Definition grid.hpp:139
std::vector< GridCell > cells
Unique identities; overlap is allowed in sibling order.
Definition grid.hpp:133
LayoutPadding padding
Insets retained during shortage.
Definition grid.hpp:132
std::int32_t column_gap
Nonnegative gap, included inside a spanning cell.
Definition grid.hpp:130
bool sticky_header
Fix header vertically when this grid owns scrolling.
Definition grid.hpp:137
std::vector< AxisSize > header_rows
Independent header rows sharing columns.
Definition grid.hpp:136
std::vector< AxisSize > rows
Explicit vertical tracks using shared size policies.
Definition grid.hpp:129
Owning preflight result independent of any tree identity or visibility.
Definition grid.hpp:97
std::size_t columns
Complete horizontal track count.
Definition grid.hpp:99
~GridPlacementPlan()=default
Keep checked-STL vector transfers fallible instead of implicit noexcept moves.
std::size_t rows
Complete vertical track count, including explicit trailing tracks.
Definition grid.hpp:100
std::vector< GridPosition > positions
One origin per input item.
Definition grid.hpp:101
std::size_t headerRows
Inferred independent header rows; empty header has zero tracks.
Definition grid.hpp:102
Resolved local origin in the same order as the input relationships.
Definition grid.hpp:91
std::size_t column
First occupied column.
Definition grid.hpp:93
std::size_t row
First occupied row.
Definition grid.hpp:92
Backgrounds only; these values never inherit into child control state styles.
Definition grid.hpp:35
GridRowStyles rows
Optional logical body row backgrounds, independent of visibility.
Definition grid.hpp:38
std::optional< CellStyle > header
Complete header background, below real controls.
Definition grid.hpp:36
bool operator==(const GridPresentation &) const =default
std::optional< CellStyle > body
Body background, including row gaps.
Definition grid.hpp:37
std::size_t maxPaintCells
Positive total background plus scroll chrome paint-work budget.
Definition grid.hpp:39
Optional owning row backgrounds; logical row zero is even.
Definition grid.hpp:28
std::optional< CellStyle > odd
Background across an odd body row, including column gaps.
Definition grid.hpp:30
bool operator==(const GridRowStyles &) const =default
std::optional< CellStyle > even
Background across an even body row, including column gaps.
Definition grid.hpp:29
Owning opt-in to the existing scroll-controller lifecycle, fixed before mount.
Definition grid.hpp:44
std::shared_ptr< ScrollController > controller
Shared owning values; empty creates an independent controller.
Definition grid.hpp:50
ScrollAreaOptions area
Existing area options; a grid adds no focus stop by default.
Definition grid.hpp:45
Owning nonnegative padding in terminal cells.
Definition layout.hpp:185
Owning child input for a pure layout computation.
Definition layout.hpp:120
Owning identity, independent of addresses; zero is never a live identity.
Definition widget_types.hpp:15
Owning overlay alignment and insets; all children share the content rectangle.
Definition grid.hpp:227
LayoutPadding padding
Insets retained during shortage.
Definition grid.hpp:228
LayoutAlign vertical
Alignment of each child across rows.
Definition grid.hpp:230
LayoutAlign horizontal
Alignment of each child across columns.
Definition grid.hpp:229
Owning ordinary-area settings; content is retained rather than automatically virtualized....
Definition scroll_options.hpp:36
Owning independent policies for width and height.
Definition layout.hpp:35