S002: portal appearance reader, watcher and Material palettes
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
parent
0d82cc3975
commit
7f94978bfb
11 changed files with 360 additions and 8 deletions
15
Cargo.lock
generated
15
Cargo.lock
generated
|
|
@ -2566,6 +2566,17 @@ dependencies = [
|
|||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "material-colors"
|
||||
version = "0.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "57a5bf6a9d1cf6319a36cebc589b9a681a6b1c699e162f27c020877783579cc7"
|
||||
dependencies = [
|
||||
"ahash",
|
||||
"indexmap",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.8.3"
|
||||
|
|
@ -4577,6 +4588,10 @@ version = "0.1.0"
|
|||
[[package]]
|
||||
name = "tunal-theme"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"material-colors",
|
||||
"zbus",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tunal-thumbs"
|
||||
|
|
|
|||
|
|
@ -11,3 +11,5 @@ publish = false
|
|||
[workspace.dependencies]
|
||||
slint = "1.19"
|
||||
slint-build = "1.19"
|
||||
zbus = { version = "5.19", default-features = false, features = ["async-io", "blocking-api"] }
|
||||
material-colors = "0.5"
|
||||
|
|
|
|||
|
|
@ -10,13 +10,13 @@
|
|||
CLAUDE.md §2 Theme, §4; docs/DECISIONS.md D-012.
|
||||
|
||||
## Steps
|
||||
- [ ] Portal reader with `zbus` blocking API (no tokio; features for blocking only): `ReadOne`, falling back to the older `Read` (doubly wrapped variant); parse `accent-color` (ddd; out-of-range = no accent) and `color-scheme` (u32: 0 none → light, 1 dark, 2 light).
|
||||
- [ ] Reduced motion: read `org.gnome.desktop.interface` `enable-animations` when the portal offers it, else `None`.
|
||||
- [ ] Watcher thread: subscribe to `SettingChanged` for both namespaces and call back with a new `Appearance { accent, scheme, animations }`.
|
||||
- [ ] Palette: `ThemeBuilder::with_source(seed)` tonal spot → light and dark schemes exported as a plain struct of the M3 roles used by the UI (ARGB u32); fixed fallback seed chosen here and recorded in DECISIONS.md.
|
||||
- [ ] No portal / no session bus / no accent: return defaults, never panic, log one line.
|
||||
- [ ] Example `cargo run -p tunal-theme --example dump` prints the appearance and a few roles.
|
||||
- [ ] PROGRESS entry, INDEX line, commit `S002: …`
|
||||
- [x] Portal reader with `zbus` blocking API (no tokio; features for blocking only): `ReadOne`, falling back to the older `Read` (doubly wrapped variant); parse `accent-color` (ddd; out-of-range = no accent) and `color-scheme` (u32: 0 none → light, 1 dark, 2 light).
|
||||
- [x] Reduced motion: read `org.gnome.desktop.interface` `enable-animations` when the portal offers it, else `None`.
|
||||
- [x] Watcher thread: subscribe to `SettingChanged` for both namespaces and call back with a new `Appearance { accent, scheme, animations }`.
|
||||
- [x] Palette: `ThemeBuilder::with_source(seed)` tonal spot → light and dark schemes exported as a plain struct of the M3 roles used by the UI (ARGB u32); fixed fallback seed chosen here and recorded in DECISIONS.md.
|
||||
- [x] No portal / no session bus / no accent: return defaults, never panic, log one line.
|
||||
- [x] Example `cargo run -p tunal-theme --example dump` prints the appearance and a few roles.
|
||||
- [x] PROGRESS entry, INDEX line, commit `S002: …`
|
||||
|
||||
## End criteria
|
||||
- `cargo build --workspace`, `cargo clippy --workspace --all-targets -- -D warnings` and `cargo fmt --check` pass.
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ Tracker: https://claude.ai/artifact/4X8YCk3cQjUGyuyCqGuU7v
|
|||
|
||||
## 02 App skeleton
|
||||
|
||||
- [ ] [S002 — Portal theme and Material schemes](02%20App%20skeleton/S002-theme-portal.md) · Opus high
|
||||
- [x] [S002 — Portal theme and Material schemes](02%20App%20skeleton/S002-theme-portal.md) · Opus high
|
||||
- [ ] [S003 — Window, Theme and Motion globals, screenshot command](02%20App%20skeleton/S003-window-tokens.md) · Opus high
|
||||
- [ ] [S004 — Base animated controls](02%20App%20skeleton/S004-base-controls.md) · Opus high
|
||||
- [ ] [S005 — Menus, dialogs and toasts](02%20App%20skeleton/S005-menus-dialogs.md) · Opus high
|
||||
|
|
|
|||
|
|
@ -4,3 +4,7 @@ version.workspace = true
|
|||
edition.workspace = true
|
||||
license.workspace = true
|
||||
publish.workspace = true
|
||||
|
||||
[dependencies]
|
||||
zbus.workspace = true
|
||||
material-colors.workspace = true
|
||||
|
|
|
|||
41
crates/tunal-theme/examples/dump.rs
Normal file
41
crates/tunal-theme/examples/dump.rs
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
//! Prints the portal appearance and a few palette roles.
|
||||
//! With `--watch`, keeps running and prints each appearance change.
|
||||
|
||||
fn main() {
|
||||
let appearance = tunal_theme::read();
|
||||
match appearance.accent {
|
||||
Some(a) => println!(
|
||||
"accent: ({:.6}, {:.6}, {:.6}) = #{:06X}",
|
||||
a.red,
|
||||
a.green,
|
||||
a.blue,
|
||||
a.rgb()
|
||||
),
|
||||
None => println!(
|
||||
"accent: none (fallback seed #{:06X})",
|
||||
tunal_theme::FALLBACK_SEED
|
||||
),
|
||||
}
|
||||
println!("scheme: {:?}", appearance.scheme);
|
||||
println!("animations: {:?}", appearance.animations);
|
||||
println!("seed: #{:06X}", appearance.seed());
|
||||
|
||||
let palette = appearance.palette();
|
||||
for (role, argb) in [
|
||||
("primary", palette.primary),
|
||||
("on_primary", palette.on_primary),
|
||||
("primary_container", palette.primary_container),
|
||||
("secondary_container", palette.secondary_container),
|
||||
("surface", palette.surface),
|
||||
("surface_container", palette.surface_container),
|
||||
("on_surface", palette.on_surface),
|
||||
("outline", palette.outline),
|
||||
] {
|
||||
println!("{role:<20} #{argb:08X}");
|
||||
}
|
||||
|
||||
if std::env::args().any(|arg| arg == "--watch") {
|
||||
let watcher = tunal_theme::watch(|appearance| println!("changed: {appearance:?}"));
|
||||
let _ = watcher.join();
|
||||
}
|
||||
}
|
||||
|
|
@ -1 +1,62 @@
|
|||
//! XDG Settings portal access and Material colour scheme generation.
|
||||
|
||||
mod palette;
|
||||
mod portal;
|
||||
|
||||
pub use palette::{FALLBACK_SEED, Palette, Palettes};
|
||||
pub use portal::{read, watch};
|
||||
|
||||
/// Accent colour as the portal reports it: sRGB components in `0.0..=1.0`.
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub struct Accent {
|
||||
pub red: f64,
|
||||
pub green: f64,
|
||||
pub blue: f64,
|
||||
}
|
||||
|
||||
impl Accent {
|
||||
/// `None` when a component is out of range (the portal's "no accent").
|
||||
pub fn new(red: f64, green: f64, blue: f64) -> Option<Self> {
|
||||
let valid = |c: f64| (0.0..=1.0).contains(&c);
|
||||
(valid(red) && valid(green) && valid(blue)).then_some(Self { red, green, blue })
|
||||
}
|
||||
|
||||
/// The accent as `0xRRGGBB`.
|
||||
pub fn rgb(&self) -> u32 {
|
||||
let byte = |c: f64| (c * 255.0).round() as u32;
|
||||
byte(self.red) << 16 | byte(self.green) << 8 | byte(self.blue)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
|
||||
pub enum ColorScheme {
|
||||
#[default]
|
||||
Light,
|
||||
Dark,
|
||||
}
|
||||
|
||||
/// Desktop appearance settings read from the portal.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Default)]
|
||||
pub struct Appearance {
|
||||
/// `None`: no accent set; use [`FALLBACK_SEED`].
|
||||
pub accent: Option<Accent>,
|
||||
pub scheme: ColorScheme,
|
||||
/// `org.gnome.desktop.interface` `enable-animations`; `None` when the portal does not offer it.
|
||||
pub animations: Option<bool>,
|
||||
}
|
||||
|
||||
impl Appearance {
|
||||
/// The seed colour for the Material schemes (`0xRRGGBB`).
|
||||
pub fn seed(&self) -> u32 {
|
||||
self.accent.map_or(FALLBACK_SEED, |a| a.rgb())
|
||||
}
|
||||
|
||||
/// The palette for the current seed and colour scheme.
|
||||
pub fn palette(&self) -> Palette {
|
||||
let palettes = Palettes::from_seed(self.seed());
|
||||
match self.scheme {
|
||||
ColorScheme::Light => palettes.light,
|
||||
ColorScheme::Dark => palettes.dark,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
83
crates/tunal-theme/src/palette.rs
Normal file
83
crates/tunal-theme/src/palette.rs
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
//! Light and dark Material 3 schemes from a seed colour (`material-colors`, tonal spot).
|
||||
|
||||
use material_colors::{color::Rgb, scheme::Scheme, theme::ThemeBuilder};
|
||||
|
||||
/// Seed used when the desktop sets no accent (`0xRRGGBB`, a muted blue; D-022).
|
||||
pub const FALLBACK_SEED: u32 = 0x3F6FB5;
|
||||
|
||||
macro_rules! palette {
|
||||
($($role:ident),* $(,)?) => {
|
||||
/// The M3 colour roles the UI uses, as opaque ARGB (`0xAARRGGBB`).
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct Palette {
|
||||
$(pub $role: u32,)*
|
||||
}
|
||||
|
||||
impl From<&Scheme> for Palette {
|
||||
fn from(scheme: &Scheme) -> Self {
|
||||
Self { $($role: argb(scheme.$role),)* }
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
palette!(
|
||||
primary,
|
||||
on_primary,
|
||||
primary_container,
|
||||
on_primary_container,
|
||||
inverse_primary,
|
||||
secondary,
|
||||
on_secondary,
|
||||
secondary_container,
|
||||
on_secondary_container,
|
||||
tertiary,
|
||||
on_tertiary,
|
||||
tertiary_container,
|
||||
on_tertiary_container,
|
||||
error,
|
||||
on_error,
|
||||
error_container,
|
||||
on_error_container,
|
||||
surface,
|
||||
surface_dim,
|
||||
surface_bright,
|
||||
surface_container_lowest,
|
||||
surface_container_low,
|
||||
surface_container,
|
||||
surface_container_high,
|
||||
surface_container_highest,
|
||||
on_surface,
|
||||
surface_variant,
|
||||
on_surface_variant,
|
||||
inverse_surface,
|
||||
inverse_on_surface,
|
||||
outline,
|
||||
outline_variant,
|
||||
shadow,
|
||||
scrim,
|
||||
);
|
||||
|
||||
/// Both schemes for one seed.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct Palettes {
|
||||
pub light: Palette,
|
||||
pub dark: Palette,
|
||||
}
|
||||
|
||||
impl Palettes {
|
||||
/// Tonal-spot schemes for `seed` (`0xRRGGBB`; any alpha byte is ignored).
|
||||
pub fn from_seed(seed: u32) -> Self {
|
||||
let schemes = ThemeBuilder::with_source(Rgb::from_u32(seed))
|
||||
.build()
|
||||
.schemes;
|
||||
Self {
|
||||
light: Palette::from(&schemes.light),
|
||||
dark: Palette::from(&schemes.dark),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn argb(rgb: Rgb) -> u32 {
|
||||
0xFF00_0000 | u32::from(rgb.red) << 16 | u32::from(rgb.green) << 8 | u32::from(rgb.blue)
|
||||
}
|
||||
139
crates/tunal-theme/src/portal.rs
Normal file
139
crates/tunal-theme/src/portal.rs
Normal file
|
|
@ -0,0 +1,139 @@
|
|||
//! `org.freedesktop.portal.Settings` over the session bus (zbus blocking API).
|
||||
|
||||
use std::thread::{self, JoinHandle};
|
||||
|
||||
use zbus::blocking::{Connection, Proxy, proxy::Builder};
|
||||
use zbus::proxy::CacheProperties;
|
||||
use zbus::zvariant::{OwnedValue, Value};
|
||||
|
||||
use crate::{Accent, Appearance, ColorScheme};
|
||||
|
||||
const APPEARANCE: &str = "org.freedesktop.appearance";
|
||||
const INTERFACE: &str = "org.gnome.desktop.interface";
|
||||
|
||||
/// Reads the current appearance. Never fails: anything missing falls back to its default,
|
||||
/// with one line on stderr naming what was missing.
|
||||
pub fn read() -> Appearance {
|
||||
match connect() {
|
||||
Ok(proxy) => read_with(&proxy, true),
|
||||
Err(err) => {
|
||||
eprintln!("tunal-theme: no settings portal ({err}); using default appearance");
|
||||
Appearance::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Watches the portal on a background thread and calls `on_change` with the full new
|
||||
/// appearance whenever the accent, colour scheme or animation setting changes.
|
||||
/// The thread ends (after one stderr line) when the bus or portal is unavailable.
|
||||
pub fn watch<F>(on_change: F) -> JoinHandle<()>
|
||||
where
|
||||
F: Fn(Appearance) + Send + 'static,
|
||||
{
|
||||
thread::spawn(move || {
|
||||
if let Err(err) = watch_loop(on_change) {
|
||||
eprintln!("tunal-theme: not watching appearance changes ({err})");
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn watch_loop(on_change: impl Fn(Appearance)) -> zbus::Result<()> {
|
||||
let proxy = connect()?;
|
||||
// Subscribe before reading so a change in between is not lost.
|
||||
let signals = proxy.receive_signal("SettingChanged")?;
|
||||
// Quiet: the caller's own `read()` has already reported anything missing.
|
||||
let mut current = read_with(&proxy, false);
|
||||
for msg in signals {
|
||||
let Ok((namespace, key, value)) = msg.body().deserialize::<(String, String, OwnedValue)>()
|
||||
else {
|
||||
continue;
|
||||
};
|
||||
let mut next = current;
|
||||
match (namespace.as_str(), key.as_str()) {
|
||||
(APPEARANCE, "accent-color") => next.accent = parse_accent(&value),
|
||||
(APPEARANCE, "color-scheme") => next.scheme = parse_scheme(&value).unwrap_or_default(),
|
||||
(INTERFACE, "enable-animations") => next.animations = parse_bool(&value),
|
||||
_ => continue,
|
||||
}
|
||||
if next != current {
|
||||
current = next;
|
||||
on_change(current);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn connect() -> zbus::Result<Proxy<'static>> {
|
||||
let conn = Connection::session()?;
|
||||
Builder::new(&conn)
|
||||
.destination("org.freedesktop.portal.Desktop")?
|
||||
.path("/org/freedesktop/portal/desktop")?
|
||||
.interface("org.freedesktop.portal.Settings")?
|
||||
.cache_properties(CacheProperties::No)
|
||||
.build()
|
||||
}
|
||||
|
||||
fn read_with(proxy: &Proxy, log: bool) -> Appearance {
|
||||
let accent = read_one(proxy, APPEARANCE, "accent-color");
|
||||
let scheme = read_one(proxy, APPEARANCE, "color-scheme");
|
||||
let animations = read_one(proxy, INTERFACE, "enable-animations");
|
||||
|
||||
let mut missing = Vec::new();
|
||||
let accent = accent.as_ref().and_then(parse_accent);
|
||||
if accent.is_none() {
|
||||
missing.push("accent-color");
|
||||
}
|
||||
let scheme = scheme.as_ref().and_then(parse_scheme);
|
||||
if scheme.is_none() {
|
||||
missing.push("color-scheme");
|
||||
}
|
||||
if log && !missing.is_empty() {
|
||||
eprintln!(
|
||||
"tunal-theme: portal has no {}; using defaults",
|
||||
missing.join(", ")
|
||||
);
|
||||
}
|
||||
Appearance {
|
||||
accent,
|
||||
scheme: scheme.unwrap_or_default(),
|
||||
animations: animations.as_ref().and_then(parse_bool),
|
||||
}
|
||||
}
|
||||
|
||||
/// `ReadOne` (portal version 2), falling back to the older `Read`, whose reply is
|
||||
/// wrapped in one more variant (unwrapped by the parsers).
|
||||
fn read_one(proxy: &Proxy, namespace: &str, key: &str) -> Option<OwnedValue> {
|
||||
proxy
|
||||
.call("ReadOne", &(namespace, key))
|
||||
.or_else(|_| proxy.call("Read", &(namespace, key)))
|
||||
.ok()
|
||||
}
|
||||
|
||||
/// Strips the variant wrappers that `Read` adds.
|
||||
fn unwrap(value: &Value<'_>) -> Value<'static> {
|
||||
let mut value = value
|
||||
.try_to_owned()
|
||||
.map(Value::from)
|
||||
.unwrap_or(Value::Bool(false));
|
||||
while let Value::Value(inner) = value {
|
||||
value = *inner;
|
||||
}
|
||||
value
|
||||
}
|
||||
|
||||
fn parse_accent(value: &OwnedValue) -> Option<Accent> {
|
||||
let (r, g, b) = <(f64, f64, f64)>::try_from(unwrap(value)).ok()?;
|
||||
Accent::new(r, g, b)
|
||||
}
|
||||
|
||||
fn parse_scheme(value: &OwnedValue) -> Option<ColorScheme> {
|
||||
match u32::try_from(unwrap(value)).ok()? {
|
||||
1 => Some(ColorScheme::Dark),
|
||||
// 0 is "no preference": light, like 2.
|
||||
_ => Some(ColorScheme::Light),
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_bool(value: &OwnedValue) -> Option<bool> {
|
||||
bool::try_from(unwrap(value)).ok()
|
||||
}
|
||||
|
|
@ -23,3 +23,4 @@ D-018 — 2026-11-18 — Elevation via pkexec/polkit: `tunal --elevated-helper`
|
|||
D-019 — 2026-11-18 — TLS and SSH crypto on rustls + ring (no aws-lc-rs, no OpenSSL) — user — S020, S028.
|
||||
D-020 — 2026-11-18 — Accepted SSH host keys go to `$XDG_CONFIG_HOME/tunal/known_hosts`, read together with `~/.ssh/known_hosts`; Tunal never writes under ~/.ssh — plan-generate — S027–S029.
|
||||
D-021 — 2026-11-18 — Playlist sort: a sort option "Playlist sort" offered only in folders under XDG_MUSIC_DIR that contain a playlist file (.m3u, .m3u8, .pls, .xspf); listed files first in playlist order, unlisted entries after in natural name order; several playlists → one option each; desc reverses; re-sorts when the playlist changes; stored as the folder's sort in `.directory` like other sorts — user request (details chosen by orchestrator, user may revise) — S008, S015.
|
||||
D-022 — 2026-11-18 — Fallback theme seed `#3F6FB5` (muted blue) when the portal gives no accent; palettes are `material-colors` 0.5 `ThemeBuilder` defaults (tonal spot, spec 2021) exported as ARGB `Palette` structs; library warnings go to stderr as one `tunal-theme: …` line (no logging crate yet) — S002 — S002, S003.
|
||||
|
|
|
|||
|
|
@ -8,5 +8,11 @@ One entry per session, newest last, ≤ 15 lines: done, verified how, not done,
|
|||
- Not done: nothing. Tracker item texts for S001 were shortened when syncing.
|
||||
- Later: —
|
||||
|
||||
## S002 — Portal theme and Material schemes
|
||||
- Done: `tunal-theme` with `read() -> Appearance`, `watch(F: Fn(Appearance) + Send + 'static) -> JoinHandle<()>`, `Appearance { accent: Option<Accent>, scheme: ColorScheme, animations: Option<bool> }` (+ `seed()`, `palette()`), `Accent { red, green, blue: f64 }` (`rgb() -> 0xRRGGBB`), `Palettes::from_seed(u32) -> { light, dark: Palette }`, `Palette` = 34 M3 roles as ARGB u32. Fallback seed `FALLBACK_SEED = 0x3F6FB5` (D-022). zbus 5.19 blocking-api + async-io, no tokio. Example `dump` (`--watch` prints changes).
|
||||
- Verified: build, clippy -D warnings, fmt --check; dump accent = busctl (0.901961, 0.176471, 0.258824); `Read` fallback forced once; no bus (`unix:path=/nonexistent`) and bare bus without portal → one stderr line, defaults, exit 0; watcher against a fake portal (private bus) reported accent, scheme and animations changes, ignored other namespaces, out-of-range accent → None.
|
||||
- Not done: nothing. Missing-setting warnings are `eprintln!` lines prefixed `tunal-theme:` (no logging crate).
|
||||
- Later: —
|
||||
|
||||
## Later
|
||||
-
|
||||
|
|
|
|||
Loading…
Reference in a new issue