Files
chandra/compositor/host/system_theme.mbt
T
Claude 005e11e7bf
CI / test (push) Failing after 17s
Chrome follows the Dark mode switch: Chandra serves the portal's color-scheme
Chrome (and Firefox, Electron, GTK 4, Qt 6) read dark/light from xdg-desktop-portal's Settings
interface. With XDG_CURRENT_DESKTOP=chandra and no chandra-portals.conf, xdg-desktop-portal found
no Settings backend (gtk/gnome are UseIn=gnome, kde is UseIn=KDE) and exported no Settings
interface at all, so the gsettings color-scheme a790901 sets never reached Chrome.

- compositor/portal: the compositor is an org.freedesktop.impl.portal.Settings backend
  (org.freedesktop.impl.portal.desktop.chandra on the session bus): Read/ReadAll of
  org.freedesktop.appearance color-scheme (1 dark, 2 light), SettingChanged when the switch flips,
  version property, introspection. Login session only; the main loop wakes on its socket.
- compositor/network/dbus.mbt: serve calls (on_call, reply, reply_error), emit and receive
  signals, sender field, session bus address.
- session/chandra.portal + chandra-portals.conf, installed to /usr/share/xdg-desktop-portal by
  install.py: Settings from chandra first, then gtk/kde/gnome; other portals from gtk/kde/gnome.
- chandra-session restarts a running xdg-desktop-portal so it reads the config for this desktop.
- scripts/test-portal-dbus.sh: end to end behind the real xdg-desktop-portal on a private bus
  (ReadOne gives 1, flipping gives SettingChanged 2). docs/dark-mode.md: how to check it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hhkztRQM7vcSuHX4bB
2026-11-25 20:30:30 +00:00

125 lines
5.0 KiB
MoonBit

