Skip to content

fix(贴图): 修复原生命令运行时失败时无法回退到 WebView 版的问题#367

Open
ExLei wants to merge 2 commits into
mosheng1:mainfrom
ExLei:fix/贴图功能回退修复

Hidden character warning

The head ref may contain hidden characters: "fix/\u8d34\u56fe\u529f\u80fd\u56de\u9000\u4fee\u590d"
Open

fix(贴图): 修复原生命令运行时失败时无法回退到 WebView 版的问题#367
ExLei wants to merge 2 commits into
mosheng1:mainfrom
ExLei:fix/贴图功能回退修复

Conversation

@ExLei

@ExLei ExLei commented May 25, 2026

Copy link
Copy Markdown
Contributor

摘要

修复前端原生 GPU 图像功能的回退逻辑缺陷:去除基于错误字符串匹配的条件判断,改为无条件回退。
虽然只是健壮性修复(实际连回退逻辑都没触发就是了(

问题描述

v0.4.0 官方发行版中,贴图功能无法调用。入口(右键菜单「贴到屏幕」、托盘菜单「贴图」)均无响应,无窗口弹出,无错误提示。

分析

实际测试中发现完整链路存在两层问题。

第一层:gpu-image-viewer 侧窗口不可见但未报错

完整版中调用贴图功能,前端收到成功返回值,toast.success 提示「图片已贴到屏幕」,但实际没有贴图窗口出现。此时前端未进入 catch 分支,回退逻辑未触发。

第二层:前端回退逻辑只匹配特定错误字符串

clipboard.js:pinImageToScreen()contextMenu.js:PreviewController.bind() 中的回退条件:

if (error?.toString?.()?.includes('not found') || error?.toString?.()?.includes('Command')) {
    // 回退到 Tauri WebView 版
}

此条件只命中「命令未注册」场景(社区编译版),未命中「命令已注册但运行时失败」场景(官方发行版):

版本 命令状态 invoke 抛出的错误 回退触发
社区编译版 未注册 "Command ... not found"
官方发行版 已注册,GPU 运行时失败 "创建原生贴图窗口失败: WebGPU adapter not available"
官方发行版 已注册,驱动兼容性失败 "GPU adapter initialization failed: DXGI_ERROR_DEVICE_REMOVED"

官方发行版中 gpu-image-viewer 插件已编译进应用,create_native_pin_from_file / show_native_image_preview 命令已注册。运行时失败时 Rust 命令返回的 Err(String) 不包含 "not found""Command" 关键词,回退条件不满足,pin_image_from_file 永远不会被调用。

Rust 侧 src-tauri/src/windows/tray/menu.rs:272-283 在同一提交(d95a542e)中已实现无条件回退(if let Err(e) = create_native_pin_from_file(…) { eprintln!(…); fallback }),前端两处是此一致模式的仅剩遗留。

两层问题叠加

场景 第一层 第二层 结果
窗口不可见但未报错 前端未进入 catch 回退不触发 ❌ 无窗口,无提示
命令抛错 进入 catch 字符串匹配失败 ❌ 报错但无回退
社区版(命令未注册) 进入 catch 字符串匹配成功 ✅ 回退正常

本 PR 修复第二层。

复现方法

社区版原生命令不存在,无法直接复现。在前端注入 mock 错误模拟 GPU 运行时失败:

// clipboard.js - pinImageToScreen()
throw new Error('GPU adapter initialization failed: DXGI_ERROR_DEVICE_REMOVED')

// contextMenu.js - PreviewController.bind()
throw new Error('GPU adapter initialization failed: DXGI_ERROR_DEVICE_REMOVED')

错误不含 "not found""Command"

复现结果

测试项 未修复版(含 mock) 修复版
右键菜单「贴到屏幕」 ❌ 报错,无窗口弹出 ✅ 回退到 WebView 版,正常贴图
托盘菜单「贴图」 ❌ 报错 ✅ 回退正常
右键菜单图片预览 ❌ 无预览弹出 ✅ 回退到 WebView 预览

修改文件

  • src/shared/api/clipboard.jspinImageToScreen() 回退逻辑,补日志,双重失败时合并错误信息
  • src/plugins/context_menu/contextMenu.jsPreviewController.bind() 预览回退逻辑,WebView 版失败时补日志
  • src/shared/utils/contextMenu.jshandleContentTypeActions() 调用方加 try/catch + toast.error

验证

  • 社区编译版:贴图和预览回退到 Tauri WebView 版,行为不变
  • Mock GPU failure:3 个入口均正常回退,双重失败时用户收到 toast 错误提示
  • git grep 确认错误字符串匹配模式(includes('not found') || includes('Command'))已从 JS 代码库完全消除

@ExLei ExLei marked this pull request as draft May 25, 2026 20:25
@ExLei ExLei changed the title fix(贴图): 修复 v0.3.2 官方发行版贴图功能无法调用的问题 fix(贴图): 修复原生命令运行时失败时无法回退到 WebView 版的问题 Jun 13, 2026
ExLei added 2 commits June 14, 2026 07:13
去除基于错误字符串匹配的回退条件(includes("not found") ||
includes("Command")),改为原生命令任何运行时失败都回退到
Tauri WebView 版并保留错误日志。行为与 Rust 侧 tray/menu.rs
一致。双重失败时合并错误信息而非丢弃原生错误上下文。

修改:
- clipboard.js: pinImageToScreen() 去除条件判断,始终回退
- contextMenu.js: PreviewController.bind() 预览回退同样修复
pin-image 操作调用 pinImageToScreen() 之前未捕获异常,
失败时用户看不到任何反馈。加入 try/catch +
toast.error 给用户友好提示。
@ExLei ExLei force-pushed the fix/贴图功能回退修复 branch from c1307d2 to 7b6064b Compare June 13, 2026 23:15
@ExLei ExLei marked this pull request as ready for review June 13, 2026 23:16
@ExLei

ExLei commented Jun 13, 2026

Copy link
Copy Markdown
Contributor Author

@mosheng1 应该不史了 来看看吧(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant