Follow desktop colours live: watch scheme.json and kdeglobals, take Caelestia's mode

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
astar76 2026-12-03 10:06:15 -06:00
commit af09e4d359
3 changed files with 74 additions and 5 deletions

View file

@ -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).

View file

@ -143,7 +143,7 @@ pub struct Shared {
pub vpn_status: RefCell<VpnStatus>,
pub speller: RefCell<Option<Speller>>,
pub spell_status: RefCell<String>,
pub system_accent: Option<egui::Color32>,
pub system_accent: Cell<Option<egui::Color32>>,
pub user_agent: RefCell<String>,
pub devtools: RefCell<Option<(u16, String)>>,
pub update: RefCell<Option<crate::updates::Release>>,
@ -241,6 +241,8 @@ pub struct App {
/// A quit or window close waiting for the pages' `beforeunload`.
pub shutdown: Option<Shutdown>,
last_tick: Instant,
/// Modification times of the desktop's colour files (`theme::desktop_files`), to follow them live.
desktop_stamps: Vec<Option<std::time::SystemTime>>,
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<WindowId> = 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.

View file

@ -169,8 +169,36 @@ fn caelestia_primary(text: &str) -> Option<Color32> {
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<std::path::PathBuf> {
[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<Option<std::time::SystemTime>> {
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<bool> {
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>) -> 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)));