Skip to content

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

feat(theme): make desktop shell follow dsh page theme

hairyfec0f045e170d
diff --git a/src-tauri/src/bridge/cmd.rs b/src-tauri/src/bridge/cmd.rs
index 42ae5c7b..3f7e4873 100644
--- a/src-tauri/src/bridge/cmd.rs
+++ b/src-tauri/src/bridge/cmd.rs
@@ -213,3 +213,9 @@ pub fn set_language(app_handle: AppHandle, lang: String) {
pub async fn toggle_sidebar() -> Result<bool, String> {
Ok(true)
}
+
+/// 当前 dsh 主题偏好(light/dark/system),用于让桌面外壳跟随内嵌页面主题
+#[tauri::command]
+pub fn get_dsh_theme(app_handle: AppHandle) -> config::theme::DshTheme {
+ config::theme::get_dsh_theme(&app_handle)
+}
diff --git a/src-tauri/src/config/mod.rs b/src-tauri/src/config/mod.rs
index 4b566bf8..5b8f52a8 100644
--- a/src-tauri/src/config/mod.rs
+++ b/src-tauri/src/config/mod.rs
@@ -3,9 +3,11 @@ mod format;
pub mod i18n;
mod runtime;
mod setting;
+mod theme;
mod utils;
pub use constants::*;
pub use format::*;
pub use runtime::*;
pub use setting::*;
+pub use theme::*;
diff --git a/src-tauri/src/config/theme.rs b/src-tauri/src/config/theme.rs
new file mode 100644
index 00000000..1dab5630
--- /dev/null
+++ b/src-tauri/src/config/theme.rs
@@ -0,0 +1,76 @@
+use std::fs;
+use std::sync::{Mutex, OnceLock};
+use tauri::{AppHandle, Emitter};
+
+use super::runtime::get_dsh_data_path;
+
+/// dsh 主题偏好(对应 `$DSH_HOME/settings.yaml` 的 `ui-theme.preference`)
+#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
+#[serde(rename_all = "lowercase")]
+pub enum DshTheme {
+ Dark,
+ Light,
+ System,
+}
+
+const DEFAULT_THEME: DshTheme = DshTheme::Dark;
+
+static LAST_EMITTED: OnceLock<Mutex<Option<DshTheme>>> = OnceLock::new();
+
+/// 读取 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) {
+ Ok(content) => content,
+ Err(err) => {
+ log::debug!("failed to read dsh settings.yaml: {}", err);
+ return DEFAULT_THEME;
+ }
+ };
+ parse_theme_preference(&content).unwrap_or(DEFAULT_THEME)
+}
+
+/// 从 settings.yaml 文本中提取 `ui-theme.preference`(light/dark/system)
+fn parse_theme_preference(content: &str) -> Option<DshTheme> {
+ let mut in_ui_theme = false;
+ for line in content.lines() {
+ let trimmed = line.trim();
+ if trimmed.starts_with("ui-theme:") {
+ in_ui_theme = true;
+ continue;
+ }
+ if !in_ui_theme {
+ continue;
+ }
+ // ui-theme 段结束(遇到无缩进的新顶层 key)
+ if !line.starts_with(' ') && !line.starts_with('\t') {
+ return None;
+ }
+ if !trimmed.is_empty() && !trimmed.starts_with('#') {
+ if let Some(value) = trimmed.strip_prefix("preference:") {
+ return match value.trim() {
+ "light" => Some(DshTheme::Light),
+ "dark" => Some(DshTheme::Dark),
+ "system" => Some(DshTheme::System),
+ _ => None,
+ };
+ }
+ }
+ }
+ None
+}
+
+/// 主题偏好变化时向前端推送 `dsh-theme-updated` 事件(仅在变化时触发一次)
+pub fn check_and_emit_theme(app_handle: &AppHandle) {
+ let theme = get_dsh_theme(app_handle);
+ let mut last = LAST_EMITTED
+ .get_or_init(|| Mutex::new(None))
+ .lock()
+ .unwrap();
+ if *last == Some(theme) {
+ return;
+ }
+ *last = Some(theme);
+ log::debug!("dsh theme preference changed: {:?}", theme);
+ let _ = app_handle.emit("dsh-theme-updated", &theme);
+}
diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs
index e1150503..10696801 100644
--- a/src-tauri/src/lib.rs
+++ b/src-tauri/src/lib.rs
@@ -106,6 +106,7 @@ fn handler() -> impl Fn(Invoke<Wry>) -> bool + Send + Sync + 'static {
bridge::cmd::clear_service_logs,
bridge::cmd::set_language,
bridge::cmd::toggle_sidebar,
+ bridge::cmd::get_dsh_theme,
]
}
diff --git a/src-tauri/src/service/scheduler/mod.rs b/src-tauri/src/service/scheduler/mod.rs
index 6f6704e0..b8cfc0cd 100644
--- a/src-tauri/src/service/scheduler/mod.rs
+++ b/src-tauri/src/service/scheduler/mod.rs
@@ -17,6 +17,7 @@ async fn scheduler_permanent_loop(app_handle: AppHandle) {
crate::task::tick_check_dsh_process::trigger(app_handle.clone())
.await
.unwrap();
+ crate::config::theme::check_and_emit_theme(app_handle.clone());
interval.tick().await;
}
}
diff --git a/src/App.tsx b/src/App.tsx
index d89e9658..24242d95 100644
--- a/src/App.tsx
+++ b/src/App.tsx
@@ -6,6 +6,7 @@ import SetupScreen, { InstallProgress, SetupStatus } from "./components/SetupScr
import SidebarPanel from "./components/SidebarPanel";
import { useI18n } from "./i18n/context";
import { generateTimestampedUrl } from "./hooks/useAutoSync";
+import { useDshTheme } from "./hooks/useDshTheme";
const MAX_RETRIES = 8;
@@ -28,6 +29,7 @@ const btnPrimary =
export default function App() {
const { t } = useI18n();
+ useDshTheme();
const [status, setStatus] = useState<SetupStatus>("checking");
const [installer, setInstaller] = useState<InstallerState>(initialInstaller);
const [errorMsg, setErrorMsg] = useState("");
@@ -227,7 +229,7 @@ export default function App() {
<button
onClick={handleToggleSidebar}
title={t("ui.settings")}
- className={`fixed top-4 z-40 flex h-9 w-9 cursor-pointer items-center justify-center rounded-md border border-line bg-panel2 text-ink shadow-lg transition-all duration-200 hover:bg-[#26262d] ${
+ className={`fixed top-4 z-40 flex h-9 w-9 cursor-pointer items-center justify-center rounded-md border border-line bg-panel2 text-ink shadow-lg transition-all duration-200 hover:bg-panel-hover ${
sidebarOpen ? "right-[316px]" : "right-4"
}`}
>
@@ -299,7 +301,7 @@ export default function App() {
<button
onClick={handleToggleSidebar}
title={t("ui.settings")}
- className={`fixed top-4 z-40 flex h-9 w-9 cursor-pointer items-center justify-center rounded-md border border-line bg-panel2 text-ink shadow-lg transition-all duration-200 hover:bg-[#26262d] ${
+ className={`fixed top-4 z-40 flex h-9 w-9 cursor-pointer items-center justify-center rounded-md border border-line bg-panel2 text-ink shadow-lg transition-all duration-200 hover:bg-panel-hover ${
sidebarOpen ? "right-[316px]" : "right-4"
}`}
>
diff --git a/src/components/SetupScreen.tsx b/src/components/SetupScreen.tsx
index 9b229724..266160ab 100644
--- a/src/components/SetupScreen.tsx
+++ b/src/components/SetupScreen.tsx
@@ -74,9 +74,9 @@ export default function SetupScreen({
<span className="min-w-[44px] text-right text-[13px] font-semibold tabular-nums text-accent2">{Math.round(percentage)}%</span>
</div>
- <div className="mb-5 min-h-[112px] max-h-[184px] overflow-y-auto rounded-lg border border-line bg-[#0a0a0c] px-3.5 py-2.5 text-left font-mono text-xs leading-[1.7]" aria-label={t("ui.install_log")}>
+ <div className="mb-5 min-h-[112px] max-h-[184px] overflow-y-auto rounded-lg border border-line bg-log-bg px-3.5 py-2.5 text-left font-mono text-xs leading-[1.7]" aria-label={t("ui.install_log")}>
{visibleLogs.slice(-LOG_LIMIT).map((line, index) => (
- <p key={`${line}-${index}`} className="m-0 flex gap-2 overflow-hidden text-ellipsis whitespace-nowrap text-[#b8b8c0]">
+ <p key={`${line}-${index}`} className="m-0 flex gap-2 overflow-hidden text-ellipsis whitespace-nowrap text-log-ink">
<span className="shrink-0 text-accent select-none">›</span>
<span className="min-w-0 overflow-hidden text-ellipsis">{line}</span>
</p>
diff --git a/src/components/SidebarPanel.tsx b/src/components/SidebarPanel.tsx
index b6c06789..ef95dea3 100644
--- a/src/components/SidebarPanel.tsx
+++ b/src/components/SidebarPanel.tsx
@@ -37,7 +37,7 @@ export default function SidebarPanel({
}: SidebarPanelProps) {
const { t, language, setLanguage } = useI18n();
const btnBase =
- "inline-flex cursor-pointer items-center justify-center rounded-md border border-line bg-panel2 px-2 py-1 text-xs text-ink transition-colors hover:border-[#3a3a44] hover:bg-[#26262d] disabled:cursor-not-allowed disabled:opacity-55";
+ "inline-flex cursor-pointer items-center justify-center rounded-md border border-line bg-panel2 px-2 py-1 text-xs text-ink transition-colors hover:border-line-strong hover:bg-panel-hover disabled:cursor-not-allowed disabled:opacity-55";
const btnPrimary = `${btnBase} border-accent bg-accent text-white hover:border-accent2 hover:bg-accent2`;
const btnDanger = `${btnBase} border-[rgba(229,72,77,0.4)] text-danger`;
const btnBlock = " mt-1.5 w-full";
@@ -248,7 +248,7 @@ export default function SidebarPanel({
↻
</button>
</h3>
- <pre className="m-0 max-h-[200px] overflow-auto whitespace-pre-wrap break-all rounded-md border border-line bg-[#0a0a0c] px-2 py-2 text-[11px] leading-[1.45] text-[#b8b8c0]">{logs || t("ui.no_logs")}</pre>
+ <pre className="m-0 max-h-[200px] overflow-auto whitespace-pre-wrap break-all rounded-md border border-line bg-log-bg px-2 py-2 text-[11px] leading-[1.45] text-log-ink">{logs || t("ui.no_logs")}</pre>
<button className={btnBase} onClick={clearLogs}>
{t("buttons.clear_logs")}
</button>
diff --git a/src/hooks/useDshTheme.ts b/src/hooks/useDshTheme.ts
new file mode 100644
index 00000000..3da88abf
--- /dev/null
+++ b/src/hooks/useDshTheme.ts
@@ -0,0 +1,73 @@
+import { useEffect, useRef, useState } from "react";
+import { invoke } from "@tauri-apps/api/core";
+import { listen, UnlistenFn } from "@tauri-apps/api/event";
+
+export type DshThemePreference = "dark" | "light" | "system";
+export type ResolvedTheme = "dark" | "light";
+
+const DARK_QUERY = "(prefers-color-scheme: dark)";
+
+function resolveTheme(preference: DshThemePreference): ResolvedTheme {
+ if (preference === "system") {
+ return window.matchMedia(DARK_QUERY).matches ? "dark" : "light";
+ }
+ return preference;
+}
+
+/**
+ * 让桌面外壳(侧边栏、设置页等)跟随内嵌 dsh 页面的主题。
+ *
+ * dsh 把主题偏好持久化在 `$DSH_HOME/settings.yaml` 的 `ui-theme.preference`
+ * (light/dark/system),后端轮询到变化后通过 `dsh-theme-updated` 事件推送,
+ * 这里解析为最终主题并写到 `<html data-theme="...">`,由 CSS 变量切换配色。
+ */
+export function useDshTheme(): ResolvedTheme {
+ const [theme, setTheme] = useState<ResolvedTheme>("dark");
+ const preferenceRef = useRef<DshThemePreference>("dark");
+
+ useEffect(() => {
+ let unlisten: UnlistenFn | null = null;
+ let disposed = false;
+
+ const apply = (preference: DshThemePreference) => {
+ preferenceRef.current = preference;
+ if (!disposed) {
+ setTheme(resolveTheme(preference));
+ }
+ };
+
+ invoke<DshThemePreference>("get_dsh_theme")
+ .then((preference) => apply(preference))
+ .catch((err) => console.error("[useDshTheme] failed to load theme:", err));
+
+ listen<DshThemePreference>("dsh-theme-updated", (event) => {
+ apply(event.payload);
+ }).then((fn) => {
+ if (disposed) {
+ fn();
+ } else {
+ unlisten = fn;
+ }
+ });
+
+ const mediaQuery = window.matchMedia(DARK_QUERY);
+ const onSystemThemeChange = () => {
+ if (preferenceRef.current === "system") {
+ setTheme(resolveTheme("system"));
+ }
+ };
+ mediaQuery.addEventListener("change", onSystemThemeChange);
+
+ return () => {
+ disposed = true;
+ mediaQuery.removeEventListener("change", onSystemThemeChange);
+ unlisten?.();
+ };
+ }, []);
+
+ useEffect(() => {
+ document.documentElement.dataset.theme = theme;
+ }, [theme]);
+
+ return theme;
+}
diff --git a/src/style/main.css b/src/style/main.css
index a7c2f998..284d2c54 100644
--- a/src/style/main.css
+++ b/src/style/main.css
@@ -4,6 +4,41 @@
@import "./components/scrollbar.css";
+/* 主题变量:默认深色,html[data-theme="light"] 切换到浅色 */
+:root {
+ --color-canvas: #0d0d0f;
+ --color-panel: #151518;
+ --color-panel-2: #1c1c21;
+ --color-panel-hover: #26262d;
+ --color-line: #2a2a31;
+ --color-line-strong: #3a3a44;
+ --color-ink: #e8e8ea;
+ --color-muted: #9a9aa3;
+ --color-accent: #4d6bfe;
+ --color-accent-2: #6e8bff;
+ --color-danger: #e5484d;
+ --color-ok: #46a758;
+ --color-log-bg: #0a0a0c;
+ --color-log-ink: #b8b8c0;
+}
+
+html[data-theme="light"] {
+ --color-canvas: #f5f6f8;
+ --color-panel: #ffffff;
+ --color-panel-2: #eef0f4;
+ --color-panel-hover: #e5e8ee;
+ --color-line: #e2e5ea;
+ --color-line-strong: #c9ced8;
+ --color-ink: #1b1d21;
+ --color-muted: #636a73;
+ --color-accent: #3b5bfe;
+ --color-accent-2: #5b76ff;
+ --color-danger: #d83a42;
+ --color-ok: #2f9e44;
+ --color-log-bg: #fafbfc;
+ --color-log-ink: #40454d;
+}
+
@layer base {
html,
body,
diff --git a/tailwind.config.js b/tailwind.config.js
index 7c756162..0aa0672a 100644
--- a/tailwind.config.js
+++ b/tailwind.config.js
@@ -4,16 +4,22 @@ export default {
theme: {
extend: {
colors: {
- canvas: "#0d0d0f",
- panel: "#151518",
- panel2: "#1c1c21",
- line: "#2a2a31",
- ink: "#e8e8ea",
- muted: "#9a9aa3",
- accent: "#4d6bfe",
- accent2: "#6e8bff",
- danger: "#e5484d",
- ok: "#46a758",
+ // 主题色通过 CSS 变量定义(见 src/style/main.css),
+ // 由 <html data-theme="light"> 在浅色/深色之间切换
+ canvas: "var(--color-canvas)",
+ panel: "var(--color-panel)",
+ panel2: "var(--color-panel-2)",
+ "panel-hover": "var(--color-panel-hover)",
+ line: "var(--color-line)",
+ "line-strong": "var(--color-line-strong)",
+ ink: "var(--color-ink)",
+ muted: "var(--color-muted)",
+ accent: "var(--color-accent)",
+ accent2: "var(--color-accent-2)",
+ danger: "var(--color-danger)",
+ ok: "var(--color-ok)",
+ "log-bg": "var(--color-log-bg)",
+ "log-ink": "var(--color-log-ink)",
},
fontFamily: {
sans: [