Skip to content

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

refactor(hooks): extract theme adaptive and wakelock release hooks

hairyf296ca42c615e
diff --git a/src/hooks/use-theme-adaptive.ts b/src/hooks/use-theme-adaptive.ts
new file mode 100644
index 00000000..4207556e
--- /dev/null
+++ b/src/hooks/use-theme-adaptive.ts
@@ -0,0 +1,15 @@
+import { usePreferredDark, useWatch } from '@reause/core'
+import { useDshStyle } from './use-dsh-style'
+import { useInvoke } from './use-invoke'
+
+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'
+ useWatch(
+ dshStyle.colorScheme || theme,
+ value => document.documentElement.dataset.theme = value,
+ { immediate: true },
+ )
+}
diff --git a/src/pet/hooks/use-wakelock-release.ts b/src/hooks/use-wakelock-release.ts
similarity index 100%
rename from src/pet/hooks/use-wakelock-release.ts
rename to src/hooks/use-wakelock-release.ts
diff --git a/src/layout/index.tsx b/src/layout/index.tsx
index 604f7d58..5c0a248d 100644
--- a/src/layout/index.tsx
+++ b/src/layout/index.tsx
@@ -1,13 +1,15 @@
-import type { DshPlugin, DshThemePreference, ResolvedTheme } from '@/types'
-import { useEventListener, useIntervalFn, useMount, usePreferredDark, useWakeLock, useWatch } from '@reause/core'
+import type { DshPlugin } from '@/types'
+import { useEventListener, useIntervalFn, useMount, useWatch } from '@reause/core'
import { useQueryClient } from '@tanstack/react-query'
import { invoke } from '@tauri-apps/api/core'
-import { useRef, useState } from 'react'
+import { useRef } from 'react'
import { useTranslation } from 'react-i18next'
import { If } from 'react-if-lite'
import { useStore } from 'valtio-define'
import { queryKeys } from '@/config/query-keys'
import { useListen } from '@/hooks/use-listen'
+import { useThemeAdaptive } from '@/hooks/use-theme-adaptive'
+import { useWakelockRelease } from '@/hooks/use-wakelock-release'
import { useCoreBreakingConfirm } from '@/ui/config/hooks/use-core-breaking-confirm'
import { toast } from '@/utils/toast'
import { store } from '../store'
@@ -25,46 +27,7 @@ interface DownloadFinishedPayload {
success: boolean
}
-/**
- * 应用根布局:只负责首次启动、壳层结构与若干后台副作用。
- *
- * 业务状态与操作方法全部收敛到 valtio-define store,各子组件自行订阅 store,
- * 不再通过 props 透传回调与状态;弹出层(关于 / 检查更新 / 应用配置 / 插件异常修复)
- * 统一由 overlastic 命令式打开,仅在需要时挂载。
- *
- * 原先独立的后台组件已内置到本组件(都只产出副作用或一个 holder,
- * 或需要「无论哪个面板挂载都在跑」的全局性,不值得各占一个文件):
- * - 桌面端更新轮询(低频 `useIntervalFn`,失败静默);
- * - 下载完成提示(订阅 `harness-download-finished`,事件到手即弹 toast);
- * - 核心更新提示(`useWatch` 观察更新状态,返回 `useCoreBreakingConfirm` 的 holder);
- * - 外壳主题同步(订阅 `dsh-theme-updated` + 系统明暗,写 `<html data-theme>`);
- * - 插件列表缓存同步(订阅 `dsh-plugins-updated`,写入 react-query 缓存,
- * 供插件面板 / 配置对话框角标 / 导航栏共用)。
- */
export function App() {
- // —— 外壳主题 ——
- // dsh 把主题偏好持久化在 `$DSH_HOME/settings.yaml` 的 `ui-theme.preference`
- // (light/dark/system),后端轮询到变化后经 `dsh-theme-updated` 推送;这里解析为
- // 最终主题写到 `<html data-theme="...">`,由 CSS 变量切换配色。
- // - 系统明暗由 reause `usePreferredDark` 实时订阅媒体查询;
- // - 首次偏好 `useMount` 拉取,后续变化 `useListen` 订阅;
- // - 主题落盘 `<html data-theme>` 用 `useWatch` 观察最终主题。
- const [themePreference, setThemePreference] = useState<DshThemePreference>('dark')
- const systemDark = usePreferredDark()
- const theme: ResolvedTheme = themePreference === 'system' ? (systemDark ? 'dark' : 'light') : themePreference
-
- useMount(() => {
- invoke<DshThemePreference>('get_dsh_theme')
- .then(setThemePreference)
- .catch(err => console.error('[App] failed to load theme:', err))
- })
-
- useListen<DshThemePreference>('dsh-theme-updated', event => setThemePreference(event.payload))
-
- useWatch(theme, (value) => {
- document.documentElement.dataset.theme = value
- }, { immediate: true })
-
// —— 插件列表缓存同步 ——
// 后端(`service/plugin/watch`)解析出完整插件列表后经 `dsh-plugins-updated` 推送,
// 这里直接写入查询缓存(事件载荷即完整列表,无需额外往返拉取)。缓存由所有消费者
@@ -81,10 +44,8 @@ export function App() {
// issue #469:本应用没有屏幕常亮的正当需求(常驻播放的 <video> 会让系统无法息屏),
// 唤醒锁一旦生效就立刻释放。
- const wakelock = useWakeLock()
- useWatch(wakelock.isActive, () => {
- void wakelock.release()
- }, { immediate: true })
+ useWakelockRelease()
+ useThemeAdaptive()
// 仅开发模式:快捷键预览「插件异常修复界面」,便于快速看到实际 UI(不影响生产构建)。
// Ctrl+Shift+1 → 运行期异常对话框(应用仍在运行)
diff --git a/src/pet/app.tsx b/src/pet/app.tsx
index 54525182..9f2ca0ce 100644
--- a/src/pet/app.tsx
+++ b/src/pet/app.tsx
@@ -6,12 +6,12 @@ import { If } from 'react-if-lite'
import { useOmitIgnoreCursorEvents } from '@/hooks/use-omit-ignore-cursor-events'
import { useWindowDraggable } from '@/hooks/use-window-draggable'
import { Hint } from '@/ui/pet/hint'
+import { useWakelockRelease } from '../hooks/use-wakelock-release'
import { PET_BASE_WIDTH, PET_DSH_ASPECT } from './constants'
import { useBubbleTracker } from './hooks/use-bubble-tracker'
import { usePetSource } from './hooks/use-pet-source'
import { normalizeSizePercent, usePetStatus } from './hooks/use-pet-status'
import { usePetWindowSize } from './hooks/use-pet-window'
-import { useWakelockRelease } from './hooks/use-wakelock-release'
import { reportPetIssue } from './utils/log'
/**
diff --git a/src/types/index.ts b/src/types/index.ts
index 4b96318b..a395d487 100644
--- a/src/types/index.ts
+++ b/src/types/index.ts
@@ -1,4 +1,3 @@
export * from './core'
export * from './plugin'
export * from './profile'
-export * from './theme'
diff --git a/src/types/theme.ts b/src/types/theme.ts
deleted file mode 100644
index ffdc3291..00000000
--- a/src/types/theme.ts
+++ /dev/null
@@ -1,5 +0,0 @@
-/** dsh 侧持久化的主题偏好(`$DSH_HOME/settings.yaml` 的 `ui-theme.preference`) */
-export type DshThemePreference = 'dark' | 'light' | 'system'
-
-/** 解析后的最终主题:落到 `<html data-theme>`,由 CSS 变量切换配色 */
-export type ResolvedTheme = 'dark' | 'light'