JFLib

34. App themes and the style at run time#

An app's own colours, corners, spacing, control heights and type do not need a style crate: an app theme (ui/theme_app.rs) lays them over the active style, from a small INI file or built in code, and takes effect on the next frame. An app that links several styles switches between them at run time. STYLES.md has the whole format and how the styles read it.

use ui::theme::{Radius, Theme, THEME_DARK, THEME_LIGHT};

// a theme file of the app (apps/NAME/assets/themes/...) or of the user (config://)
match ui.load_theme("app://themes/ocean.theme") {
    Ok(()) => {}
    Err(e) => self.error = e.to_string(),   // "Zeile 5: „#12345“ ist keine Farbe (#RRGGBB oder #AARRGGBB)"
}
// an editor: start from the active theme, change a token, apply it live, save it
let mut th = ui.app_theme().cloned().unwrap_or_default();
th.name = String::from("Eigenes");
th.accent[THEME_LIGHT] = Some(0x6B4FBB);                 // the style derives its accent ramp
th.colors[THEME_DARK][(T_BG / 4) as usize] = Some(0xFF17_2029);
th.radius = Some(Radius::Dip(8));
th.spacing = 110;                                        // percent: box padding and gaps
th.control_height = 36;                                  // DIP: buttons, text / combo boxes
th.text[TS_BODY as usize].size_fx = Some(15 << 8);
ui.set_theme(&th)?;                                      // a font that cannot be loaded: Err, theme stays
th.save("config://themes/eigenes.theme");                // the text of a theme file (to_ini)
ui.clear_theme();                                        // the style's own look again

// several styles: build_rs.py links every style_NAME the sources name
ui.styles_add(&[&style_winui::STYLE, &style_classic::STYLE, &style_kde::STYLE]);
let names = ui.style_names();                            // the app's own style first
ui.set_style("kde");                                     // tokens, fonts, metrics, painters
# app://themes/ocean.theme (apps/themedemo/assets/themes)
[theme]
name = Ozean
[light]
accent = #0F7B8A
bg = #EAF3F5
text = #0B2A33
[dark]
accent = #4FC3D1
bg = #0E1E24
[metrics]
radius = 8
spacing = 110%
control_height = 36
[text]
body.size = 15
subtitle.weight = bold
  • What a theme sets: every T_* colour token for light, dark ([colors] both) and high contrast, the accent (through the style's ramp; it wins over the app's and the system's), rounded corners that are not pills (radius = 8 DIP or 150%), box padding and gaps (spacing), the height of standard buttons, text boxes, combo boxes, drop-down and split buttons (control_height; it grows with a text scale above 100 %), size, line height and weight of each text style and a font family (a font file next to the theme, a VFS path, or an installed family) with axis values when it is variable (family.axes = wdth=87.5, opsz=12; the 400 / 600 / 700 faces come from its wght, also for a file instead of a synthetic bold; opsz else follows 11 pt). Light / dark / system and high contrast stay the app's and the user's choice: the theme has colours for each.
  • Errors name the line and come in the UI language (ThemeError { line, msg }); a broken file or a missing font leaves the previous theme as it was. Without a theme the rendering is the style's to the pixel.
  • Limits: a style's own fixed values stay (classic's grey 3D face and bevels come from its palette, not from tokens; title bar fonts; paddings inside a widget). The style switch replaces the embedded fonts (FONT_REG .. FONT_ICON) for the whole process; fonts the app loaded itself keep their ids. build_rs.py passes the extra style crates on every target; apps/themedemo is built and tested on Windows and Linux.
  • Command line and scripts: --theme-file PATH (after App::init); scripts theme file PATH, theme none, style NAME, expect theme "NAME"|none, expect theme error "TEXT", expect style NAME, expect token NAME #RRGGBB, expect pixel X Y #RRGGBB, expect same FILE.png (the same pixels as an earlier shot), rect * * * 36 (§12).

Tests: apps/themedemo -- the page "Darstellung" (style, light / dark / system, high contrast, the two sample themes, a live editor for accent, background, corners, spacing, control height, text size and a system font, saving to config://themes/eigenes.theme and loading a theme file by its path) with a preview of the common widgets; tests/themes.uis (tokens, pixels and rectangles of both themes in light, dark and high contrast, back to the same pixels, all four linked styles with and without a theme, broken files, the editor, save and load), tests/text150.uis (at 150 % text). apps/gallery/tests/themes.uis and themes150.uis (the sample themes and a font family from files over the gallery; they hold in the builds of every style), uitest --only themes (the format, the errors with their lines, the text form, Ini::items).

Source: docs/GUIDE.md in the JFLib repository.