///| Dark mode for apps: the settings module's Dark mode switch also sets the desktop-wide
/// preferences other programs follow, so browsers, GTK and Qt apps go dark or light with the
/// desktop.
///
/// - GTK and browsers: `org.gnome.desktop.interface color-scheme` (gsettings) is what
/// xdg-desktop-portal's org.freedesktop.appearance color-scheme reports; Chrome/Chromium,
/// Electron, Firefox, GTK 4 / libadwaita and Qt 6.5+ apps follow it. `gtk-theme` is switched
/// between Adwaita and Adwaita-dark for GTK 3 apps (and Chrome's GTK mode), but only when it is
/// one of those two, so a hand-picked theme is left alone. `gtk-application-prefer-dark-theme`
/// in $XDG_CONFIG_HOME/gtk-{3,4}.0/settings.ini covers systems without gsettings.
/// - Qt / KDE: apps using KDE's color schemes read kdeglobals. `plasma-apply-colorscheme
/// BreezeDark|BreezeLight` rewrites it when Plasma's tools are installed, else kwriteconfig6/5
/// sets [General] ColorScheme. qt6ct/qt5ct users whose color file is a light/dark variant get
/// the other one.
///
/// Only the login session (--drm) does this: a nested Chandra window must not restyle the desktop
/// it runs in. Missing tools are skipped silently.
///|
let system_theme_script : String =
#|if command -v gsettings >/dev/null 2>&1; then
#| gsettings set org.gnome.desktop.interface color-scheme @SCHEME@ 2>/dev/null
#| case "$(gsettings get org.gnome.desktop.interface gtk-theme 2>/dev/null)" in
#| "'Adwaita'"|"'Adwaita-dark'") gsettings set org.gnome.desktop.interface gtk-theme @GTK@ ;;
#| esac
#|fi
#|cfg="${XDG_CONFIG_HOME:-$HOME/.config}"
#|k=gtk-application-prefer-dark-theme
#|for d in gtk-3.0 gtk-4.0; do
#| f="$cfg/$d/settings.ini"
#| mkdir -p "$cfg/$d" || continue
#| if [ -f "$f" ] && grep -q "^$k" "$f"; then
#| sed -i "s/^$k.*/$k=@V@/" "$f"
#| elif [ -f "$f" ] && grep -q '^\[Settings\]' "$f"; then
#| sed -i "/^\[Settings\]/a $k=@V@" "$f"
#| else
#| printf '[Settings]\n%s=@V@\n' "$k" >> "$f"
#| fi
#|done
#|if command -v plasma-apply-colorscheme >/dev/null 2>&1; then
#| plasma-apply-colorscheme @KDE@ >/dev/null 2>&1
#|else
#| for w in kwriteconfig6 kwriteconfig5; do
#| if command -v $w >/dev/null 2>&1; then
#| $w --file kdeglobals --group General --key ColorScheme @KDE@
#| break
#| fi
#| done
#|fi
#|for q in qt6ct qt5ct; do
#| c="$cfg/$q/$q.conf"
#| [ -f "$c" ] || continue
#| p=$(sed -n 's/^color_scheme_path=//p' "$c")
#| n=$(printf '%s' "$p" | sed 's/@FROM@/@TO@/I')
#| [ "$n" != "$p" ] && [ -f "$n" ] && sed -i "s|^color_scheme_path=.*|color_scheme_path=$n|" "$c"
#|done
#|true
///| The shell script that sets the system-wide preference to dark (`dark`) or light.
pub fn system_theme_command(dark : Bool) -> String {
let subst = [
("@SCHEME@", if dark { "prefer-dark" } else { "default" }),
("@GTK@", if dark { "Adwaita-dark" } else { "Adwaita" }),
("@V@", if dark { "1" } else { "0" }),
("@KDE@", if dark { "BreezeDark" } else { "BreezeLight" }),
("@FROM@", if dark { "light" } else { "dark" }),
("@TO@", if dark { "dark" } else { "light" }),
]
let mut s = system_theme_script
for p in subst {
s = s.replace_all(old=p.0, new=p.1)
}
s
}
///| Tell the system the desktop is dark or light (see above), and apps through our own portal
/// backend (compositor/portal), which xdg-desktop-portal relays to them live. A no-op unless
/// `sync_system_theme` is on.
fn ChandraCompositor::sync_system_theme_pref(self : ChandraCompositor, dark : Bool) -> Unit {
if self.sync_system_theme {
ignore(@sys.spawn(system_theme_command(dark), self.shell.display))
match self.settings_portal {
Some(p) => p.set_dark(dark)
None => {
let p = @portal.SettingsPortal::new(dark)
self.settings_portal = Some(p)
p.tick(@sys.monotonic_ms())
}
}
}
}
///| Serve calls to the portal backend (each main-loop pass).
fn ChandraCompositor::portal_tick(self : ChandraCompositor) -> Unit {
match self.settings_portal {
Some(p) => p.tick(@sys.monotonic_ms())
None => ()
}
}
///| The portal backend's bus socket, for the main loop to wake on (-1 when there is none).
pub fn ChandraCompositor::portal_fd(self : ChandraCompositor) -> Int {
match self.settings_portal {
Some(p) => p.fd()
None => -1
}
}
///|
test "the system theme command sets the GTK, portal and Qt/KDE preferences" {
let d = system_theme_command(true)
assert_true(d.contains("color-scheme prefer-dark"))
assert_true(d.contains("gtk-theme Adwaita-dark"))
assert_true(d.contains("$k=1"))
assert_true(d.contains("plasma-apply-colorscheme BreezeDark"))
assert_true(d.contains("--key ColorScheme BreezeDark"))
assert_false(d.contains("@"))
let l = system_theme_command(false)
assert_true(l.contains("color-scheme default"))
assert_true(l.contains("gtk-theme Adwaita ;"))
assert_true(l.contains("$k=0"))
assert_true(l.contains("plasma-apply-colorscheme BreezeLight"))
assert_true(l.contains("s/dark/light/I"))
}