Files
image-workbench-desktop/src/ui-desktop-shell.js
T

86 lines
3.1 KiB
JavaScript
Raw Permalink 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.
/* ui-desktop-shell.js —— 桌面端外壳适配(2026-11-18)
*
* 平台迁移后需要「网页做不到、只有桌面壳能做」的三件事,集中放这里,
* 避免 app.js 继续膨胀(source-text-ratchet 上限):
* 1) 外链一律交给系统浏览器:桌面 webview 里点 <a target=_blank> 会把整个应用导航成网页
* 2) 桌面原生通知:WKWebView 不支持 Web Notification
* 3) 捕获阶段拦截外链点击(含中键)
*
* 网页版不装 Tauri,isTauri() 恒 false ⇒ 全部走浏览器原生行为,语义不变。
*/
;(function (global) {
'use strict'
const g = /** @type {any} */ (global || {})
const EXTERNAL_RE = /^https?:\/\//i
function tauri () {
const T = g.__TAURI__
return (T && T.core && typeof T.core.invoke === 'function') ? T : null
}
function openExternal (url) {
const u = String(url || '')
if (!EXTERNAL_RE.test(u)) return false
const T = tauri()
if (T) {
try { T.core.invoke('open_external_url', { url: u }) } catch (e) { /* 忽略:下面还会尝试原生窗口 */ }
return true
}
try { global.open(u, '_blank', 'noopener') } catch (e) { /* ignore */ }
return true
}
/** 生成完成通知:桌面走原生;返回 true 表示已处理(调用方不要再走 Web Notification) */
function notifyGenerationDone (info) {
if (!tauri()) return false
const x = info || {}
const total = Number(x.total) || 0
const failed = Number(x.failed) || 0
const title = failed ? '出图完成(部分失败)' : '出图完成'
const body = failed
? ('成功 ' + Math.max(0, total - failed) + ' 张,失败 ' + failed + ' 张')
: ('共生成 ' + total + ' 张图片')
try { tauri().core.invoke('notify_desktop', { title: title, body: body }) } catch (e) { /* ignore */ }
return true
}
function notifyDesktop (title, body) {
const T = tauri()
if (!T) return false
try { T.core.invoke('notify_desktop', { title: String(title || ''), body: String(body || '') }) } catch (e) { /* ignore */ }
return true
}
/* 捕获阶段拦截外链点击(含中键):桌面端不允许把应用窗口导航走 */
function installLinkGuard (isTauri) {
if (typeof document === 'undefined' || !document.addEventListener) return false
if (isTauri && !isTauri()) return false
const pick = (e) => {
const t = e.target
const a = (t && t.closest) ? t.closest('a[href]') : null
if (!a) return ''
const href = a.getAttribute('href') || ''
return EXTERNAL_RE.test(href) ? href : ''
}
document.addEventListener('click', (e) => {
const href = pick(e)
if (!href) return
e.preventDefault()
e.stopPropagation()
openExternal(href)
}, true)
document.addEventListener('auxclick', (e) => {
if (e.button !== 1) return
const href = pick(e)
if (!href) return
e.preventDefault()
openExternal(href)
}, true)
return true
}
g.IWBUIShell = Object.freeze({ openExternal, notifyDesktop, notifyGenerationDone, installLinkGuard })
})(typeof window !== 'undefined' ? window : globalThis)