Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
theme_selection.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3#pragma once
4
5#include <portals/managed_diagnostics.hpp>
6#include <portals/style.hpp>
7#include <portals/text_value.hpp>
8
9#include <initializer_list>
10#include <source_location>
11#include <stop_token>
12
13namespace portals {
14class ManagedScene;
15
16namespace detail {
17struct ThemeSelectionRecord;
18}
19
20namespace ui {
21class Scene;
22
23namespace detail {
24struct ThemeSelectionTarget;
25struct ThemeSelectionAccess;
26struct ThemeSelectorAccess;
27struct ThemeSelectionDescription;
28struct PreparedThemeAccess;
29} // namespace detail
30
32enum class ThemeSelectionStatus {
33 IDLE,
34 ACTIVE,
35 SUPERSEDED
36};
37
43 ThemeChoiceData(std::string_view id, const GraphemeText &label) : id(id), label(label) {
44 }
45
47 ~ThemeChoiceData() = default;
48 std::string id;
50 std::optional<GraphemeText> description;
51};
52
56 ~ThemeCatalog() = default;
57 std::vector<std::shared_ptr<const ThemeChoiceData>> choices =
58 std::vector<std::shared_ptr<const ThemeChoiceData>>(0);
59};
60
67 std::shared_ptr<const ThemeCatalog> catalog;
68 ThemeSelectionStatus status{ThemeSelectionStatus::IDLE};
69 std::optional<std::string> previewedId;
70 std::optional<std::string> lastCommittedId;
71 std::optional<std::string> requestedId;
72 bool preparing{};
73 std::optional<ManagedError> preparationError;
74};
75
80 std::size_t maxChoices{64};
81 std::size_t maxIdBytes{128};
82 std::size_t maxLabelBytes{512};
83 std::size_t maxDescriptionBytes{2048};
84 std::size_t maxTextBytes{1024 * 1024};
85 TextLimits textLimits{2048, 4096, 16384, 32768};
86 std::function<std::expected<std::string, TextResourceError>(std::string_view)> textResolver;
87 std::optional<StyleLayerOptions> layers;
88};
89
96public:
103 template <class Factory>
104 requires std::copy_constructible<Factory> && std::invocable<Factory &> &&
105 (std::same_as<std::invoke_result_t<Factory &>, StyleSheet> ||
106 std::same_as<std::invoke_result_t<Factory &>, std::expected<StyleSheet, StyleError>>)
107 ThemeChoice(std::string_view id, TextValue label, Factory factory, std::optional<TextValue> description = {})
108 : id_(id), label_(std::move(label)), description_(std::move(description)),
109 factory_([factory = std::move(factory)]() mutable -> std::expected<StyleSheet, StyleError> {
110 return std::invoke(factory);
111 }) {
112 }
113
118 template <class Factory>
119 requires std::copy_constructible<Factory> && std::invocable<Factory &> &&
120 (std::same_as<std::invoke_result_t<Factory &>, StyleSheet> ||
121 std::same_as<std::invoke_result_t<Factory &>, std::expected<StyleSheet, StyleError>>)
122 ThemeChoice(std::string_view id, std::string_view label, Factory factory, std::optional<TextValue> description = {})
123 : ThemeChoice(id, TextValue(label), std::move(factory), std::move(description)) {
124 }
125
127 ~ThemeChoice() = default;
128
129private:
130 friend class portals::ManagedScene;
131 friend struct detail::PreparedThemeAccess;
132
133 struct PreparedThemeTag {};
134
143 template <class Factory>
144 requires std::copy_constructible<Factory> && std::invocable<Factory &, std::stop_token> &&
145 (std::same_as<std::invoke_result_t<Factory &, std::stop_token>, StyleSheet> ||
146 std::same_as<std::invoke_result_t<Factory &, std::stop_token>, std::expected<StyleSheet, StyleError>>)
147 ThemeChoice(PreparedThemeTag, std::string_view id, TextValue label, Factory factory,
148 std::optional<TextValue> description = {})
149 : id_(id), label_(std::move(label)), description_(std::move(description)),
150 preparedFactory_(std::make_shared<PreparedFactory>(
151 [factory = std::move(factory)](std::stop_token stop) mutable -> std::expected<StyleSheet, StyleError> {
152 return std::invoke(factory, stop);
153 })) {
154 }
155
156 using PreparedFactory = std::function<std::expected<StyleSheet, StyleError>(std::stop_token)>;
157 std::string id_;
158 TextValue label_;
159 std::optional<TextValue> description_;
160 std::function<std::expected<StyleSheet, StyleError>()> factory_;
161 std::shared_ptr<const PreparedFactory> preparedFactory_;
162};
163
164namespace detail {
166struct PreparedThemeAccess {
167 template <class Factory>
168 static ThemeChoice make(std::string_view id, TextValue label, Factory factory, std::optional<TextValue> description) {
169 return ThemeChoice(ThemeChoice::PreparedThemeTag{}, id, std::move(label), std::move(factory),
170 std::move(description));
171 }
172};
173
175} // namespace detail
176
193template <class Factory>
194 requires std::copy_constructible<Factory> && std::invocable<Factory &, std::stop_token> &&
195 (std::same_as<std::invoke_result_t<Factory &, std::stop_token>, StyleSheet> ||
196 std::same_as<std::invoke_result_t<Factory &, std::stop_token>, std::expected<StyleSheet, StyleError>>)
197ThemeChoice preparedTheme(std::string_view id, TextValue label, Factory factory,
198 std::optional<TextValue> description = {}) {
199 return detail::PreparedThemeAccess::make(id, std::move(label), std::move(factory), std::move(description));
200}
201
208template <class Factory>
209 requires std::copy_constructible<Factory> && std::invocable<Factory &, std::stop_token> &&
210 (std::same_as<std::invoke_result_t<Factory &, std::stop_token>, StyleSheet> ||
211 std::same_as<std::invoke_result_t<Factory &, std::stop_token>, std::expected<StyleSheet, StyleError>>)
212ThemeChoice preparedTheme(std::string_view id, std::string_view label, Factory factory,
213 std::optional<TextValue> description = {}) {
214 return preparedTheme(id, TextValue(label), std::move(factory), std::move(description));
215}
216
225public:
236 std::expected<void, ManagedError> preview(std::string_view id,
237 std::source_location origin = std::source_location::current()) const;
243 std::expected<void, ManagedError> commit(std::source_location origin = std::source_location::current()) const;
248 std::expected<void, ManagedError> cancel(std::source_location origin = std::source_location::current()) const;
252 std::expected<std::shared_ptr<const ThemeSelectionSnapshot>, ManagedError> snapshot() const;
253
254private:
255 friend class portals::ManagedScene;
256 friend struct detail::ThemeSelectionAccess;
257 friend struct detail::ThemeSelectorAccess;
258 std::shared_ptr<detail::ThemeSelectionTarget> target_;
259};
260
266public:
271 ThemeSelectionStage(ThemeSelectionRef reference, std::initializer_list<ThemeChoice> catalog,
272 ThemeSelectionOptions options = {},
273 std::source_location origin = std::source_location::current());
277 friend Scene &operator|(Scene &owner, const ThemeSelectionStage &stage);
281 friend Scene operator|(Scene &&input, const ThemeSelectionStage &stage);
282
283private:
284 std::shared_ptr<const detail::ThemeSelectionDescription> description_;
285 std::source_location origin_;
286};
287
293ThemeSelectionStage themeSelection(ThemeSelectionRef reference, std::initializer_list<ThemeChoice> catalog,
294 ThemeSelectionOptions options = {},
295 std::source_location origin = std::source_location::current());
296} // namespace ui
297} // namespace portals
Owning validated UTF-8 and immutable extended grapheme measurements.
Definition text.hpp:145
Additive lifecycle owner over one existing Application subtree. UI affine and nonmovable....
Definition managed.hpp:530
Owning literal bytes or a resource key, measured only at resolution.
Definition text_value.hpp:42
constexpr TextValue(const TextValue &other)=default
Copy expression bytes and kind into independent storage.
Single-use lazy owner of one Application and ManagedScene; no second runtime. Construct and operate o...
Definition ui_scene.hpp:253
Owning catalog input; legacy factories run synchronously once per explicit preview attempt on UI....
Definition theme_selection.hpp:95
ThemeChoice(std::string_view id, std::string_view label, Factory factory, std::optional< TextValue > description={})
Definition theme_selection.hpp:122
ThemeChoice(std::string_view id, TextValue label, Factory factory, std::optional< TextValue > description={})
Definition theme_selection.hpp:107
~ThemeChoice()=default
Retain fallible ordinary copies of the owning catalog input.
Weak UI-affine observation of the one scene-owned catalog/model. Copying/destroying observations neve...
Definition theme_selection.hpp:224
std::expected< void, ManagedError > preview(std::string_view id, std::source_location origin=std::source_location::current()) const
std::expected< void, ManagedError > cancel(std::source_location origin=std::source_location::current()) const
ThemeSelectionRef()
Construct a cold observation on the UI thread; allocation may throw before an owner exists.
std::expected< std::shared_ptr< const ThemeSelectionSnapshot >, ManagedError > snapshot() const
std::expected< void, ManagedError > commit(std::source_location origin=std::source_location::current()) const
Owning deferred scene configuration; setup publishes the observation only after normalization succeed...
Definition theme_selection.hpp:265
friend Scene operator|(Scene &&input, const ThemeSelectionStage &stage)
ThemeSelectionStage(ThemeSelectionRef reference, std::initializer_list< ThemeChoice > catalog, ThemeSelectionOptions options={}, std::source_location origin=std::source_location::current())
friend Scene & operator|(Scene &owner, const ThemeSelectionStage &stage)
consteval auto id(StyleState state=StyleState::ANY, bool readOnly=false)
Definition style_static.hpp:865
Public Portals API.
Definition action.hpp:8
Original error plus independent runner/restoration and cleanup failures.
Definition managed_diagnostics.hpp:62
Allocation-free error; message borrows a static string literal.
Definition style.hpp:307
Owned immutable-at-use rules; constructing typed values may allocate and throw.
Definition style.hpp:233
Owning limits checked without silently truncating input or clusters.
Definition text.hpp:57
Fixed owning metadata shared by independent snapshots.
Definition theme_selection.hpp:54
std::vector< std::shared_ptr< const ThemeChoiceData > > choices
Ordered metadata; no factories or callbacks.
Definition theme_selection.hpp:57
~ThemeCatalog()=default
Keep checked vector transfers fallible.
GraphemeText label
Resolved display label under the receiving scene width policy.
Definition theme_selection.hpp:49
std::optional< GraphemeText > description
Optional resolved explanation, independently owned.
Definition theme_selection.hpp:50
ThemeChoiceData(std::string_view id, const GraphemeText &label)
Definition theme_selection.hpp:43
std::string id
Unique application catalog ID, never an engine token.
Definition theme_selection.hpp:48
~ThemeChoiceData()=default
Keep owning string/text transfers fallible with the checked standard library.
Finite normalization budgets and optional first-preview layer admission.
Definition theme_selection.hpp:77
std::size_t maxIdBytes
Positive UTF-8 byte cap for each nonempty unique ID.
Definition theme_selection.hpp:81
std::optional< StyleLayerOptions > layers
Absent preserves established budgets or uses engine defaults.
Definition theme_selection.hpp:87
~ThemeSelectionOptions()=default
Keep owning resolver copies fallible.
std::size_t maxChoices
Positive maximum number of catalog entries.
Definition theme_selection.hpp:80
TextLimits textLimits
Positive per-value validation limits beneath byte caps.
Definition theme_selection.hpp:85
std::size_t maxTextBytes
Positive aggregate logical ID/label/description byte cap.
Definition theme_selection.hpp:84
std::size_t maxLabelBytes
Positive resolved label byte cap.
Definition theme_selection.hpp:82
std::size_t maxDescriptionBytes
Positive resolved description byte cap.
Definition theme_selection.hpp:83
std::function< std::expected< std::string, TextResourceError >(std::string_view)> textResolver
Local UI resolver.
Definition theme_selection.hpp:86
Owning point-in-time read model; IDs do not claim the identity of an external theme....
Definition theme_selection.hpp:64
std::optional< std::string > lastCommittedId
Historical successful application choice; absent initially.
Definition theme_selection.hpp:70
ThemeSelectionStatus status
Current preview authority status.
Definition theme_selection.hpp:68
std::optional< std::string > previewedId
Installed choice when ACTIVE; historical choice when SUPERSEDED.
Definition theme_selection.hpp:69
std::shared_ptr< const ThemeCatalog > catalog
Fixed normalized catalog, usable after scene shutdown.
Definition theme_selection.hpp:67
std::optional< ManagedError > preparationError
Exact latest preparation/install failure; absent on success.
Definition theme_selection.hpp:73
bool preparing
Current prepared intent is waiting for admission, work or UI delivery.
Definition theme_selection.hpp:72
std::optional< std::string > requestedId
Latest explicit prepared intent, independent of the installed preview.
Definition theme_selection.hpp:71
~ThemeSelectionSnapshot()=default
Keep owning ID transfers fallible.