feat(browser)!: 1.4.0 工具栏重做(缩放设定框 + 加载态 + 站内地址回填)
validate / validate (push) Canceled after 0s

配合宿主 v3.25.29(修好了「网页压住地址栏」与「内容不跟画布缩放」)。

- 工具栏:← → ⟳(加载中变「停止」)、地址栏、− 100% + 缩放设定框(可直接输入
  百分比,双击回 100%,25%~300%)、↗ 独立窗口打开、✕ 关闭网页(paused,⟳ 可恢复)
- 状态回传:监听宿主 document 上的 iwb-cwv-state,站内跳转后地址栏跟随真实地址
- 标题为空时自动用站点名,多个网页节点并排时分得清
- 图标改 SVG(测试桩没有 createElementNS 时退回文字图标)
- 节点默认 560x520,数据新增 zoom / paused
- render 挂 dispose,销毁时清掉事件路由表引用
- harness 62 项(新增缩放框/暂停恢复/状态回填/禁用态断言)
This commit is contained in:
qiqi committed 2026-11-19 07:14:06 +08:00
1 parent 2dc43b701f
commit f7e2b786c7
4 files changed
+414 -72

No files matched your search

+286 -43
View File
@@ -42,17 +42,26 @@
'.iwb-browser-dot{display:inline-block;width:6px;height:6px;border-radius:50%;background:#1d9e75;margin-right:6px;vertical-align:middle}' +
'.iwb-browser-dot-off{background:#888780}' +
// ── 画布内网页节点 ──────────────────────────────────────────────────
// 节点体只需要地址栏 + 一个空锚点:网页本体是原生子 webview,盖在锚点的屏幕矩形上。
'.iwb-node-body.iwb-plugin-node-body.iwb-cwv-body{padding:0;display:flex;flex-direction:column;overflow:hidden;min-height:340px}' +
'.iwb-cwv-bar{display:flex;align-items:center;gap:4px;padding:5px 8px;border-bottom:1px solid var(--iwb-border,#444);flex:0 0 auto}' +
'.iwb-cwv-btn{cursor:pointer;border:1px solid var(--iwb-border,#444);border-radius:5px;background:var(--iwb-layer,#2a2a2a);color:var(--iwb-text,#e8e8e8);font-size:12px;line-height:1;padding:5px 7px;min-width:26px}' +
'.iwb-cwv-btn:hover:not(:disabled){background:var(--iwb-hover,#3a3a3a)}' +
'.iwb-cwv-btn:disabled{opacity:.35;cursor:default}' +
'.iwb-cwv-url{flex:1;min-width:0;padding:5px 8px;border-radius:5px;border:1px solid var(--iwb-border,#444);background:var(--iwb-layer,#2a2a2a);color:var(--iwb-text,#e8e8e8);font-size:12px;box-sizing:border-box}' +
'.iwb-cwv-url[aria-invalid=true]{border-color:#e05252}' +
'.iwb-cwv-stage{position:relative;flex:1 1 auto;min-height:0;background:var(--iwb-layer2,#1e1e1e)}' +
// 节点体 = 工具栏 + 一个空锚点。网页本体是宿主用 Tauri add_child 创建的原生子
// webview,精确盖在锚点的屏幕矩形上(位置同步在宿主 src/canvas-webview.js)。
'.iwb-node-body.iwb-plugin-node-body.iwb-cwv-body{padding:0;display:flex;flex-direction:column;overflow:hidden;min-height:300px}' +
'.iwb-cwv-bar{display:flex;align-items:center;gap:3px;padding:4px 6px;border-bottom:1px solid var(--iwb-border,#d5d9e0);background:var(--iwb-layer,#f0f1f4);flex:0 0 auto;box-sizing:border-box;min-height:34px}' +
'.iwb-cwv-btn{display:inline-flex;align-items:center;justify-content:center;cursor:pointer;border:0;border-radius:6px;background:transparent;color:var(--iwb-text,#1c2333);width:26px;height:26px;padding:0;flex:0 0 auto;opacity:.8}' +
'.iwb-cwv-btn:hover:not(:disabled){background:var(--iwb-border,#d5d9e0);opacity:1}' +
'.iwb-cwv-btn:disabled{opacity:.26;cursor:default}' +
'.iwb-cwv-url{flex:1 1 auto;min-width:40px;height:26px;padding:0 8px;border-radius:6px;border:1px solid var(--iwb-border,#d5d9e0);background:var(--iwb-layer2,#fff);color:var(--iwb-text,#1c2333);font-size:12px;font-family:inherit;box-sizing:border-box;outline:none}' +
'.iwb-cwv-url:focus{border-color:var(--iwb-brand,#6b7280)}' +
'.iwb-cwv-url[aria-invalid=true]{border-color:#e5484d}' +
'.iwb-cwv-zoom{display:flex;align-items:center;flex:0 0 auto;height:26px;border:1px solid var(--iwb-border,#d5d9e0);border-radius:6px;background:var(--iwb-layer2,#fff);overflow:hidden}' +
'.iwb-cwv-zoom .iwb-cwv-btn{width:22px;height:24px;border-radius:0}' +
'.iwb-cwv-zpct{width:42px;height:24px;border:0;background:transparent;color:var(--iwb-text,#1c2333);font-size:11px;font-family:inherit;text-align:center;padding:0;outline:none;font-variant-numeric:tabular-nums}' +
'.iwb-cwv-zpct:focus{background:var(--iwb-layer,#f0f1f4)}' +
'.iwb-cwv-sep{width:1px;height:16px;background:var(--iwb-border,#d5d9e0);flex:0 0 auto;margin:0 1px}' +
'.iwb-cwv-stage{position:relative;flex:1 1 auto;min-height:0;background:var(--iwb-layer2,#fff)}' +
'.iwb-cwv-anchor{position:absolute;inset:0}' +
'.iwb-cwv-empty{position:absolute;inset:0;display:flex;align-items:center;justify-content:center;text-align:center;padding:18px;font-size:12px;line-height:1.6;color:var(--iwb-text-dim,#9aa0a6)}'
'.iwb-cwv-empty{position:absolute;inset:0;display:flex;align-items:center;justify-content:center;text-align:center;padding:18px;font-size:12px;line-height:1.7;white-space:pre-line;color:var(--iwb-text2,#4d5668)}' +
'.iwb-cwv-spin{animation:iwb-cwv-rot .9s linear infinite}' +
'@keyframes iwb-cwv-rot{from{transform:rotate(0)}to{transform:rotate(360deg)}}'
var style = document.createElement('style')
style.id = 'iwb-browser-style'
style.textContent = css
@@ -298,14 +307,114 @@
// ── 画布内网页节点 ─────────────────────────────────────────────────────────
// 与面板的区别:这里网页**长在画布上**,跟着节点一起平移/缩放,可以和其他节点并排对照。
//
// 实现要点:节点体只画「地址栏 + 一个空的锚点 div」,网页本体是宿主用
// 实现要点:节点体只画「工具栏 + 一个空的锚点 div」,网页本体是宿主用
// Tauri `Window::add_child` 创建的原生子 webview,被精确盖在锚点的屏幕矩形上。
// 所以千万不要往锚点里塞任何内容 —— 原生层永远压在上面,塞了也看不见。
// 位置同步在宿主的 src/canvas-webview.js,本插件只负责:写 data-iwb-cwv 属性(告诉宿主
// 盖哪儿)、把用户输入的地址写回节点数据(data.url)、发控制命令。
//
// 约定(宿主 src/canvas-webview.js 读取,插件只写属性、不直接碰原生层):
// data-iwb-cwv 节点 id(有它宿主才会给这个节点建视图)
// data-iwb-cwv-url 要加载的地址(去掉 = 不加载)
// data-iwb-cwv-zoom 用户缩放倍数;宿主再乘画布缩放传给页面 zoom
// data-iwb-cwv-paused "1" = 暂停(✕ 之后),宿主会关掉视图并保持关闭
//
// 反向通道:宿主在 document 上派发 `iwb-cwv-state`({id, loading, url, failed}),
// 插件据此把地址栏换成站内跳转后的真实地址、把 ⟳ 换成「停止」。
//
// 已知限制(原生层的物理属性,不是 bug):网页区域内的鼠标事件由网页自己消费,
// 所以拖动节点要用标题栏;节点被别的节点盖住时网页仍然可见。
var SVG_NS = 'http://www.w3.org/2000/svg'
var ICON_D = {
back: 'M11.5 4.5 6 10l5.5 5.5',
forward: 'M8.5 4.5 14 10l-5.5 5.5',
reload: 'M15.5 10a5.5 5.5 0 1 1-1.6-3.9M15.5 4.2v3.5h-3.5',
stop: 'M7 7h6v6H7z',
close: 'M6.5 6.5l7 7M13.5 6.5l-7 7',
external: 'M8.5 11.5 14 6M14 6h-3.4M14 6v3.4',
minus: 'M6.5 10h7',
plus: 'M10 6.5v7M6.5 10h7'
}
var ICON_TEXT = {
back: '←', forward: '→', reload: '⟳', stop: '■', close: '✕',
external: '↗', minus: '−', plus: '+'
}
function icon(name) {
// 测试用的极简 DOM(tools/harness.mjs)没有 SVG API:退回文字图标,
// 真实客户端一定走上面这条分支,不影响观感。
if (!document.createElementNS) {
var sp = document.createElement('span')
sp.textContent = ICON_TEXT[name] || ''
return sp
}
var svg = document.createElementNS(SVG_NS, 'svg')
svg.setAttribute('viewBox', '0 0 20 20')
svg.setAttribute('width', '15')
svg.setAttribute('height', '15')
svg.setAttribute('aria-hidden', 'true')
var p = document.createElementNS(SVG_NS, 'path')
p.setAttribute('d', ICON_D[name] || '')
if (name === 'stop') {
p.setAttribute('fill', 'currentColor')
} else {
svg.setAttribute('fill', 'none')
svg.setAttribute('stroke', 'currentColor')
svg.setAttribute('stroke-width', '1.6')
svg.setAttribute('stroke-linecap', 'round')
svg.setAttribute('stroke-linejoin', 'round')
}
svg.appendChild(p)
return svg
}
var ZOOM_MIN = 0.25
var ZOOM_MAX = 3
var webMap = {} // nodeId → state(`iwb-cwv-state` 事件按 id 路由到对应节点)
function pctText(z) { return Math.round(z * 100) + '%' }
function clampZoom(z) {
var v = Math.round(Number(z) * 100) / 100
if (!isFinite(v) || v <= 0) v = 1
return Math.min(ZOOM_MAX, Math.max(ZOOM_MIN, v))
}
// 缩放值只有一个来源:st.zoom。改完立刻写锚点属性,宿主下一帧就按新值推 zoom。
function setZoom(st, next, commit) {
var z = clampZoom(next)
st.zoom = z
if (st.zpct && document.activeElement !== st.zpct) st.zpct.value = pctText(z)
st.anchor.setAttribute('data-iwb-cwv-zoom', String(z))
if (st.zMinus) st.zMinus.disabled = z <= ZOOM_MIN
if (st.zPlus) st.zPlus.disabled = z >= ZOOM_MAX
if (commit && st.node && st.props && typeof st.props.onUpdate === 'function') {
st.props.onUpdate(st.node.id, { zoom: z })
}
}
function setLoading(st, loading) {
st.loading = !!loading
if (!st.reload) return
st.reload.textContent = ''
st.reload.appendChild(icon(loading ? 'stop' : 'reload'))
st.reload.title = loading ? '停止加载' : '刷新'
st.reload.setAttribute('data-act', loading ? 'stop' : 'reload')
}
// 地址栏里显示的地址:输入框正在编辑时绝不去覆盖(否则每敲一个字都被顶回去)
function setAddress(st, url) {
if (!st.input) return
if (document.activeElement === st.input) return
var next = String(url || '')
if (st.input.value !== next) st.input.value = next
}
function ensurePausedText(st, paused) {
if (!st.empty) return
if (paused) {
st.empty.style.display = ''
st.empty.textContent = '已关闭这个网页。\n点上方 ⟳ 或重新输入网址即可继续。'
}
}
function renderWebNode(node, props, host, ctx) {
var st = host && host.__iwbWeb
if (!st) {
@@ -316,14 +425,24 @@
}
st.node = node
st.props = props
st.ctx = ctx
st.id = (node && node.id) || ''
if (st.id) webMap[st.id] = st
var url = String((node && node.data && node.data.url) || '').trim()
if (node && node.id) st.anchor.setAttribute('data-iwb-cwv', node.id)
var d = (node && node.data) || {}
var url = String(d.url || '').trim()
var paused = d.paused === true || d.paused === '1' || d.paused === 1
if (st.id) st.anchor.setAttribute('data-iwb-cwv', st.id)
if (url) st.anchor.setAttribute('data-iwb-cwv-url', url)
else st.anchor.removeAttribute('data-iwb-cwv-url')
if (paused) st.anchor.setAttribute('data-iwb-cwv-paused', '1')
else st.anchor.removeAttribute('data-iwb-cwv-paused')
// 输入框正在编辑时不要覆盖,否则每敲一个字都会被旧值顶回去
if (document.activeElement !== st.input) st.input.value = url
if (node && node.id !== st.lastNodeId) st.lastNodeId = node.id
setAddress(st, st.pageUrl && !paused && st.pageUrl !== url ? st.pageUrl : url)
// 每次渲染都写一遍(值没变时只是几次属性赋值)——否则新建节点不会写下 zoom 属性
setZoom(st, d.zoom || 1, false)
var supported = !!invoke()
// 画布内的网页靠宿主的 src/canvas-webview.js 做位置同步并把命令发给 Rust。
@@ -338,73 +457,191 @@
st.empty.textContent = '画布内网页需要桌面客户端;网页版请用左侧「浏览器」面板。'
} else if (!engine) {
st.empty.style.display = ''
st.empty.textContent = '当前客户端还没有画布内网页能力(需要 v3.25.28 及以上)。\n请先更新客户端;升级前可以点上方 ✕ 用「浏览器」面板打开。'
st.empty.textContent = '当前客户端还没有画布内网页能力(需要 v3.25.29 及以上)。\n请先更新客户端;升级前可以点上方 ↗ 用「浏览器」面板打开。'
} else if (paused) {
ensurePausedText(st, true)
} else {
st.empty.style.display = 'none'
}
var on = !!(url && supported)
for (var i = 0; i < st.buttons.length; i++) st.buttons[i].disabled = !on
var acts = st.acts || []
for (var i = 0; i < acts.length; i++) acts[i].disabled = !on
if (st.zMinus) st.zMinus.disabled = !on || st.zoom <= ZOOM_MIN
if (st.zPlus) st.zPlus.disabled = !on || st.zoom >= ZOOM_MAX
if (st.zpct) st.zpct.disabled = !on
}
function buildWebNode(host, ctx) {
var mkBtn = function (act, text, title) {
var b = el('button', { class: 'iwb-cwv-btn', type: 'button', title: title, text: text })
b.setAttribute('data-act', act)
var mkBtn = function (name, title, act) {
var b = el('button', { class: 'iwb-cwv-btn', type: 'button', title: title })
b.appendChild(icon(name))
if (act) b.setAttribute('data-act', act)
b.disabled = true
return b
}
var back = mkBtn('back', '←', '后退')
var fwd = mkBtn('forward', '→', '前进')
var reload = mkBtn('reload', '⟳', '刷新')
var close = mkBtn('close', '✕', '关闭网页(节点保留)')
var back = mkBtn('back', '后退', 'back')
var fwd = mkBtn('forward', '前进', 'forward')
var reload = mkBtn('reload', '刷新', 'reload')
var close = mkBtn('close', '关闭这个网页(节点保留,可再打开)')
var open = mkBtn('external', '在独立窗口/浏览器面板打开')
var zMinus = el('button', { class: 'iwb-cwv-btn', type: 'button', title: '缩小内容' })
zMinus.appendChild(icon('minus'))
var zPlus = el('button', { class: 'iwb-cwv-btn', type: 'button', title: '放大内容' })
zPlus.appendChild(icon('plus'))
// 可编辑的缩放设定框:点一下全选,输入 120 回车即 120%
var zpct = el('input', {
class: 'iwb-cwv-zpct', type: 'text',
title: '内容缩放:点一下可直接输入百分比,回车生效;双击恢复 100%',
spellcheck: 'false'
})
zpct.value = '100%'
var zoomBox = el('div', { class: 'iwb-cwv-zoom' }, [zMinus, zpct, zPlus])
var input = el('input', { class: 'iwb-cwv-url', type: 'text', placeholder: '输入网址后回车' })
var bar = el('div', { class: 'iwb-cwv-bar' }, [back, fwd, reload, input, close])
var sep = el('div', { class: 'iwb-cwv-sep' })
var bar = el('div', { class: 'iwb-cwv-bar' }, [back, fwd, reload, input, zoomBox, sep, open, close])
// 锚点:宿主 canvas-webview.js 按这个 div 的屏幕矩形去摆原生 webview
var anchor = el('div', { class: 'iwb-cwv-anchor' })
var empty = el('div', { class: 'iwb-cwv-empty' })
var stage = el('div', { class: 'iwb-cwv-stage' }, [anchor, empty])
host.appendChild(bar)
host.appendChild(stage)
return { input: input, anchor: anchor, empty: empty, buttons: [back, fwd, reload, close], node: null, props: null }
return {
id: '', node: null, props: null, ctx: ctx,
bar: bar, input: input, anchor: anchor, empty: empty, stage: stage,
back: back, fwd: fwd, reload: reload, close: close, open: open,
zMinus: zMinus, zPlus: zPlus, zpct: zpct,
acts: [back, fwd, reload, close, open],
zoom: 1, loading: false, pageUrl: '', lastNodeId: ''
}
}
function bindWebNode(st, ctx) {
var commit = function () {
var url = normalizeUrl(st.input.value)
if (!url) { st.input.setAttribute('aria-invalid', 'true'); return }
if (!url) { st.input.setAttribute('aria-invalid', 'true'); return false }
st.input.removeAttribute('aria-invalid')
if (!st.node || !st.props || typeof st.props.onUpdate !== 'function') return
st.props.onUpdate(st.node.id, { url: url })
if (!st.node || !st.props || typeof st.props.onUpdate !== 'function') return false
var patch = { url: url, paused: false }
// 标题还空着就顺手写上站点名:多个网页节点并排时才分得清(用户随时可改)
var d = st.node.data || {}
if (!(d.title && String(d.title).trim())) patch.title = hostOf(url)
st.pageUrl = url
st.props.onUpdate(st.node.id, patch)
// 不依赖 React 重渲染的时机:属性一改,宿主下一帧就会读到并导航
st.anchor.setAttribute('data-iwb-cwv-url', url)
st.anchor.removeAttribute('data-iwb-cwv-paused')
st.empty.style.display = 'none'
for (var i = 0; i < st.buttons.length; i++) st.buttons[i].disabled = false
for (var i = 0; i < st.acts.length; i++) st.acts[i].disabled = false
return true
}
st.input.addEventListener('keydown', function (e) {
if (e.key === 'Enter') { e.preventDefault(); e.stopPropagation(); commit() }
if (e.key === 'Enter') {
e.preventDefault(); e.stopPropagation()
if (commit()) st.input.blur()
} else if (e.key === 'Escape') {
e.preventDefault(); e.stopPropagation()
st.input.removeAttribute('aria-invalid')
setAddress(st, (st.node && st.node.data && st.node.data.url) || '')
st.input.blur()
}
})
st.input.addEventListener('focus', function () { try { st.input.select() } catch (e) { /* 忽略 */ } })
// 失焦时把显示值收敛回已生效地址(用户可能敲了半截就点走)
st.input.addEventListener('blur', function () {
if (st.input.getAttribute('aria-invalid')) { st.input.removeAttribute('aria-invalid'); return }
var url = (st.node && st.node.data && st.node.data.url) || ''
st.input.value = String(url || '')
setAddress(st, st.pageUrl || (st.node && st.node.data && st.node.data.url) || '')
})
st.buttons.forEach(function (b) {
// ── 缩放 ──
st.zMinus.addEventListener('click', function (e) {
e.preventDefault(); e.stopPropagation()
setZoom(st, st.zoom - 0.1, true)
})
st.zPlus.addEventListener('click', function (e) {
e.preventDefault(); e.stopPropagation()
setZoom(st, st.zoom + 0.1, true)
})
st.zpct.addEventListener('keydown', function (e) {
if (e.key === 'Enter') {
e.preventDefault(); e.stopPropagation()
var v = parseFloat(String(st.zpct.value).replace('%', '').trim())
if (isFinite(v) && v > 0) setZoom(st, v / 100, true)
st.zpct.value = pctText(st.zoom)
st.zpct.blur()
} else if (e.key === 'Escape') {
e.preventDefault(); e.stopPropagation()
st.zpct.value = pctText(st.zoom)
st.zpct.blur()
}
})
st.zpct.addEventListener('blur', function () { st.zpct.value = pctText(st.zoom) })
st.zpct.addEventListener('dblclick', function (e) {
e.preventDefault(); e.stopPropagation()
setZoom(st, 1, true)
st.zpct.value = pctText(st.zoom)
})
st.zpct.addEventListener('focus', function () { try { st.zpct.select() } catch (e) { /* 忽略 */ } })
st.acts.forEach(function (b) {
b.addEventListener('click', function (e) {
e.preventDefault()
e.stopPropagation()
var f = invoke()
if (!f || !st.node) return
f('canvas_webview_action', { id: st.node.id, action: b.getAttribute('data-act') })
.catch(function (err) { ctx.log('画布网页控制失败:' + String(err)) })
var act = b.getAttribute('data-act')
// 已关闭(paused)时点 ⟳/←/→:先恢复加载,再让宿主重建视图
if (act && st.anchor.getAttribute('data-iwb-cwv-paused') === '1') {
if (st.props && typeof st.props.onUpdate === 'function') {
st.props.onUpdate(st.node.id, { paused: false })
}
st.anchor.removeAttribute('data-iwb-cwv-paused')
if (st.empty) st.empty.style.display = 'none'
return
}
if (act) {
f('canvas_webview_action', { id: st.node.id, action: act })
.catch(function (err) { ctx.log('画布网页控制失败:' + String(err)) })
return
}
// ✕ 和 ↗ 没有 data-act:前者暂停,后者开独立窗口
if (b === st.close) {
var d = st.node.data || {}
st.anchor.setAttribute('data-iwb-cwv-paused', '1')
if (st.props && typeof st.props.onUpdate === 'function') {
st.props.onUpdate(st.node.id, { url: d.url || '', paused: true })
}
ensurePausedText(st, true)
} else if (b === st.open) {
var target = (st.pageUrl || (st.node.data && st.node.data.url) || '').trim()
if (target) openUrl(ctx, target)
}
})
})
// 宿主回报的网页状态(加载中 / 站内跳转后的真实地址 / 失败)
if (!bindWebNode.listening && document.addEventListener) {
bindWebNode.listening = true
document.addEventListener('iwb-cwv-state', function (ev) {
var p = (ev && ev.detail) || {}
var s = p.id && webMap[p.id]
if (!s) return
if (p.url) s.pageUrl = p.url
setLoading(s, !!p.loading)
setAddress(s, s.pageUrl)
})
}
}
// 节点被删除时把引用摘掉,避免事件路由表长期持有已卸载的 DOM
function disposeWebNode(host) {
var st = host && host.__iwbWeb
if (st && st.id && webMap[st.id] === st) delete webMap[st.id]
}
global.IWBPluginHost.register({
id: 'browser',
name: '应用内浏览器',
version: '1.3.0',
version: '1.4.0',
apiVersion: 1,
capabilities: ['panels', 'nodeTypes'],
settings: {
@@ -428,12 +665,18 @@
id: 'web',
title: '网页',
icon: 'globe',
height: 480,
height: 520,
// 宽度要够才看得清网页;宿主 addPluginNode 会带上这个值(未声明则用通用 264)
width: 520,
defaults: { url: '' },
width: 560,
// zoom:相对节点的内容缩放(宿主再乘画布缩放);paused:✕ 之后暂停加载
defaults: { url: '', zoom: 1, paused: false },
ports: { in: false, out: false },
render: function (node, props, host) { renderWebNode(node, props, host, ctx) }
// 宿主在节点销毁时调 render.dispose(host):顺手把事件路由表里的引用摘掉
render: (function () {
function r(node, props, host) { renderWebNode(node, props, host, ctx) }
r.dispose = disposeWebNode
return r
})()
})
}
return {}