Files
image-workbench-desktop/src/ui-nav-rail.js
T

226 lines
13 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.
/* ui-nav-rail.js —— 最左侧一级导航(v3.25.59)
*
* 设计(用户裁定):
* · 52px、**纯图标**(无文字),占原来 wsrail 的槽位,不额外挤占画布
* · 顶部:软件 logo(品牌占位,不跳转)
* · 主组:首页(画布库)/ 列表(画布工作区)/ 插件(整页插件管理)
* · 底部组:帮助 / 快捷键 / 皮肤 / 设置 / 更多
* · 选中态 = 深色圆底 + 白色图标;未选中 = 线性图标;悬停 = 浅灰圆底
*
* 2026-11-25:底部「更多」从空操作改为**快捷菜单**(首页 / 插件 / 源码 / 版本号),
* 并在桌面端自动检查更新:有新版本时 ⋯ 变成「圆形上箭头」升级图标,点开菜单首项即可升级。
* 更新检查/安装走 Tauri 的 check_update / install_update(签名校验在 Rust 侧)。
* 全部状态自持于本组件,app.js 不需要新增状态或逻辑。
*/
;(function (global) {
'use strict'
const React = global.React || {}
const ReactDOM = global.ReactDOM || {}
const el = (type, prop, ...children) => React.createElement(type, prop, ...children)
const useState = React.useState
const useEffect = React.useEffect
// 升级图标(圆形内向上箭头):ICONS 在 app.js 里且行数已到棘轮上限,这里内联一份,不动宿主。
// v3.26.95:升级中**箭头不转**,外圈变成真实下载进度环(一圈闭合),进度来自 updater 的 on_chunk。
const RING_R = 9
const RING_C = 2 * Math.PI * RING_R
function UpgradeIcon (props) {
const pp = props || {}
const upgrading = !!pp.upgrading
const pct = (typeof pp.progress === 'number' && isFinite(pp.progress)) ? Math.max(0, Math.min(100, pp.progress)) : null
const indeterminate = upgrading && pct === null
const circle = {
cx: 12, cy: 12, r: RING_R, fill: 'none', stroke: 'currentColor', strokeWidth: 2,
transform: 'rotate(-90 12 12)',
}
return el('svg', {
className: 'iwb-ic iwb-nav-up-ic', viewBox: '0 0 24 24', fill: 'none', stroke: 'currentColor',
strokeWidth: 2, strokeLinecap: 'round', strokeLinejoin: 'round', 'aria-hidden': true,
},
upgrading ? el('circle', Object.assign({}, circle, { className: 'iwb-nav-up-track', opacity: 0.22 }), null) : null,
upgrading
? el('circle', Object.assign({}, circle, {
className: 'iwb-nav-up-bar' + (indeterminate ? ' iwb-nav-up-indet' : ''),
strokeLinecap: 'round',
strokeDasharray: indeterminate ? '10 47' : RING_C,
strokeDashoffset: indeterminate ? 0 : RING_C * (1 - pct / 100),
}), null)
: el('circle', circle, null),
el('path', { d: 'M12 16V8' }, null),
el('path', { d: 'm8.5 11.5 3.5-3.5 3.5 3.5' }, null),
)
}
// 「更多」菜单里的首页/插件是**网站页面**,不是软件内部页面(用户 2026-11-25 澄清);
// 内部页面仍由左侧导航栏的图标切换。
const WEB_HOME = 'https://download.maqiart.eu.org/'
const WEB_PLUGINS = 'https://download.maqiart.eu.org/plugins/'
const SOURCE_URL = 'https://github.com/malongan/image-workbench-web'
/* 外部链接统一走 IWBUIShell.openExternal:桌面端调 Rust 的 open_external_url 交给系统浏览器,
* Web 端退化为 window.open。**不用 <a href>** —— 桌面端的链接守卫在捕获阶段
* stopPropagation(src/ui-desktop-shell.js),菜单项的 onClick 收不到,点完菜单会留在原地。 */
function openWeb (url) {
const S = (typeof global !== 'undefined' && global.IWBUIShell) || null
if (S && typeof S.openExternal === 'function' && S.openExternal(url)) return
try { global.open(url, '_blank', 'noopener') } catch (e) { /* ignore */ }
}
function invoke (cmd) {
const T = (typeof window !== 'undefined') ? window.__TAURI__ : null
if (!T || !T.core || typeof T.core.invoke !== 'function') return Promise.reject(new Error('桌面端不可用'))
return T.core.invoke(cmd)
}
function IWBUINavRail (props) {
const I = props.I
const view = props.view || 'canvas' // 'home' | 'canvas' | 'plugins'
const onHome = props.onHome
const onCanvas = props.onCanvas
const onPlugins = props.onPlugins
const pages = Array.isArray(props.pages) ? props.pages : []
const activePage = String(props.activePage || '')
const onPage = props.onPage
const onHelp = props.onHelp
const onHotkeys = props.onHotkeys
const onSkin = props.onSkin
const onSettings = props.onSettings
const skinIcon = props.skinIcon || 'moon'
const logoIcon = props.logoIcon || 'palette'
const appVersion = props.appVersion || ''
const [moreOpen, setMoreOpen] = useState(false)
const [morePos, setMorePos] = useState(null)
const [update, setUpdate] = useState(null) // null=无更新;{version,...}=有新版本
const [updateMsg, setUpdateMsg] = useState('')
const [upgrading, setUpgrading] = useState(false) // 升级中:外圈进度环(v3.26.95)
const [updateProgress, setUpdateProgress] = useState(null) // 0~100;null = 不知道总大小
const [updatePhase, setUpdatePhase] = useState('') // 'downloading' | 'installing'
const checkUpdate = () => {
setUpdateMsg('正在检查…')
return invoke('check_update').then((r) => {
setUpdate(r && r.version ? r : null)
setUpdateMsg(r && r.version ? ('发现新版本 v' + r.version) : '已是最新版本')
}).catch((e) => {
setUpdate(null)
const m = String((e && e.message) || e)
// 网页版没有 Tauri 桥,检查更新必然失败;不把这种「预期内不可用」当成错误抛给用户
setUpdateMsg(m.indexOf('桌面端不可用') >= 0 ? '' : '检查失败:' + m)
})
}
// 挂载后静默检查一次(仅桌面端;网页版 invoke 立即 reject,被 catch 吞掉,不留 UI 噪音)
useEffect(() => { checkUpdate() }, [])
const upgrade = () => {
if (upgrading) return
setUpgrading(true)
setUpdateProgress(0)
setUpdatePhase('downloading')
setUpdateMsg('正在下载并安装,完成后会自动重启…')
invoke('install_update').then(() => setUpdateMsg('安装完成,正在重启…'))
.catch((e) => { setUpgrading(false); setUpdateProgress(null); setUpdatePhase(''); setUpdateMsg('更新失败:' + String((e && e.message) || e)) })
}
// Rust 侧 updater 的 on_chunk 通过 iwb-update-progress 事件回报真实字节数。
// 只显示下载阶段;安装阶段没有字节进度,环走满并显示「正在安装…」。
useEffect(() => {
if (!upgrading) return undefined
let off = null
let cancelled = false
try {
const T = (typeof window !== 'undefined') ? window.__TAURI__ : null
if (T && T.event && typeof T.event.listen === 'function') {
T.event.listen('iwb-update-progress', (ev) => {
if (cancelled) return
const d = (ev && ev.payload) || {}
if (d.phase === 'installing') { setUpdatePhase('installing'); setUpdateProgress(100); return }
if (d.phase === 'downloading') {
setUpdatePhase('downloading')
const total = Number(d.total) || 0
const done = Number(d.downloaded) || 0
if (total > 0) setUpdateProgress(Math.max(1, Math.min(99, Math.round(done / total * 100))))
else setUpdateProgress(null)
}
}).then((f) => { if (cancelled) { try { f() } catch (e) { /* ignore */ } } else off = f }).catch(() => { /* 事件通道不可用:退回不确定态 */ })
}
} catch (e) { /* ignore */ }
return () => { cancelled = true; if (off) { try { off() } catch (e) { /* ignore */ } } }
}, [upgrading])
useEffect(() => {
if (!moreOpen) return undefined
const onDown = (e) => { if (!e.target.closest || !e.target.closest('.iwb-nav-more-menu, .iwb-nav-more-btn')) setMoreOpen(false) }
const onKey = (e) => { if (e.key === 'Escape') setMoreOpen(false) }
document.addEventListener('pointerdown', onDown, true)
document.addEventListener('keydown', onKey, true)
return () => { document.removeEventListener('pointerdown', onDown, true); document.removeEventListener('keydown', onKey, true) }
}, [moreOpen])
const item = (key, iconName, title, active, onClick) => el('button', {
key,
type: 'button',
className: 'iwb-nav-item' + (active ? ' on' : ''),
title,
'aria-label': title,
'aria-pressed': active ? 'true' : 'false',
onPointerDown: (e) => e.stopPropagation(),
onClick: (e) => { e.stopPropagation(); if (typeof onClick === 'function') onClick(e) },
}, I(iconName))
const toggleMore = (e) => {
const r = e.currentTarget.getBoundingClientRect()
setMorePos({ left: Math.max(6, r.right + 6), bottom: Math.max(6, global.innerHeight - r.bottom + 6) })
setMoreOpen((v) => !v)
}
const act = (fn) => (e) => { e.stopPropagation(); setMoreOpen(false); if (typeof fn === 'function') fn(e) }
const menuItem = (key, iconNode, text, onClick, cls, title) => el('button', {
key, type: 'button', className: 'iwb-ctx-item' + (cls ? ' ' + cls : ''), onClick, title: title || text,
}, iconNode, el('span', null, text))
const upgradingText = updatePhase === 'installing' ? '正在安装…' : ('正在下载…' + (typeof updateProgress === 'number' ? ' ' + updateProgress + '%' : ''))
const menu = !moreOpen || !morePos ? null : ReactDOM.createPortal(
el('div', {
className: 'iwb-ctxmenu iwb-nav-more-menu',
style: { left: morePos.left + 'px', bottom: morePos.bottom + 'px' },
onPointerDown: (e) => e.stopPropagation(),
},
update ? menuItem('up', el(UpgradeIcon, { upgrading, progress: updateProgress }), upgrading ? upgradingText : ('升级到 v' + update.version + (update.notes ? ':' + update.notes : '')), act(upgrade), 'iwb-nav-more-up' + (upgrading ? ' iwb-nav-more-up-busy' : '')) : null,
// 首页 / 插件 / 源码地址:全部打开**网站**(用户 2026-11-25 裁定),不再切软件内页面
menuItem('home', I('home'), '首页', act(() => openWeb(WEB_HOME)), null, '在浏览器打开官网首页 ' + WEB_HOME),
menuItem('plugins', I('box'), '插件', act(() => openWeb(WEB_PLUGINS)), null, '在浏览器打开插件市场 ' + WEB_PLUGINS),
menuItem('src', I('github'), '源码地址', act(() => openWeb(SOURCE_URL)), null, '在浏览器打开源码仓库 ' + SOURCE_URL),
el('div', { key: 'ver', className: 'iwb-nav-more-ver' }, '版本 ' + (appVersion || '未知')),
updateMsg ? el('div', { key: 'msg', className: 'iwb-nav-more-ver' }, updateMsg) : null,
!update ? menuItem('check', I('refresh'), '检查更新', act(checkUpdate)) : null,
),
global.document.body,
)
return el(React.Fragment, null,
el('nav', { className: 'iwb-navrail', 'aria-label': '一级导航' },
el('div', { className: 'iwb-navrail-group' },
item('home', 'home', '首页(画布库)', view === 'home', onHome),
item('canvas', 'layer', '列表(画布工作区)', view === 'canvas', onCanvas),
item('plugins', 'box', '插件', view === 'plugins', onPlugins),
pages.map((p) => item('pg:' + p.pluginId + ':' + p.id, p.icon || 'queue', p.title, activePage === (p.pluginId + ':' + p.id), () => { if (typeof onPage === 'function') onPage(p.pluginId, p.id) })),
),
el('div', { className: 'iwb-navrail-spacer' }),
el('div', { className: 'iwb-navrail-group' },
item('help', 'help', '帮助', false, onHelp),
item('hotkeys', 'circle-dot', '快捷键', false, onHotkeys),
item('skin', skinIcon, '切换主题', false, onSkin),
item('settings', 'settings', '设置', false, onSettings),
el('button', {
key: 'more', type: 'button',
className: 'iwb-nav-item iwb-nav-more-btn' + (update ? ' has-update' : '') + (moreOpen ? ' on' : '') + (upgrading ? ' iwb-nav-more-upgrading' : ''),
title: update ? ('发现新版本 v' + update.version + ',点击升级') : '更多',
'aria-label': update ? '发现新版本,点击升级' : '更多',
'aria-expanded': moreOpen ? 'true' : 'false',
onPointerDown: (e) => e.stopPropagation(),
onClick: (e) => { e.stopPropagation(); toggleMore(e) },
}, update ? el(UpgradeIcon, { upgrading, progress: updateProgress }) : I('more')),
),
),
menu,
)
}
global.IWBUINavRail = IWBUINavRail
})(typeof window !== 'undefined' ? window : globalThis)