Follow the desktop accent colour and colour scheme (XDG portal)
Read org.freedesktop.appearance accent-color and color-scheme from org.freedesktop.portal.Settings over GDBus in the GTK stub and follow SettingChanged. The accent recolours the selected tab, sliders, peak meters and the checked Default button through an application-priority GtkCssProvider (named colours and --accent-* variables too, for themes that use them); the colour scheme sets the theme's dark/light variant. Without a portal or accent the theme's own stays. Colour conversion, WCAG contrast (readable foreground on the accent, readable accent text on the window) and the CSS live in model/accent.mbt with unit tests. The smoke test runs a fake portal on a private session bus, switches the accent live from green to orange and renders the tabs again (docs/screenshots/accent-*.png). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
bc675459d9
commit
74d85ecdfc
16 files changed
+947
-12
No files matched your search
@@ -0,0 +1,120 @@
|
||||
///|
|
||||
fn hex(s : String) -> @model.Rgb {
|
||||
@model.Rgb::parse_hex(s).unwrap()
|
||||
}
|
||||
|
||||
///|
|
||||
test "from_portal accepts 0..1 and rejects unset values" {
|
||||
assert_eq(
|
||||
@model.Rgb::from_portal(1.0, 0.5, 0.0),
|
||||
Some({ r: 1.0, g: 0.5, b: 0.0, }),
|
||||
)
|
||||
assert_eq(@model.Rgb::from_portal(0.0, 0.0, 0.0), Some(@model.black))
|
||||
// GNOME reports (-1, -1, -1) before an accent is chosen.
|
||||
assert_eq(@model.Rgb::from_portal(-1.0, -1.0, -1.0), None)
|
||||
assert_eq(@model.Rgb::from_portal(0.2, 1.5, 0.2), None)
|
||||
assert_eq(@model.Rgb::from_portal(0.0 / 0.0, 0.2, 0.2), None)
|
||||
}
|
||||
|
||||
///|
|
||||
test "hex round trip" {
|
||||
assert_eq(hex("#3584e4").to_hex(), "#3584e4")
|
||||
assert_eq(hex("#ED5B00").to_hex(), "#ed5b00")
|
||||
assert_eq(hex("#fff"), @model.white)
|
||||
assert_eq(hex("#000000"), @model.black)
|
||||
assert_eq(@model.Rgb::parse_hex("3584e4"), None)
|
||||
assert_eq(@model.Rgb::parse_hex("#3584e"), None)
|
||||
assert_eq(@model.Rgb::parse_hex("#zzzzzz"), None)
|
||||
// Portal doubles are rounded to the nearest byte.
|
||||
assert_eq(
|
||||
@model.Rgb::from_portal(1.0, 0.471, 0.0).unwrap().to_hex(),
|
||||
"#ff7800",
|
||||
)
|
||||
}
|
||||
|
||||
///|
|
||||
test "contrast ratio" {
|
||||
assert_eq(@model.contrast_ratio(@model.white, @model.black), 21.0)
|
||||
assert_eq(@model.contrast_ratio(@model.black, @model.black), 1.0)
|
||||
// GTK's blue against white (well known: ~3.76).
|
||||
let r = @model.contrast_ratio(hex("#3584e4"), @model.white)
|
||||
assert_true(r > 3.7 && r < 3.8)
|
||||
// Symmetric.
|
||||
assert_eq(@model.contrast_ratio(@model.white, hex("#3584e4")), r)
|
||||
}
|
||||
|
||||
///|
|
||||
test "readable foreground on accent backgrounds" {
|
||||
// Dark accents keep white text.
|
||||
assert_eq(@model.readable_fg(hex("#1c71d8")), @model.white)
|
||||
assert_eq(@model.readable_fg(hex("#9141ac")), @model.white)
|
||||
assert_eq(@model.readable_fg(hex("#c01c28")), @model.white)
|
||||
// GTK's blue: below 4.5 with white (black would contrast more), but
|
||||
// above 3, so it keeps white like GTK does.
|
||||
assert_eq(@model.readable_fg(hex("#3584e4")), @model.white)
|
||||
// Bright accents get black text.
|
||||
assert_eq(@model.readable_fg(hex("#ff7800")), @model.black)
|
||||
assert_eq(@model.readable_fg(hex("#f6d32d")), @model.black)
|
||||
assert_eq(@model.readable_fg(hex("#33d17a")), @model.black)
|
||||
// Whatever is chosen beats the alternative or meets AA.
|
||||
for c in ["#3584e4", "#ff7800", "#2190a4", "#c88800", "#6f8396", "#ffffff"] {
|
||||
let bg = hex(c)
|
||||
let fg = @model.readable_fg(bg)
|
||||
let other = if fg == @model.white { @model.black } else { @model.white }
|
||||
assert_true(
|
||||
@model.contrast_ratio(bg, fg) >= @model.MIN_UI_CONTRAST ||
|
||||
@model.contrast_ratio(bg, fg) >= @model.contrast_ratio(bg, other),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
test "accent text colour is readable on the window background" {
|
||||
for
|
||||
c in [
|
||||
"#3584e4", "#ff7800", "#f6d32d", "#33d17a", "#1c71d8", "#000000", "#ffffff",
|
||||
] {
|
||||
for dark in [false, true] {
|
||||
let t = @model.accent_text_color(hex(c), dark)
|
||||
assert_true(
|
||||
@model.contrast_ratio(t, @model.window_bg(dark)) >=
|
||||
@model.MIN_TEXT_CONTRAST,
|
||||
)
|
||||
}
|
||||
}
|
||||
// Already readable: unchanged.
|
||||
assert_eq(@model.accent_text_color(hex("#1a5fb4"), false), hex("#1a5fb4"))
|
||||
// Yellow on a light window is darkened, on a dark one kept.
|
||||
assert_true(
|
||||
@model.accent_text_color(hex("#f6d32d"), false).luminance() <
|
||||
hex("#f6d32d").luminance(),
|
||||
)
|
||||
assert_eq(@model.accent_text_color(hex("#f6d32d"), true), hex("#f6d32d"))
|
||||
}
|
||||
|
||||
///|
|
||||
test "accent css overrides named colours and widget fills" {
|
||||
let css = @model.accent_css(hex("#ff7800"), false)
|
||||
assert_true(css.contains("@define-color accent_bg_color #ff7800;"))
|
||||
assert_true(css.contains("@define-color accent_fg_color #000000;"))
|
||||
assert_true(css.contains("@define-color theme_selected_bg_color #ff7800;"))
|
||||
assert_true(css.contains("--accent-bg-color: #ff7800;"))
|
||||
assert_true(
|
||||
css.contains("scale > trough > highlight { background-color: #ff7800;"),
|
||||
)
|
||||
assert_true(css.contains("levelbar > trough > block.high"))
|
||||
assert_true(css.contains("tab:checked { box-shadow: inset 0 -4px #ff7800; }"))
|
||||
assert_true(css.contains("button.mm-default:checked"))
|
||||
// The text colour is the darkened accent, not the raw one.
|
||||
let text = @model.accent_text_color(hex("#ff7800"), false).to_hex()
|
||||
assert_true(text != "#ff7800")
|
||||
assert_true(css.contains("@define-color accent_color \{text};"))
|
||||
}
|
||||
|
||||
///|
|
||||
test "color scheme from portal" {
|
||||
assert_eq(@model.ColorScheme::from_portal(0), @model.NoPreference)
|
||||
assert_eq(@model.ColorScheme::from_portal(1), @model.Dark)
|
||||
assert_eq(@model.ColorScheme::from_portal(2), @model.Light)
|
||||
assert_eq(@model.ColorScheme::from_portal(7), @model.NoPreference)
|
||||
}
|
||||
Reference in new issue
Block a user