5#include <portals/style_types.hpp>
6#include <portals/widget.hpp>
8#include <initializer_list>
20struct StyleThemeReceiver;
22struct StyleRoleTestAccess;
49 std::vector<std::string>(0);
51 std::vector<std::string>
baseTypes = std::vector<std::string>(
70 std::vector<std::string>
classes = std::vector<std::string>(0);
98constexpr bool validStyleRoleName(std::string_view name)
noexcept {
100 name.back() ==
'-') {
103 for (
const char value : name) {
104 if (!((value >=
'a' && value <=
'z') || (value >=
'0' && value <=
'9') || value ==
'-')) {
138 std::vector<StyleRoleValue>
values = std::vector<StyleRoleValue>(0);
157 std::shared_ptr<const StyleRolePalette>
snapshot() const noexcept;
162 friend class StyleEngine;
163 friend struct detail::StyleRoleTestAccess;
164 std::shared_ptr<detail::StyleRoleState> state_;
165 std::shared_ptr<const
void> participation_;
214 std::shared_ptr<const detail::ScrollbarGlyphRefs> scrollbarGlyphs;
216 std::shared_ptr<const StyleSizing>
sizing;
217 std::shared_ptr<const std::vector<StyleRolePatch>>
roles;
224 ((!scrollbarGlyphs && !other.scrollbarGlyphs) ||
225 (scrollbarGlyphs && other.scrollbarGlyphs &&
226 detail::equalScrollbarGlyphs(*scrollbarGlyphs, *other.scrollbarGlyphs))) &&
240 std::vector<StyleRule>(0);
241 std::optional<NodeId>
278 friend class StyleEngine;
279 std::weak_ptr<const void> lifetime_;
280 std::thread::id owner_;
281 std::uint64_t epoch_{};
318 return property ? std::string_view(
property) : std::string_view{};
332std::expected<StyleSheet, StyleError>
parsePss(std::string_view source);
344 bool readOnly =
false)
noexcept;
411 std::expected<Invalidation, StyleError>
bindBatch(std::span<const StyleRegistration> registrations);
417 std::span<const std::string_view> names);
423 std::initializer_list<std::string_view> names) {
424 return registerRoles(node, std::span<const std::string_view>(names.begin(), names.size()));
437 std::span<const std::string_view> names);
444 std::initializer_list<std::string_view> names) {
445 return replaceRoles(registration, std::span<const std::string_view>(names.begin(), names.size()));
461 std::expected<Invalidation, StyleError>
replacePss(std::string_view source);
465 std::expected<Invalidation, StyleError>
refresh();
498 std::expected<Invalidation, StyleError>
updateLayers(std::span<const StyleLayerUpdate> updates);
516 friend class ManagedScene;
517 friend struct StyleLayerTestAccess;
521 : id(identity), order(installationOrder), tier(priority), name(
"") {
531 std::shared_ptr<const PssDocument> document;
535 std::vector<Layer> layers = std::vector<Layer>(0);
536 StyleLayerOptions options;
537 std::uint64_t epoch{};
538 std::uint64_t nextId{1};
539 std::uint64_t nextOrder{1};
540 std::uint64_t owner{};
541 std::uint64_t themeBase{};
542 std::uint64_t preview{};
545 enum class Mutation {
558 struct PreviewCapture {
559 std::uint64_t revision{};
560 detail::StyleThemeLease lease;
561 std::weak_ptr<const void> lifetime;
564 using RoleRegistry = std::vector<std::weak_ptr<detail::StyleRoleState>>;
571 std::expected<Invalidation, StyleError>
572 install(StyleSheet sheet, std::vector<Binding> &&bindings, LayerState *candidate =
nullptr,
bool wholeReplace =
false,
573 const detail::StyleThemeLease *lease =
nullptr,
const PreviewCapture *previewCapture =
nullptr,
574 bool notifyTheme =
false, std::shared_ptr<const RoleRegistry> *roleCandidate =
nullptr,
575 detail::StyleRoleState *replacedRole =
nullptr);
576 std::expected<Invalidation, StyleError> replaceOwnedTheme(
const StyleSheet &sheet, detail::StyleThemeLease lease);
577 std::expected<void, StyleError> mergeBindings(std::span<const StyleRegistration> registrations,
578 std::optional<std::vector<Binding>> &destination);
579 std::expected<Invalidation, StyleError>
580 mutate(Mutation mutation, StyleLayerOptions options,
StyleTier tier, std::string_view name,
const StyleSheet *sheet,
581 std::span<const StyleLayerUpdate> updates, std::span<const StyleRegistration> registrations,
582 StyleLayerHandle *added =
nullptr,
const detail::StyleThemeLease *lease =
nullptr,
583 const PssDocument *document =
nullptr, std::optional<StyleLayerOptions> admissionOptions = std::nullopt,
584 const PreviewCapture *previewCapture =
nullptr);
585 PreviewCapture capturePreview() const noexcept;
586 std::expected<
void, StyleError> validatePreviewCapture(const PreviewCapture &capture) const;
587 bool ownsPreview(const StyleLayerHandle &handle) const noexcept;
588 std::expected<
void, StyleError> validateHandle(const StyleLayerHandle &handle) const;
589 std::expected<
void, StyleError> validateLease(detail::StyleThemeLease lease) const;
590 detail::StyleThemeLease themeLease() const noexcept;
591 StyleLayerHandle handle(std::uint64_t
id, std::uint64_t epoch) const noexcept;
592 ComputedStyle resolve(const StyleSheet &sheet, const LayerState &state, const StyleTarget &target,
593 ComputedStyle defaults, NodeId node = {},
bool readOnly =
false)
const noexcept;
594 std::expected<void, StyleError> checkBoundary(
bool completing =
false)
const;
596 std::weak_ptr<detail::StyleThemeReceiver> themeReceiver_;
597 std::size_t maxBindings_;
599 std::vector<Binding> bindings_ = std::vector<Binding>(0);
600 StyleRoleLimits roleLimits_;
601 std::shared_ptr<const RoleRegistry> roleRegistrations_;
602 std::uint64_t revision_{};
603 std::shared_ptr<const void> lifetime_ = std::make_shared<const char>(
char{});
604 const std::thread::id owner_ = std::this_thread::get_id();
605 LayerState layerState_;
606 bool stagingLayers_{};
Copyable typed observer; never owns or exposes an unchecked widget pointer.
Definition widget.hpp:92
UI-owned style transaction coordinator; tree must outlive this nonmovable object. Bindings own metada...
Definition style.hpp:383
std::expected< StyleRoleRegistration, StyleError > registerRoles(Node< Widget > node, std::span< const std::string_view > names)
Register an existing bound node for a bounded semantic projection.
std::uint64_t revision() const noexcept
Inspect on UI; no allocation or tree access.
std::expected< Invalidation, StyleError > refresh()
Recompute against current manual widget options, between frames.
std::expected< Invalidation, StyleError > bindBatch(std::span< const StyleRegistration > registrations)
Update metadata in one transaction, then recompute every bound widget once. Duplicate input nodes ret...
std::expected< StyleLayerHandle, StyleError > addLayer(StyleTier tier, std::string_view name, const StyleSheet &sheet)
Install an owning APPLICATION or USER layer with a unique tier name.
std::expected< Invalidation, StyleError > replacePss(std::string_view source)
Parse and install without ever exposing a partial stylesheet.
std::expected< Invalidation, StyleError > updateLayers(std::span< const StyleLayerUpdate > updates)
Atomically update existing unique handles. Empty input is a no-op.
std::expected< Invalidation, StyleError > replace(StyleSheet sheet)
Install a complete typed stylesheet atomically between frames.
std::expected< Invalidation, StyleError > bind(Node< Widget > node, StyleTarget target)
Register/update metadata and atomically recompute every bound widget.
std::expected< StyleLayerHandle, StyleError > baseLayer(StyleTier tier) const
Observe one permanent base slot on its UI thread.
std::expected< Invalidation, StyleError > defaults(const StyleSheet &sheet)
Replace the DEFAULTS slot beneath all other layers.
std::expected< Invalidation, StyleError > enableLayers(StyleLayerOptions options={})
Enable owning tiers atomically; equal configuration is a no-op.
std::expected< StyleRoleRegistration, StyleError > replaceRoles(const StyleRoleRegistration ®istration, std::span< const std::string_view > names)
Atomically replace the bounded names of a live semantic registration.
std::expected< StyleRoleRegistration, StyleError > registerRoles(Node< Widget > node, std::initializer_list< std::string_view > names)
Definition style.hpp:422
std::expected< Invalidation, StyleError > theme(const StyleSheet &sheet)
Replace THEME and hand authority to the caller, including equal values.
std::expected< Invalidation, StyleError > unregisterRoles(const StyleRoleRegistration ®istration)
StyleEngine(WidgetTree &tree, std::size_t maxBindings=4096, StyleRoleLimits roleLimits={})
std::expected< StyleRoleRegistration, StyleError > replaceRoles(const StyleRoleRegistration ®istration, std::initializer_list< std::string_view > names)
Definition style.hpp:443
std::expected< Invalidation, StyleError > replaceLayer(StyleLayerHandle handle, const StyleSheet &sheet)
Replace a non-base layer while retaining its installation order.
std::expected< Invalidation, StyleError > removeLayer(StyleLayerHandle handle)
Remove declarations; base slots remain and revoke their owner.
Weak UI-affine observer; destruction does not remove its layer.
Definition style.hpp:272
StyleLayerHandle()=default
Construct an empty observer; layer mutations reject it as INVALID_ARGUMENT.
UI-thread scoped registration; the engine retains only a weak observer. Copies share participation....
Definition style.hpp:151
StyleRoleRegistration()=default
Empty allocation-free handle.
std::uint64_t revision() const noexcept
std::shared_ptr< const StyleRolePalette > snapshot() const noexcept
constexpr auto PREVIEW
Local work only.
Definition document_highlight_types.hpp:47
Public Portals API.
Definition action.hpp:8
StyleSheet gruvboxTheme()
constexpr std::size_t MAX_STYLE_BASE_TYPES
Maximum owned ancestor names per style target, excluding its immediate type.
Definition style.hpp:37
StyleSheet themeFromTokens(ThemeTokens tokens)
constexpr std::size_t MAX_STYLE_ROLE_PATCHES
Maximum aggregate typed semantic patches in one admitted stylesheet.
Definition style.hpp:95
@ DISABLED
Recording is stopped; retained evidence remains available.
Definition diagnostics.hpp:72
ComputedStyle resolveStyle(const StyleSheet &sheet, const StyleTarget &target, ComputedStyle defaults={}, bool readOnly=false) noexcept
Resolve one subject through the same engine used by typed and PSS installation.
std::expected< StyleSheet, StyleError > parsePss(std::string_view source)
Parse strict PSS v0 without I/O or mutation, on any thread.
constexpr std::size_t MAX_STYLE_ROLE_NAME_BYTES
Maximum ASCII bytes in a registered lower-kebab semantic role name.
Definition style.hpp:93
StyleSheet tokyoNightTheme()
StyleSheet catppuccinLatteTheme()
StyleState
Presentation states; ANY matches every state without changing behavior.
Definition style.hpp:26
@ NORMAL
Ordinary content.
Definition style.hpp:28
@ FOCUSED
Synchronized control focus.
Definition style.hpp:29
@ CARET
Editor software caret.
Definition style.hpp:33
@ ANY
Match all presentation variants.
Definition style.hpp:27
@ SELECTED
Selected row or editor range.
Definition style.hpp:31
constexpr std::size_t MAX_STYLE_RULES
Finite typed/PSS rule budget.
Definition style.hpp:302
StyleTier
Independent cascade priority, before selector specificity.
Definition style.hpp:251
@ APPLICATION
Application overrides, above THEME.
Definition style.hpp:254
@ USER
User overrides, above APPLICATION.
Definition style.hpp:255
@ DEFAULTS
Declarations above widget defaults and below all other tiers.
Definition style.hpp:252
@ THEME
Reserved theme declarations controlled by one authority lease.
Definition style.hpp:253
std::expected< void, StyleError > validateStyleTarget(const StyleTarget &target) noexcept
Validate bounded ancestor metadata without allocation or retaining input.
constexpr std::size_t MAX_PSS_BYTES
Finite parser input byte budget.
Definition style.hpp:304
Invalidation
Accumulated work; layout always includes painting.
Definition widget_types.hpp:41
@ 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
@ INVALID_ARGUMENT
An option, enum value or operation argument is invalid.
Definition widget_types.hpp:27
@ NORMAL
Unclassified source and whitespace.
Definition document_highlight_types.hpp:53
Owning resolved presentation; no actions or model state are encoded.
Definition style_types.hpp:169
Owning parsed declarations and provenance; input bytes need not survive the call. Metadata is not aut...
Definition pss_data.hpp:259
Owning RGB color; terminal encoding belongs to the output adapter.
Definition cell_buffer.hpp:20
Typed declarations; absent values preserve less-specific rules or widget defaults.
Definition style.hpp:169
StylePaintDeclarations scrollbarThumbStart
Thumb start overrides, inheriting the common thumb.
Definition style.hpp:188
std::optional< ScrollbarThumbPattern > scrollbarThumbPattern
Independent scrollbar source.
Definition style.hpp:185
StylePaintDeclarations scrollbarThumbBody
Thumb body overrides, inheriting the common thumb.
Definition style.hpp:189
StylePaintDeclarations scrollbarEnds
Scrollbar ends overrides.
Definition style.hpp:181
StylePaintDeclarations scrollbarThumbEnd
Thumb end overrides, inheriting the common thumb.
Definition style.hpp:190
StylePaintDeclarations scrollbarThumbSingle
Thumb single overrides, inheriting the common thumb.
Definition style.hpp:191
StylePaintDeclarations scrollbarCorner
Scrollbar corner overrides.
Definition style.hpp:182
std::optional< RgbColor > foreground
RGB text color.
Definition style.hpp:170
StylePaintDeclarations sliderThumb
Slider position indicator overrides.
Definition style.hpp:194
std::optional< std::int32_t > minWidth
Nonnegative cell minimum; allowed only on state-independent selectors.
Definition style.hpp:175
StylePaintDeclarations sliderTrack
Unfilled Slider rail overrides, inheriting the resolved state.
Definition style.hpp:192
std::optional< bool > bold
Explicit true/false bold request.
Definition style.hpp:172
std::optional< ScrollbarArrowPattern > scrollbarArrowPattern
Independent scrollbar source.
Definition style.hpp:186
std::optional< bool > italic
Explicit true/false italic request.
Definition style.hpp:173
StylePaintDeclarations frameLine
Frame perimeter overrides, inheriting the common resolved state.
Definition style.hpp:176
std::optional< ScrollbarPattern > scrollbarTrackPattern
Independent scrollbar source.
Definition style.hpp:184
std::optional< GlyphFallback > glyphFallback
Explicit portable one-cell admission fallback.
Definition style.hpp:196
bool operator==(const StyleDeclarations &other) const =default
StylePaintDeclarations scrollbarArrows
Functional arrow button overrides.
Definition style.hpp:183
std::optional< RgbColor > background
RGB fill color.
Definition style.hpp:171
StylePaintDeclarations frameTitle
Frame title text overrides.
Definition style.hpp:177
StylePaintDeclarations frameEnds
Frame title prefix/suffix overrides.
Definition style.hpp:178
StylePaintDeclarations sliderFill
Filled Slider rail overrides.
Definition style.hpp:193
StylePaintDeclarations scrollbarTrack
Scrollbar track overrides.
Definition style.hpp:179
std::optional< bool > underline
Explicit true/false underline request.
Definition style.hpp:174
StylePaintDeclarations scrollbarThumb
Scrollbar thumb overrides.
Definition style.hpp:180
std::optional< GlyphProfile > glyphProfile
Named base beneath independent patterns and literal sources.
Definition style.hpp:195
Allocation-free error; message borrows a static string literal.
Definition style.hpp:307
std::uint64_t layerId
Failing resident/candidate layer identity; zero for legacy or typed sources.
Definition style.hpp:314
std::string_view propertyName() const noexcept
Definition style.hpp:317
TreeError cause
Tree/validation/allocation failure category.
Definition style.hpp:308
std::size_t column
One-based byte column, or one for typed/tree errors.
Definition style.hpp:310
const char * property
Null or a library static null-terminated property identity for a failed literal preparation.
Definition style.hpp:313
std::string_view message
Static diagnostic; never borrows source input.
Definition style.hpp:311
std::size_t line
One-based PSS line, or one for typed/tree errors.
Definition style.hpp:309
Aggregate logical owning payload budgets; base slots count as layers.
Definition style.hpp:259
std::size_t maxOwnedBytes
Logical payload charge; not a whole-heap allocator cap.
Definition style.hpp:263
std::size_t maxRules
Rules summed across the complete resident table.
Definition style.hpp:261
bool operator==(const StyleLayerOptions &other) const =default
std::size_t maxLayers
Resident layer slots, including the two permanent bases.
Definition style.hpp:260
std::size_t maxDeclarations
Present optional fields, including explicit false values.
Definition style.hpp:262
Replace an existing layer, or remove it when sheet is absent.
Definition style.hpp:286
std::optional< StyleSheet > sheet
Owning replacement or nullopt for removal.
Definition style.hpp:288
StyleLayerHandle handle
Existing layer identity; duplicate identities reject the batch.
Definition style.hpp:287
Sparse paint attributes for an existing presentation part.
Definition style.hpp:81
std::optional< RgbColor > foreground
RGB text color.
Definition style.hpp:82
std::optional< bool > underline
Explicit underline request.
Definition style.hpp:86
bool operator==(const StylePaintDeclarations &other) const =default
std::optional< RgbColor > background
RGB fill color.
Definition style.hpp:83
std::optional< bool > italic
Explicit italic request.
Definition style.hpp:85
std::optional< bool > bold
Explicit bold request.
Definition style.hpp:84
Owned metadata for one checked target in an atomic binding batch.
Definition style.hpp:56
Node< Widget > node
Observing handle; the tree retains widget ownership.
Definition style.hpp:57
StyleTarget target
Matching metadata copied into the engine on success.
Definition style.hpp:58
Limits paid only by opt-in semantic registrations.
Definition style.hpp:121
std::size_t maxRolesPerRegistration
Positive maximum names in one registration.
Definition style.hpp:123
std::size_t maxTotalRoles
Positive aggregate count of live registered names.
Definition style.hpp:124
std::size_t maxRegistrations
Positive maximum live registrations, also bounded by maxBindings.
Definition style.hpp:122
Owning role projection; retain a const shared snapshot across node/engine retirement.
Definition style.hpp:136
~StyleRolePalette()=default
Keep checked vector transfers throwable.
std::optional< CellStyle > find(std::string_view name, StyleState state=StyleState::NORMAL) const noexcept
std::vector< StyleRoleValue > values
Bounded registered names only.
Definition style.hpp:138
bool operator==(const StyleRolePalette &) const =default
Cold sparse paint patch; names never encode focus or selection state.
Definition style.hpp:113
StylePaintDeclarations paint
Absent leaves inherit the resolved ordinary surface.
Definition style.hpp:116
std::string name
Lower-kebab name, copied by owning declarations.
Definition style.hpp:115
~StyleRolePatch()=default
Checked container copies may allocate; suppress implicit noexcept moves.
bool operator==(const StyleRolePatch &) const =default
One immutable-at-publication role in normal/focused/disabled/selected/header/caret order.
Definition style.hpp:128
bool operator==(const StyleRoleValue &) const =default
std::array< CellStyle, 6 > states
Actual paint values after the existing cascade.
Definition style.hpp:131
std::string name
Registered owned name.
Definition style.hpp:130
~StyleRoleValue()=default
Keep checked string transfers throwable.
One owned selector/declaration pair in source order.
Definition style.hpp:205
StyleDeclarations declarations
Definition style.hpp:212
std::shared_ptr< const std::vector< StyleRolePatch > > roles
Optional cold immutable semantic declarations.
Definition style.hpp:217
StyleSelector selector
Matching and specificity criteria.
Definition style.hpp:211
bool operator==(const StyleRule &other) const
Definition style.hpp:222
~StyleRule()=default
Destroy owned values. Deliberately suppress implicit noexcept moves: checked MSVC container moves can...
std::shared_ptr< const StyleSizing > sizing
Optional immutable runtime sizing; shared ownership, never a view.
Definition style.hpp:216
Conjunction of optional type, id, classes, one state and read-only; no hierarchy combinators.
Definition style.hpp:62
std::string type
Empty or * means any type.
Definition style.hpp:68
StyleState state
Optional presentation-state predicate.
Definition style.hpp:71
bool readOnly
True requires read-only; false adds no qualifier.
Definition style.hpp:72
~StyleSelector()=default
Destroy owned values. Deliberately suppress implicit noexcept moves: checked MSVC container moves can...
bool operator==(const StyleSelector &other) const =default
std::vector< std::string > classes
Every named class is required.
Definition style.hpp:70
Owned immutable-at-use rules; constructing typed values may allocate and throw.
Definition style.hpp:233
bool operator==(const StyleSheet &other) const =default
std::optional< NodeId > scope
Optional exact node restriction; preserves selector specificity and source metadata.
Definition style.hpp:242
std::vector< StyleRule > rules
Source-order rules, at most MAX_STYLE_RULES when installed.
Definition style.hpp:239
~StyleSheet()=default
Destroy owned values. Deliberately suppress implicit noexcept moves: checked MSVC container moves can...
Owned subject metadata, supplied explicitly for built-in or custom widget types.
Definition style.hpp:40
std::vector< std::string > baseTypes
Nearest-base-first owning names, at most MAX_STYLE_BASE_TYPES; no empty, wildcard,...
Definition style.hpp:51
~StyleTarget()=default
Destroy owned values. Deliberately suppress implicit noexcept moves: checked MSVC container moves can...
std::vector< std::string > classes
Case-sensitive class names; all selector classes must match.
Definition style.hpp:48
bool control
Also match Control selectors; explicit targets opt in independently of their type name.
Definition style.hpp:50
std::string type
Case-sensitive type name, for example Button or Table.
Definition style.hpp:46
Typed palette tokens for the built-in theme factories.
Definition style.hpp:347
RgbColor foreground
Normal foreground.
Definition style.hpp:348
RgbColor background
Normal background.
Definition style.hpp:349
RgbColor accent
Focus background, active frame foreground and contrasting caret foreground.
Definition style.hpp:350
RgbColor selection
Selected-row/range background.
Definition style.hpp:352
RgbColor heading
Heading foreground.
Definition style.hpp:353
RgbColor muted
Disabled foreground.
Definition style.hpp:351
Private captured theme authority, including legacy epoch admission.
Definition style.hpp:293
std::uint64_t epoch
Engine identity generation captured at attach.
Definition style.hpp:295
std::uint64_t owner
Reserved THEME authority generation.
Definition style.hpp:297
bool layered
Captured opt-in mode; controllers never silently adopt a new mode.
Definition style.hpp:294