Skip to content

Commit 0b6da27

Browse files
authored
feat(plugins): 视图事件带 width,画布可以写 resize 跟随窗口重新排版 (#109)
会报告宽度的宿主在每个视图事件里带 width(排版视图节点用的宽度,也是顶层画布的宽度),量到宽度之后才发 open;画布的 on 可以写 resize,宽度停止变化 400 ms 后宿主发 resize 和新的 width。契约 README 同时写明缩放为 1 时怎样放 viewBox:不写 pan 时显示整个 viewBox、从左上角开始,写了 pan 时宽度占满视图。 Core 画布校验接受 on: ["viewport", "resize"],schema 同步;Rust、Java、Node 三个 SDK 支持 width 和 resize 并补 README;settings.md 中英说明区域画布按 width 排版和旧宿主下的固定宽度;更新记录登记本次补充,并把 #105 的条目移到时间线。取代 #107。
1 parent 9dea6aa commit 0b6da27

16 files changed

Lines changed: 137 additions & 24 deletions

File tree

‎rust/crates/plugin-ui/src/canvas.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ pub(crate) fn check(walk: &mut Walk<'_>, value: &Value, path: &str) -> Checked<(
9898
));
9999
}
100100
}
101-
events(map, path, &["viewport"])?;
101+
events(map, path, &["viewport", "resize"])?;
102102
let nodes = map
103103
.get("nodes")
104104
.ok_or_else(|| fail(at(path, "nodes"), "Required."))?;

