Follow the desktop accent colour and colour scheme (XDG portal) (#3)
CI / check (push) Successful in 9s
CI / native (push) Successful in 1m14s

This commit was merged in pull request #3.
This commit is contained in:
nandi committed 2026-11-23 01:56:20 +00:00
1 parent 02b08cb471
commit 1746884744
16 files changed
+947 -12

No files matched your search

+65 -5
View File
@@ -134,6 +134,8 @@ fn new_row_view(app : App, tab : @model.Tab, row : @model.Row) -> RowView {
)
tags.push(tag)
b.set_tooltip("Set as default (fallback) device")
// Shown in the accent colour when checked (see @model.accent_css).
b.add_css_class("mm-default")
header.append(b)
Some(b)
}
@@ -470,10 +472,52 @@ fn build_ui() -> App {
app
}
///|
/// Follow the desktop's accent colour and dark/light preference (XDG Desktop
/// Portal). Without a portal, or with no accent set, the theme's own accent
/// stays.
///|
/// The accent applied last ("#rrggbb", or "" for the theme's own).
let applied_accent : Ref[String] = { val: "", }
///|
fn follow_appearance(log~ : Bool = false) -> Unit {
let _ = @gtk.watch_appearance(fn(a) {
let scheme = @model.ColorScheme::from_portal(a.color_scheme)
@gtk.set_color_scheme(a.color_scheme)
let dark = match scheme {
Dark => true
Light => false
NoPreference => @gtk.prefers_dark()
}
let (r, g, b) = a.accent
let accent = @model.Rgb::from_portal(r, g, b)
match accent {
Some(c) => @gtk.set_accent_css(@model.accent_css(c, dark))
None => @gtk.set_accent_css("")
}
applied_accent.val = match accent {
Some(c) => c.to_hex()
None => ""
}
if log {
// The smoke test checks these lines to see live updates arrive.
let name = match accent {
Some(c) => c.to_hex()
None => "theme default"
}
println("moonmix: accent \{name}, \{if dark { "dark" } else { "light" }}")
}
})
}
///|
/// Test hook: MOONMIX_SCREENSHOT=/path/prefix renders every tab to
/// <prefix>-<n>.png once connected, then exits. Used for headless CI/smoke
/// runs (GDK_BACKEND=broadway or x11 under Xvfb).
/// runs (GDK_BACKEND=broadway or x11 under Xvfb). With
/// MOONMIX_SCREENSHOT_ACCENT=#rrggbb it first waits (up to 20 s) until the
/// portal has delivered that accent colour.
fn screenshot_mode(app : App, prefix : String) -> Unit {
let tab_count = app.tabs.length()
// Offscreen rendering occasionally yields an empty frame right after a tab
@@ -504,7 +548,19 @@ fn screenshot_mode(app : App, prefix : String) -> Unit {
})
}
@gtk.timeout(1500, fn() { shoot(0, 0) })
let want = env_var("MOONMIX_SCREENSHOT_ACCENT").unwrap_or("")
fn start(waited : Int32) -> Unit {
if want == "" || applied_accent.val == want || waited >= 200 {
if waited >= 200 {
println("moonmix: accent \{want} never arrived")
}
shoot(0, 0)
} else {
@gtk.timeout(100, fn() { start(waited + 1) })
}
}
@gtk.timeout(1500, fn() { start(0) })
}
///|
@@ -518,6 +574,11 @@ fn main {
println("moonmix: cannot open a display (is WAYLAND_DISPLAY/DISPLAY set?)")
c_exit(1)
}
let screenshot = match env_var("MOONMIX_SCREENSHOT") {
Some(prefix) if prefix != "" => Some(prefix)
_ => None
}
follow_appearance(log=screenshot is Some(_))
let app = build_ui()
@wp.on_event(fn(ev) { app.on_wp_event(ev) })
if !@wp.start() {
@@ -525,9 +586,8 @@ fn main {
}
app.window.present()
app.start_meter_timer()
match env_var("MOONMIX_SCREENSHOT") {
Some(prefix) if prefix != "" => screenshot_mode(app, prefix)
_ => ()
if screenshot is Some(prefix) {
screenshot_mode(app, prefix)
}
@gtk.run()
}