Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
style_types.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3#pragma once
4
5#include <array>
6#include <memory>
7#include <optional>
8#include <portals/cell_buffer.hpp>
9#include <portals/glyph_profile.hpp>
10#include <portals/scrollbar_skin.hpp>
11#include <string>
12#include <type_traits>
13
14namespace portals {
15namespace detail {
17inline constexpr std::size_t SCROLLBAR_AXIS_LITERAL_GLYPH_COUNT = 10;
18inline constexpr std::size_t SCROLLBAR_LITERAL_THUMB_SEGMENTS = 6;
19inline constexpr std::size_t SCROLLBAR_LITERAL_CORNER = 2 * SCROLLBAR_AXIS_LITERAL_GLYPH_COUNT;
20inline constexpr std::size_t SCROLLBAR_LITERAL_GLYPH_COUNT = SCROLLBAR_LITERAL_CORNER + 1;
21
22// A leaf owns its source through resolution, preparation and retained snapshots.
23struct ScrollbarGlyphLiteral {
24 std::string text;
25 std::shared_ptr<const std::string> source;
26 std::size_t line{1};
27 std::size_t column{1};
28
29 bool operator==(const ScrollbarGlyphLiteral &other) const noexcept {
30 return text == other.text;
31 }
32};
33
34using ScrollbarGlyphRefs = std::array<std::shared_ptr<const ScrollbarGlyphLiteral>, SCROLLBAR_LITERAL_GLYPH_COUNT>;
35using ScrollbarGlyphStates = std::array<ScrollbarGlyphRefs, 6>;
36
37static_assert(std::is_nothrow_default_constructible_v<ScrollbarGlyphStates>);
38static_assert(std::is_nothrow_copy_constructible_v<ScrollbarGlyphStates>);
39static_assert(std::is_nothrow_move_constructible_v<ScrollbarGlyphStates>);
40
41// Some standard libraries do not declare optional's nontrivial copy noexcept. This concrete payload
42// only copies shared owners; emplace/reset therefore preserve the established nonthrowing style copies.
43struct ScrollbarGlyphStorage : std::optional<ScrollbarGlyphStates> {
44 using Base = std::optional<ScrollbarGlyphStates>;
45 using Base::Base;
46 using Base::operator=;
47
48 ScrollbarGlyphStorage() = default;
49 ScrollbarGlyphStorage(ScrollbarGlyphStorage &&) = default;
50 ScrollbarGlyphStorage &operator=(ScrollbarGlyphStorage &&) = default;
51
52 ScrollbarGlyphStorage(const ScrollbarGlyphStorage &other) noexcept {
53 if (other) {
54 emplace(*other);
55 }
56 }
57
58 ScrollbarGlyphStorage &operator=(const ScrollbarGlyphStorage &other) noexcept {
59 if (this != &other) {
60 if (other) {
61 emplace(*other);
62 } else {
63 reset();
64 }
65 }
66
67 return *this;
68 }
69};
70
71static_assert(sizeof(ScrollbarGlyphStorage) == sizeof(std::optional<ScrollbarGlyphStates>));
72
73bool equalScrollbarGlyphs(const ScrollbarGlyphRefs &left, const ScrollbarGlyphRefs &right) noexcept;
74bool equalScrollbarGlyphs(const ScrollbarGlyphStates &left, const ScrollbarGlyphStates &right) noexcept;
76} // namespace detail
77
80 std::optional<CellStyle> line{};
81 std::optional<CellStyle> title{};
82 std::optional<CellStyle> ends{};
83
85 bool operator==(const FrameAppearance &other) const = default;
86};
87
92 std::optional<CellStyle> track{};
93 std::optional<CellStyle> thumb{};
94 std::optional<CellStyle> ends{};
95 std::optional<CellStyle> corner{};
96 std::optional<CellStyle> arrows{};
97
98 std::optional<ScrollbarPattern> trackPattern{};
99 std::optional<ScrollbarThumbPattern> thumbPattern{};
100 std::optional<ScrollbarArrowPattern> arrowPattern{};
101
103 bool operator==(const ScrollbarAppearance &other) const = default;
104};
105
118
124
126 bool operator==(const SliderAppearance &other) const = default;
127};
128
131 std::optional<GlyphProfile> profile{};
132 std::optional<GlyphFallback> fallback{};
133
135 constexpr bool operator==(const GlyphAppearance &other) const = default;
136};
137
145
149 std::int32_t extent{};
150 std::uint32_t weight{1};
151
153 constexpr bool operator==(const StyleAxisSize &other) const = default;
154};
155
158 std::optional<StyleAxisSize> width;
159 std::optional<StyleAxisSize> height;
160 std::optional<std::int32_t> maxWidth;
161 std::optional<std::int32_t> minHeight;
162 std::optional<std::int32_t> maxHeight;
163
165 constexpr bool operator==(const StyleSizing &other) const = default;
166};
167
176 std::int32_t minWidth{};
177 std::array<FrameAppearance, 6> frame{};
178 std::array<ScrollbarAppearance, 6> scrollbar{};
183 std::optional<std::array<ScrollbarThumbAppearance, 6>> scrollbarThumbSegments{};
188 std::optional<std::array<SliderAppearance, 6>> sliderParts{};
189 std::array<GlyphAppearance, 6> glyphs{};
191 detail::ScrollbarGlyphStorage scrollbarGlyphs{};
193 std::optional<StyleSizing> sizing{};
197 bool operator==(const ComputedStyle &other) const noexcept;
198};
199
200namespace detail {
202struct StyleGlyphBlock {
203 std::array<GlyphAppearance, 6> values{};
204 std::shared_ptr<const ScrollbarGlyphStates> literals;
205 std::shared_ptr<const StyleSizing> sizing;
206 std::shared_ptr<const std::array<SliderAppearance, 6>> sliderParts;
207
208 bool operator==(const StyleGlyphBlock &other) const noexcept;
209};
210
211struct StyleSnapshotBlocks {
212 std::shared_ptr<const std::array<FrameAppearance, 6>> frame;
213 std::shared_ptr<const std::array<ScrollbarAppearance, 6>> scrollbar;
214 std::shared_ptr<const std::array<ScrollbarThumbAppearance, 6>> thumb;
215 std::shared_ptr<const StyleGlyphBlock> glyphs;
216};
217
218std::size_t styleBlockHash(const std::array<FrameAppearance, 6> &value) noexcept;
219std::size_t styleBlockHash(const std::array<ScrollbarAppearance, 6> &value) noexcept;
220std::size_t styleBlockHash(const std::array<ScrollbarThumbAppearance, 6> &value) noexcept;
221std::size_t styleBlockHash(const std::array<GlyphAppearance, 6> &value) noexcept;
222std::size_t styleBlockHash(const StyleGlyphBlock &value) noexcept;
224} // namespace detail
225
232 class PreparedTag {
233 friend class StyleEngine;
234 friend class StyleSnapshot;
235 PreparedTag() = default;
236
237 public:
238 PreparedTag(const PreparedTag &) = default;
239 };
240
241public:
248 std::int32_t minWidth{};
249
253 explicit StyleSnapshot(const ComputedStyle &value);
255 StyleSnapshot(PreparedTag, const ComputedStyle &value, detail::StyleSnapshotBlocks blocks) noexcept;
257
259 const std::array<FrameAppearance, 6> &frame() const noexcept;
261 const std::array<ScrollbarAppearance, 6> &scrollbar() const noexcept;
263 const std::array<ScrollbarThumbAppearance, 6> *scrollbarThumbSegments() const noexcept;
265 const std::array<SliderAppearance, 6> *sliderParts() const noexcept;
267 const std::array<GlyphAppearance, 6> &glyphs() const noexcept;
269 const detail::ScrollbarGlyphStates *scrollbarGlyphs() const noexcept;
271
272 const StyleSizing *sizing() const noexcept;
274 bool hasFrame() const noexcept;
276 bool hasScrollbar() const noexcept;
278 bool hasThumbSegments() const noexcept;
280 bool hasGlyphs() const noexcept;
282 ComputedStyle flatten() const noexcept;
284 bool operator==(const ComputedStyle &other) const noexcept;
286 bool operator==(const StyleSnapshot &other) const noexcept;
288 std::size_t contentHash() const noexcept;
290 static std::size_t contentHash(const ComputedStyle &value) noexcept;
291
292private:
293 friend class StyleEngine;
294 detail::StyleSnapshotBlocks blocks_;
295};
296
297namespace detail {
299
300struct ComputedStyleView {
301 const CellStyle &normal;
302 const CellStyle &focused;
303 const CellStyle &disabled;
304 const CellStyle &selected;
305 const CellStyle &header;
306 const CellStyle &caret;
307 std::int32_t minWidth;
308 const std::array<FrameAppearance, 6> &frame;
309 const std::array<ScrollbarAppearance, 6> &scrollbar;
310 const std::array<ScrollbarThumbAppearance, 6> *scrollbarThumbSegments;
311 const std::array<SliderAppearance, 6> *sliderParts;
312 const std::array<GlyphAppearance, 6> &glyphs;
313 const ScrollbarGlyphStates *scrollbarGlyphs;
314 const StyleSizing *sizing;
315
316 ComputedStyleView(const ComputedStyle &value) noexcept
317 : normal(value.normal), focused(value.focused), disabled(value.disabled), selected(value.selected),
318 header(value.header), caret(value.caret), minWidth(value.minWidth), frame(value.frame),
319 scrollbar(value.scrollbar),
320 scrollbarThumbSegments(value.scrollbarThumbSegments ? &*value.scrollbarThumbSegments : nullptr),
321 sliderParts(value.sliderParts ? &*value.sliderParts : nullptr), glyphs(value.glyphs),
322 scrollbarGlyphs(value.scrollbarGlyphs ? &*value.scrollbarGlyphs : nullptr),
323 sizing(value.sizing ? &*value.sizing : nullptr) {
324 }
325
326 ComputedStyleView(const StyleSnapshot &value) noexcept
327 : normal(value.normal), focused(value.focused), disabled(value.disabled), selected(value.selected),
328 header(value.header), caret(value.caret), minWidth(value.minWidth), frame(value.frame()),
329 scrollbar(value.scrollbar()), scrollbarThumbSegments(value.scrollbarThumbSegments()),
330 sliderParts(value.sliderParts()), glyphs(value.glyphs()), scrollbarGlyphs(value.scrollbarGlyphs()),
331 sizing(value.sizing()) {
332 }
333};
334
336} // namespace detail
337} // namespace portals
bool hasGlyphs() const noexcept
const std::array< ScrollbarThumbAppearance, 6 > * scrollbarThumbSegments() const noexcept
CellStyle disabled
Disabled control text and fill.
Definition style_types.hpp:244
CellStyle header
Fixed table heading.
Definition style_types.hpp:246
const std::array< FrameAppearance, 6 > & frame() const noexcept
const std::array< GlyphAppearance, 6 > & glyphs() const noexcept
bool hasScrollbar() const noexcept
const StyleSizing * sizing() const noexcept
ComputedStyle flatten() const noexcept
const std::array< ScrollbarAppearance, 6 > & scrollbar() const noexcept
CellStyle normal
Ordinary text and fill.
Definition style_types.hpp:242
const std::array< SliderAppearance, 6 > * sliderParts() const noexcept
CellStyle caret
Editor software caret.
Definition style_types.hpp:247
bool hasFrame() const noexcept
bool hasThumbSegments() const noexcept
CellStyle selected
Selected row or editor range.
Definition style_types.hpp:245
CellStyle focused
Focused control text and fill.
Definition style_types.hpp:243
StyleSnapshot(const ComputedStyle &value)
Prepare the complete immutable value; may throw std::bad_alloc.
std::size_t contentHash() const noexcept
std::int32_t minWidth
Nonnegative hard column minimum.
Definition style_types.hpp:248
Public Portals API.
Definition action.hpp:8
ui::ValueText text(std::format_string< const ui::detail::TextArgumentValue< Args > &... > pattern, Args &&...args)
Declare live formatted text over explicit scalar roots, fields or value properties.
Definition ui_value_text.hpp:567
StyleSizeMode
State-independent style request for one allocation axis.
Definition style_types.hpp:139
@ AUTOMATIC
Use the ordinary preferred/intrinsic allocation.
Definition style_types.hpp:140
@ 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
Small owning paint value, independent of themes and text metrics.
Definition cell_buffer.hpp:35
Owning resolved presentation; no actions or model state are encoded.
Definition style_types.hpp:169
CellStyle normal
Ordinary text and fill.
Definition style_types.hpp:170
std::optional< StyleSizing > sizing
Inline scratch constraints; resolution and copying never allocate.
Definition style_types.hpp:193
bool operator==(const ComputedStyle &other) const noexcept
std::optional< std::array< ScrollbarThumbAppearance, 6 > > scrollbarThumbSegments
Optional complete segment values in normal/focused/disabled/selected/header/caret order....
Definition style_types.hpp:183
CellStyle disabled
Disabled control text and fill.
Definition style_types.hpp:172
std::array< GlyphAppearance, 6 > glyphs
Definition style_types.hpp:189
CellStyle caret
Editor software caret.
Definition style_types.hpp:175
CellStyle header
Fixed table heading.
Definition style_types.hpp:174
std::array< ScrollbarAppearance, 6 > scrollbar
Parts in normal/focused/disabled/selected/header/caret order.
Definition style_types.hpp:178
std::int32_t minWidth
Nonnegative hard column minimum; zero adds no style constraint.
Definition style_types.hpp:176
CellStyle selected
Selected list/table row or editor range.
Definition style_types.hpp:173
std::array< FrameAppearance, 6 > frame
Parts in normal/focused/disabled/selected/header/caret order.
Definition style_types.hpp:177
CellStyle focused
Focused control text and fill.
Definition style_types.hpp:171
std::optional< std::array< SliderAppearance, 6 > > sliderParts
Complete Slider paints in normal/focused/disabled/selected/header/caret order. Engaged only for match...
Definition style_types.hpp:188
Resolved frame parts; absent parts inherit the ordinary control state.
Definition style_types.hpp:79
bool operator==(const FrameAppearance &other) const =default
std::optional< CellStyle > ends
Prefix/suffix appearance, including their spacing.
Definition style_types.hpp:82
std::optional< CellStyle > title
Title text appearance.
Definition style_types.hpp:81
std::optional< CellStyle > line
Perimeter glyph appearance.
Definition style_types.hpp:80
Logical glyph overrides; absent values retain the control's owning manual map.
Definition style_types.hpp:130
std::optional< GlyphFallback > fallback
Explicit admission fallback.
Definition style_types.hpp:132
std::optional< GlyphProfile > profile
Explicit base map, independent of colors.
Definition style_types.hpp:131
constexpr bool operator==(const GlyphAppearance &other) const =default
Resolved scrollbar parts; absent values inherit the ordinary control state. Colors and attributes are...
Definition style_types.hpp:91
std::optional< CellStyle > track
Track cells outside the thumb.
Definition style_types.hpp:92
std::optional< ScrollbarThumbPattern > thumbPattern
Resolved independent glyph source.
Definition style_types.hpp:99
std::optional< ScrollbarArrowPattern > arrowPattern
Resolved independent glyph source.
Definition style_types.hpp:100
std::optional< CellStyle > arrows
Functional end buttons, independent of decorative ends.
Definition style_types.hpp:96
std::optional< ScrollbarPattern > trackPattern
Resolved independent glyph source.
Definition style_types.hpp:98
bool operator==(const ScrollbarAppearance &other) const =default
std::optional< CellStyle > corner
Intersecting horizontal and vertical gutters.
Definition style_types.hpp:95
std::optional< CellStyle > thumb
Moving thumb cells.
Definition style_types.hpp:93
std::optional< CellStyle > ends
Optional decorative endpoint cells.
Definition style_types.hpp:94
Complete resolved thumb segment values, shared by both axes. Missing declarations inherit the common ...
Definition style_types.hpp:109
CellStyle start
First cell of a thumb of length at least two.
Definition style_types.hpp:110
CellStyle single
Only cell of a one-cell thumb.
Definition style_types.hpp:113
CellStyle body
Interior cells of a thumb of length at least three.
Definition style_types.hpp:111
bool operator==(const ScrollbarThumbAppearance &other) const =default
CellStyle end
Last cell of a thumb of length at least two.
Definition style_types.hpp:112
Complete resolved Slider part paints, inheriting the ordinary control state.
Definition style_types.hpp:120
bool operator==(const SliderAppearance &other) const =default
CellStyle thumb
Movable position indicator.
Definition style_types.hpp:123
CellStyle track
Unfilled rail cells.
Definition style_types.hpp:121
CellStyle fill
Filled rail prefix.
Definition style_types.hpp:122
One tagged style mode; changing mode leaves no former exact-size constraint.
Definition style_types.hpp:147
StyleSizeMode mode
Allocation request.
Definition style_types.hpp:148
std::uint32_t weight
Positive FLEX weight.
Definition style_types.hpp:150
constexpr bool operator==(const StyleAxisSize &other) const =default
std::int32_t extent
Nonnegative cells for FIXED; otherwise zero.
Definition style_types.hpp:149
Sparse style sizing; legacy min-width remains solely in minWidth.
Definition style_types.hpp:157
constexpr bool operator==(const StyleSizing &other) const =default
std::optional< StyleAxisSize > width
Horizontal mode request.
Definition style_types.hpp:158
std::optional< std::int32_t > maxHeight
Inclusive nonnegative row cap.
Definition style_types.hpp:162
std::optional< std::int32_t > maxWidth
Inclusive nonnegative column cap.
Definition style_types.hpp:160
std::optional< std::int32_t > minHeight
Nonnegative hard row minimum.
Definition style_types.hpp:161
std::optional< StyleAxisSize > height
Vertical mode request.
Definition style_types.hpp:159