feat(auto-theme): read full DE color scheme for system theme
Instead of using wallpaper k-means + accent override (which produced wrong backgrounds), the system source now reads ALL colors from the DE config (kdeglobals for KDE, dconf hints for GNOME). Maps Window/View/Button/Selection/Header sections directly to CSS variables — bg-primary from Window, bg-secondary from View, bg-tertiary from Button, accent from Selection, text from ForegroundNormal, etc. Adds SystemColorScheme type, get_system_color_scheme() in system.rs, generate_theme_from_scheme() in generator.rs, and two new V2 commands: v2_generate_theme_from_system_colors and v2_get_system_color_scheme. Frontend tryAccentColorTheme() replaced with trySystemColorSchemeTheme() which calls the new endpoint. Old color math helpers removed.
This commit is contained in:
6 files changed
+390
-138
No files matched your search
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use super::{GeneratedTheme, PaletteColor, ThemePalette};
|
use super::{GeneratedTheme, PaletteColor, SystemColorScheme, ThemePalette};
|
||||||
|
|
||||||
/// Generate a full CSS theme from an extracted palette.
|
/// Generate a full CSS theme from an extracted palette.
|
||||||
pub fn generate_theme(palette: &ThemePalette, source: &str) -> GeneratedTheme {
|
pub fn generate_theme(palette: &ThemePalette, source: &str) -> GeneratedTheme {
|
||||||
@@ -142,6 +142,193 @@ pub fn generate_theme(palette: &ThemePalette, source: &str) -> GeneratedTheme {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Generate a full CSS theme directly from a DE color scheme.
|
||||||
|
///
|
||||||
|
/// Instead of k-means extraction, this maps the DE's semantic colors (window bg,
|
||||||
|
/// view bg, button bg, selection, etc.) directly to CSS variables — producing a
|
||||||
|
/// theme that matches the system look exactly.
|
||||||
|
pub fn generate_theme_from_scheme(scheme: &SystemColorScheme) -> GeneratedTheme {
|
||||||
|
let mut vars = HashMap::new();
|
||||||
|
|
||||||
|
// Determine dark/light from the window background
|
||||||
|
let window_bg = scheme
|
||||||
|
.window_bg
|
||||||
|
.unwrap_or(PaletteColor::new(40, 40, 40));
|
||||||
|
let is_dark = window_bg.luminance() < 0.5;
|
||||||
|
|
||||||
|
// ── Backgrounds ──────────────────────────────────────────────────────
|
||||||
|
vars.insert("--bg-primary".into(), window_bg.to_hex());
|
||||||
|
|
||||||
|
let bg_secondary = scheme.view_bg.unwrap_or_else(|| {
|
||||||
|
if is_dark {
|
||||||
|
window_bg.shift_lightness(0.03)
|
||||||
|
} else {
|
||||||
|
window_bg.shift_lightness(-0.03)
|
||||||
|
}
|
||||||
|
});
|
||||||
|
vars.insert("--bg-secondary".into(), bg_secondary.to_hex());
|
||||||
|
|
||||||
|
let bg_tertiary = scheme.button_bg.unwrap_or_else(|| {
|
||||||
|
if is_dark {
|
||||||
|
window_bg.shift_lightness(0.10)
|
||||||
|
} else {
|
||||||
|
window_bg.shift_lightness(-0.10)
|
||||||
|
}
|
||||||
|
});
|
||||||
|
vars.insert("--bg-tertiary".into(), bg_tertiary.to_hex());
|
||||||
|
|
||||||
|
// Hover: midpoint between primary and secondary
|
||||||
|
let bg_hover = scheme.window_bg_alt.unwrap_or_else(|| {
|
||||||
|
PaletteColor::new(
|
||||||
|
((window_bg.r as u16 + bg_secondary.r as u16) / 2) as u8,
|
||||||
|
((window_bg.g as u16 + bg_secondary.g as u16) / 2) as u8,
|
||||||
|
((window_bg.b as u16 + bg_secondary.b as u16) / 2) as u8,
|
||||||
|
)
|
||||||
|
});
|
||||||
|
vars.insert("--bg-hover".into(), bg_hover.to_hex());
|
||||||
|
|
||||||
|
// ── Text ─────────────────────────────────────────────────────────────
|
||||||
|
let text_primary = scheme
|
||||||
|
.window_fg
|
||||||
|
.unwrap_or(if is_dark {
|
||||||
|
PaletteColor::new(223, 223, 223)
|
||||||
|
} else {
|
||||||
|
PaletteColor::new(36, 36, 36)
|
||||||
|
});
|
||||||
|
let text_primary = ensure_text_contrast(text_primary, &window_bg, is_dark);
|
||||||
|
vars.insert("--text-primary".into(), text_primary.to_hex());
|
||||||
|
|
||||||
|
// Secondary: slightly dimmed from primary
|
||||||
|
let text_secondary = scheme.view_fg.unwrap_or_else(|| {
|
||||||
|
text_primary.shift_lightness(if is_dark { -0.10 } else { 0.10 })
|
||||||
|
});
|
||||||
|
vars.insert("--text-secondary".into(), text_secondary.to_hex());
|
||||||
|
|
||||||
|
let text_muted = scheme.window_fg_inactive.unwrap_or_else(|| {
|
||||||
|
text_primary.shift_lightness(if is_dark { -0.25 } else { 0.25 })
|
||||||
|
});
|
||||||
|
vars.insert("--text-muted".into(), text_muted.to_hex());
|
||||||
|
|
||||||
|
let text_disabled = text_muted.shift_lightness(if is_dark { -0.10 } else { 0.10 });
|
||||||
|
vars.insert("--text-disabled".into(), text_disabled.to_hex());
|
||||||
|
|
||||||
|
// ── Accent (selection) ───────────────────────────────────────────────
|
||||||
|
let accent = scheme
|
||||||
|
.accent
|
||||||
|
.or(scheme.selection_bg)
|
||||||
|
.unwrap_or(PaletteColor::new(0, 120, 215));
|
||||||
|
vars.insert("--accent-primary".into(), accent.to_hex());
|
||||||
|
|
||||||
|
let accent_hover = scheme
|
||||||
|
.selection_hover
|
||||||
|
.unwrap_or_else(|| accent.shift_lightness(0.10));
|
||||||
|
vars.insert("--accent-hover".into(), accent_hover.to_hex());
|
||||||
|
vars.insert(
|
||||||
|
"--accent-active".into(),
|
||||||
|
accent.shift_lightness(-0.10).to_hex(),
|
||||||
|
);
|
||||||
|
|
||||||
|
// Button text on accent
|
||||||
|
let btn_text = scheme.selection_fg.unwrap_or(
|
||||||
|
if accent.luminance() < 0.5 {
|
||||||
|
PaletteColor::new(255, 255, 255)
|
||||||
|
} else {
|
||||||
|
PaletteColor::new(0, 0, 0)
|
||||||
|
},
|
||||||
|
);
|
||||||
|
vars.insert("--btn-primary-text".into(), btn_text.to_hex());
|
||||||
|
|
||||||
|
// ── Status colors ────────────────────────────────────────────────────
|
||||||
|
// Use system negative/neutral if available, else fallback to safe defaults
|
||||||
|
let danger = scheme
|
||||||
|
.fg_negative
|
||||||
|
.unwrap_or(if is_dark {
|
||||||
|
PaletteColor::new(239, 68, 68)
|
||||||
|
} else {
|
||||||
|
PaletteColor::new(220, 38, 38)
|
||||||
|
});
|
||||||
|
vars.insert("--danger".into(), danger.to_hex());
|
||||||
|
vars.insert("--danger-bg".into(), danger.to_rgba(0.1));
|
||||||
|
vars.insert("--danger-border".into(), danger.to_rgba(0.3));
|
||||||
|
vars.insert(
|
||||||
|
"--danger-hover".into(),
|
||||||
|
danger.to_rgba(if is_dark { 0.2 } else { 0.15 }),
|
||||||
|
);
|
||||||
|
|
||||||
|
let warning = scheme
|
||||||
|
.fg_neutral
|
||||||
|
.unwrap_or(if is_dark {
|
||||||
|
PaletteColor::new(251, 191, 36)
|
||||||
|
} else {
|
||||||
|
PaletteColor::new(217, 119, 6)
|
||||||
|
});
|
||||||
|
vars.insert("--warning".into(), warning.to_hex());
|
||||||
|
vars.insert("--warning-bg".into(), warning.to_rgba(0.1));
|
||||||
|
vars.insert("--warning-border".into(), warning.to_rgba(0.3));
|
||||||
|
vars.insert(
|
||||||
|
"--warning-hover".into(),
|
||||||
|
warning.to_rgba(if is_dark { 0.2 } else { 0.15 }),
|
||||||
|
);
|
||||||
|
|
||||||
|
// ── Borders ──────────────────────────────────────────────────────────
|
||||||
|
let border_subtle = if is_dark {
|
||||||
|
window_bg.shift_lightness(0.06)
|
||||||
|
} else {
|
||||||
|
window_bg.shift_lightness(-0.06)
|
||||||
|
};
|
||||||
|
let border_strong = if is_dark {
|
||||||
|
window_bg.shift_lightness(0.12)
|
||||||
|
} else {
|
||||||
|
window_bg.shift_lightness(-0.12)
|
||||||
|
};
|
||||||
|
vars.insert("--border-subtle".into(), border_subtle.to_hex());
|
||||||
|
vars.insert("--border-strong".into(), border_strong.to_hex());
|
||||||
|
|
||||||
|
// ── Alpha tokens ─────────────────────────────────────────────────────
|
||||||
|
let alpha_base = if is_dark {
|
||||||
|
(255, 255, 255)
|
||||||
|
} else {
|
||||||
|
(0, 0, 0)
|
||||||
|
};
|
||||||
|
let alpha_levels: &[(f64, &str)] = &[
|
||||||
|
(0.04, "--alpha-4"),
|
||||||
|
(0.05, "--alpha-5"),
|
||||||
|
(0.06, "--alpha-6"),
|
||||||
|
(0.08, "--alpha-8"),
|
||||||
|
(0.10, "--alpha-10"),
|
||||||
|
(0.15, "--alpha-15"),
|
||||||
|
(0.18, "--alpha-18"),
|
||||||
|
(0.20, "--alpha-20"),
|
||||||
|
(0.25, "--alpha-25"),
|
||||||
|
(0.30, "--alpha-30"),
|
||||||
|
(0.35, "--alpha-35"),
|
||||||
|
(0.40, "--alpha-40"),
|
||||||
|
(0.45, "--alpha-45"),
|
||||||
|
(0.50, "--alpha-50"),
|
||||||
|
(0.60, "--alpha-60"),
|
||||||
|
(0.70, "--alpha-70"),
|
||||||
|
(0.80, "--alpha-80"),
|
||||||
|
(0.85, "--alpha-85"),
|
||||||
|
(0.90, "--alpha-90"),
|
||||||
|
(0.95, "--alpha-95"),
|
||||||
|
];
|
||||||
|
for (alpha, name) in alpha_levels {
|
||||||
|
vars.insert(
|
||||||
|
name.to_string(),
|
||||||
|
format!(
|
||||||
|
"rgba({}, {}, {}, {})",
|
||||||
|
alpha_base.0, alpha_base.1, alpha_base.2, alpha
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
GeneratedTheme {
|
||||||
|
variables: vars,
|
||||||
|
is_dark,
|
||||||
|
source: "system-colors".to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Ensure text has at least WCAG AA contrast (4.5:1) against the background.
|
/// Ensure text has at least WCAG AA contrast (4.5:1) against the background.
|
||||||
fn ensure_text_contrast(
|
fn ensure_text_contrast(
|
||||||
text: PaletteColor,
|
text: PaletteColor,
|
||||||
|
|||||||
@@ -166,6 +166,45 @@ pub struct GeneratedTheme {
|
|||||||
pub source: String,
|
pub source: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Full color scheme read from the desktop environment.
|
||||||
|
///
|
||||||
|
/// Maps directly to the semantic color roles in KDE kdeglobals, GNOME dconf, etc.
|
||||||
|
/// Each field is optional because not all DEs expose all colors.
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct SystemColorScheme {
|
||||||
|
// Backgrounds
|
||||||
|
pub window_bg: Option<PaletteColor>,
|
||||||
|
pub window_bg_alt: Option<PaletteColor>,
|
||||||
|
pub view_bg: Option<PaletteColor>,
|
||||||
|
pub button_bg: Option<PaletteColor>,
|
||||||
|
pub header_bg: Option<PaletteColor>,
|
||||||
|
pub header_bg_inactive: Option<PaletteColor>,
|
||||||
|
pub tooltip_bg: Option<PaletteColor>,
|
||||||
|
|
||||||
|
// Foregrounds (text)
|
||||||
|
pub window_fg: Option<PaletteColor>,
|
||||||
|
pub window_fg_inactive: Option<PaletteColor>,
|
||||||
|
pub view_fg: Option<PaletteColor>,
|
||||||
|
pub button_fg: Option<PaletteColor>,
|
||||||
|
|
||||||
|
// Selection / accent
|
||||||
|
pub selection_bg: Option<PaletteColor>,
|
||||||
|
pub selection_fg: Option<PaletteColor>,
|
||||||
|
pub selection_hover: Option<PaletteColor>,
|
||||||
|
pub accent: Option<PaletteColor>,
|
||||||
|
|
||||||
|
// Semantic
|
||||||
|
pub fg_link: Option<PaletteColor>,
|
||||||
|
pub fg_negative: Option<PaletteColor>,
|
||||||
|
pub fg_neutral: Option<PaletteColor>,
|
||||||
|
pub fg_positive: Option<PaletteColor>,
|
||||||
|
|
||||||
|
// Window manager
|
||||||
|
pub wm_active_bg: Option<PaletteColor>,
|
||||||
|
pub wm_active_fg: Option<PaletteColor>,
|
||||||
|
pub wm_inactive_bg: Option<PaletteColor>,
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ use std::fs;
|
|||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
|
|
||||||
use super::PaletteColor;
|
use super::{PaletteColor, SystemColorScheme};
|
||||||
|
|
||||||
/// Supported desktop environments.
|
/// Supported desktop environments.
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||||
@@ -511,6 +511,141 @@ fn parse_cosmic_color(content: &str) -> Option<PaletteColor> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ── Full color scheme ────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
/// Read the full system color scheme from the current DE.
|
||||||
|
pub fn get_system_color_scheme() -> Result<SystemColorScheme, String> {
|
||||||
|
let de = detect_desktop_environment();
|
||||||
|
match de {
|
||||||
|
DesktopEnvironment::KdePlasma => get_kde_color_scheme(),
|
||||||
|
DesktopEnvironment::Gnome => get_gnome_color_scheme(),
|
||||||
|
_ => Err(format!(
|
||||||
|
"Full color scheme not supported for {}",
|
||||||
|
de.display_name()
|
||||||
|
)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_kde_color_scheme() -> Result<SystemColorScheme, String> {
|
||||||
|
let home = env::var("HOME").map_err(|_| "HOME not set".to_string())?;
|
||||||
|
let config_path = format!("{}/.config/kdeglobals", home);
|
||||||
|
let content =
|
||||||
|
fs::read_to_string(&config_path).map_err(|e| format!("Cannot read kdeglobals: {}", e))?;
|
||||||
|
|
||||||
|
// Also check [General] AccentColor
|
||||||
|
let accent_explicit = read_kde_color_key(&content, "[General]", "AccentColor");
|
||||||
|
|
||||||
|
let scheme = SystemColorScheme {
|
||||||
|
// Window
|
||||||
|
window_bg: read_kde_color_key(&content, "[Colors:Window]", "BackgroundNormal"),
|
||||||
|
window_bg_alt: read_kde_color_key(&content, "[Colors:Window]", "BackgroundAlternate"),
|
||||||
|
view_bg: read_kde_color_key(&content, "[Colors:View]", "BackgroundNormal"),
|
||||||
|
button_bg: read_kde_color_key(&content, "[Colors:Button]", "BackgroundNormal"),
|
||||||
|
header_bg: read_kde_color_key(&content, "[Colors:Header]", "BackgroundNormal"),
|
||||||
|
header_bg_inactive: read_kde_color_key(&content, "[Colors:Header][Inactive]", "BackgroundNormal"),
|
||||||
|
tooltip_bg: read_kde_color_key(&content, "[Colors:Tooltip]", "BackgroundNormal"),
|
||||||
|
|
||||||
|
// Foregrounds
|
||||||
|
window_fg: read_kde_color_key(&content, "[Colors:Window]", "ForegroundNormal"),
|
||||||
|
window_fg_inactive: read_kde_color_key(&content, "[Colors:Window]", "ForegroundInactive"),
|
||||||
|
view_fg: read_kde_color_key(&content, "[Colors:View]", "ForegroundNormal"),
|
||||||
|
button_fg: read_kde_color_key(&content, "[Colors:Button]", "ForegroundNormal"),
|
||||||
|
|
||||||
|
// Selection / accent
|
||||||
|
selection_bg: read_kde_color_key(&content, "[Colors:Selection]", "BackgroundNormal"),
|
||||||
|
selection_fg: read_kde_color_key(&content, "[Colors:Selection]", "ForegroundNormal"),
|
||||||
|
selection_hover: read_kde_color_key(&content, "[Colors:Selection]", "DecorationHover"),
|
||||||
|
accent: accent_explicit.or_else(|| {
|
||||||
|
read_kde_color_key(&content, "[Colors:Selection]", "DecorationFocus")
|
||||||
|
}),
|
||||||
|
|
||||||
|
// Semantic
|
||||||
|
fg_link: read_kde_color_key(&content, "[Colors:Window]", "ForegroundLink"),
|
||||||
|
fg_negative: read_kde_color_key(&content, "[Colors:Window]", "ForegroundNegative"),
|
||||||
|
fg_neutral: read_kde_color_key(&content, "[Colors:Window]", "ForegroundNeutral"),
|
||||||
|
fg_positive: read_kde_color_key(&content, "[Colors:Window]", "ForegroundPositive"),
|
||||||
|
|
||||||
|
// Window manager
|
||||||
|
wm_active_bg: read_kde_color_key(&content, "[WM]", "activeBackground"),
|
||||||
|
wm_active_fg: read_kde_color_key(&content, "[WM]", "activeForeground"),
|
||||||
|
wm_inactive_bg: read_kde_color_key(&content, "[WM]", "inactiveBackground"),
|
||||||
|
};
|
||||||
|
|
||||||
|
// Validate: at minimum we need window_bg and some accent
|
||||||
|
if scheme.window_bg.is_none() {
|
||||||
|
return Err("KDE color scheme missing Colors:Window BackgroundNormal".into());
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(scheme)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_gnome_color_scheme() -> Result<SystemColorScheme, String> {
|
||||||
|
// GNOME exposes much less via dconf. We can detect dark vs light and accent.
|
||||||
|
// Full background/text colors are determined by GTK theme, not easily readable.
|
||||||
|
let accent = get_gnome_accent().ok();
|
||||||
|
|
||||||
|
// Detect dark/light preference
|
||||||
|
let is_dark = Command::new("gsettings")
|
||||||
|
.args(["get", "org.gnome.desktop.interface", "color-scheme"])
|
||||||
|
.output()
|
||||||
|
.ok()
|
||||||
|
.and_then(|o| {
|
||||||
|
if o.status.success() {
|
||||||
|
let val = String::from_utf8_lossy(&o.stdout).trim().to_lowercase();
|
||||||
|
Some(val.contains("dark"))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.unwrap_or(true);
|
||||||
|
|
||||||
|
// Build a minimal scheme with GNOME defaults
|
||||||
|
let (bg, bg_alt, view_bg, btn_bg, fg, fg_inactive) = if is_dark {
|
||||||
|
(
|
||||||
|
PaletteColor::new(36, 36, 36), // Adwaita Dark
|
||||||
|
PaletteColor::new(48, 48, 48),
|
||||||
|
PaletteColor::new(30, 30, 30),
|
||||||
|
PaletteColor::new(60, 60, 60),
|
||||||
|
PaletteColor::new(255, 255, 255),
|
||||||
|
PaletteColor::new(140, 140, 140),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
(
|
||||||
|
PaletteColor::new(246, 245, 244), // Adwaita Light
|
||||||
|
PaletteColor::new(235, 235, 235),
|
||||||
|
PaletteColor::new(255, 255, 255),
|
||||||
|
PaletteColor::new(225, 225, 225),
|
||||||
|
PaletteColor::new(36, 36, 36),
|
||||||
|
PaletteColor::new(120, 120, 120),
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(SystemColorScheme {
|
||||||
|
window_bg: Some(bg),
|
||||||
|
window_bg_alt: Some(bg_alt),
|
||||||
|
view_bg: Some(view_bg),
|
||||||
|
button_bg: Some(btn_bg),
|
||||||
|
header_bg: None,
|
||||||
|
header_bg_inactive: None,
|
||||||
|
tooltip_bg: None,
|
||||||
|
window_fg: Some(fg),
|
||||||
|
window_fg_inactive: Some(fg_inactive),
|
||||||
|
view_fg: Some(fg),
|
||||||
|
button_fg: Some(PaletteColor::new(255, 255, 255)),
|
||||||
|
selection_bg: accent.clone(),
|
||||||
|
selection_fg: Some(PaletteColor::new(255, 255, 255)),
|
||||||
|
selection_hover: None,
|
||||||
|
accent,
|
||||||
|
fg_link: None,
|
||||||
|
fg_negative: Some(PaletteColor::new(224, 27, 36)),
|
||||||
|
fg_neutral: Some(PaletteColor::new(205, 147, 9)),
|
||||||
|
fg_positive: Some(PaletteColor::new(38, 162, 105)),
|
||||||
|
wm_active_bg: None,
|
||||||
|
wm_active_fg: None,
|
||||||
|
wm_inactive_bg: None,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
fn is_image_path(path: &str) -> bool {
|
fn is_image_path(path: &str) -> bool {
|
||||||
let lower = path.to_lowercase();
|
let lower = path.to_lowercase();
|
||||||
lower.ends_with(".png")
|
lower.ends_with(".png")
|
||||||
|
|||||||
@@ -10175,6 +10175,17 @@ pub async fn v2_generate_theme_from_wallpaper() -> Result<crate::auto_theme::Gen
|
|||||||
.map_err(|e| format!("Theme generation task failed: {}", e))?
|
.map_err(|e| format!("Theme generation task failed: {}", e))?
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tauri::command]
|
||||||
|
pub fn v2_generate_theme_from_system_colors() -> Result<crate::auto_theme::GeneratedTheme, String> {
|
||||||
|
let scheme = crate::auto_theme::system::get_system_color_scheme()?;
|
||||||
|
Ok(crate::auto_theme::generator::generate_theme_from_scheme(&scheme))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tauri::command]
|
||||||
|
pub fn v2_get_system_color_scheme() -> Result<crate::auto_theme::SystemColorScheme, String> {
|
||||||
|
crate::auto_theme::system::get_system_color_scheme()
|
||||||
|
}
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
#[allow(non_snake_case)]
|
#[allow(non_snake_case)]
|
||||||
pub async fn v2_extract_palette(
|
pub async fn v2_extract_palette(
|
||||||
|
|||||||
@@ -1110,6 +1110,8 @@ pub fn run() {
|
|||||||
commands_v2::v2_get_system_accent_color,
|
commands_v2::v2_get_system_accent_color,
|
||||||
commands_v2::v2_generate_theme_from_image,
|
commands_v2::v2_generate_theme_from_image,
|
||||||
commands_v2::v2_generate_theme_from_wallpaper,
|
commands_v2::v2_generate_theme_from_wallpaper,
|
||||||
|
commands_v2::v2_generate_theme_from_system_colors,
|
||||||
|
commands_v2::v2_get_system_color_scheme,
|
||||||
commands_v2::v2_extract_palette,
|
commands_v2::v2_extract_palette,
|
||||||
])
|
])
|
||||||
.run(tauri::generate_context!())
|
.run(tauri::generate_context!())
|
||||||
|
|||||||
@@ -144,85 +144,15 @@ function clearAutoThemeStorage(): void {
|
|||||||
|
|
||||||
// ── Internal: Theme generation strategies ───────────────────────────────────
|
// ── Internal: Theme generation strategies ───────────────────────────────────
|
||||||
|
|
||||||
/** Try to generate theme from DE accent color. Returns null if unavailable. */
|
/**
|
||||||
async function tryAccentColorTheme(): Promise<GeneratedTheme | null> {
|
* Try to generate theme from the DE's full color scheme.
|
||||||
|
* This reads backgrounds, text, accent, and all semantic colors directly
|
||||||
|
* from the DE configuration (kdeglobals for KDE, dconf for GNOME, etc.)
|
||||||
|
* — no wallpaper extraction or k-means needed.
|
||||||
|
*/
|
||||||
|
async function trySystemColorSchemeTheme(): Promise<GeneratedTheme | null> {
|
||||||
try {
|
try {
|
||||||
const accentColor = await invoke<PaletteColor>('v2_get_system_accent_color');
|
return await invoke<GeneratedTheme>('v2_generate_theme_from_system_colors');
|
||||||
// Build a theme using the accent color with a neutral dark base
|
|
||||||
// We generate from a synthetic image isn't ideal, so we build the theme
|
|
||||||
// by passing the accent to the wallpaper generator with a hint
|
|
||||||
// For now, use the accent as override on a wallpaper-based theme
|
|
||||||
// or generate a minimal theme from accent alone.
|
|
||||||
//
|
|
||||||
// Strategy: try wallpaper first (for backgrounds), but override accent with system accent
|
|
||||||
let baseTheme: GeneratedTheme | null = null;
|
|
||||||
try {
|
|
||||||
baseTheme = await invoke<GeneratedTheme>('v2_generate_theme_from_wallpaper');
|
|
||||||
} catch {
|
|
||||||
// No wallpaper available, build from accent alone
|
|
||||||
}
|
|
||||||
|
|
||||||
if (baseTheme) {
|
|
||||||
// Override accent variables with system accent color
|
|
||||||
const accentHex = `#${accentColor.r.toString(16).padStart(2, '0')}${accentColor.g.toString(16).padStart(2, '0')}${accentColor.b.toString(16).padStart(2, '0')}`;
|
|
||||||
baseTheme.variables['--accent-primary'] = accentHex;
|
|
||||||
// Compute hover/active shifts (simple lightness approximation)
|
|
||||||
baseTheme.variables['--accent-hover'] = shiftHexLightness(accentHex, 0.10);
|
|
||||||
baseTheme.variables['--accent-active'] = shiftHexLightness(accentHex, -0.10);
|
|
||||||
// Button text: white if accent is dark
|
|
||||||
const accentLum = (0.2126 * srgbLinear(accentColor.r) + 0.7152 * srgbLinear(accentColor.g) + 0.0722 * srgbLinear(accentColor.b));
|
|
||||||
baseTheme.variables['--btn-primary-text'] = accentLum < 0.5 ? '#ffffff' : '#000000';
|
|
||||||
baseTheme.source = 'system-accent+wallpaper';
|
|
||||||
return baseTheme;
|
|
||||||
}
|
|
||||||
|
|
||||||
// No wallpaper: build minimal theme from accent color only
|
|
||||||
// Use a neutral dark base
|
|
||||||
const isDark = true; // default to dark
|
|
||||||
const vars: Record<string, string> = {};
|
|
||||||
const accentHex = `#${accentColor.r.toString(16).padStart(2, '0')}${accentColor.g.toString(16).padStart(2, '0')}${accentColor.b.toString(16).padStart(2, '0')}`;
|
|
||||||
|
|
||||||
// Dark base backgrounds
|
|
||||||
vars['--bg-primary'] = '#0f0f0f';
|
|
||||||
vars['--bg-secondary'] = '#1a1a1a';
|
|
||||||
vars['--bg-tertiary'] = '#2a2a2a';
|
|
||||||
vars['--bg-hover'] = '#1f1f1f';
|
|
||||||
|
|
||||||
// Text
|
|
||||||
vars['--text-primary'] = '#ffffff';
|
|
||||||
vars['--text-secondary'] = '#cccccc';
|
|
||||||
vars['--text-muted'] = '#888888';
|
|
||||||
vars['--text-disabled'] = '#555555';
|
|
||||||
|
|
||||||
// Accent from system
|
|
||||||
vars['--accent-primary'] = accentHex;
|
|
||||||
vars['--accent-hover'] = shiftHexLightness(accentHex, 0.10);
|
|
||||||
vars['--accent-active'] = shiftHexLightness(accentHex, -0.10);
|
|
||||||
const accentLum = (0.2126 * srgbLinear(accentColor.r) + 0.7152 * srgbLinear(accentColor.g) + 0.0722 * srgbLinear(accentColor.b));
|
|
||||||
vars['--btn-primary-text'] = accentLum < 0.5 ? '#ffffff' : '#000000';
|
|
||||||
|
|
||||||
// Status
|
|
||||||
vars['--danger'] = '#ef4444';
|
|
||||||
vars['--danger-bg'] = 'rgba(239, 68, 68, 0.1)';
|
|
||||||
vars['--danger-border'] = 'rgba(239, 68, 68, 0.3)';
|
|
||||||
vars['--danger-hover'] = 'rgba(239, 68, 68, 0.2)';
|
|
||||||
vars['--warning'] = '#fbbf24';
|
|
||||||
vars['--warning-bg'] = 'rgba(251, 191, 36, 0.1)';
|
|
||||||
vars['--warning-border'] = 'rgba(251, 191, 36, 0.3)';
|
|
||||||
vars['--warning-hover'] = 'rgba(251, 191, 36, 0.2)';
|
|
||||||
|
|
||||||
// Borders
|
|
||||||
vars['--border-subtle'] = '#2a2a2a';
|
|
||||||
vars['--border-strong'] = '#3a3a3a';
|
|
||||||
|
|
||||||
// Alpha tokens (white-based for dark)
|
|
||||||
const alphaLevels = [0.04, 0.05, 0.06, 0.08, 0.10, 0.15, 0.18, 0.20, 0.25, 0.30, 0.35, 0.40, 0.45, 0.50, 0.60, 0.70, 0.80, 0.85, 0.90, 0.95];
|
|
||||||
const alphaNames = ['4', '5', '6', '8', '10', '15', '18', '20', '25', '30', '35', '40', '45', '50', '60', '70', '80', '85', '90', '95'];
|
|
||||||
for (let i = 0; i < alphaLevels.length; i++) {
|
|
||||||
vars[`--alpha-${alphaNames[i]}`] = `rgba(255, 255, 255, ${alphaLevels[i]})`;
|
|
||||||
}
|
|
||||||
|
|
||||||
return { variables: vars, is_dark: isDark, source: 'system-accent' };
|
|
||||||
} catch {
|
} catch {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -238,58 +168,6 @@ async function generateFromImage(imagePath: string): Promise<GeneratedTheme> {
|
|||||||
return invoke<GeneratedTheme>('v2_generate_theme_from_image', { imagePath });
|
return invoke<GeneratedTheme>('v2_generate_theme_from_image', { imagePath });
|
||||||
}
|
}
|
||||||
|
|
||||||
// ── Color math helpers (frontend-side, minimal) ─────────────────────────────
|
|
||||||
|
|
||||||
function srgbLinear(c: number): number {
|
|
||||||
const s = c / 255;
|
|
||||||
return s <= 0.04045 ? s / 12.92 : Math.pow((s + 0.055) / 1.055, 2.4);
|
|
||||||
}
|
|
||||||
|
|
||||||
function shiftHexLightness(hex: string, amount: number): string {
|
|
||||||
const r = parseInt(hex.slice(1, 3), 16) / 255;
|
|
||||||
const g = parseInt(hex.slice(3, 5), 16) / 255;
|
|
||||||
const b = parseInt(hex.slice(5, 7), 16) / 255;
|
|
||||||
|
|
||||||
const max = Math.max(r, g, b);
|
|
||||||
const min = Math.min(r, g, b);
|
|
||||||
let h = 0;
|
|
||||||
let s = 0;
|
|
||||||
const l = (max + min) / 2;
|
|
||||||
|
|
||||||
if (max !== min) {
|
|
||||||
const d = max - min;
|
|
||||||
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
|
|
||||||
if (max === r) h = ((g - b) / d + (g < b ? 6 : 0)) / 6;
|
|
||||||
else if (max === g) h = ((b - r) / d + 2) / 6;
|
|
||||||
else h = ((r - g) / d + 4) / 6;
|
|
||||||
}
|
|
||||||
|
|
||||||
const newL = Math.max(0, Math.min(1, l + amount));
|
|
||||||
|
|
||||||
function hue2rgb(p: number, q: number, ht: number): number {
|
|
||||||
if (ht < 0) ht += 1;
|
|
||||||
if (ht > 1) ht -= 1;
|
|
||||||
if (ht < 1 / 6) return p + (q - p) * 6 * ht;
|
|
||||||
if (ht < 1 / 2) return q;
|
|
||||||
if (ht < 2 / 3) return p + (q - p) * (2 / 3 - ht) * 6;
|
|
||||||
return p;
|
|
||||||
}
|
|
||||||
|
|
||||||
let nr: number, ng: number, nb: number;
|
|
||||||
if (s === 0) {
|
|
||||||
nr = ng = nb = newL;
|
|
||||||
} else {
|
|
||||||
const q = newL < 0.5 ? newL * (1 + s) : newL + s - newL * s;
|
|
||||||
const p = 2 * newL - q;
|
|
||||||
nr = hue2rgb(p, q, h + 1 / 3);
|
|
||||||
ng = hue2rgb(p, q, h);
|
|
||||||
nb = hue2rgb(p, q, h - 1 / 3);
|
|
||||||
}
|
|
||||||
|
|
||||||
const toHex = (v: number) => Math.round(v * 255).toString(16).padStart(2, '0');
|
|
||||||
return `#${toHex(nr)}${toHex(ng)}${toHex(nb)}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ── Public API ──────────────────────────────────────────────────────────────
|
// ── Public API ──────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -319,17 +197,17 @@ export async function enableAutoTheme(
|
|||||||
let palette: ThemePalette | null = null;
|
let palette: ThemePalette | null = null;
|
||||||
|
|
||||||
if (source === 'system') {
|
if (source === 'system') {
|
||||||
// Cascade: accent color → wallpaper → error
|
// Cascade: full color scheme → wallpaper → error
|
||||||
const accentTheme = await tryAccentColorTheme();
|
const schemeTheme = await trySystemColorSchemeTheme();
|
||||||
if (accentTheme) {
|
if (schemeTheme) {
|
||||||
theme = accentTheme;
|
theme = schemeTheme;
|
||||||
} else {
|
} else {
|
||||||
// Fallback to wallpaper
|
// Fallback to wallpaper if DE color scheme is unavailable
|
||||||
try {
|
try {
|
||||||
theme = await generateFromWallpaper();
|
theme = await generateFromWallpaper();
|
||||||
} catch (wallpaperErr) {
|
} catch (wallpaperErr) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Could not infer system colors or wallpaper. ${wallpaperErr instanceof Error ? wallpaperErr.message : String(wallpaperErr)}`
|
`Could not read system color scheme or wallpaper. ${wallpaperErr instanceof Error ? wallpaperErr.message : String(wallpaperErr)}`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user