Skip to content

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

fix(pet): keep pet DOM mounted across hide/show and cache builtin asset root

hairyf24cc1fd2718f
diff --git a/src-tauri/src/bridge/pet.rs b/src-tauri/src/bridge/pet.rs
index 669a522f..dc4a8f5f 100644
--- a/src-tauri/src/bridge/pet.rs
+++ b/src-tauri/src/bridge/pet.rs
@@ -646,12 +646,32 @@ fn builtin_asset_file(name: &str) -> Option<(&'static str, &'static str)> {
})
}
+/// 内置资源目录(canonicalize 后)的进程级缓存。
+///
+/// `dsh-pet://` 协议对每个视频的加载请求都会走 `resolve_builtin_asset_path`,
+/// 而 debug 构建下 `bundled_plugin_dir` 每次都会全量重扫 `packages/*` 并解析
+/// 每个包的 package.json(`discover_dev_internal_plugins`),11 个内置资产 +
+/// config 的几十次请求会把初次加载拖慢到数秒~20 秒;内置资源运行期只读且目录
+/// 结构固定(首装后不变),缓存一次即可。
+fn builtin_assets_root(app: &AppHandle) -> Result<&'static PathBuf, String> {
+ static ROOT: OnceLock<Result<PathBuf, String>> = OnceLock::new();
+ ROOT.get_or_init(|| {
+ let plugin = match crate::service::plugin::bundled_plugin_dir(app, "dsh-tauri-pet") {
+ Some(plugin) => plugin,
+ None => {
+ return Err("PET_BUILTIN_ASSET_NOT_FOUND: dsh-tauri-pet is not deployed".to_string())
+ }
+ };
+ plugin.join("assets").canonicalize().map_err(|error| {
+ format!("PET_BUILTIN_ASSET_ROOT_FAILED: failed to resolve assets directory: {error}")
+ })
+ })
+ .as_ref()
+ .map_err(|error| error.clone())
+}
+
fn resolve_builtin_asset_path(app: &AppHandle, file: &str) -> Result<PathBuf, String> {
- let plugin = crate::service::plugin::bundled_plugin_dir(app, "dsh-tauri-pet")
- .ok_or_else(|| "PET_BUILTIN_ASSET_NOT_FOUND: dsh-tauri-pet is not deployed".to_string())?;
- let assets_root = plugin.join("assets").canonicalize().map_err(|error| {
- format!("PET_BUILTIN_ASSET_ROOT_FAILED: failed to resolve assets directory: {error}")
- })?;
+ let assets_root = builtin_assets_root(app)?;
let candidate = assets_root.join(file);
let link_metadata = fs::symlink_metadata(&candidate).map_err(|error| {
format!("PET_BUILTIN_ASSET_READ_FAILED: failed to inspect {}: {error}", candidate.display())
diff --git a/src/pet/components/pet.tsx b/src/pet/components/pet.tsx
index 7cacf1f1..1e4c50f9 100644
--- a/src/pet/components/pet.tsx
+++ b/src/pet/components/pet.tsx
@@ -203,6 +203,19 @@ export function Pet(props: PetProps) {
return () => query.removeEventListener('change', updateMotion)
}, [])
+ useEffect(() => {
+ // 窗口由隐藏恢复显示(主 webview 重新开启宠物)时,WebView2 在隐藏窗口
+ // 上可能暂停媒体播放;这里在 visible 恢复为 true 时重放前台视频,避免
+ // 宠物「显示出来但静止不动」。元素始终常驻 DOM(CSS invisible),无需重载 src。
+ if (!visible)
+ return undefined
+ const front = frontIdxRef.current === 0 ? videoARef.current : videoBRef.current
+ if (front !== null && front.paused && front.src !== '' && !front.error) {
+ void front.play().catch(() => setFailed(true))
+ }
+ return undefined
+ }, [visible])
+
useEffect(() => {
if (isBuiltin)
return undefined
@@ -424,63 +437,70 @@ export function Pet(props: PetProps) {
} as CSSProperties
return (
- <If cond={visible}>
- <main className="pointer-events-none fixed inset-0 flex items-end justify-center overflow-visible" style={style}>
- {/* 视频筐本身不响应事件:可交互面收缩到下方 PET_HIT_BOX 命中区(与 dsh-pet
- .dsh-pet-hit 一致),事件从命中区冒泡到 app.tsx 的 dragRef 壳触发拖拽。 */}
- <div className="pointer-events-none relative h-[calc(var(--pet-width)*var(--pet-aspect))] w-[var(--pet-width)] select-none">
- <If cond={isBuiltin}>
- {/* 双 video 缓冲:前台 opacity-100 淡入、后台 opacity-0 淡出,
- 切换经 loadeddata 就绪后交换(见开关 effect),无空窗/黑帧闪跳。
- 视频均 pointer-events-none,避免截获命中区外的点击。 */}
- <video
- ref={videoARef}
- className={`pointer-events-none absolute inset-0 h-full w-full object-contain transition-opacity duration-200 ${frontIdx === 0 ? 'opacity-100' : 'opacity-0'}`}
- muted
- playsInline
- preload="auto"
- onError={() => setFailed(true)}
- />
- <video
- ref={videoBRef}
- className={`pointer-events-none absolute inset-0 h-full w-full object-contain transition-opacity duration-200 ${frontIdx === 1 ? 'opacity-100' : 'opacity-0'}`}
- muted
- playsInline
- preload="auto"
- onError={() => setFailed(true)}
- />
- </If>
- <If cond={!isBuiltin && hasCustomAsset}>
- <div
- ref={spriteRef}
- className="pointer-events-none absolute inset-0 bg-contain bg-no-repeat"
- style={{
- backgroundImage: customAsset ? `url(${customAsset.spritesheet})` : undefined,
- backgroundSize: customAsset ? `${customAsset.columns * 100}% ${customAsset.rows * 100}%` : undefined,
- }}
- />
- {/* 探测精灵图真实像素尺寸,换算成帧比例供窗口缩放使用;0×0 不可见。 */}
- <img
- className="pointer-events-none absolute h-0 w-0 opacity-0"
- src={customAsset?.spritesheet}
- alt=""
- draggable={false}
- onError={() => setFailed(true)}
- onLoad={handleSpriteLoaded}
- />
- </If>
- <If cond={failed && assets.fallback !== undefined}>
- <img className="pointer-events-none absolute inset-0 h-full w-full object-contain" src={assets.fallback} alt="" draggable={false} />
- </If>
- {/* 命中区:唯一可交互面(拖拽/双击),尺寸与 dsh-pet .dsh-pet-hit 一致。 */}
+ // 保持 <main> 与全部子元素(双 video、命中区)常驻 DOM,visible=false 时仅用
+ // visibility:hidden 隐藏:隐藏/重开(主 webview 关闭宠物再开启)不会重挂载出
+ // 新元素,useDrag 的监听器与视频加载 effect 都只绑定/加载一次,无需 F5 恢复;
+ // 不用 display:none —— useOmitIgnoreCursorEvents 依赖 hitbox 的 getBoundingClientRect
+ // 做鼠标穿透判定,display:none 会得到全 0 rect。窗口本身的显隐由 Rust 的
+ // show/hide 控制,这里只负责 WebView 内部一致性。
+ <main
+ className={`pointer-events-none fixed inset-0 flex items-end justify-center overflow-visible ${visible ? '' : 'invisible'}`}
+ style={style}
+ >
+ {/* 视频筐本身不响应事件:可交互面收缩到下方 PET_HIT_BOX 命中区(与 dsh-pet
+ .dsh-pet-hit 一致),事件从命中区冒泡到 app.tsx 的 dragRef 壳触发拖拽。 */}
+ <div className="pointer-events-none relative h-[calc(var(--pet-width)*var(--pet-aspect))] w-[var(--pet-width)] select-none">
+ <If cond={isBuiltin}>
+ {/* 双 video 缓冲:前台 opacity-100 淡入、后台 opacity-0 淡出,
+ 切换经 loadeddata 就绪后交换(见开关 effect),无空窗/黑帧闪跳。
+ 视频均 pointer-events-none,避免截获命中区外的点击。 */}
+ <video
+ ref={videoARef}
+ className={`pointer-events-none absolute inset-0 h-full w-full object-contain transition-opacity duration-200 ${frontIdx === 0 ? 'opacity-100' : 'opacity-0'}`}
+ muted
+ playsInline
+ preload="auto"
+ onError={() => setFailed(true)}
+ />
+ <video
+ ref={videoBRef}
+ className={`pointer-events-none absolute inset-0 h-full w-full object-contain transition-opacity duration-200 ${frontIdx === 1 ? 'opacity-100' : 'opacity-0'}`}
+ muted
+ playsInline
+ preload="auto"
+ onError={() => setFailed(true)}
+ />
+ </If>
+ <If cond={!isBuiltin && hasCustomAsset}>
<div
- ref={props.hitboxRef}
- className="pointer-events-auto absolute cursor-grab touch-none select-none"
- style={PET_HIT_BOX}
+ ref={spriteRef}
+ className="pointer-events-none absolute inset-0 bg-contain bg-no-repeat"
+ style={{
+ backgroundImage: customAsset ? `url(${customAsset.spritesheet})` : undefined,
+ backgroundSize: customAsset ? `${customAsset.columns * 100}% ${customAsset.rows * 100}%` : undefined,
+ }}
+ />
+ {/* 探测精灵图真实像素尺寸,换算成帧比例供窗口缩放使用;0×0 不可见。 */}
+ <img
+ className="pointer-events-none absolute h-0 w-0 opacity-0"
+ src={customAsset?.spritesheet}
+ alt=""
+ draggable={false}
+ onError={() => setFailed(true)}
+ onLoad={handleSpriteLoaded}
/>
- </div>
- </main>
- </If>
+ </If>
+ <If cond={failed && assets.fallback !== undefined}>
+ <img className="pointer-events-none absolute inset-0 h-full w-full object-contain" src={assets.fallback} alt="" draggable={false} />
+ </If>
+ {/* 命中区:唯一可交互面(拖拽/双击),尺寸与 dsh-pet .dsh-pet-hit 一致。 */}
+ <div
+ ref={props.hitboxRef}
+ className="pointer-events-auto absolute cursor-grab touch-none select-none"
+ style={PET_HIT_BOX}
+ />
+ </div>
+ </main>
)
}