From 74d8d2d3cf8bfe355158a6dca2203aca4e3dcea6 Mon Sep 17 00:00:00 2001 From: vicrodh <103399283+vicrodh@users.noreply.github.com> Date: Mon, 23 Feb 2026 03:42:37 -0600 Subject: [PATCH] fix(auto-theme): async spawn_blocking for image processing, KDE color scheme fallback - V2 commands (generate_theme_from_image, generate_theme_from_wallpaper, extract_palette) now use tokio::task::spawn_blocking to avoid freezing the main thread during CPU-bound image decoding and k-means clustering. This lets the UI spinner animate properly. - KDE accent detection now falls back to Colors:Selection DecorationFocus and BackgroundNormal from kdeglobals when no explicit AccentColor is set. This reads the actual KDE color scheme accent instead of only looking for the Plasma 6 custom accent field. cc --- src-tauri/src/auto_theme/system.rs | 43 ++++++++++++++++++++++++++++- src-tauri/src/commands_v2.rs | 44 ++++++++++++++++++++---------- 2 files changed, 71 insertions(+), 16 deletions(-) diff --git a/src-tauri/src/auto_theme/system.rs b/src-tauri/src/auto_theme/system.rs index eaa38be1f..24f90d6a1 100644 --- a/src-tauri/src/auto_theme/system.rs +++ b/src-tauri/src/auto_theme/system.rs @@ -210,6 +210,7 @@ fn get_kde_accent() -> Result { let content = fs::read_to_string(&config_path).map_err(|e| format!("Cannot read kdeglobals: {}", e))?; + // 1. Try explicit AccentColor in [General] (Plasma 6 custom accent) let mut in_general = false; for line in content.lines() { let trimmed = line.trim(); @@ -229,7 +230,47 @@ fn get_kde_accent() -> Result { } } - Err("AccentColor not found in kdeglobals".into()) + // 2. Fallback: read from color scheme sections (DecorationFocus or Selection background) + // Priority: Colors:Selection DecorationFocus → BackgroundNormal → Colors:View DecorationFocus + let fallback_sections = [ + ("[Colors:Selection]", "DecorationFocus"), + ("[Colors:Selection]", "BackgroundNormal"), + ("[Colors:View]", "DecorationFocus"), + ]; + + for (section, key) in &fallback_sections { + if let Some(color) = read_kde_color_key(&content, section, key) { + return Ok(color); + } + } + + Err("AccentColor not found in kdeglobals (no explicit accent or color scheme)".into()) +} + +/// Read a specific key from a KDE config section, parsing "r,g,b" format. +fn read_kde_color_key(content: &str, section: &str, key: &str) -> Option { + let mut in_section = false; + let prefix = format!("{}=", key); + + for line in content.lines() { + let trimmed = line.trim(); + + if trimmed == section { + in_section = true; + continue; + } + if trimmed.starts_with('[') { + in_section = false; + continue; + } + + if in_section && trimmed.starts_with(&prefix) { + let value = trimmed[prefix.len()..].trim(); + return parse_rgb_csv(value).ok(); + } + } + + None } // --- COSMIC --- diff --git a/src-tauri/src/commands_v2.rs b/src-tauri/src/commands_v2.rs index c3e1e789e..2d00dd256 100644 --- a/src-tauri/src/commands_v2.rs +++ b/src-tauri/src/commands_v2.rs @@ -10127,6 +10127,8 @@ pub async fn v2_dynamic_suggest_raw( } // ── Auto-Theme commands ───────────────────────────────────────────────────── +// Image processing (decode + k-means) is CPU-bound, so heavy commands use +// spawn_blocking to avoid freezing the main thread / UI spinner. #[tauri::command] pub fn v2_detect_desktop_environment() -> crate::auto_theme::system::DesktopEnvironment { @@ -10145,30 +10147,42 @@ pub fn v2_get_system_accent_color() -> Result Result { - let palette = crate::auto_theme::palette::extract_palette(&imagePath)?; - Ok(crate::auto_theme::generator::generate_theme( - &palette, - &imagePath, - )) + tokio::task::spawn_blocking(move || { + let palette = crate::auto_theme::palette::extract_palette(&imagePath)?; + Ok(crate::auto_theme::generator::generate_theme( + &palette, + &imagePath, + )) + }) + .await + .map_err(|e| format!("Theme generation task failed: {}", e))? } #[tauri::command] -pub fn v2_generate_theme_from_wallpaper() -> Result { - let wallpaper = crate::auto_theme::system::get_system_wallpaper()?; - let palette = crate::auto_theme::palette::extract_palette(&wallpaper)?; - Ok(crate::auto_theme::generator::generate_theme( - &palette, - &wallpaper, - )) +pub async fn v2_generate_theme_from_wallpaper() -> Result { + tokio::task::spawn_blocking(|| { + let wallpaper = crate::auto_theme::system::get_system_wallpaper()?; + let palette = crate::auto_theme::palette::extract_palette(&wallpaper)?; + Ok(crate::auto_theme::generator::generate_theme( + &palette, + &wallpaper, + )) + }) + .await + .map_err(|e| format!("Theme generation task failed: {}", e))? } #[tauri::command] #[allow(non_snake_case)] -pub fn v2_extract_palette( +pub async fn v2_extract_palette( imagePath: String, ) -> Result { - crate::auto_theme::palette::extract_palette(&imagePath) + tokio::task::spawn_blocking(move || { + crate::auto_theme::palette::extract_palette(&imagePath) + }) + .await + .map_err(|e| format!("Palette extraction task failed: {}", e))? }