Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,16 @@

### 事件与答案

- 宿主按契约发送 `open`、`close`、`click`、`change`、`select`、`activate`、`viewport`、`settings` 事件,每个事件调用一次视图函数,参数里带 `instance`、`surface`、`language` 和 `settings{effective, draft}`。
- 每个视图同一时间只发一个调用,按顺序发送。`viewport` 和 `settings` 停止变化 400 ms 后才发,等待中的同类事件合并成最新一个。输入框只在回车、失焦或提交时发一次 `change`,不按键调用。
- 宿主按契约发送 `open`、`close`、`click`、`change`、`select`、`activate`、`viewport`、`resize`、`settings` 事件,每个事件调用一次视图函数,参数里带 `instance`、`surface`、`language`、`width` 和 `settings{effective, draft}`。`width` 是视图节点排版用的宽度(整数点,也是顶层画布的宽度);工具窗口要减去视图四周各 10 点的内边距。宿主在绘制时量出宽度,量到之后才发 `open`。
- 每个视图同一时间只发一个调用,按顺序发送。`viewport`、`resize` 和 `settings` 停止变化 400 ms 后才发,等待中的同类事件合并成最新一个;`resize` 只在画出来的视图里有画布在 `on` 写了它时发,发送时带当时的宽度。输入框只在回车、失焦或提交时发一次 `change`,不按键调用。
- 函数返回整棵视图 `{view}`、补丁 `{patches}`(`set`、`replace`、`remove`、`insert`,最多 512 个)或 `{}`。宿主把答案连同清单、位置和上一次画的视图交给 Core 的 `plugins.ui.applyResult`,Core 校验并应用补丁后返回完整的下一棵视图,宿主只画这棵。宿主不保留自己的校验器或补丁引擎。
- 本版没有插件主动推送:视图只在回答事件时变化。

### 画布

