Skip to content

DeepSeek Harness Tauri 桌面版 | Only 8mb installer, zero environment setup, preset plugins, Windows / macOS / Linux.

fix(desktop): 首次进入主题默认跟随系统

hairyf5e5009ee22b7
diff --git a/src-tauri/src/config/theme.rs b/src-tauri/src/config/theme.rs
index e949c3ff..d19cbd33 100644
--- a/src-tauri/src/config/theme.rs
+++ b/src-tauri/src/config/theme.rs
@@ -15,11 +15,15 @@ pub enum DshTheme {
System,
}
-const DEFAULT_THEME: DshTheme = DshTheme::Dark;
+/// 未设置过主题偏好时的回退值:跟随系统外观。
+///
+/// 首次进入(`settings.yaml` 不存在)或偏好值缺失/非法时都走这里;回退成固定
+/// 深色会让「跟随系统」的默认体验失效(浅色系统上启动即深色)。
+const DEFAULT_THEME: DshTheme = DshTheme::System;
static LAST_EMITTED: OnceLock<Mutex<Option<DshTheme>>> = OnceLock::new();
-/// 读取 dsh 主题偏好;settings.yaml 缺失或解析失败时回退为深色
+/// 读取 dsh 主题偏好;settings.yaml 缺失或解析失败时回退为跟随系统
pub fn get_dsh_theme(app_handle: &AppHandle) -> DshTheme {
let settings_path = get_dsh_data_path(app_handle).join("settings.yaml");
let content = match fs::read_to_string(&settings_path) {
@@ -105,3 +109,35 @@ pub fn check_and_emit_theme(app_handle: &AppHandle) {
#[cfg(target_os = "macos")]
apply_window_theme(app_handle, theme);
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ /// 首次进入(`settings.yaml` 不存在)与偏好值非法时都回退「跟随系统」;
+ /// 回退固定深色会让浅色系统上的默认外观失效(首次进入即深色)。
+ #[test]
+ fn default_theme_follows_system() {
+ assert_eq!(DEFAULT_THEME, DshTheme::System);
+ }
+
+ #[test]
+ fn parses_known_preference_values() {
+ let cases = [
+ ("ui-theme:\n preference: dark\n", DshTheme::Dark),
+ ("ui-theme:\n preference: light\n", DshTheme::Light),
+ ("ui-theme:\n preference: system\n", DshTheme::System),
+ ];
+ for (content, expected) in cases {
+ assert_eq!(parse_theme_preference(content), Some(expected), "content={content:?}");
+ }
+ }
+
+ /// 未知取值与整段缺失都返回 `None`,由调用方回退 `DEFAULT_THEME`。
+ #[test]
+ fn rejects_unknown_and_missing_preference() {
+ assert_eq!(parse_theme_preference("ui-theme:\n preference: blue\n"), None);
+ assert_eq!(parse_theme_preference("ui-theme:\n density: compact\n"), None);
+ assert_eq!(parse_theme_preference("editor:\n fontSize: 12\n"), None);
+ }
+}
diff --git a/src/hooks/use-theme-adaptive.ts b/src/hooks/use-theme-adaptive.ts
index 4207556e..657bb74c 100644
--- a/src/hooks/use-theme-adaptive.ts
+++ b/src/hooks/use-theme-adaptive.ts
@@ -6,7 +6,11 @@ export function useThemeAdaptive() {
const themePreference = useInvoke<'dark' | 'light' | 'system'>('get_dsh_theme')
const [dshStyle] = useDshStyle()
const systemDark = usePreferredDark()
- const theme = themePreference === 'system' ? (systemDark ? 'dark' : 'light') : themePreference || 'dark'
+ // 显式偏好优先;`system` 与尚未取到偏好(useInvoke 首次返回 undefined)都按系统
+ // 偏好折算——首次进入(settings.yaml 不存在)时后端回退 `system`,这里不能先落深色。
+ const theme = themePreference === 'light' || themePreference === 'dark'
+ ? themePreference
+ : (systemDark ? 'dark' : 'light')
useWatch(
dshStyle.colorScheme || theme,
value => document.documentElement.dataset.theme = value,