‎rust/crates/plugin-ui/src/lib.rs‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ mod tests {
204204
]}
205205
]},
206206
{"type":"canvas","id":"graph","label":"Module graph","height":200,"viewBox":[0,0,400,200],
207-
"pan":true,"zoom":[0.5,4],"on":["viewport"],"nodes":[
207+
"pan":true,"zoom":[0.5,4],"on":["viewport","resize"],"nodes":[
208208
{"type":"rect","x":0,"y":0,"w":400,"h":200,"radius":8,"fill":{"linear":[[0,"#1E1F22"],[1,"$surface"]],"angle":90}},
209209
{"type":"path","d":"M 10 10 L 390 190","stroke":{"light":"#3574F0","dark":"#548AF7"},"width":2,"dash":[4,2],"cap":"round"},
210210
{"type":"group","id":"node.core","x":40,"y":40,"w":120,"h":48,"radius":6,"on":["click"],
@@ -424,6 +424,14 @@ mod tests {
424424
}
425425
let bad_box = json!([{"type":"canvas","id":"c","label":"C","height":10,"viewBox":[0,0,0,10],"nodes":[]}]);
426426
assert_eq!(field(&context, bad_box), "view[0].viewBox");
427+
let on = |events: serde_json::Value| {
428+
json!([{"type":"canvas","id":"c","label":"C","height":10,"viewBox":[0,0,10,10],"on":events,"nodes":[]}])
429+
};
430+
assert_eq!(field(&context, on(json!(["click"]))), "view[0].on[0]");
431+
assert_eq!(
432+
field(&context, on(json!(["resize", "resize"]))),
433+
"view[0].on[1]"
434+
);
427435
}
428436

429437
#[test]

‎shared/plugins/contract/v1/README.md‎

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5129,7 +5129,7 @@ event, with these params:
51295129

51305130
| Field | Meaning |
51315131
| --- | --- |
5132-
| `event` | `open`, `close`, `click`, `change`, `select`, `activate`, `viewport`, or `settings` (below) |
5132+
| `event` | `open`, `close`, `click`, `change`, `select`, `activate`, `viewport`, `resize`, or `settings` (below) |
51335133
| `instance` | Host-chosen string naming this showing of the view. A new `open` gets a new instance; the same tool window open in two workspaces has two. |
51345134
| `surface` | `{toolWindowID}` or `{settingRegionID}`: the declaration being drawn |
51355135
| `language` | Interface language tag, such as `en` or `zh-Hans`. View text is plugin content; the backend returns it in this language. |
@@ -5140,6 +5140,7 @@ event, with these params:
51405140
| `setting` | `change` of a control bound to a setting: the setting ID |
51415141
| `item` | `select`, `activate`: the tree item ID; `node` is the tree's ID |
51425142
| `viewport` | `viewport`: `{x, y, width, height}`, the visible part of the canvas in view-box coordinates |
5143+
| `width` | Every event, from a host that reports it: the width in points the host lays the view's nodes out in, which is also the width of a canvas at the top level, a whole number of at least 1. A host that reports it sends `open` only once it knows the width. |
51435144

51445145
| Event | Sent when | Result |
51455146
| --- | --- | --- |
@@ -5149,13 +5150,14 @@ event, with these params:
51495150
| `change` | A `textField` commits on Enter, focus loss, or submit; a `select`, `segmented`, or `toggle` changes | Any |
51505151
| `select`, `activate` | A tree item is selected; double-clicked or activated with Return | Any |
51515152
| `viewport` | 400 ms after a pan or zoom stops, only for a canvas whose `on` lists `viewport` | Any |
5153+
| `resize` | 400 ms after the view's `width` stops changing, only while the drawn view has a canvas whose `on` lists `resize` | Any |
51525154
| `settings` | `effective` or `draft` changes: the user edits a declared row or a bound control, applies, cancels, or a value changes elsewhere | Any |
51535155

51545156
Hovering, pressing, keyboard focus and focus rings, tooltips, tree expansion,
51555157
scrolling, panning, zooming, and typing never reach the plugin. A text field
51565158
does not call the backend per keystroke. The host sends one event at a time
5157-
per instance, in order, and may merge waiting `viewport` or `settings` events
5158-
into the latest one. A view changes only in answer to an event; this version
5159+
per instance, in order, and may merge waiting `viewport`, `resize`, or
5160+
`settings` events into the latest one. A view changes only in answer to an event; this version
51595161
has no way for a plugin to push a view on its own.
51605162

51615163
#### Results and patches
@@ -5241,11 +5243,35 @@ follows [source-backed settings](#source-backed-settings-editors).
52415243
```
52425244

52435245
A canvas draws its `nodes` in order, in `viewBox` coordinates
5244-
(`[x, y, width, height]`) scaled to the width of the view and `height` points
5246+
(`[x, y, width, height]`), in an area as wide as the view and `height` points
52455247
tall (1–2000). `label` is the scene's accessible name. `pan` lets the user
52465248
drag the empty area, and `zoom` is the `[minimum, maximum]` range around 1
52475249
(0.05–20); the host draws the zoom buttons. The scroll wheel scrolls the page;
52485250
⌘ or Ctrl with the wheel zooms, and so does a pinch on a host that reports one.
5251+
`on` lists the events the canvas sends: `viewport` and `resize` (see
5252+
[Events](#events)).
5253+
5254+
At zoom 1 the host scales the `viewBox` in one of two ways:
5255+
5256+
- **Without `pan`**, the whole `viewBox` shows: it is scaled to the width of
5257+
the view, but no taller than `height`, and starts at the area's top-left
5258+
corner. A scene whose `viewBox` is as wide as the view and `height` tall
5259+
draws at one point a unit. In a wider view it keeps that size, whole, with
5260+
the area's trailing part left empty; in a narrower one it shrinks to the
5261+
width and leaves the area's bottom empty.
5262+
- **With `pan`**, the `viewBox`'s width fills the view and its top edge is at
5263+
the area's top; the user pans to the parts below the area. Zoomed out until
5264+
the `viewBox` is narrower than the area, it is centered across it.
5265+
5266+
A scene that follows the window lays itself out for the event's `width`: it
5267+
gives its `viewBox` that width, picks `height` for what it draws there, and
5268+
lists `resize` in `on`, so the host sends `resize` with the new `width` once
5269+
the window stops being resized and the plugin returns the scene laid out
5270+
again. Until the answer arrives the host scales the scene it has, as above. A
5271+
host that does not report `width` sends no `resize` and may refuse a canvas
5272+
that lists it, so a plugin lists `resize` only in answer to an event that
5273+
carried `width`, and otherwise lays the scene out for a width of its own
5274+
choosing.
52495275

52505276
| `type` | Fields |
52515277
| --- | --- |

‎shared/plugins/contract/v1/plugin-ui.schema.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@
341341
}
342342
},
343343
"canvas": {
344-
"description": "A vector scene the host draws natively in view-box coordinates, scaled to the view width. Pan, zoom, hover states and tooltips stay in the host; viewport sends viewport events after the user pans or zooms. Canvas text is not searchable; a settings region names its words in keywords.",
344+
"description": "A vector scene the host draws natively in view-box coordinates, in an area as wide as the view and height points tall. Without pan, the whole view box shows at zoom 1, scaled to the view width but no taller than height, from the top-left corner; with pan, its width fills the view. Pan, zoom, hover states and tooltips stay in the host; viewport sends viewport events after the user pans or zooms, and resize sends resize events with the new view width after the window is resized. Canvas text is not searchable; a settings region names its words in keywords.",
345345
"type": "object",
346346
"additionalProperties": false,
347347
"required": ["type", "id", "label", "height", "viewBox", "nodes"],
@@ -365,7 +365,7 @@
365365
"maxItems": 2,
366366
"items": { "type": "number", "minimum": 0.05, "maximum": 20 }
367367
},
368-
"on": { "type": "array", "maxItems": 1, "uniqueItems": true, "items": { "const": "viewport" } },
368+
"on": { "type": "array", "maxItems": 2, "uniqueItems": true, "items": { "enum": ["viewport", "resize"] } },
369369
"nodes": { "$ref": "#/$defs/shapes" }
370370
}
371371
},

‎shared/plugins/reference/domains/settings.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
- `keywords`:1–32 个,忽略大小写互不相同,每个 1–64 个字符、不换行、首尾没有空白。设置搜索除了页面标题和声明的行,也按这些词找到页面,并打开到这个插件的部分。
8787
- `view`:`{function}`,插件后端的视图函数,画在页面标题和插件声明的行之间。视图可以放宿主控件、画布,也可以用绑定控件编辑本插件的设置(和声明的行改的是同一份草稿)、放本插件按钮和显示本插件工具解析结果的工具行,但不能放树。需要 `backend`、`ui` 0 和 `ui.contribute` 权限;页面打开时宿主启动后端。
8888
- 只写关键词的区域不需要后端。区域本身不保存任何值。声明区域也算使用了 `settings`。
89+
- 区域里的画布要跟随窗口宽度时,按事件里的 `width` 排版:`viewBox` 的宽度就设成 `width`,`height` 取在这个宽度下画出来需要的高度,并在画布的 `on` 里写 `resize`。用户调整窗口大小、停下 400 ms 后,宿主发 `resize` 和新的 `width`,插件按新宽度返回重新排版的场景,场景始终一个单位一个点。不写 `pan` 时,宿主在答复到达前按[画布](../../contract/v1/README.md#canvas)的规则显示整个 `viewBox`,不会放大到裁掉下半部分。事件里没有 `width` 的旧宿主不发 `resize`,也可能拒绝写了它的画布,所以这时不写 `resize`,按固定宽度排版:桌面端窗口最小时,语言页的区域约 610 点宽,构建系统页和编译器页右侧详情里的区域约 340 点宽。
8990

9091
### 两层保存与生效顺序
9192

@@ -238,6 +239,7 @@ Content that declared rows cannot draw goes in `settingRegions` (at most 16, uni
238239
- `keywords`: 1–32, distinct ignoring case, each 1–64 characters without line breaks or surrounding spaces. Settings search finds the page by them in addition to the page title and the declared rows, and opens the page at this plugin's part.
239240
- `view`: `{function}`, a view function of the plugin's backend, drawn between the page header and the plugin's declared rows. The view may hold host controls and canvases, edit the plugin's own settings through bound controls (the same draft the declared rows edit), show the plugin's buttons and tool rows with how its tools resolve, but no tree. It needs `backend`, `ui` 0, and the `ui.contribute` permission; the host starts the backend when the page opens.
240241
- A region with keywords only needs no backend. A region stores no values. Declaring regions counts as using `settings`.
242+
- A canvas in a region that follows the window lays itself out for the event's `width`: its `viewBox` is `width` wide, its `height` is what the scene needs at that width, and its `on` lists `resize`. Once the user stops resizing the window for 400 ms, the host sends `resize` with the new `width`, and the plugin answers the scene laid out again, so it always draws at one point a unit. Without `pan`, the host shows the whole `viewBox` until the answer arrives (see [canvas](../../contract/v1/README.md#canvas)), never scaled up so far that its bottom is cut off. An older host sends no `width` and no `resize` and may refuse a canvas that lists it, so without `width` a plugin leaves `resize` out and lays the scene out for a fixed width: in Lithe desktop's smallest window a region is about 610 points wide on a language page and about 340 in the detail pane of a build system or compiler page.
241243

242244
### Two layers and their order
243245

‎shared/plugins/reference/protocol-changelog.md‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
## 待合入
1616

17-
- `ui` 0、`settings` 0 内的说明补充(API-13、API-19):契约 README 写明画布 `image` 节点怎样读取:路径留在插件包内、经过真实目录、指向普通 PNG 文件,单个文件最多 256 KiB、每边最多 1024 像素(预算表新增一行);宿主在场景第一次画到它时于窗口线程外读取,保留到视图关闭,读到之前或读取失败时场景不画它,失败只记一次日志。`image` 节点的字段表补上 `opacity`。文字是一行,基线在 `y`,`align` 把行首(默认)、中点或行尾放在 `x`;渐变 `angle` 与 CSS `linear-gradient` 一致(0 向上、90 向右、默认 180 向下),渐变线穿过形状中心;分组的 `x`、`y` 放置原点,`scale` 缩放内部,`children`、`w`、`h`、`radius` 都在分组自己的坐标里;捏合缩放只在宿主能上报捏合时可用。fixture `fixtures/ui/view-results.json` 的文字基线移到卡片中间。[能力与协议版本对照](capabilities-and-versions.md)删掉“插件视图尚未在桌面端绘制”的脚注,`domains/ui.md`、`domains/settings.md` 的桌面端一行改为已呈现插件视图、设置区域和包内翻译 `l10n/`([1lck/Lithe-IDEA#2181](https://github.com/1lck/Lithe-IDEA/issues/2181))。没有新增版本号、宿主方法、通知或 SDK 版本。实现见 [#105](https://github.com/Lithe-IDEA/Lithe-core-rust/pull/105)。
17+
- `ui` 0 内的补充(API-13):视图事件新增 `width` 和 `resize`,画布可以跟随窗口宽度重新排版。会报告宽度的宿主在每个视图事件里带 `width`(排版视图节点用的宽度,单位为点,至少为 1 的整数,也是顶层画布的宽度),并且量到宽度之后才发 `open`;画布的 `on` 除 `viewport` 外可以写 `resize`,视图里有这样的画布时,宽度停止变化 400 ms 后宿主发 `resize` 和新的 `width`,等待中的 `resize` 可以合并成最新的一个。插件只有在收到带 `width` 的事件后才写 `resize`:不报告宽度的宿主不发 `resize`,它的 Core 也会拒绝写了 `resize` 的画布。契约 README 同时写明画布在缩放 1 时怎样放 `viewBox`:不写 `pan` 时显示整个 `viewBox`,按视图宽度缩放但不超过 `height`,从区域左上角开始;写了 `pan` 时宽度占满视图、上边对齐区域顶部,缩小到比区域窄时水平居中。以前的说明只有“按视图宽度缩放、`height` 点高”,在更宽的窗口里会裁掉场景下半部分。Core 的画布校验接受 `on: ["viewport", "resize"]`,`plugin-ui.schema.json` 的 canvas 说明和 `on` 同步;Rust SDK 新增 `ViewEventKind::Resize` 和 `ViewEvent::width`,Java SDK 新增 `Kind.RESIZE` 和 `width()`,Node SDK 的 `ViewEventKind` 加上 `resize`、`ViewEvent` 加上 `width`、画布 `on` 的类型放宽,三个 SDK 的 README 补充用法;`domains/settings.md` 中英两段说明区域画布怎样按 `width` 排版,以及旧宿主下的固定宽度(语言页约 610 点、构建系统页和编译器页右侧详情约 340 点)([1lck/Lithe-IDEA#2181](https://github.com/1lck/Lithe-IDEA/issues/2181))。没有新增能力版本、宿主方法、通知或 SDK 版本,取代 [#107](https://github.com/Lithe-IDEA/Lithe-core-rust/pull/107)。
1818

1919
## 按协议查首次出现
2020

@@ -144,6 +144,12 @@
144144

145145
## 时间线(新的在上)
146146

147+
### 2026-11-19
148+
149+
| PR | 修改人 | API 编号 | 协议 / 能力 | 改了什么 |
150+
| --- | --- | --- | --- | --- |
151+
| [#105](https://github.com/Lithe-IDEA/Lithe-core-rust/pull/105) | [@xiaoyumuxi](https://github.com/xiaoyumuxi) | API-13、API-19 | `ui` 0、`settings` 0 内的说明补充(API-13、API-19) | 契约 README 写明画布 `image` 节点怎样读取:路径留在插件包内、经过真实目录、指向普通 PNG 文件,单个文件最多 256 KiB、每边最多 1024 像素(预算表新增一行);宿主在场景第一次画到它时于窗口线程外读取,保留到视图关闭,读到之前或读取失败时场景不画它,失败只记一次日志。`image` 节点的字段表补上 `opacity`。文字是一行,基线在 `y`,`align` 把行首(默认)、中点或行尾放在 `x`;渐变 `angle` 与 CSS `linear-gradient` 一致(0 向上、90 向右、默认 180 向下),渐变线穿过形状中心;分组的 `x`、`y` 放置原点,`scale` 缩放内部,`children`、`w`、`h`、`radius` 都在分组自己的坐标里;捏合缩放只在宿主能上报捏合时可用。fixture `fixtures/ui/view-results.json` 的文字基线移到卡片中间。[能力与协议版本对照](capabilities-and-versions.md)删掉“插件视图尚未在桌面端绘制”的脚注,`domains/ui.md`、`domains/settings.md` 的桌面端一行改为已呈现插件视图、设置区域和包内翻译 `l10n/`([1lck/Lithe-IDEA#2181](https://github.com/1lck/Lithe-IDEA/issues/2181))。没有新增版本号、宿主方法、通知或 SDK 版本。 |
152+
147153
### 2026-11-18
148154

149155
| PR | 修改人 | API 编号 | 协议 / 能力 | 改了什么 |

‎shared/plugins/sdk/java/README.md‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,11 @@ handler answers a `ViewResult`:
104104
Nodes are plain JSON maps shaped as
105105
[`plugin-ui.schema.json`](../../contract/v1/plugin-ui.schema.json) describes.
106106
Core checks every answer, and the host draws nothing that Core refused. Return
107-
view text in `event.language()`.
107+
view text in `event.language()`. A host that reports the view's width sends
108+
it as `event.width()` on every event; a canvas that follows the window lays
109+
itself out at that width, lists `resize` in its `on`, and answers
110+
`Kind.RESIZE` with the scene laid out again
111+
([Canvas](../../contract/v1/README.md#canvas)).
108112

109113
```java
110114
backend.view("explorer.draw", (event, context) -> {

‎shared/plugins/sdk/java/plugin-sdk/src/main/java/io/github/litheidea/plugin/sdk/ViewEvent.java‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ public enum Kind {
2828
SELECT("select"),
2929
ACTIVATE("activate"),
3030
VIEWPORT("viewport"),
31+
/**
32+
* The view's width stopped changing, for a view with a canvas whose
33+
* {@code on} lists {@code resize}; {@link ViewEvent#width()} is the new width.
34+
*/
35+
RESIZE("resize"),
3136
/** The effective or draft setting values changed. */
3237
SETTINGS("settings");
3338

@@ -96,6 +101,7 @@ public double height() {
96101
private final String setting;
97102
private final String item;
98103
private final Viewport viewport;
104+
private final Double width;
99105

100106
private ViewEvent(Map<String, Object> params) {
101107
kind = Kind.parse(required(params, "event"));
@@ -128,6 +134,7 @@ private ViewEvent(Map<String, Object> params) {
128134
Map<String, Object> area = object(box);
129135
viewport = new Viewport(number(area, "x"), number(area, "y"), number(area, "width"), number(area, "height"));
130136
}
137+
width = params.get("width") == null ? null : number(params, "width");
131138
}
132139

133140
/**
@@ -213,6 +220,15 @@ public Viewport viewport() {
213220
return viewport;
214221
}
215222

223+
/**
224+
* The width in points the host lays the view out in, on every event from a
225+
* host that reports it; or null. List {@code resize} in a canvas's
226+
* {@code on} only when it is present: a host without it may refuse the canvas.
227+
*/
228+
public Double width() {
229+
return width;
230+
}
231+
216232
private static PluginSdkError invalid() {
217233
return new PluginSdkError(INVALID_EVENT, "Invalid view event.");
218234
}

0 commit comments

Comments
 (0)