Skip to content

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

feat(web): shim iframe notifications and window visibility

hairyf9f4568c31219
diff --git a/package.json b/package.json
index e3dbf247..f983e088 100644
--- a/package.json
+++ b/package.json
@@ -28,6 +28,7 @@
"react-dom": "^19.2.8",
"react-i18next": "^17.0.11",
"react-if-lite": "^0.1.0",
+ "react-use": "^17.6.1",
"tailwind-variants": "^3.3.1",
"valtio-define": "^1.4.1"
},
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 99aea195..dcfb18ae 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -49,6 +49,9 @@ importers:
react-if-lite:
specifier: ^0.1.0
version: 0.1.0([email protected])
+ react-use:
+ specifier: ^17.6.1
tailwind-variants:
specifier: ^3.3.1
diff --git a/src/components/harness-webview.tsx b/src/components/harness-webview.tsx
index 607d8141..9009cd4e 100644
--- a/src/components/harness-webview.tsx
+++ b/src/components/harness-webview.tsx
@@ -1,7 +1,9 @@
/* eslint-disable react/dom-no-unsafe-iframe-sandbox */
import { CircleExclamation } from '@gravity-ui/icons'
+import { useRef } from 'react'
import { useTranslation } from 'react-i18next'
import { useStore } from 'valtio-define'
+import { useIframeShim } from '@/hooks/use-iframe-shim'
import { harness } from '../store/modules/harness'
import Loadable from './loadable'
import PreinstallSetup from './preinstall-setup'
@@ -23,6 +25,10 @@ export default function HarnessWebview() {
serviceUrl,
} = useStore(harness)
+ const iframeRef = useRef<HTMLIFrameElement>(null)
+
+ useIframeShim(iframeRef)
+
if (status === 'error') {
return (
<main className="relative flex-1 bg-canvas">
@@ -54,9 +60,10 @@ export default function HarnessWebview() {
? (
<iframe
key={iframeKey}
+ ref={iframeRef}
className="block h-full w-full border-none bg-load-bg"
src={iframeSrc}
- allow="clipboard-read; clipboard-write; camera; microphone; geolocation; display-capture; autoplay; encrypted-media; fullscreen; notifications"
+ allow="clipboard-read; clipboard-write; camera; microphone; geolocation; display-capture; autoplay; encrypted-media; fullscreen; notifications *"
sandbox="allow-same-origin allow-scripts allow-popups allow-forms allow-modals allow-downloads allow-storage-access-by-user-activation"
onLoad={harness.markIframeLoaded}
onError={harness.markIframeError}
diff --git a/src/hooks/use-iframe-shim.ts b/src/hooks/use-iframe-shim.ts
new file mode 100644
index 00000000..9088510c
--- /dev/null
+++ b/src/hooks/use-iframe-shim.ts
@@ -0,0 +1,133 @@
+import type { RefObject } from 'react'
+import { invoke } from '@tauri-apps/api/core'
+import { listen } from '@tauri-apps/api/event'
+import { getCurrentWindow } from '@tauri-apps/api/window'
+import { useEffect } from 'react'
+import { useEvent, useInterval, useMountedState } from 'react-use'
+
+interface NativeNotificationMessage {
+ source?: 'dsh-notification-bridge'
+ type?: string
+ title?: string
+ body?: string
+ tag?: string
+ sessionId?: string
+ requireInteraction?: boolean
+}
+
+export function useIframeShim(iframeRef: RefObject<HTMLIFrameElement | null>) {
+ const isMounted = useMountedState()
+
+ // 接收 iframe 的「原生通知」请求,转发给 Tauri 命令弹出系统通知
+ function handleMessage(event: MessageEvent<NativeNotificationMessage>) {
+ const data = event.data
+ if (!data || typeof data !== 'object' || data.source !== 'dsh-notification-bridge') {
+ return
+ }
+ // 只接受 DSH 直接 iframe 发来的消息;不兼容多层嵌套 iframe。
+ if (event.source !== iframeRef.current?.contentWindow) {
+ return
+ }
+ if (data.type !== 'dsh://native-notification') {
+ return
+ }
+ void invoke('show_native_notification', {
+ payload: {
+ title: data.title ?? '',
+ body: data.body ?? '',
+ tag: data.tag ?? null,
+ sessionId: data.sessionId ?? null,
+ requireInteraction: Boolean(data.requireInteraction),
+ },
+ }).catch(error => console.error('[notification] show_native_notification failed:', error))
+ }
+
+ useEvent('message', handleMessage)
+
+ // 系统通知点击 → 通知 iframe 聚焦对应会话
+ useEffect(() => {
+ let unlisten: (() => void) | undefined
+ void listen<{ sessionId?: string | null, title?: string, tag?: string }>(
+ 'dsh-notification-clicked',
+ (event) => {
+ const payload = event.payload
+ iframeRef.current?.contentWindow?.postMessage(
+ {
+ type: 'dsh://focus-session',
+ sessionId: payload.sessionId || undefined,
+ title: payload.title || undefined,
+ tag: payload.tag || undefined,
+ },
+ '*',
+ )
+ },
+ ).then((unlistener) => {
+ // 订阅完成前若已卸载则立即释放,避免回调泄漏
+ if (isMounted()) {
+ unlisten = unlistener
+ }
+ else {
+ unlistener()
+ }
+ })
+ return () => {
+ unlisten?.()
+ }
+ // eslint-disable-next-line react/exhaustive-deps
+ }, [])
+
+ // 将窗口可见性(最小化/隐藏/失焦)同步给 iframe,便于其暂停渲染
+ function syncVisibility() {
+ void (async () => {
+ try {
+ const appWindow = getCurrentWindow()
+ const [minimized, visible] = await Promise.all([
+ appWindow.isMinimized(),
+ appWindow.isVisible(),
+ ])
+ const hidden = minimized || !visible
+ iframeRef.current?.contentWindow?.postMessage(
+ { type: 'dsh://visibility-state', hidden },
+ '*',
+ )
+ }
+ catch (error) {
+ console.error('[notification] sync visibility failed:', error)
+ }
+ })()
+ }
+
+ // 窗口焦点/尺寸变化时即时同步可见性
+ useEffect(() => {
+ let unlisteners: Array<() => void> = []
+ void (async () => {
+ try {
+ const appWindow = getCurrentWindow()
+ syncVisibility()
+ const unFocus = await appWindow.onFocusChanged(() => {
+ void syncVisibility()
+ })
+ const unResized = await appWindow.onResized(() => {
+ void syncVisibility()
+ })
+ if (isMounted()) {
+ unlisteners = [unFocus, unResized]
+ }
+ else {
+ unFocus()
+ unResized()
+ }
+ }
+ catch (error) {
+ console.error('[notification] visibility listeners failed:', error)
+ }
+ })()
+ return () => {
+ unlisteners.forEach(fn => fn())
+ }
+ // eslint-disable-next-line react/exhaustive-deps
+ }, [])
+
+ // 兜底轮询:覆盖监听不到的状态变化(如任务栏切换)
+ useInterval(syncVisibility, 1000)
+}