SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Home: a theme's File view follows its own colours where it sets none (#4868) (#4924)
dudedagger420c0c4db612c2
diff --git a/crates/ui-egui/src/theme_files.rs b/crates/ui-egui/src/theme_files.rsindex 6fedcef9b6..c766e152e4 100644--- a/crates/ui-egui/src/theme_files.rs+++ b/crates/ui-egui/src/theme_files.rs@@ -478,9 +478,76 @@ pub fn resolve(theme: &ThemeFile) -> Tokens {// Malformed hand-edited saved settings cannot introduce non-finite drawing geometry.t.borders = parse_borders(&theme.borders).unwrap_or_default();t.icon_palette = parse_icon_palette(&theme.icon_palette).unwrap_or_default();+ // A file that sets no colours is its base palette, exactly.+ if !theme.colors.is_empty() {+ file_view_from_theme(&mut t, theme.base.is_dark(), |k| theme.colors.contains_key(k));+ }t}+/// The File view's colours a theme leaves unset follow the theme's own panel, border, accent and+/// text, not its base's fixed blue rail and grey cards (#4868). Each ink is whichever candidate+/// reads best on the fills it sits on.+fn file_view_from_theme(t: &mut Tokens, dark: bool, set: impl Fn(&str) -> bool) {+ use crate::theme::contrast_ratio;+ let fallback = t.text;+ let best = |inks: &[Color32], fills: &[Color32]| {+ let worst = |ink: Color32| fills.iter().map(|f| contrast_ratio(ink, *f)).fold(f64::INFINITY, f64::min);+ inks.iter().copied().max_by(|a, b| worst(*a).total_cmp(&worst(*b))).unwrap_or(fallback)+ };+ if !set("file_background") {+ t.file_background = t.panel;+ }+ if !set("file_surface") {+ t.file_surface = t.dialog_bg;+ }+ if !set("file_border") {+ t.file_border = t.border;+ }+ let inks = [t.on_accent, t.text, t.panel, t.app_bar, t.form_ink];+ let (rail_set, hover_set) = (set("file_rail"), set("file_rail_hover"));+ if !rail_set {+ // The accent deepened as the built-in palettes deepen theirs (Dark's rail is about 0.55 of+ // its accent, Light's about 0.85), toward the theme's own darkest colour.+ let (toward, k) = if dark { (t.app_bar, 0.45) } else { (t.text, 0.15) };+ t.file_rail = t.accent.lerp_to_gamma(toward, k);+ }+ if !hover_set {+ t.file_rail_hover = t.file_rail.lerp_to_gamma(t.accent, 0.25);+ }+ if !set("file_rail_ink") {+ t.file_rail_ink = best(&inks, &[t.file_rail, t.file_rail_hover]);+ }+ // A rail still too mid-toned for any ink goes further from the ink until the ink reads.+ let away = best(&inks, &[t.file_rail_ink]);+ for _ in 0..10 {+ let reads = |fill: Color32| contrast_ratio(t.file_rail_ink, fill) >= 4.5;+ if (rail_set || reads(t.file_rail)) && (hover_set || reads(t.file_rail_hover)) {+ break;+ }+ if !rail_set {+ t.file_rail = t.file_rail.lerp_to_gamma(away, 0.1);+ }+ if !hover_set {+ t.file_rail_hover = t.file_rail_hover.lerp_to_gamma(away, 0.1);+ }+ }+ if !set("file_primary") {+ t.file_primary = t.accent_soft;+ }+ if !set("file_primary_border") {+ t.file_primary_border = t.accent;+ }+ if !set("file_primary_ink") {+ t.file_primary_ink = best(&[t.accent, t.text, t.on_accent], &[t.file_primary, t.file_background]);+ }+ if !set("file_dim") {+ // Dim where dim text still reads on the page and the cards, else the plain text colour.+ let on = [t.file_surface, t.file_background];+ t.file_dim = if on.iter().all(|f| contrast_ratio(t.text_dim, *f) >= 4.5) { t.text_dim } else { best(&[t.text, t.form_ink], &on) };+ }+}+/// A complete theme file of a palette: every key, so it is a starting point for a new theme.pub fn export(name: &str, base: Base, t: &Tokens) -> Value {let mut t = *t;@@ -737,6 +804,42 @@ mod tests {}}+ /// #4868: a theme that sets none of the File view's colours (all the built-in ones) gets them+ /// from its own palette, readable: the rail and primary action from its accent, the page from+ /// its panel, not Dark's or Light's blue and grey. One it does set stays as set.+ #[test]+ fn the_file_view_follows_a_theme_that_does_not_set_its_colours() {+ for theme in builtin() {+ let t = resolve(theme);+ if !theme.colors.contains_key("file_rail") {+ assert_eq!((t.file_background, t.file_primary), (t.panel, t.accent_soft), "{}", theme.id);+ let base = if theme.base.is_dark() { Tokens::dark() } else { Tokens::light() };+ assert!(t.accent == base.accent || t.file_rail != base.file_rail, "{}: the rail is the base's", theme.id);+ }+ // 4.5:1, or the best any of the theme's own inks reaches on that fill when that is less+ // (Solarized Light's text on its soft accent is 4.29:1): a derived colour can't do+ // better than the theme itself.+ let own = [t.text, t.form_ink, t.accent, t.on_accent];+ let floor = |fill: Color32| own.iter().map(|i| crate::theme::contrast_ratio(*i, fill)).fold(0.0, f64::max).min(4.5) - 1e-6;+ for (ink, fill) in [+ (t.file_rail_ink, t.file_rail),+ (t.file_rail_ink, t.file_rail_hover),+ (t.file_primary_ink, t.file_primary),+ (t.file_primary_ink, t.file_background),+ (t.file_dim, t.file_surface),+ (t.file_dim, t.file_background),+ ] {+ let ratio = crate::theme::contrast_ratio(ink, fill);+ assert!(ratio >= floor(fill), "{}: {ink:?} on {fill:?} is {ratio:.2}:1 (floor {:.2})", theme.id, floor(fill));+ }+ }+ let green = builtin().iter().find(|t| t.id == "green-phosphor").unwrap();+ assert_ne!(resolve(green).file_rail, Tokens::dark().file_rail, "Green Phosphor's rail isn't Dark's blue");+ let own = parse(r##"{"format":"solidcraft-theme/1","name":"Own rail","base":"dark","colors":{"accent":"#ff0000","file_rail":"#102030"}}"##)+ .unwrap();+ assert_eq!(resolve(&own).file_rail, Color32::from_rgb(0x10, 0x20, 0x30));+ }+#[test]fn a_short_file_sets_only_its_colours() {let t = parse(r##"{"format":"solidcraft-theme/1","name":"My Theme!","base":"light","colors":{"panel":"#102030","triad":["#ff0000","#00ff00","#0000ff80"]}}"##).unwrap();