Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
Choosing and updating a theme

Русская версия

A theme supplies presentation rules and a palette. An application can install one with ui::theme:

#include <portals/ui.hpp>
#include <portals/ui_style.hpp>
int main() {
using namespace portals;
return ui::scene(ui::vbox(
ui::label("F10: quit"),
ui::button("Save")))
| ui::theme(nordTheme())
| ui::onKey("F10", ui::quit)
| ui::runApp();
}
Public Portals API.
Definition action.hpp:8
StyleSheet nordTheme()

The short guide and package setup explain how to build a client. The compact runner attempts cleanup and reports failures; declarative UI describes checked construction. The fragments below assume an open, idle scene, ui::Scene, named scene, using namespace portals;, and <portals/ui_style.hpp>.

Built-in palettes

Factory Appearance
modernTheme() Modern dark palette
turboTheme() Turbo-like palette
nordTheme() Nord dark palette
gruvboxTheme() Gruvbox dark palette
tokyoNightTheme() Tokyo Night dark palette
catppuccinLatteTheme() Catppuccin Latte light palette

Factories return a StyleSheet that owns its rules and may allocate. The ui::Scene facade collects installation errors for scene.checked() or the runner. Theme changes belong between frames on the UI thread.

A custom palette

ThemeTokens supplies six named palette values, or theme tokens, each stored as an RgbColor:

scene
.foreground = RgbColor{220, 225, 232},
.background = RgbColor{24, 28, 36},
.accent = RgbColor{75, 180, 220},
.muted = RgbColor{125, 135, 150},
.selection = RgbColor{45, 75, 110},
.heading = RgbColor{255, 200, 85}
}));
StyleSheet themeFromTokens(ThemeTokens tokens)
Owning RGB color; terminal encoding belongs to the output adapter.
Definition cell_buffer.hpp:20
Typed palette tokens for the built-in theme factories.
Definition style.hpp:347
Token Role
foreground Normal and selected text
background Normal fill, focused text and caret fill
accent Focused fill and caret text
muted Disabled text
selection Selected row or editor range fill
heading Header text

The built-in rules request bold for focus, selection and headers. The caret reverses the focused field colors. Additional control-specific choices can be expressed with PSS rules. Runtime PSS v0 cannot refer to these C++ tokens by name.

Application styles during a theme change

scene
| ui::style("Button { min-width: 12; }", {.name = "layout"})
| ui::style("Label { bold: true; }",
{.tier = StyleTier::USER, .name = "contrast"})
| ui::theme(gruvboxTheme());
StyleSheet gruvboxTheme()
@ USER
User overrides, above APPLICATION.
Definition style.hpp:255

Theme replacement updates THEME. APPLICATION and USER layers stay installed above it. If an application layer sets a foreground color, that declaration continues to take priority over the theme's foreground. Style precedence and layer contracts describe the details.

Palette rules from a file

The repository includes themes/modern.pss and themes/turbo.pss, matching their C++ factories. Their rules can be loaded as an application layer:

scene
| ui::style(ui::file("themes/modern.pss"), {.name = "palette-file"});

Style-source stages accept APPLICATION and USER. A palette installed there can override colors from ui::theme. Current theme factories and ui::theme(StyleSheet) replace the reserved THEME slot; direct PSS-file theme loading is TBD — M5. PSS v0 has no automatic reload or theme-variable substitution. File loading is described in the styling guide.

Changing a typed color while the application runs

The experimental C++ descriptor supports named RgbColor, bool and std::int32_t parameters. This setup declares a live accent:

namespace sc = portals::style::ct;
constexpr auto THEME = sc::sheet(
sc::parameters(sc::parameter<"accent">(sc::rgb(75, 180, 220))),
sc::rule(sc::selector<"Button">(),
sc::foreground(sc::token<"accent">())),
sc::rule(sc::selector<"Button">(StyleState::FOCUSED),
sc::background(sc::mix(sc::token<"accent">(),
sc::rgb(24, 28, 36), 50))));
auto stage = ui::liveTheme(THEME);
auto colors = stage.ref();
scene
| stage;
Owning compile-time PSS v0 snapshots using the existing runtime cascade.
Definition pss_compact.hpp:8
@ FOCUSED
Synchronized control focus.
Definition style.hpp:29

The UI stage is the configuration object returned by ui::liveTheme(THEME); in this example it is stored as stage. Applying scene | stage attaches the controller.

After successful attachment, an event handler on the UI thread can update it:

auto changed = colors.set<"accent">(sc::rgb(255, 200, 85));

The result is true when the value changed and false when it was already equal. An error preserves previous values and styles. mix accepts a percentage from 0 to 100. Atomic batches, exact type checks and controller lifetime are described in compile-time styles. The scene owns the controller. colors observes it without extending its lifetime. Reads and updates require the UI thread. A closed scene or superseded theme controller returns a typed error; a fresh stage is required for a new controller.

Terminal appearance

True color preserves RGB values. ANSI16 maps them to a smaller palette and can merge nearby colors. Bold, italic and underline depend on terminal support.

Planned theme support

Canonical PSS theme sources and expanded parameters are TBD — M5. Importing external theme formats is TBD, with a milestone still to be assigned. Planned features describes these changes.