Load bread-theme CSS from the window's monitor palette
All checks were successful
dev release / build (push) Successful in 6m1s

Pin bread-theme to v0.7.4.
This commit is contained in:
Breadway 2026-08-16 13:23:21 +08:00
parent 11c4ecb3cc
commit a6cb245ae4
3 changed files with 302 additions and 179 deletions

454
src/Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -36,7 +36,7 @@ toml_edit = "0.22"
tokio = { version = "1", features = ["process", "io-util", "time", "macros"] } tokio = { version = "1", features = ["process", "io-util", "time", "macros"] }
notify = "7" notify = "7"
regex = "1" regex = "1"
bread-theme = { git = "https://git.breadway.dev/Breadway/bread-ecosystem", tag = "v0.7.2" } bread-theme = { git = "https://git.breadway.dev/Breadway/bread-ecosystem", tag = "v0.7.4" }
bread-utils = { git = "https://git.breadway.dev/Breadway/bread-ecosystem", tag = "v0.7.2", features = ["toml"] } bread-utils = { git = "https://git.breadway.dev/Breadway/bread-ecosystem", tag = "v0.7.2", features = ["toml"] }
anyhow = "1" anyhow = "1"

View file

@ -11,17 +11,26 @@ use tauri::{AppHandle, Emitter, Manager};
/// Initial theme fetch — called once by the frontend at startup. /// Initial theme fetch — called once by the frontend at startup.
#[tauri::command] #[tauri::command]
pub fn get_theme_css() -> String { pub fn get_theme_css(window: tauri::WebviewWindow) -> String {
render_theme_css() render_theme_css(&palette_for_window(&window))
} }
fn render_theme_css() -> String { fn palette_for_window(window: &tauri::WebviewWindow) -> bread_theme::Palette {
let palette = bread_theme::load_palette(); window
.current_monitor()
.ok()
.flatten()
.and_then(|m| m.name().map(|s| s.to_string()))
.map(|name| bread_theme::load_palette_for(&name))
.unwrap_or_else(bread_theme::load_palette)
}
fn render_theme_css(palette: &bread_theme::Palette) -> String {
// bread-theme v0.7.1 exposes Palette + ink_on + tokens, but not the // bread-theme v0.7.1 exposes Palette + ink_on + tokens, but not the
// later css_custom_properties / css_tokens helpers (those landed after // later css_custom_properties / css_tokens helpers (those landed after
// the tag). Emit the same :root custom-property names the Svelte app // the tag). Emit the same :root custom-property names the Svelte app
// already uses so a tag pin doesn't require a web-side rename. // already uses so a tag pin doesn't require a web-side rename.
format!("{}\n{}", css_custom_properties(&palette), css_tokens()) format!("{}\n{}", css_custom_properties(palette), css_tokens())
} }
fn css_custom_properties(p: &bread_theme::Palette) -> String { fn css_custom_properties(p: &bread_theme::Palette) -> String {
@ -103,7 +112,11 @@ pub fn watch_and_emit(app: &AppHandle) {
EventKind::Create(_) | EventKind::Modify(_) | EventKind::Remove(_) EventKind::Create(_) | EventKind::Modify(_) | EventKind::Remove(_)
) && event.paths.iter().any(|p| p == &target_for_watcher); ) && event.paths.iter().any(|p| p == &target_for_watcher);
if touches_target { if touches_target {
let _ = app_for_watcher.emit("theme-changed", render_theme_css()); let css = app_for_watcher
.get_webview_window("main")
.map(|w| render_theme_css(&palette_for_window(&w)))
.unwrap_or_else(|| render_theme_css(&bread_theme::load_palette()));
let _ = app_for_watcher.emit("theme-changed", css);
} }
}, },
notify::Config::default(), notify::Config::default(),