- 节点是 `rect`、`path`、`text`、`image`、`group`,按 `viewBox` 坐标缩放到视图宽度。
- 节点是 `rect`、`path`、`text`、`image`、`group`,用 `viewBox` 坐标,画在和视图一样宽、`height` 点高的区域里。
- 缩放为 1 时,不写 `pan` 的画布显示整个 `viewBox`:按视图宽度缩放,但不超过 `height`,从左上角开始;写了 `pan` 的画布按视图宽度缩放,缩小到比区域窄时水平居中。只按宽度缩放会在宽窗口里把场景放大、裁掉下半部分,0.8.7/0.8.8 的 Go Modules 预览就是这样。
- 跟随窗口的场景:插件把 `viewBox` 宽度设成事件里的 `width`,按这个宽度排版,并在 `on` 里写 `resize`;窗口变化后宿主发 `resize`,插件按新宽度重新排版。新答复到达之前,宿主按上面的规则缩放已有场景。旧宿主不带 `width`,它的 Core 也拒绝写了 `resize` 的画布,所以插件只在收到 `width` 后才写 `resize`,否则按固定宽度排版(语言页约 610 点,构建系统页和编译器页的详情约 340 点)(Core [#109](https://github.com/Lithe-IDEA/Lithe-core-rust/pull/109))。
- 颜色可以是固定值、主题令牌(`$accent`、`$text` 等)、深浅两套值,或线性渐变;令牌和深浅两套值跟随主题。宿主控件不接受颜色、字体、尺寸和坐标。
- 可点击的分组必须有 `id`、`w`、`h` 和 `label`。悬停、按下、选中的样式写在 `states`(`hover`、`pressed`、`selected`)里,由宿主本地切换。
- 普通滚轮滚动页面;⌘ 或 Ctrl 加滚轮缩放,拖动空白处平移,缩放按钮由宿主画。悬停提示停留 500 ms 后显示。
Expand All @@ -63,7 +65,7 @@

### 生命周期

视图显示时发 `open`,消失时发 `close`。关闭、停用、更新或卸载插件、关闭项目、切换界面语言时,宿主取消未完成的调用并丢弃晚到的答案;切换语言后用新的 `instance` 重新打开。视图状态只存在后端和宿主内存里,不写进插件包或应用包。
视图显示后第一次绘制量到宽度时发 `open`,消失时发 `close`;还没画过就消失的视图两个都不发。关闭、停用、更新或卸载插件、关闭项目、切换界面语言时,宿主取消未完成的调用并丢弃晚到的答案;切换语言后用新的 `instance` 重新打开。视图状态只存在后端和宿主内存里,不写进插件包或应用包。

### Viva 尚不支持的画布能力

Expand Down Expand Up @@ -96,6 +98,7 @@

- 2026-11-12:渲染归 API-13,读写设置归 API-19,插件文字本地化归 API-37,都是现有编号的扩展,不新增编号。契约在 v0 里直接删掉 `native-ui`,换成插件视图,不升版本号(版本已于 2026-11-11 全部归 0)。
- 2026-11-18:设置区域只用插件视图,需要自由绘制时用画布(保留场景),不嵌网页;网页只用于工具窗口,沿用[插件自带网页界面](2026-10-18-plugin-web-content-in-host-windows.md)。顺序是先合设计和原型,再做 Core 契约加 SDK,最后做桌面端 Viva 渲染。
- 2026-11-19:画布要自动适应窗口:不放大、不裁剪,并按真实宽度重新排版。做法是事件带 `width`、画布可写 `resize`,属于 API-13 的补充,不升能力版本(Core #109 取代只改缩放规则的 #107)。

## 考虑过的备选方案

Expand Down Expand Up @@ -136,7 +139,7 @@

## 验证

- `scripts/desktop/desktop-go.sh test ./internal/pluginview/ ./internal/pluginservice/ ./internal/workbench/ ./internal/settingswindow/ ./internal/core/ -count=1`:覆盖事件顺序与去抖、晚到答案丢弃、失败重试、节点解析、画布路径与颜色、图片限制、嵌套树高度、视图事件参数(Core 契约 fixture)、区域绑定设置与搜索关键词。
- `scripts/desktop/desktop-go.sh test ./internal/pluginview/ ./internal/pluginservice/ ./internal/workbench/ ./internal/settingswindow/ ./internal/core/ -count=1`:覆盖事件顺序与去抖、量到宽度后才发 `open`、每个事件带 `width`、`resize` 的去抖与合并(含无头窗口缩小后发送新宽度)、晚到答案丢弃、失败重试、节点解析、画布路径与颜色、图片限制、嵌套树高度、视图事件参数(Core 契约 fixture)、区域绑定设置与搜索关键词。
- `scripts/desktop/desktop-go.sh test -count=1 -tags 'vivahead lithevisual' -run 'TestVisualPluginView' ./internal/pluginview/` 和 `scripts/desktop/desktop-go.sh test -count=1 -tags 'vivahead lithevisual' -run 'TestVisualPluginRegion' ./internal/settingswindow/`:设置 `LITHE_VISUAL_OUTPUT_DIR` 后输出工具窗口视图和设置区域的深浅主题截图。
- `scripts/desktop/verify/verify-runtime-bundle-immutability.sh`:确认插件包和应用包在运行中不被写入。
- 原生整机验收:安装一个声明了视图工具窗口和设置区域的测试插件,检查点击、编辑、树选择与激活、画布平移缩放、绑定设置的应用与取消、后端报错后的重试、停用插件后视图关闭;macOS 上打开插件区域时不出现新的 `com.apple.WebKit` 进程。Windows 在虚拟机上重复。
Expand Down
34 changes: 23 additions & 11 deletions desktop/internal/pluginview/canvas.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ import (
"github.com/vivaui/viva/ui"
)

// A canvas draws its scene in view-box coordinates scaled to the width of the view: at zoom
// 1 the view box's width fills the canvas, and its top edge is at the canvas's top. The user
// drags the empty area to pan (pan) and zooms with ⌘ or Control and the wheel, about the
// pointer, or with the host's zoom buttons, about the middle (zoom). The plain wheel scrolls
// the page. A scene narrower than the canvas is centered; one shorter starts at the top.
// A canvas draws its scene in view-box coordinates. One without pan shows its whole view box
// at zoom 1: scaled to the width of the view, but never taller than the canvas, from the
// canvas's top-left corner, so a scene designed at the narrowest page keeps its size and
// stays whole as the window widens. One with pan is scaled to the width of the view: at
// zoom 1 the view box's width fills the canvas and its top edge is at the canvas's top; a
// scene narrower than the canvas is centered, one shorter starts at the top. The user drags
// the empty area to pan (pan) and zooms with ⌘ or Control and the wheel, about the pointer,
// or with the host's zoom buttons, about the middle (zoom). The plain wheel scrolls the page.
// There is no pinch to zoom: Viva reports no trackpad magnify yet (#2182).
const (
// canvasZoomStep is how far a zoom button zooms.
Expand All @@ -34,8 +37,8 @@ const (
// drag under way, and the groups the pointer rests on. It lives in memory and ends with the
// showing.
type canvasState struct {
// zoom is the user's zoom, 1 at the view's width; x and y are the view-box point at the
// canvas's top-left corner. placed reports that x and y were set.
// zoom is the user's zoom, 1 at the canvas's fitted scale (scale); x and y are the
// view-box point at the canvas's top-left corner. placed reports that x and y were set.
zoom, x, y float64
placed bool

Expand Down Expand Up @@ -98,16 +101,22 @@ func zoomRange(n *Node) (low, high float64) {
}

// scale is how many pixels one view-box unit takes in rect at the state's zoom; 0 for a
// view box without area, which draws nothing.
// view box without area, which draws nothing. At zoom 1 the view box's width fills rect,
// and a canvas without pan also keeps the view box's height within rect's.
func (s *canvasState) scale(rect gfx.Rect, n *Node) float64 {
if n.ViewBox[2] <= 0 || n.ViewBox[3] <= 0 || rect.Width <= 0 {
return 0
}
return float64(rect.Width) / n.ViewBox[2] * s.zoom
k := float64(rect.Width) / n.ViewBox[2]
if !n.Pan && rect.Height > 0 {
k = math.Min(k, float64(rect.Height)/n.ViewBox[3])
}
return k * s.zoom
}

// clamp keeps the visible part inside the view box: a view box narrower than the canvas
// is centered across it, one shorter than the canvas starts at its top.
// is centered across it when the canvas pans and starts at its leading edge when it does
// not; one shorter than the canvas starts at its top.
func (s *canvasState) clamp(rect gfx.Rect, n *Node) {
k := s.scale(rect, n)
if k <= 0 {
Expand All @@ -119,7 +128,10 @@ func (s *canvasState) clamp(rect gfx.Rect, n *Node) {
}
width, height := float64(rect.Width)/k, float64(rect.Height)/k
if width >= box[2] {
s.x = box[0] - (width-box[2])/2
s.x = box[0]
if n.Pan {
s.x -= (width - box[2]) / 2
}
} else {
s.x = math.Min(math.Max(s.x, box[0]), box[0]+box[2]-width)
}
Expand Down
36 changes: 35 additions & 1 deletion desktop/internal/pluginview/canvas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,40 @@ func TestCanvasScaleFollowsTheWidthAndTheZoom(t *testing.T) {
}
}

func TestCanvasWithoutPanShowsItsWholeViewBoxFromTheTopLeft(t *testing.T) {
// A scene designed 600 × 300 in a canvas 300 points tall.
n := &Node{Type: "canvas", ID: "preview", ViewBox: [4]float64{10, 20, 600, 300}}
state := &canvasState{zoom: 1}

// As wide as designed it takes one pixel a unit.
if got := state.scale(gfx.Rect{Width: 600, Height: 300}, n); got != 1 {
t.Fatalf("scale at the designed width = %v, want 1", got)
}
// Narrower, it shrinks to the width.
if got := state.scale(gfx.Rect{Width: 300, Height: 300}, n); got != 0.5 {
t.Fatalf("scale at half the width = %v, want 0.5", got)
}
// Wider, it keeps its size, whole, from the leading edge.
wide := gfx.Rect{Width: 1000, Height: 300}
if got := state.scale(wide, n); got != 1 {
t.Fatalf("scale wider than designed = %v, want the height kept", got)
}
state.clamp(wide, n)
if state.x != 10 || state.y != 20 {
t.Fatalf("wider than designed at (%v, %v), want the view box's top-left corner", state.x, state.y)
}
if got := state.viewport(wide, n); got != (Viewport{X: 10, Y: 20, Width: 1000, Height: 300}) {
t.Fatalf("viewport = %+v", got)
}

// Its zoom scales the fitted scene.
n.Zoom = &[2]float64{1, 2}
state = &canvasState{zoom: 2}
if got := state.scale(wide, n); got != 2 {
t.Fatalf("scale at zoom 2 = %v, want twice the fitted scale", got)
}
}

func TestCanvasClampKeepsTheViewInsideTheViewBox(t *testing.T) {
n := testCanvas()
n.ViewBox = [4]float64{100, 50, 400, 200}
Expand Down Expand Up @@ -99,7 +133,7 @@ func TestCanvasZoomAtKeepsThePointUnderThePointer(t *testing.T) {
func openCanvas(t *testing.T) (*backend, *Session, *canvasState) {
b := newBackend(t)
s := b.session()
s.Open()
show(s)
b.answered(s)
state := &canvasState{zoom: 2}
state.make()
Expand Down
27 changes: 24 additions & 3 deletions desktop/internal/pluginview/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,17 @@ func Draw(c ui.C, s *Session, host *Host, fill bool) {
}
}
r := &renderer{c: c, s: s, host: host}
root := ui.Column(c).FillWidth().Gap(10)
// The paint measures the width the nodes are laid out in: the view's, less the body's
// padding in a tool window. It sends open, and later resize; the scroll bars of the
// body float over it, so they take none of the width.
if s.measure == nil {
s.measure = s.measured
}
s.inset = 0
if fill {
s.inset = 2 * bodyPadding
}
root := ui.Column(c).FillWidth().Gap(10).Draw(s.measure)
if fill {
root = root.Fill()
}
Expand All @@ -91,19 +101,30 @@ func Draw(c ui.C, s *Session, host *Host, fill bool) {
nodes := s.View()
if fill && !hasTree(nodes) {
ui.Scroll(c).FillWidth().Grow(1).Children(func() {
ui.Column(c).FillWidth().Gap(10).PadX(10).PadY(10).Children(func() { r.nodes(c, nodes) })
ui.Column(c).FillWidth().Gap(10).PadX(bodyPadding).PadY(bodyPadding).Children(func() { r.nodes(c, nodes) })
})
return
}
body := ui.Column(c).FillWidth().Gap(10)
if fill {
body = body.Grow(1).PadX(10).PadY(10)
body = body.Grow(1).PadX(bodyPadding).PadY(bodyPadding)
}
r.fillTree = fill
body.Children(func() { r.nodes(c, nodes) })
})
}

// bodyPadding is the space around the nodes of a view that fills a tool window.
const bodyPadding = 10

// measured is the paint callback that gives the session the width its nodes are laid
// out in, and asks for the frame that sends a resize once it is due.
func (s *Session) measured(d *ui.DrawContext) {
if wait := s.Measured(d.Rect.Width - s.inset); wait > 0 && d.Canvas != nil {
d.Canvas.RequestFrameIn(wait)
}
}

// hasTree reports a tree among the top-level nodes: it scrolls by itself, so the body does not.
func hasTree(nodes []Node) bool {
for i := range nodes {
Expand Down
32 changes: 31 additions & 1 deletion desktop/internal/pluginview/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,17 @@ func drawView(t *testing.T, view string) *viewDriver {
b := newBackend(t)
b.answer = func(map[string]any) (json.RawMessage, bool, error) { return json.RawMessage(view), true, nil }
s := b.session()
s.Open()
show(s)
b.answered(s)
if !s.Drawn() {
t.Fatalf("the open answer was not drawn: %v", s.Err())
}
return paintView(t, b, s)
}

// paintView draws the session as a tool window body in a headless window 480 points wide.
func paintView(t *testing.T, b *backend, s *Session) *viewDriver {
t.Helper()
theme := ui.LightTheme()
background := theme.Background
invalidator := &app.Invalidator{}
Expand Down Expand Up @@ -240,3 +246,27 @@ func TestDrawShowsTheFailureWithRetry(t *testing.T) {

// errPluginFailed is a backend's own failure, shown untranslated under the host's words.
var errPluginFailed = errors.New("the index is locked")

func TestDrawSendsOpenAndResizeWithTheWidthItsNodesAreLaidOutIn(t *testing.T) {
b := newBackend(t)
b.answer = func(map[string]any) (json.RawMessage, bool, error) { return resizeView(), true, nil }
s := b.session()
s.Open()
d := paintView(t, b, s)
// The window is 480 points wide, and the body pads the nodes by 10 on each side.
if open := d.sent(); open["event"] != "open" || open["width"] != 460.0 {
t.Fatalf("event = %v, want open at the width the first paint measured", open)
}
d.repaint()
if err := d.driver.Resize(400, 400); err != nil {
t.Fatal(err)
}
if len(b.pending) != 0 {
t.Fatal("resize went out before the width settled")
}
b.now = b.now.Add(settleDelay)
d.repaint()
if resize := d.sent(); resize["event"] != "resize" || resize["width"] != 380.0 {
t.Fatalf("event = %v, want resize at the new width", resize)
}
}
Loading
Loading