Files
chandra/scripts/test-kde-colors.sh
T
Claude 42aa770452
CI / test (push) Failing after 17s
Dark mode for KDE/Qt apps: write kdeglobals colors and set QT_QPA_PLATFORMTHEME
KWrite stayed light with the Dark mode switch on. KDE apps read their colors
from the [Colors:*] groups of kdeglobals, not from the scheme's name, and the
switch only set [General] ColorScheme with kwriteconfig. Qt 6 apps also had no
platform theme under XDG_CURRENT_DESKTOP=chandra, so Qt reported an unknown
(light) color scheme whatever the portal said, and KDE Frameworks 6 apps
picked Breeze Light.

- session/chandra-kde-colors dark|light copies the scheme's color groups into
  kdeglobals (counterpart of the current scheme, else Breeze, else Chandra's
  own ChandraDark/ChandraLight) and emits KGlobalSettings.notifyChange so
  running apps repaint. The compositor calls it from the switch.
- chandra-session sets QT_QPA_PLATFORMTHEME to kde (plasma-integration) or
  xdgdesktopportal when unset, and passes it to the activation environment.
- install.py installs the script and the two color schemes.
- scripts/test-kde-colors.sh tests the rewriting, and that a running KWrite
  turns dark live; docs/dark-mode.md explains it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hjmk6mgq6Gg5vgKfMp
2026-11-25 22:33:29 +00:00

75 lines
3.8 KiB
Bash
Executable File

#!/usr/bin/env bash
# Test session/chandra-kde-colors, the Dark mode switch's KDE/Qt part (docs/dark-mode.md).
#
# 1. kdeglobals handling, with no KDE packages needed: the scheme's color groups replace the old
# ones, other groups and keys stay, ColorScheme is set, Chandra's own schemes are the fallback,
# a FooLight/FooDark pair is kept.
# 2. With kwrite, Xvfb and ImageMagick installed (apt: kwrite plasma-integration xvfb imagemagick
# dbus), the real thing: KWrite starts light, the script flips it, and with the KDE platform
# theme (QT_QPA_PLATFORMTHEME=kde) the running window turns dark. Skipped when they're missing.
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
KC="$ROOT/session/chandra-kde-colors"
tmp=$(mktemp -d); trap 'rm -rf "$tmp"' EXIT
fail() { echo "FAIL: $*" >&2; exit 1; }
mkdir -p "$tmp/data/color-schemes" "$tmp/cfg"
cp "$ROOT"/session/color-schemes/*.colors "$tmp/data/color-schemes/"
run() { XDG_CONFIG_HOME="$tmp/cfg" XDG_DATA_HOME="$tmp/none" XDG_DATA_DIRS="$tmp/data" "$KC" "$@"; }
kg="$tmp/cfg/kdeglobals"
val() { sed -n "/^\[$1\]\$/,/^\[/s/^$2=//p" "$kg" | head -n 1; }
# No kdeglobals yet, no Breeze: Chandra's schemes.
[ "$(run dark)" = ChandraDark ] || fail "dark without Breeze should pick ChandraDark"
[ "$(val General ColorScheme)" = ChandraDark ] || fail "ColorScheme not set"
[ "$(val Colors:Window BackgroundNormal)" = 31,41,55 ] || fail "Colors:Window not copied"
[ "$(val WM activeBackground)" = 31,41,55 ] || fail "WM not copied"
# Existing settings survive; stale colors are replaced, not duplicated.
printf '[General]\nColorScheme=BreezeLight\nfont=Sans,10\n\n[Colors:Window]\nBackgroundNormal=1,2,3\nStale=1\n\n[Icons]\nTheme=breeze\n' >"$kg"
run dark >/dev/null
[ "$(val General font)" = Sans,10 ] || fail "[General] font lost"
[ "$(val Icons Theme)" = breeze ] || fail "[Icons] lost"
grep -q '^Stale=' "$kg" && fail "old color group kept"
[ "$(grep -c '^\[Colors:Window\]' "$kg")" = 1 ] || fail "Colors:Window duplicated"
[ "$(grep -c '^ColorScheme=' "$kg")" = 1 ] || fail "ColorScheme duplicated"
run light >/dev/null
[ "$(val General ColorScheme)" = ChandraLight ] || fail "light"
[ "$(val Colors:View BackgroundNormal)" = 255,255,255 ] || fail "light colors"
# A dark/light pair of the user's own is kept: MyLight -> MyDark.
sed 's/^ColorScheme=.*/ColorScheme=MyDark/; s/^BackgroundNormal=17,24,39/BackgroundNormal=9,9,9/' \
"$tmp/data/color-schemes/ChandraDark.colors" >"$tmp/data/color-schemes/MyDark.colors"
sed -i 's/^ColorScheme=.*/ColorScheme=MyLight/' "$kg"
[ "$(run dark)" = MyDark ] || fail "MyLight should become MyDark"
[ "$(val Colors:View BackgroundNormal)" = 9,9,9 ] || fail "MyDark colors"
# No kdeglobals [General] at all.
printf '[Icons]\nTheme=breeze\n' >"$kg"
run dark >/dev/null
[ "$(val General ColorScheme)" = ChandraDark ] || fail "[General] not added"
run bogus 2>/dev/null && fail "bad argument accepted"
echo "kdeglobals: ok"
# 2. Real KWrite.
for c in kwrite Xvfb import convert dbus-run-session; do
command -v $c >/dev/null 2>&1 || { echo "skipping the KWrite check ($c not installed)"; exit 0; }
done
export XDG_CONFIG_HOME="$tmp/kw" HOME="$tmp/kw/home" DISPLAY=:77 QT_QPA_PLATFORM=xcb \
QT_QPA_PLATFORMTHEME=kde KC tmp
mkdir -p "$HOME"
Xvfb :77 -screen 0 800x600x24 >/dev/null 2>&1 & xvfb=$!
trap 'kill $xvfb 2>/dev/null; rm -rf "$tmp"' EXIT
sleep 1
dbus-run-session -- sh -c '
"$KC" light >/dev/null
kwrite >/dev/null 2>&1 & k=$!
sleep 6; import -window root "$tmp/before.png"
"$KC" dark >/dev/null
sleep 3; import -window root "$tmp/after.png"
kill $k' 2>/dev/null
lum() { convert "$1" -crop 1x1+400+300 -colorspace Gray -format '%[fx:int(255*mean)]' info:; }
b=$(lum "$tmp/before.png"); a=$(lum "$tmp/after.png")
echo "KWrite window gray level: light $b, after the switch $a"
[ "$b" -gt 150 ] || fail "KWrite didn't start light"
[ "$a" -lt 100 ] || fail "KWrite didn't turn dark live"
echo "KWrite: ok"