Files
makeCAD/crates/ui-egui/src/theme_options.rs
T
Nathan Dane d7cba9e96d Preferences: theme options list and the opacity option (#8876) (#9807)
* claim: #8876

* claim: #8876

* Theme options: persist per-theme values and expose preferences contracts

* Restore selected theme mode after stopping a followed theme

* claim: #8876

* Preferences: render theme options and apply readable panel opacity

* Use floating-point scale expectation in theme preferences scenario

* Keep panel opacity colour construction inside theme tokens

* Complete theme options translations in Arabic Hebrew and Vietnamese

* Let standalone theme sliders fill their option cells
2026-10-31 04:01:29 +00:00

88 lines
3.1 KiB
Rust

//! Standard behaviours are registered here; file declarations alone do not offer a control.
use crate::{
theme::Tokens,
theme_files::{OptionDef, OptionValue, ThemeFile},
};
use std::collections::BTreeMap;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Availability {
Available,
/// Message-catalog id, translated at the presentation boundary.
#[allow(dead_code)] // Hardware-dependent registrations arrive with blur and tab providers.
Unavailable(&'static str),
}
pub(crate) struct Provider {
pub id: &'static str,
pub label: &'static str,
pub help: &'static str,
pub availability: fn() -> Availability,
pub apply: fn(&mut Tokens, &OptionValue),
}
// Future blur/tab_colours/tab_fade_ms implementations register their behaviour here.
pub(crate) const PROVIDERS: &[Provider] = &[Provider {
id: "opacity",
label: "prefs.theme_option_opacity",
help: "prefs.theme_option_opacity_help",
availability: || Availability::Available,
apply: apply_opacity,
}];
pub(crate) fn standard(id: &str) -> bool {
matches!(id, "opacity" | "blur" | "tab_colours" | "tab_fade_ms")
}
pub(crate) fn provider<'a>(id: &str, providers: &'a [Provider]) -> Option<&'a Provider> {
providers.iter().find(|p| p.id == id)
}
/// A child of an absent provider is absent too, even if it has a custom id.
pub(crate) fn offered<'a>(file: &'a ThemeFile, providers: &[Provider]) -> Vec<&'a OptionDef> {
let ordered = crate::theme_files::ordered_options(file);
ordered
.iter()
.copied()
.filter(|option| {
let mut current = Some(*option);
for _ in 0..=crate::theme_files::MAX_OPTIONS {
let Some(def) = current else { return true };
if standard(&def.id) && provider(&def.id, providers).is_none() {
return false;
}
current = def.parent.as_ref().and_then(|id| ordered.iter().find(|o| &o.id == id).copied());
}
false
})
.collect()
}
pub(crate) fn apply(file: &ThemeFile, values: &BTreeMap<String, OptionValue>, tokens: &mut Tokens) {
let offered = offered(file, PROVIDERS);
for def in &offered {
let mut parent = def.parent.as_deref();
let mut active = true;
for _ in 0..crate::theme_files::MAX_OPTIONS {
let Some(id) = parent else { break };
active &= values.get(id) == Some(&OptionValue::Bool(true));
active &= provider(id, PROVIDERS).is_none_or(|p| (p.availability)() == Availability::Available);
parent = offered.iter().find(|o| o.id == id).and_then(|o| o.parent.as_deref());
}
if let Some(provider) = provider(&def.id, PROVIDERS)
&& active
&& (provider.availability)() == Availability::Available
&& let Some(value) = values.get(&def.id)
{
(provider.apply)(tokens, value);
}
}
}
/// Adapt the option value; all colour math belongs to the theme boundary.
fn apply_opacity(t: &mut Tokens, value: &OptionValue) {
if let OptionValue::Num(percent) = value {
t.apply_panel_opacity(*percent);
}
}