JFLib

Theme#

Source: lib/ui/theme.rs

theme -- colour tokens, accent colour and type ramp of the linked style  [src/ui/ui_theme.s]

  theme_set(mode)            0 light, 1 dark, 2 follow the system (plat::theme)
  theme_dark() -> bool       the dark theme is active
  accent_set(rgb)            0xRRGGBB accent, 0 = the style's default (an app's accent
                             stops the system's)
  high_contrast() / set_contrast(Some(on) | None)   the high-contrast theme: every token
                             from the scheme's colours (Windows: the system's; else white on
                             black or black on white), no translucent fills; None follows
                             the system; --contrast high|normal
  set_reduced_motion(Some(on) | None)   reduced motion (anim_off) forced on or off, or as
                             the system says (None); --anim off|on
  text_scale() / set_text_scale(percent)   text sizes and line heights (not the controls)
  sys_apply(settings)        (runtime) plat::sys_settings: theme, accent, contrast, reduced
                             motion (anim_off), text scale, double-click and caret times,
                             font smoothing and its subpixel order, the wheel's lines and
                             characters; what the app or the command line fixed stays
  wheel_lines() / wheel_chars() / wheel_notch_px(line_px, page_px, horizontal)   what a
                             notch of the wheel scrolls (the system's setting)
  color(tok) -> argb         0xAARRGGBB of a T_* token (byte offset)
  ts_font(ts)                select font and pixel size of a TS_* text style
  ts_line(ts) -> px          line height of a text style
  ts_base(ts) -> px          baseline offset from the line top
  dp_text(dip) -> px         dp(dip) grown with a text scale above 100 % (pills around text)
  color_mix(a, b, t)         per-channel a + (b - a) * t / 256 (t 0..256)
  luminance(rgb) / contrast_ratio(a, b)   WCAG 2 relative luminance (0..1) of 0xRRGGBB and
                             the contrast of two colours (1..21; text wants 4.5, graphics 3)
App themes (theme_app.rs: Theme, its file format and errors) on top of the style:
  set_theme(&theme) -> Result   from the next frame on; a font that cannot be loaded is an
                             error and the previous theme stays; clear_theme() the style's
                             own look again; app_theme() -> Option<&Theme>;
                             load_theme(path) = Theme::load + set_theme
  The theme layer: theme_build (colours: the theme's tokens over the style's, its accent
  through the style's ramp), cmd_rrect / cmd_shadow (look_radius: corners that are not pills),
  box_begin / gridbox_begin (dp_space: padding and gaps), the size calls of buttons, text
  boxes, combo boxes, drop-down and split buttons (ctrl_size), ts_font / ts_line (sizes,
  line heights, weights, the family's faces).  Without a theme every value is the style's.
The run-time style switch (styles linked side by side, build_rs.py: every style_NAME the
app's sources name):
  styles_add(&[&style_kde::STYLE, ..])   style_names() -> names   style_name() -> current
  set_style(name) -> bool    the style's fonts replace ids 0..3 (Text::set_embedded), the
                             tokens are built again, everything is redrawn and laid out
look.serial counts the changes of theme and style: caches of laid-out text compare it.

The values come from the style (Style::tokens, accent, ts_size / ts_line / ts_font).
  • fn color_mix(a: u32, b: u32, t: i32) -> u32 color_mix(a, b, t): per-channel a + (b - a) * t / 256 (t 0..256), every channel including alpha

  • fn luminance(rgb: u32) -> f32 the WCAG 2 relative luminance of 0xRRGGBB (alpha ignored): 0 black .. 1 white

  • fn contrast_ratio(a: u32, b: u32) -> f32 the WCAG 2 contrast ratio of two colours (alpha ignored): 1 (equal) .. 21 (black on white); text needs 4.5 (large text 3), graphical objects and controls 3

  • fn contrast_tokens(c: &plat::HcColors) -> [u32; T_COUNT] the tokens of a high-contrast scheme: opaque fills from its colours, borders and strokes in the text colour, accents in the highlight colour, links in the link colour

impl Ui

  • fn theme_set(&mut self, mode: u32) ui_theme_set(mode): 0 light, 1 dark, 2 the system setting (unknown: dark)
  • fn theme_dark(&self) -> bool ui_theme_dark: the dark theme is active
  • fn accent_set(&mut self, rgb: u32) ui_accent_set(0xRRGGBB), 0 = the style's default; the system's accent no longer applies
  • fn high_contrast(&self) -> bool the high-contrast theme is active
  • fn set_contrast(&mut self, on: Option<bool>) high contrast on (Some(true)), off (Some(false)) or as the system says (None)
  • fn set_reduced_motion(&mut self, on: Option<bool>) reduced motion on (Some(true): no tweens, no fades), off (Some(false)) or as the system says (None); like --anim, a forced value outlasts the system's changes
  • fn text_scale(&self) -> u32 the text scale in percent (100 = the style's sizes)
  • fn font_prefs(&self) -> (Option<bool>, Option<u8>, Option<u8>) the system's font rendering: (smoothing, plat::FONT_HINT_*, plat::FONT_RGBA_*), None where the system says nothing; smoothing off (ui.text.antialias()) and the subpixel order (ui.text.subpixel(): subpixel smoothing on the window's framebuffer) are applied, hinting is not
  • fn wheel_lines(&self) -> u32 the lines a notch of the wheel scrolls as the system says it (1..=100; plat::WHEEL_PAGE: a page): Windows' mouse settings (SPI_GETWHEELSCROLLLINES), KDE's WheelScrollLines; 3 elsewhere and in headless runs (jfgui_WHEEL_LINES). The library's views scroll that many lines per notch (120 of UiInput::wheel_y): WHEEL_LINE_DIP each, their text's lines in text views; an app that scrolls by lines of its own takes this many per notch too
  • fn wheel_chars(&self) -> u32 the characters a notch of the horizontal wheel scrolls (1..=100; Windows SPI_GETWHEELSCROLLCHARS; 3 elsewhere and in headless runs, jfgui_WHEEL_CHARS)
  • fn wheel_notch_px(&self, line_px: i32, page_px: i32, horizontal: bool) -> i32 the pixels a notch of the wheel scrolls in a view whose lines are line_px high and whose page is page_px: wheel_lines lines (a page for plat::WHEEL_PAGE); horizontal: wheel_chars characters of line_px
  • fn set_text_scale(&mut self, percent: u32) scale text sizes and line heights (50..300 %); the system's scale no longer applies
  • fn color(&self, tok: u32) -> u32 ui_color(tok): 0xAARRGGBB of a token (byte offset; any value reads inside the table)
  • fn ts_font(&mut self, ts: u32) ui_ts_font(ts): select the font and pixel size (size * dpi / 96, 24.8) of a style
  • fn ts_line(&self, ts: u32) -> i32 ui_ts_line(ts): line height of a text style in px
  • fn set_theme(&mut self, th: &Theme) -> Result<(), ThemeError> the app's theme over the style (theme_app.rs) from the next frame on: colours of light, dark and high contrast, the accent, corners, spacing, control heights, text styles and a font family. A font that cannot be loaded is an error; the previous theme stays.
  • fn clear_theme(&mut self) no app theme: the style's own look again
  • fn app_theme(&self) -> Option<&Theme> the active app theme (None: the style's look)
  • fn load_theme(&mut self, path: &str) -> Result<(), ThemeError> read a theme file (app://, config://, a path) and make it the app's theme; on an error the previous theme stays
  • fn styles_add(&mut self, styles: &[&'static dyn Style]) the styles set_style can switch to: the app names the style crates it links besides its own (build_rs.py links every style_NAME its sources name): ui.styles_add(&[&style_classic::STYLE, &style_kde::STYLE])
  • fn style_names(&self) -> Vec<&'static str> the names of the styles set_style knows (the app's own first, then as added)
  • fn style_name(&self) -> &'static str the name of the style in use (Style::name)
  • fn set_style(&mut self, name: &str) -> bool switch to another linked style (styles_add) from the next frame on: its tokens, fonts, metrics and painters; the app theme stays on top -> false: no such style
  • fn dp_text(&self, dip: i32) -> i32 dp_text(dip): DIP -> px, grown with a text scale above 100 % (the height of parts around one line of text, such as badge pills)
  • fn ts_base(&mut self, ts: u32) -> i32 ui_ts_base(ts): baseline offset from the top of the line box (px; selects the font)

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