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
This commit is contained in:
2 files changed
+59
-4
No files matched your search
@@ -210,6 +210,7 @@ fn get_kde_accent() -> Result<PaletteColor, String> {
|
|||||||
let content =
|
let content =
|
||||||
fs::read_to_string(&config_path).map_err(|e| format!("Cannot read kdeglobals: {}", e))?;
|
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;
|
let mut in_general = false;
|
||||||
for line in content.lines() {
|
for line in content.lines() {
|
||||||
let trimmed = line.trim();
|
let trimmed = line.trim();
|
||||||
@@ -229,7 +230,47 @@ fn get_kde_accent() -> Result<PaletteColor, String> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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<PaletteColor> {
|
||||||
|
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 ---
|
// --- COSMIC ---
|
||||||
|
|||||||
@@ -10127,6 +10127,8 @@ pub async fn v2_dynamic_suggest_raw(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ── Auto-Theme commands ─────────────────────────────────────────────────────
|
// ── 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]
|
#[tauri::command]
|
||||||
pub fn v2_detect_desktop_environment() -> crate::auto_theme::system::DesktopEnvironment {
|
pub fn v2_detect_desktop_environment() -> crate::auto_theme::system::DesktopEnvironment {
|
||||||
@@ -10145,30 +10147,42 @@ pub fn v2_get_system_accent_color() -> Result<crate::auto_theme::PaletteColor, S
|
|||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
#[allow(non_snake_case)]
|
#[allow(non_snake_case)]
|
||||||
pub fn v2_generate_theme_from_image(
|
pub async fn v2_generate_theme_from_image(
|
||||||
imagePath: String,
|
imagePath: String,
|
||||||
) -> Result<crate::auto_theme::GeneratedTheme, String> {
|
) -> Result<crate::auto_theme::GeneratedTheme, String> {
|
||||||
|
tokio::task::spawn_blocking(move || {
|
||||||
let palette = crate::auto_theme::palette::extract_palette(&imagePath)?;
|
let palette = crate::auto_theme::palette::extract_palette(&imagePath)?;
|
||||||
Ok(crate::auto_theme::generator::generate_theme(
|
Ok(crate::auto_theme::generator::generate_theme(
|
||||||
&palette,
|
&palette,
|
||||||
&imagePath,
|
&imagePath,
|
||||||
))
|
))
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
.map_err(|e| format!("Theme generation task failed: {}", e))?
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub fn v2_generate_theme_from_wallpaper() -> Result<crate::auto_theme::GeneratedTheme, String> {
|
pub async fn v2_generate_theme_from_wallpaper() -> Result<crate::auto_theme::GeneratedTheme, String> {
|
||||||
|
tokio::task::spawn_blocking(|| {
|
||||||
let wallpaper = crate::auto_theme::system::get_system_wallpaper()?;
|
let wallpaper = crate::auto_theme::system::get_system_wallpaper()?;
|
||||||
let palette = crate::auto_theme::palette::extract_palette(&wallpaper)?;
|
let palette = crate::auto_theme::palette::extract_palette(&wallpaper)?;
|
||||||
Ok(crate::auto_theme::generator::generate_theme(
|
Ok(crate::auto_theme::generator::generate_theme(
|
||||||
&palette,
|
&palette,
|
||||||
&wallpaper,
|
&wallpaper,
|
||||||
))
|
))
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
.map_err(|e| format!("Theme generation task failed: {}", e))?
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
#[allow(non_snake_case)]
|
#[allow(non_snake_case)]
|
||||||
pub fn v2_extract_palette(
|
pub async fn v2_extract_palette(
|
||||||
imagePath: String,
|
imagePath: String,
|
||||||
) -> Result<crate::auto_theme::ThemePalette, String> {
|
) -> Result<crate::auto_theme::ThemePalette, String> {
|
||||||
|
tokio::task::spawn_blocking(move || {
|
||||||
crate::auto_theme::palette::extract_palette(&imagePath)
|
crate::auto_theme::palette::extract_palette(&imagePath)
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
.map_err(|e| format!("Palette extraction task failed: {}", e))?
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user