134 lines
4.7 KiB
Rust
134 lines
4.7 KiB
Rust
//! Feeds the desktop appearance (and agent overrides) into the Slint `Theme` and `MotionPrefs`.
|
|
|
|
use slint::{Color, ComponentHandle};
|
|
use tunal_theme::{Appearance, ColorScheme, Palette, Palettes};
|
|
|
|
use crate::{Gallery, MainWindow, MotionPrefs, Theme};
|
|
|
|
/// Overrides for agents and screenshots: `TUNAL_SEED=#rrggbb`, `TUNAL_SCHEME=light|dark`,
|
|
/// `TUNAL_REDUCED_MOTION=1`. Unset or invalid values leave the portal value in place.
|
|
#[derive(Debug, Clone, Copy, Default)]
|
|
pub struct Overrides {
|
|
pub seed: Option<u32>,
|
|
pub scheme: Option<ColorScheme>,
|
|
pub reduced_motion: bool,
|
|
}
|
|
|
|
impl Overrides {
|
|
pub fn from_env() -> Self {
|
|
let var = |name| std::env::var(name).ok().filter(|v: &String| !v.is_empty());
|
|
let seed = var("TUNAL_SEED").and_then(|v| {
|
|
let hex = v.strip_prefix('#').filter(|h| h.len() == 6);
|
|
let seed = hex.and_then(|h| u32::from_str_radix(h, 16).ok());
|
|
if seed.is_none() {
|
|
eprintln!("tunal: ignoring TUNAL_SEED={v:?}, expected #rrggbb");
|
|
}
|
|
seed
|
|
});
|
|
let scheme = var("TUNAL_SCHEME").and_then(|v| match v.as_str() {
|
|
"light" => Some(ColorScheme::Light),
|
|
"dark" => Some(ColorScheme::Dark),
|
|
_ => {
|
|
eprintln!("tunal: ignoring TUNAL_SCHEME={v:?}, expected light or dark");
|
|
None
|
|
}
|
|
});
|
|
let reduced_motion = var("TUNAL_REDUCED_MOTION").is_some_and(|v| v == "1");
|
|
Self {
|
|
seed,
|
|
scheme,
|
|
reduced_motion,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// A window with the `Theme` and `MotionPrefs` globals.
|
|
pub trait Themed: ComponentHandle + 'static {
|
|
fn theme(&self) -> Theme<'_>;
|
|
fn motion_prefs(&self) -> MotionPrefs<'_>;
|
|
}
|
|
|
|
macro_rules! themed {
|
|
($($window:ty),*) => {
|
|
$(impl Themed for $window {
|
|
fn theme(&self) -> Theme<'_> {
|
|
self.global::<Theme>()
|
|
}
|
|
fn motion_prefs(&self) -> MotionPrefs<'_> {
|
|
self.global::<MotionPrefs>()
|
|
}
|
|
})*
|
|
};
|
|
}
|
|
|
|
themed!(MainWindow, Gallery);
|
|
|
|
/// Reads the portal, applies it, and keeps the UI in step with later changes.
|
|
pub fn start(ui: &impl Themed, overrides: Overrides) {
|
|
apply(ui, &tunal_theme::read(), overrides);
|
|
let weak = ui.as_weak();
|
|
tunal_theme::watch(move |appearance| {
|
|
// Fails only once the event loop has quit; nothing left to theme then.
|
|
let _ = weak.upgrade_in_event_loop(move |ui| apply(&ui, &appearance, overrides));
|
|
});
|
|
}
|
|
|
|
pub fn apply(ui: &impl Themed, appearance: &Appearance, overrides: Overrides) {
|
|
let seed = overrides.seed.unwrap_or_else(|| appearance.seed());
|
|
let scheme = overrides.scheme.unwrap_or(appearance.scheme);
|
|
let palettes = Palettes::from_seed(seed);
|
|
let palette = match scheme {
|
|
ColorScheme::Light => palettes.light,
|
|
ColorScheme::Dark => palettes.dark,
|
|
};
|
|
let theme = ui.theme();
|
|
theme.set_dark(scheme == ColorScheme::Dark);
|
|
set_colors(&theme, &palette);
|
|
let reduced = overrides.reduced_motion || appearance.animations == Some(false);
|
|
ui.motion_prefs().set_reduced(reduced);
|
|
}
|
|
|
|
macro_rules! colors {
|
|
($($role:ident: $setter:ident),* $(,)?) => {
|
|
fn set_colors(theme: &Theme, palette: &Palette) {
|
|
$(theme.$setter(Color::from_argb_encoded(palette.$role));)*
|
|
}
|
|
};
|
|
}
|
|
|
|
colors!(
|
|
primary: set_primary,
|
|
on_primary: set_on_primary,
|
|
primary_container: set_primary_container,
|
|
on_primary_container: set_on_primary_container,
|
|
inverse_primary: set_inverse_primary,
|
|
secondary: set_secondary,
|
|
on_secondary: set_on_secondary,
|
|
secondary_container: set_secondary_container,
|
|
on_secondary_container: set_on_secondary_container,
|
|
tertiary: set_tertiary,
|
|
on_tertiary: set_on_tertiary,
|
|
tertiary_container: set_tertiary_container,
|
|
on_tertiary_container: set_on_tertiary_container,
|
|
error: set_error,
|
|
on_error: set_on_error,
|
|
error_container: set_error_container,
|
|
on_error_container: set_on_error_container,
|
|
surface: set_surface,
|
|
surface_dim: set_surface_dim,
|
|
surface_bright: set_surface_bright,
|
|
surface_container_lowest: set_surface_container_lowest,
|
|
surface_container_low: set_surface_container_low,
|
|
surface_container: set_surface_container,
|
|
surface_container_high: set_surface_container_high,
|
|
surface_container_highest: set_surface_container_highest,
|
|
on_surface: set_on_surface,
|
|
surface_variant: set_surface_variant,
|
|
on_surface_variant: set_on_surface_variant,
|
|
inverse_surface: set_inverse_surface,
|
|
inverse_on_surface: set_inverse_on_surface,
|
|
outline: set_outline,
|
|
outline_variant: set_outline_variant,
|
|
shadow: set_shadow,
|
|
scrim: set_scrim,
|
|
);
|