Skip to content

feat(workbench): open Mermaid diagrams in full-screen zoomable viewer - #7128

Merged
me2seeks merged 2 commits into
rust-previewfrom
feat/mermaid-zoom-viewer
Nov 21, 2026
Merged

me2seeks merged 2 commits into
rust-previewfrom
feat/mermaid-zoom-viewer

Conversation

@me2seeks

@me2seeks me2seeks commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

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.

  • Open Mermaid diagrams full screen (MermaidView.tsx, locale-conversation.ts): add a header full-screen expand button beside "Show diagram source" and make the rendered diagram itself clickable to open ImageViewer (while preserving click-to-edit in editable Markdown file previews). Closing the viewer restores focus to the button that opened it.
  • Light-mode contrast in the dark viewer (MermaidView.tsx): Pele SVGs have transparent backgrounds, so when opening a Mermaid diagram from light mode, render a dark-palette copy for the always-dark ImageViewer with generation cancellation so dark lines and labels stay readable.
  • Smoother zoom-out, zoom-in, and navigation (ImageViewer.tsx):
    • Add pointer-anchored mouse wheel zoom (lines and Ctrl/Cmd + wheel), touchpad pinch zoom (onPinch), touchpad two-finger panning (points), and double-click toggle between zoomed and fitted views.
    • Clamp zoom bounds, display live zoom percentage ({width} × {height} · {zoom}%), and bind plain zoom keys (+/-/0/1) alongside mod+ shortcuts across the full-screen modal so desktop menu bars or canvas drags cannot block keyboard zoom-out.
  • Windows/Linux top-right window caption safety (ImageViewer.tsx): align the viewer header with TITLE_BAR_HEIGHT, inset left/right by controlsLeft(true) and controlsRight(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 with C.chrome in 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 in bindings::diagrams::tests:
    • diagram_opens_full_screen_zooms_in_and_out_and_closes
    • windows_viewer_close_button_stays_clear_of_caption_controls
    • light_mode_viewer_renders_dark_palette_diagram
  • cargo test --locked -p maka-cli --features desktop-tests --test native-desktop -- desktop_mermaid_renders_markdown_and_toggles_its_source --nocapture — passed in both dark-wide and light-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 (including mermaid.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, and git 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):

Appearance Inline Mermaid diagram Full-screen viewer (zoomed to 156%)
Dark Dark theme inline Mermaid diagram with full-screen expand button Dark theme Mermaid diagram zoomed to 156% in full-screen ImageViewer
Light Light theme inline Mermaid diagram with full-screen expand button Light theme Mermaid diagram re-rendered with dark palette in full-screen ImageViewer

AI use

Select exactly one:

  • No generative tool made a substantive contribution
  • Generative tooling made a substantive contribution

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

  • Tests cover the change and fail without it
  • Format, typecheck and the affected suites pass locally

Does this PR entail a change in behavior?

  • Yes — described under Summary above
  • No

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 M4n5ter left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 whenever values.pinch.is_some() or EventKind::PinchEnd is registered.
  • third-party/nsh/crates/nsh-core/src/input.rs:741-755 (wheel_listener) returns takes = !scroll_seen && !scrolls = false when the onPinch listener itself supports native pinch.
  • In both third-party/nsh/crates/nsh-platform-macos/src/input.rs:2995-3054 (magnify → pinch_event) and third-party/nsh/crates/nsh-platform-linux/src/input.rs:2418-2485 (touchpad_pinch → pinch_event), pinch_event still dispatches onPinch (handlePinch → zoomBy) and returns false, so execution falls through to trackpad_transform:
    1. During the gesture, native anim.transform_moved sets scale = start * t.scale on the UI thread while JS handlePinch simultaneously queues scale.adjust(applied, 0) (nsh-core/src/animation/graph.rs:606-614 multiplies the current value), fighting and overshooting on every frame.
    2. When the gesture ends, trackpad_transform fires onPinchEnd({ scale: t.scale }). handlePinchEnd then computes raw = currentScale * gestureScale even though currentScale was already multiplied by t.scale during handlePinch—squaring currentScale and zoomPercent (e.g., pinching to 1.44× jumps the readout to 207%), permanently desyncing currentScale from the rendered scale, and applying a bogus correction via scale.adjust(clamped / raw, 0) when hitting minScale/maxScale.
  • Safari Web (third-party/nsh/crates/nsh-platform-web/web/nsh.js:5135-5165) hits the same takes = false check and skips e.preventDefault(), zooming the page alongside handlePinch.

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_diagram in products/maka/workbench/src/bindings/diagrams.rs asserts 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: the ownTitleBar ? { ... } : { ... } header ternary duplicates ~7 identical layout properties; sharing the common base style object would make it cleaner.
  • products/maka/workbench/app/MermaidView.tsx: the diagram(interactive) helper inside <Each> is only called once per branch and duplicates objectFit: 'contain'; flattening with const interactive = !props.editable is 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:
    1. 捏合过程中,UI 线程的 anim.transform_moved 不断把 scale 设为 start * t.scale,同时 JS 线程的 handlePinch 排队下发 scale.adjust(applied, 0)(nsh-core/src/animation/graph.rs:606-614 直接乘在当前值上),每帧互相覆盖和超调。
    2. 手势结束时,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) 还会把画面错误拉偏。
  • 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
@me2seeks
me2seeks merged commit 5d6a8f7 into rust-preview Nov 21, 2026
10 of 12 checks passed
@me2seeks
me2seeks deleted the feat/mermaid-zoom-viewer branch November 21, 2026 15:48
@me2seeks

Copy link
Copy Markdown
Contributor Author

Addressed in c63d9caf9:

  • Moved trackpad onPinch to an inner viewport so it takes the gesture exclusively, while touchscreen pinches still reach the outer native pinch / onPinchEnd binding.
  • Added focusable={false} to the pressable viewport and simplified the header and Mermaid diagram styles.
  • Added headless trackpad-pinch dispatch with the macOS/Linux listener/native ordering. The regression now covers 144% pinch-in without the 207% double-scale, pinch-out to 100%, Ctrl+wheel zoom in/out, double-click zoom, and restoration to actual size. Temporarily restoring the old same-view binding makes the test fail at 207% as expected.
  • Removed the hardcoded light/dark hex assertions while still checking that the viewer requests a newly resolved palette.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants