Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
recipe_sizing.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/ui_control_context.hpp>
8
9namespace portals::ui::detail {
11
12enum class SizeDimension {
13 WIDTH,
14 HEIGHT,
15 FLEX_X,
16 FLEX_Y,
17 FLEX,
22 FIT_WIDTH,
23 FIT_HEIGHT,
24 AUTO_WIDTH,
25 AUTO_HEIGHT
26};
27
28struct SizeChange {
29 SizeDimension dimension;
30 std::int32_t extent{};
31 std::uint32_t weight{1};
32};
33
35struct RecipeAxisSize {
36 std::optional<std::int32_t> minimum;
37 std::optional<std::int32_t> preferred;
38 std::optional<std::int32_t> maximum;
39 std::optional<SizeMode> mode;
40 std::optional<std::uint32_t> weight;
41
42 constexpr void stage(SizeChange change) noexcept {
43 switch (change.dimension) {
44 case SizeDimension::WIDTH:
45 case SizeDimension::HEIGHT:
46 minimum = preferred = maximum = change.extent;
47 mode = SizeMode::FIXED;
48 weight = 1;
49 break;
50 case SizeDimension::FLEX_X:
51 case SizeDimension::FLEX_Y:
52 case SizeDimension::FLEX:
53 preferred = 0;
54 mode = SizeMode::FLEX;
55 weight = change.weight;
56 break;
57 case SizeDimension::MIN_WIDTH:
58 case SizeDimension::MIN_HEIGHT:
59 minimum = change.extent;
60 break;
61 case SizeDimension::MAX_WIDTH:
62 case SizeDimension::MAX_HEIGHT:
63 maximum = change.extent;
64 break;
65 case SizeDimension::FIT_WIDTH:
66 case SizeDimension::FIT_HEIGHT:
67 preferred = INTRINSIC_EXTENT;
68 mode = SizeMode::CONTENT;
69 break;
70 case SizeDimension::AUTO_WIDTH:
71 case SizeDimension::AUTO_HEIGHT:
72 preferred = INTRINSIC_EXTENT;
73 mode = SizeMode::AUTOMATIC;
74 weight = 1;
75 break;
76 }
77 }
78
79 constexpr AxisSize apply(AxisSize value) const noexcept {
80 if (minimum) {
81 value.minimum = *minimum;
82 }
83
84 if (preferred) {
85 value.preferred = *preferred;
86 }
87
88 if (maximum) {
89 value.maximum = *maximum;
90 }
91
92 if (mode) {
93 value.mode = *mode;
94 }
95
96 if (weight) {
97 value.weight = *weight;
98 }
99
100 return value;
101 }
102};
103
105struct RecipeSizing {
106 std::optional<RecipeAxisSize> width;
107 std::optional<RecipeAxisSize> height;
108
110 constexpr void overlay(const RecipeSizing &source) noexcept {
111 const auto mergeAxis = [](auto &target, const auto &patch) {
112 if (!patch) {
113 return;
114 }
115
116 if (!target) {
117 target.emplace();
118 }
119
120 const auto assign = [](auto &destination, const auto &value) {
121 if (value) {
122 destination = value;
123 }
124 };
125
126 assign(target->minimum, patch->minimum);
127 assign(target->preferred, patch->preferred);
128 assign(target->maximum, patch->maximum);
129 assign(target->mode, patch->mode);
130 assign(target->weight, patch->weight);
131 };
132
133 mergeAxis(width, source.width);
134 mergeAxis(height, source.height);
135 }
136
137 constexpr void stage(SizeChange change, bool activate) noexcept {
138 const auto stageAxis = [&](std::optional<RecipeAxisSize> &axis) {
139 if (activate && !axis) {
140 axis.emplace();
141 }
142
143 if (axis) {
144 axis->stage(change);
145 }
146 };
147
148 switch (change.dimension) {
149 case SizeDimension::WIDTH:
150 case SizeDimension::FLEX_X:
151 case SizeDimension::MIN_WIDTH:
152 case SizeDimension::MAX_WIDTH:
153 case SizeDimension::FIT_WIDTH:
154 case SizeDimension::AUTO_WIDTH:
155 stageAxis(width);
156 break;
157 case SizeDimension::HEIGHT:
158 case SizeDimension::FLEX_Y:
159 case SizeDimension::MIN_HEIGHT:
160 case SizeDimension::MAX_HEIGHT:
161 case SizeDimension::FIT_HEIGHT:
162 case SizeDimension::AUTO_HEIGHT:
163 stageAxis(height);
164 break;
165 case SizeDimension::FLEX:
166 stageAxis(width);
167 stageAxis(height);
168 break;
169 }
170 }
171
172 constexpr SizePolicy apply(SizePolicy value) const noexcept {
173 if (width) {
174 value.width = width->apply(value.width);
175 }
176
177 if (height) {
178 value.height = height->apply(value.height);
179 }
180
181 return value;
182 }
183};
184
189inline RecipeSizing controlSizing(const ControlSettings &settings, const ControlSettings *excluded = nullptr) noexcept {
190 RecipeSizing result;
191 const auto stage = [&](SizeDimension dimension, auto value) {
192 if (value) {
193 result.stage({dimension, static_cast<std::int32_t>(*value)}, true);
194 }
195 };
196 const auto flex = [&](SizeDimension dimension, std::optional<std::uint32_t> value) {
197 if (value) {
198 result.stage({dimension, 0, *value}, true);
199 }
200 };
201
202 const bool excludeWidth = excluded && (excluded->width || excluded->fitWidth || excluded->flexX || excluded->flex);
203 const bool excludeHeight = excluded && (excluded->height || excluded->fitHeight || excluded->flexY || excluded->flex);
204
205 if (!excludeWidth) {
206 stage(SizeDimension::WIDTH, settings.width);
207 flex(SizeDimension::FLEX_X, settings.flexX ? settings.flexX : settings.flex);
208
209 if (settings.fitWidth) {
210 result.stage({*settings.fitWidth ? SizeDimension::FIT_WIDTH : SizeDimension::AUTO_WIDTH}, true);
211 }
212 }
213
214 if (!excludeHeight) {
215 stage(SizeDimension::HEIGHT, settings.height);
216 flex(SizeDimension::FLEX_Y, settings.flexY ? settings.flexY : settings.flex);
217
218 if (settings.fitHeight) {
219 result.stage({*settings.fitHeight ? SizeDimension::FIT_HEIGHT : SizeDimension::AUTO_HEIGHT}, true);
220 }
221 }
222
223 const auto bound = [](auto &axis, auto value, bool minimum, bool exclude) {
224 if (!value || exclude) {
225 return;
226 }
227
228 if (!axis) {
229 axis.emplace();
230 }
231
232 auto &destination = minimum ? axis->minimum : axis->maximum;
233
234 destination = !destination ? *value : minimum ? std::max(*destination, *value) : std::min(*destination, *value);
235 };
236
237 bound(result.width, settings.minWidth, true, excluded && excluded->minWidth.has_value());
238 bound(result.width, settings.maxWidth, false, excluded && excluded->maxWidth.has_value());
239 bound(result.height, settings.minHeight, true, excluded && excluded->minHeight.has_value());
240 bound(result.height, settings.maxHeight, false, excluded && excluded->maxHeight.has_value());
241
242 return result;
243}
244
246} // namespace portals::ui::detail
auto change(T value)
Definition style_static.hpp:1496
@ WIDTH
Runtime state-independent horizontal mode or exact extent.
Definition pss_data.hpp:121
@ MAX_WIDTH
Runtime state-independent hard column cap.
Definition pss_data.hpp:123
@ MIN_WIDTH
State-independent cell minimum.
Definition pss_data.hpp:19
@ HEIGHT
Runtime state-independent vertical mode or exact extent.
Definition pss_data.hpp:122
@ MAX_HEIGHT
Runtime state-independent hard row cap.
Definition pss_data.hpp:125
@ MIN_HEIGHT
Runtime state-independent hard row minimum.
Definition pss_data.hpp:124
@ 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