From af09e4d3594c3e7f343e93a2f010f77bc1feda0e Mon Sep 17 00:00:00 2001 From: astar76 Date: Thu, 3 Dec 2026 10:06:15 -0600 Subject: [PATCH] Follow desktop colours live: watch scheme.json and kdeglobals, take Caelestia's mode Co-Authored-By: Claude Opus 5.5 --- docs/PROGRESS.md | 8 ++++++- src/app.rs | 17 ++++++++++++--- src/theme.rs | 54 +++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 74 insertions(+), 5 deletions(-) diff --git a/docs/PROGRESS.md b/docs/PROGRESS.md index d513e67..7d2cbaf 100644 --- a/docs/PROGRESS.md +++ b/docs/PROGRESS.md @@ -575,6 +575,12 @@ Later: S010 list: user checks the Discord icon with their own client (the Discor - Not done: named profiles, a master-password vault, the old reading list, settings and cookies of the old Nacre; the native file dialog; the user's real data (they press Import). - Later: the binary logs a CRITICAL about the build folder's tests/smoke/pages (compiled-in path) at start; `install.sh` has no lock against running twice and wipes `target/` even while a cargo build uses it. +## Desktop colours live (2026-12-03) +- Nacre follows Caelestia's `scheme.json` and KDE's `kdeglobals` live: the tick checks their mtimes (`theme::file_stamps`) about every second; on a change it re-reads the system accent and dark mode and restyles every window (no restart). +- "System" theme mode now takes Caelestia's `mode` (dark/light) first, then GTK, then GNOME (`theme::system_dark`). +- Verified: `cargo test` 103 + webext 1; hidden smoke on a throwaway home: scheme.json rewritten from purple/dark to green/light mid-run, the new tab page and chrome went green and light (before/after shots read). +- Not done: GNOME portal `accent-color` live watch. + ## Later - A link followed inside a frame still goes through the tab's navigation policy (WebKit's GTK API has no frame info; S050). - file:// media outside the XDG user folders doesn't play (WebKit sandbox, D-043). @@ -583,7 +589,7 @@ Later: S010 list: user checks the Discord icon with their own client (the Discor - Bookmark flyout stays open after switching tabs and shows the old page's bookmark (S006 keys.txt shot). - Wikipedia search widget ignores the selftest's Enter (plain forms submit); check with a real keyboard (S006). - YouTube's result list lays out at zero size; CSS mask-image icons paint as squares (Servo layout, S006). -- System accent: watch caelestia scheme.json / the portal `accent-color` and restyle live (R1-11). +- System accent: the GNOME portal `accent-color` isn't watched live (only Caelestia's scheme.json and kdeglobals). - Reader mode, split view, translate, real gamepad rumble (D-002: not planned for now). - AppImage / tar.gz release (D-001, D-054). - Shortcut icons: a favicon cache by host (today only open tabs' favicons show). diff --git a/src/app.rs b/src/app.rs index d5b4bbd..679d179 100644 --- a/src/app.rs +++ b/src/app.rs @@ -143,7 +143,7 @@ pub struct Shared { pub vpn_status: RefCell, pub speller: RefCell>, pub spell_status: RefCell, - pub system_accent: Option, + pub system_accent: Cell>, pub user_agent: RefCell, pub devtools: RefCell>, pub update: RefCell>, @@ -241,6 +241,8 @@ pub struct App { /// A quit or window close waiting for the pages' `beforeunload`. pub shutdown: Option, last_tick: Instant, + /// Modification times of the desktop's colour files (`theme::desktop_files`), to follow them live. + desktop_stamps: Vec>, last_save: Instant, last_slow: Instant, /// Sealed cookies (D-037), `None` when they are kept for the run only. @@ -313,7 +315,7 @@ impl App { vpn_status: RefCell::new(VpnStatus::Off), speller: RefCell::new(None), spell_status: RefCell::new(String::new()), - system_accent: theme::system_accent(), + system_accent: Cell::new(theme::system_accent()), user_agent: RefCell::new(String::new()), devtools: RefCell::new(None), update: RefCell::new(None), @@ -368,6 +370,7 @@ impl App { exiting: false, shutdown: None, last_tick: Instant::now(), + desktop_stamps: theme::file_stamps(&theme::desktop_files()), last_save: Instant::now(), last_slow: Instant::now() - Duration::from_secs(3600), cookies: None, @@ -1277,6 +1280,14 @@ impl App { if self.shared.toasts.borrow().len() != before { self.redraw_all(); } + // A new wallpaper, scheme or mode on the desktop (Caelestia's scheme.json, kdeglobals). + let stamps = theme::file_stamps(&theme::desktop_files()); + if stamps != self.desktop_stamps { + self.desktop_stamps = stamps; + self.shared.system_accent.set(theme::system_accent()); + *self.shared.system_dark.borrow_mut() = theme::system_dark(gtk_dark()); + self.restyle_all(); + } // Loading spinners and media position need regular redraws. let busy: Vec = self.windows.iter().filter(|(_, w)| w.tabs.iter().any(|t| t.loading() || t.audible())).map(|(id, _)| *id).collect(); for w in busy { @@ -1618,7 +1629,7 @@ impl App { let settings = self.shared.settings.borrow(); let dark = theme::is_dark(&settings, *self.shared.system_dark.borrow()); let site = win.tab().and_then(Tab::site_colour); - let palette = theme::Palette::new(theme::seed(&settings, site, self.shared.system_accent), dark, &settings.theme.variant, settings.theme.contrast); + let palette = theme::Palette::new(theme::seed(&settings, site, self.shared.system_accent.get()), dark, &settings.theme.variant, settings.theme.contrast); let ctx = win.ctx(); theme::apply(&ctx, &palette, &settings); // Always, so going back to 100 % takes effect too. diff --git a/src/theme.rs b/src/theme.rs index 7625de4..8f388a0 100644 --- a/src/theme.rs +++ b/src/theme.rs @@ -169,8 +169,36 @@ fn caelestia_primary(text: &str) -> Option { parse_hex(hex) } -/// Whether the system prefers dark ("System" mode): GTK's answer, else the GNOME setting. +/// The files the desktop's colours come from: Caelestia's scheme.json and KDE's kdeglobals. +pub fn desktop_files() -> Vec { + [dirs::state_dir().map(|d| d.join("caelestia/scheme.json")), dirs::config_dir().map(|d| d.join("kdeglobals"))].into_iter().flatten().collect() +} + +/// Modification times of `files` (None: missing); a different answer means the desktop's colours may have changed. +pub fn file_stamps(files: &[std::path::PathBuf]) -> Vec> { + files.iter().map(|f| std::fs::metadata(f).and_then(|m| m.modified()).ok()).collect() +} + +/// The light/dark mode of a Caelestia `scheme.json` (`{"mode": "dark", ...}`). +fn caelestia_dark(text: &str) -> Option { + let json: serde_json::Value = serde_json::from_str(text).ok()?; + match json.get("mode")?.as_str()? { + "dark" => Some(true), + "light" => Some(false), + _ => None, + } +} + +/// Whether the system prefers dark ("System" mode): Caelestia's scheme mode, else GTK's answer, else the GNOME +/// setting. pub fn system_dark(gtk_dark: Option) -> bool { + #[cfg(unix)] + if let Some(path) = dirs::state_dir().map(|d| d.join("caelestia/scheme.json")) + && let Ok(text) = std::fs::read_to_string(path) + && let Some(dark) = caelestia_dark(&text) + { + return dark; + } match gtk_dark { Some(dark) => dark, None => { @@ -714,6 +742,30 @@ mod tests { assert_eq!(caelestia_primary("not json"), None); } + #[test] + fn caelestia_scheme_mode() { + assert_eq!(caelestia_dark(r#"{"mode": "light", "colours": {}}"#), Some(false)); + assert_eq!(caelestia_dark(r#"{"mode": "dark"}"#), Some(true)); + assert_eq!(caelestia_dark(r#"{"colours": {}}"#), None); + } + + #[test] + fn desktop_file_changes_are_seen() { + let dir = std::env::temp_dir().join(format!("nacre-theme-test-{}", crate::util::random_hex(6))); + std::fs::create_dir_all(&dir).unwrap(); + let files = vec![dir.join("scheme.json"), dir.join("kdeglobals")]; + let missing = file_stamps(&files); + assert_eq!(missing, vec![None, None]); + std::fs::write(&files[0], "{}").unwrap(); + let written = file_stamps(&files); + assert_ne!(written, missing); + assert_eq!(file_stamps(&files), written); + let later = std::time::SystemTime::now() + std::time::Duration::from_secs(5); + std::fs::File::options().write(true).open(&files[0]).unwrap().set_modified(later).unwrap(); + assert_ne!(file_stamps(&files), written); + std::fs::remove_dir_all(&dir).unwrap(); + } + #[test] fn colours() { assert_eq!(parse_hex("#6750A4"), Some(Color32::from_rgb(0x67, 0x50, 0xA4)));