Files
image-workbench-desktop/src/runtime-desktop-bridge.js
T

42 lines
3.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* runtime-desktop-bridge.js —— 桌面端(Tauri)能力桥(2026-11-16)
*
* 为什么单独一个模块:
* src/*.js 不允许反向引用 app.js 定义的 window 全局(architecture-boundary-guard 会红)。
* 而「原生另存为 / 默认下载文件夹 / 打开数据目录」的实现必须在 app.js 的保存链路里。
* 所以这里只做**登记 + 转发**:app.js 启动时 configure() 注入实现,其它模块只认 IWBDesktop。
*
* 网页版:不会 configure ⇒ ready() 为 false ⇒ 各模块据此隐藏桌面专属 UI,行为与原来完全一致。
*/
;(function (global) {
'use strict'
const state = { impl: null }
const bridge = {
// app.js 启动时调用一次,注入真实实现
configure(impl) { state.impl = impl || null },
ready() { return !!state.impl },
isTauri() { return !!(state.impl && state.impl.isTauri()) },
// 保存 Blob:桌面端走原生另存为/静默写盘,网页端由 app.js 内部回退到 <a download>
saveBlob(blob, name) { return state.impl ? state.impl.saveBlob(blob, name) : undefined },
getDownloadDir() { return state.impl ? state.impl.getDownloadDir() : '' },
setDownloadDir(dir) { if (state.impl) state.impl.setDownloadDir(dir) },
pickDownloadDir() { return state.impl ? state.impl.pickDownloadDir() : Promise.resolve('') },
getSilentSave() { return !!(state.impl && state.impl.getSilentSave()) },
setSilentSave(on) { if (state.impl) state.impl.setSilentSave(on) },
// 自动下载(v3.25.20):与「点下载是否弹框」解耦,只决定出图后是否自动落盘
getAutoDownload() { return !!(state.impl && state.impl.getAutoDownload && state.impl.getAutoDownload()) },
setAutoDownload(on) { if (state.impl && state.impl.setAutoDownload) state.impl.setAutoDownload(on) },
openDataDir() { return state.impl ? state.impl.openDataDir() : Promise.reject(new Error('仅桌面端可用')) },
// 原生剪贴板图片(v3.26):WKWebView 里 paste 事件拿不到 Finder 复制的图片,
// navigator.clipboard.write 也常不可用,统一走 Rust 侧 arboard。
clipboardReadImage() { return state.impl && state.impl.clipboardReadImage ? state.impl.clipboardReadImage() : Promise.resolve(null) },
clipboardWriteImage(blob) { return state.impl && state.impl.clipboardWriteImage ? state.impl.clipboardWriteImage(blob) : Promise.resolve(false) },
market: {
list() { return state.impl && state.impl.marketList ? state.impl.marketList() : Promise.resolve([]) },
installed() { return state.impl && state.impl.marketInstalled ? state.impl.marketInstalled() : Promise.resolve([]) },
install(id) { return state.impl && state.impl.marketInstall ? state.impl.marketInstall(id) : Promise.resolve({ ok: false, error: '仅桌面端可用' }) },
uninstall(id) { return state.impl && state.impl.marketUninstall ? state.impl.marketUninstall(id) : Promise.resolve({ ok: false, error: '仅桌面端可用' }) },
},
}
global.IWBDesktop = bridge
})(typeof window !== 'undefined' ? window : globalThis)