Skip to content

feat(rune-kit): Canvas and ZoomPan, a zoom/pan/fit pane for any content - #2180

Merged
opencodejr[bot] merged 1 commit into
devfrom
bot/issue-720
Nov 25, 2026
Merged

opencodejr[bot] merged 1 commit into
devfrom
bot/issue-720

Conversation

@opencodejr

@opencodejr opencodejr Bot commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Closes #720.

What

Adds a rune_kit::zoom_pan module, exporting Canvas and ZoomPan: a zoom/pan pane for arbitrary content (a tree, a diagram, a chart), not only images.

  • ZoomPan is the app-owned state.
    • The view is a Fit/Held enum: it fits the content until a hand touches it and refits on resize only until then. A touched view survives a resize.
    • scale() gives the effective scale and fitted() whether it is still fitting.
    • to_content / to_viewport map pane points through the transform, and content_rect() gives where the content draws.
    • fit, zoom_at, zoom_by and pan_by change the view. Pan clamping is the image preview's clamp_axis.
  • Canvas::new(lens, content_size, el) and Canvas::scaled(lens, content_size, |scale| el) draw the content.
    • The content lays out at its own size and draws scaled with El::scale about its top-left (origin(0, 0)) in a clipped, focusable pane.
    • Builder options: .key, .label, .padding, .min_scale (0.25), .max_scale (8), .grows.
    • scaled builds the content for the scale of the build it is in, so detail can drop out as soon as a resize changes the fit.
  • Input:
    • A background drag pans past a 3 pt threshold; a cancelled drag restores the view it found.
    • A double-click fits.
    • Ctrl/⌘ + wheel zooms about the pointer, and a pinch does the same on macOS.
    • A plain wheel pans overflowing content and otherwise returns false, so an outer scroll view gets it.
    • With the pane focused, +/= and -/_ zoom by √2 and 0 fits, bare or with ⌘/Ctrl. A press focuses the pane.
    • Buttons inside the content keep their presses, since hit testing follows drawn rects.
  • Press handling is an explicit state machine (zoom_pan/gesture.rs, step returns Effects).
  • Changelog: changelog/unreleased/2026-11-25-kit-zoom-pan-canvas.md.

image_zoom is unchanged.

Testing

Not compiled or run: there was no Rust toolchain in the environment where this was written. Unit tests are in crates/rune-kit/src/zoom_pan/tests.rs and cover:

  • fit and refit on resize, and no growth past 1:1 by default;
  • min_scale bounding the fit and leaving the content pannable;
  • a touched view surviving a resize, and fit restoring fitting;
  • zoom keeping the point under the pointer, with to_content/to_viewport round-tripping;
  • a no-op pan staying fitted;
  • the gesture machine's threshold, double-click fit and cancel restore.

The expected values were worked out by hand and the API calls checked against rune's sources. scripts/file-size.sh passes and the hygiene grep is clean.

Please run before merging:

cargo test -p rune-kit --lib zoom_pan
cargo test -p rune -p rune-mermaid -p rune-kit --features rune-kit/markdown,rune-kit/mermaid
cargo lint

Closes #720

@opencodejr
opencodejr Bot merged commit 54bebc6 into dev Nov 25, 2026
1 check passed
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.

rune-kit: a zoom/pan/fit canvas for arbitrary content, not only images

0 participants