Repository navigation
feat(workbench): open Mermaid diagrams in full-screen zoomable viewer - #7128
Conversation
Allow expanding rendered Mermaid diagrams from the header button or by clicking the diagram (preserving click-to-edit in editable Markdown files), re-render light-mode diagrams with the dark palette for contrast inside the dark viewer, add wheel/pinch/double-click/plain-key zoom-out and zoom-in controls with bounds, and keep the viewer's close button clear of Windows/Linux top-right window caption controls. Generated-by: Maka
M4n5ter
left a comment
There was a problem hiding this comment.
English
Overall this is a clean, focused PR—pointer-anchored zoom math, preserving click-to-edit in editable Markdown previews, viewerGeneration cancellation for light-mode re-rendering, and binding plain zoom keys alongside mod+ shortcuts are all solid. There is one real blocker on macOS/Linux trackpad pinch and a couple of smaller issues below.
1. [Blocker] pinch={{ scale }}, onPinch, and onPinchEnd on the same <View> double-scale and square zoomPercent on macOS/Linux trackpad pinch
In products/maka/workbench/app/ImageViewer.tsx, Image viewport binds all three props on the same <View>:
pan={{ x, y }}
pinch={{ scale }}
onWheel={handleWheel}
onPinch={handlePinch}
onPinchEnd={handlePinchEnd}In nsh, putting onPinch on a view that also has native pinch / onPinchEnd causes both paths to run on every trackpad pinch:
third-party/nsh/crates/nsh-core/src/input.rs:622-631(transforms) reports native pinch support whenevervalues.pinch.is_some()orEventKind::PinchEndis registered.third-party/nsh/crates/nsh-core/src/input.rs:741-755(wheel_listener) returnstakes = !scroll_seen && !scrolls = falsewhen theonPinchlistener itself supports native pinch.- In both
third-party/nsh/crates/nsh-platform-macos/src/input.rs:2995-3054(magnify→pinch_event) andthird-party/nsh/crates/nsh-platform-linux/src/input.rs:2418-2485(touchpad_pinch→pinch_event),pinch_eventstill dispatchesonPinch(handlePinch→zoomBy) and returnsfalse, so execution falls through totrackpad_transform:- During the gesture, native
anim.transform_movedsetsscale = start * t.scaleon the UI thread while JShandlePinchsimultaneously queuesscale.adjust(applied, 0)(nsh-core/src/animation/graph.rs:606-614multiplies the current value), fighting and overshooting on every frame. - When the gesture ends,
trackpad_transformfiresonPinchEnd({ scale: t.scale }).handlePinchEndthen computesraw = currentScale * gestureScaleeven thoughcurrentScalewas already multiplied byt.scaleduringhandlePinch—squaringcurrentScaleandzoomPercent(e.g., pinching to1.44×jumps the readout to207%), permanently desyncingcurrentScalefrom the renderedscale, and applying a bogus correction viascale.adjust(clamped / raw, 0)when hittingminScale/maxScale.
- During the gesture, native
- Safari Web (
third-party/nsh/crates/nsh-platform-web/web/nsh.js:5135-5165) hits the sametakes = falsecheck and skipse.preventDefault(), zooming the page alongsidehandlePinch.
Suggested fix: Don't place onPinch on the same <View> as pinch={{ scale }} / onPinchEnd. Putting onPinch on an inner flex: 1 child <View> inside the outer pinch={{ scale }} / onPinchEnd viewport lets wheel_listener match the inner view first with takes = true (so trackpad pinches are owned exclusively by onPinch), while touchscreen two-finger pinches still walk up to the outer pinch binding.
2. [Minor] onPress on Image viewport adds an invisible dead Tab stop
third-party/nsh/crates/nsh-core/src/props.rs:542-544 (ViewProps::focus_stop) defaults focusable to takes_presses (true when onPress is attached), which macOS is_key_stop (nsh-platform-macos/src/input.rs:1233-1250) treats as a keyboard Tab stop. Since Image viewport has no focusStyle and handleViewportPress ignores keyboard activation (clickCount !== 2), pressing Tab in the viewer lands on an invisible no-op stop. Adding focusable={false} to the viewport <View> avoids this.
3. Tests
- The new pointer/gesture paths in
ImageViewer(wheel zoom/pan, touchpad pinch, double-click) currently have no test coverage, which is why the trackpad pinch double-scaling slipped through. Adding a regression test covering pinch/wheel behavior once fixed would give much stronger confidence. light_mode_viewer_renders_dark_palette_diagraminproducts/maka/workbench/src/bindings/diagrams.rsasserts hardcoded hex tokens (#1c1d1f/#f0f1f3), which couples the test to specific theme shades; asserting against the dark palette or dark-vs-light contrast without hardcoded literals would be more resilient.
4. Minor cleanup (non-blocking)
products/maka/workbench/app/ImageViewer.tsx: theownTitleBar ? { ... } : { ... }header ternary duplicates ~7 identical layout properties; sharing the common base style object would make it cleaner.products/maka/workbench/app/MermaidView.tsx: thediagram(interactive)helper inside<Each>is only called once per branch and duplicatesobjectFit: 'contain'; flattening withconst interactive = !props.editableis simpler to read.
简体中文
整体实现比较干净聚焦:以指针为锚点的缩放公式、可编辑 Markdown 预览中保留点击编辑、亮色模式下用 viewerGeneration 防竞态重绘暗色图、以及额外绑定普通缩放键绕开桌面菜单栏抢占 mod+plus/minus/0 都比较稳妥。不过存在一个 macOS/Linux 触控板捏合必现的阻塞问题,以及几处小问题:
1. [Blocker] ImageViewer.tsx 在同一个 <View> 上同时绑定 pinch={{ scale }}、onPinch 和 onPinchEnd,导致 macOS/Linux 触控板捏合双重缩放且 zoomPercent 平方
在 products/maka/workbench/app/ImageViewer.tsx 中,Image viewport 同时挂了:
pan={{ x, y }}
pinch={{ scale }}
onWheel={handleWheel}
onPinch={handlePinch}
onPinchEnd={handlePinchEnd}在 nsh 的事件分发里,把 onPinch 和原生 pinch / onPinchEnd 放在同一个 view 上,会导致每次触控板捏合两条路径同时生效:
third-party/nsh/crates/nsh-core/src/input.rs:622-631(transforms)只要检测到pinch或EventKind::PinchEnd,就会判定该 view 支持原生 pinch。third-party/nsh/crates/nsh-core/src/input.rs:741-755(wheel_listener)在命中的onPinch监听节点自身也支持原生 pinch 时,返回takes = !scroll_seen && !scrolls = false。- 在 macOS(
third-party/nsh/crates/nsh-platform-macos/src/input.rs:2995-3054,magnify→pinch_event)和 Linux(third-party/nsh/crates/nsh-platform-linux/src/input.rs:2418-2485,touchpad_pinch→pinch_event)上,pinch_event依然会派发onPinch(执行handlePinch→zoomBy),但因为返回false,外层不会提前return,而是继续跌落进trackpad_transform:- 捏合过程中,UI 线程的
anim.transform_moved不断把scale设为start * t.scale,同时 JS 线程的handlePinch排队下发scale.adjust(applied, 0)(nsh-core/src/animation/graph.rs:606-614直接乘在当前值上),每帧互相覆盖和超调。 - 手势结束时,
trackpad_transform触发onPinchEnd({ scale: t.scale }),handlePinchEnd计算raw = currentScale * gestureScale——但currentScale在handlePinch中已经乘过一遍t.scale,这里会直接再乘一遍(平方)(例如捏合到1.44×,松手后显示跳到207%),导致currentScale与实际渲染的scale永久失步;触及minScale/maxScale时scale.adjust(clamped / raw, 0)还会把画面错误拉偏。
- 捏合过程中,UI 线程的
- Safari Web(
third-party/nsh/crates/nsh-platform-web/web/nsh.js:5135-5165)也会因为takes = false跳过e.preventDefault(),导致捏合时整个页面跟着缩放。
建议修法:不要把 onPinch 和 pinch={{ scale }} / onPinchEnd 放在同一个 <View> 上。把 onPinch 移到外层 pinch={{ scale }} / onPinchEnd 里面的一个 flex: 1 子 <View> 上:触控板捏合时 wheel_listener 会先命中内层且 takes = true(由 onPinch 独占处理),而触屏双指捏合不走 onPinch,仍会向上命中外层的 pinch 绑定。
2. [Minor] Image viewport 绑定 onPress 未设置 focusable={false},新增了一个不可见的无效 Tab 焦点
third-party/nsh/crates/nsh-core/src/props.rs:542-544(ViewProps::focus_stop)会将带 onPress 的 view 默认视为键盘焦点(self.focusable.unwrap_or(takes_presses)),macOS 的 is_key_stop(nsh-platform-macos/src/input.rs:1233-1250)据此将其加入 Tab 顺序。由于 Image viewport 没有 focusStyle,且 handleViewportPress 对键盘触发无响应(clickCount !== 2),在全屏查看器里按 Tab 会多出一个看不见且按 Enter/Space 无效的焦点停顿。给该 <View> 加上 focusable={false} 即可。
3. 测试反馈
- 本次新增的
ImageViewer手势/指针交互(滚轮缩放与平移、触控板捏合、双击缩放)目前没有对应测试覆盖,上面的触控板捏合双重缩放 bug 也因此漏测。建议修复后补上针对 pinch/wheel 行为的回归测试。 products/maka/workbench/src/bindings/diagrams.rs中的light_mode_viewer_renders_dark_palette_diagram直接断言了硬编码色值#1c1d1f和#f0f1f3,和具体主题色阶耦合较紧;改为校验与暗色面板一致或与亮色输出不同会更耐重构。
4. 少量代码清理建议(非阻塞)
products/maka/workbench/app/ImageViewer.tsx:顶栏ownTitleBar ? { ... } : { ... }三元表达式里重复写了 7 个完全相同的布局属性,提取公共基础对象再展开会清爽不少。products/maka/workbench/app/MermaidView.tsx:<Each>内部的diagram(interactive)辅助函数每条分支只调用一次且重复了objectFit: 'contain',直接用const interactive = !props.editable摊平更易读。
Move trackpad pinch listening inside the native touch viewport so macOS and Linux cannot apply the same gesture twice. Cover pinch, wheel, and double-click zoom restoration, remove the viewport's dead Tab stop, and simplify the reviewed styles and palette assertions. Generated-by: Maka
|
Addressed in
Local verification passed: Workbench 77 lib + 3 Markdown tests, NSH testing 114 tests, 225 Node tests, generated TypeScript project check, and real native Desktop Mermaid tests in dark-wide and light-wide. |
Summary
Mermaid diagrams rendered in conversations and Bot replies previously displayed as a static
<Image>squeezed into the chat column width with no way to zoom or inspect wide flowcharts.MermaidView.tsx,locale-conversation.ts): add a header full-screen expand button beside "Show diagram source" and make the rendered diagram itself clickable to openImageViewer(while preserving click-to-edit ineditableMarkdown file previews). Closing the viewer restores focus to the button that opened it.MermaidView.tsx): Pele SVGs have transparent backgrounds, so when opening a Mermaid diagram from light mode, render a dark-palette copy for the always-darkImageViewerwith generation cancellation so dark lines and labels stay readable.ImageViewer.tsx):linesandCtrl/Cmd+ wheel), touchpad pinch zoom (onPinch), touchpad two-finger panning (points), and double-click toggle between zoomed and fitted views.{width} × {height} · {zoom}%), and bind plain zoom keys (+/-/0/1) alongsidemod+shortcuts across the full-screen modal so desktop menu bars or canvas drags cannot block keyboard zoom-out.ImageViewer.tsx): align the viewer header withTITLE_BAR_HEIGHT, inset left/right bycontrolsLeft(true)andcontrolsRight(true)so the viewer close button sits clear of the native Windows minimize/maximize/close controls rather than stacking underneath or overlapping them, and back the top-right caption area withC.chromein light mode so dark native glyphs remain visible.Verification
Passed locally on macOS:
cargo test -p maka-workbench— 80 passed (77 lib + 3 markdown, 0 failed, 1 ignored), including 3 new headless UI regressions inbindings::diagrams::tests:diagram_opens_full_screen_zooms_in_and_out_and_closeswindows_viewer_close_button_stays_clear_of_caption_controlslight_mode_viewer_renders_dark_palette_diagramcargo test --locked -p maka-cli --features desktop-tests --test native-desktop -- desktop_mermaid_renders_markdown_and_toggles_its_source --nocapture— passed in bothdark-wideandlight-wide(real Host + off-screen desktop renderer), exercising inline rendering, source toggle, full-screen open, zoom-in pixel growth, zoom-out, and close.node --test products/maka/workbench/tests/*.test.mjs— all passed (includingmermaid.test.mjs).cargo run -p maka-workbench --example ui_types+tsc --noEmit— 0 errors.cargo fmt -p maka-workbench -p maka-terminal-shell -- --check,biome format,scripts/asf-license-headers.mjs check, andgit diff --check— all passed.Not run locally: native Windows desktop runtime (verified Windows caption inset coordinates in headless tests with
Os::Windows+title_bar.right = 138px; CI pending).Desktop screenshots
Captured from the real native desktop test against an isolated Host in dark and light appearances (1280×800):
AI use
Select exactly one:
Tool(s) and scope: Maka investigated the Mermaid rendering and image viewer paths, implemented the changes and regressions, ran local tests and desktop captures, and drafted this PR description under human direction.
Checklist
Does this PR entail a change in behavior?