From f4761dc706d668eeb81b43da7e599cb9e94504ef Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sat, 28 Nov 2026 20:38:51 +0000 Subject: [PATCH] canvas: Fit to Window fits before the pass's input, so a pinch or Ctrl+wheel at the fit zooms The canvas applied the Fit on Screen the app queued every frame in fit mode after the pass's wheel and pinch, which undid them. Fit mode now gives the view without a rect, which the canvas fits on the pass's own rect before reading input. Restores canvas::tests::wheel_and_pinch_zoom_keep_the_point_under_the_pointer, zoom_commands_keep_the_middle_of_the_view, format_painter_sets_the_cursor and animation_painter_sets_the_cursor. Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/canvas.rs | 15 ++--- crates/ui-egui/src/canvas/tests.rs | 102 ++++++++++++++++++++++++++++- 2 files changed, 108 insertions(+), 9 deletions(-) diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs index aa8b0b96f..1c8afd895 100644 --- a/crates/ui-egui/src/canvas.rs +++ b/crates/ui-egui/src/canvas.rs @@ -4,8 +4,8 @@ use craft::behavior::canvas::zoom; use craft::behavior::kurbo::{Rect as DocRect, Size}; use craft::behavior::{Proposal, ZoomScheme}; use craft::ui::canvas::{ - Canvas, CanvasScrollbars, NavigationOptions, PageBorder, PageFrame, Pasteboard, ShownBars, ToolHost, ViewCommands, ViewRequest, ViewTarget, - WheelScheme, canvas_space, tool_pass, + Canvas, CanvasScrollbars, NavigationOptions, PageBorder, PageFrame, Pasteboard, ShownBars, ToolHost, ViewTarget, WheelScheme, canvas_space, + tool_pass, }; use craft::ui::contract::{Bind, Props, Show, ShowContainer, response_of}; use craft::ui::display::{Shade, Text}; @@ -52,17 +52,17 @@ pub(crate) fn show(app: &mut SlideApp, ui: &mut Ui) { let ppp = ui.ctx().pixels_per_point(); let mut view = app.canvas_view; let mut jumped = false; - let fit = [ViewRequest::Place(ViewCommands::FitOnScreen)]; - let queued = if let Some(zoom) = app.ui.zoom { + if let Some(zoom) = app.ui.zoom { let device = f64::from(zoom) * scheme.hundred; if (zoom::device(&view, ppp) / scheme.hundred) as f32 != zoom { view = zoom::with_device(&view, device, size, ppp, &scheme); jumped = true; } - &[][..] } else { - &fit[..] - }; + // Fit to Window: a view without a rect opens fitted on this pass's rect before the pass's input, so a pinch or + // wheel zoom made at the fit still applies (a queued Fit on Screen would run after it and undo it). + view.rect = Rect::NOTHING; + } let strips = (app.ui.ruler && !app.anim_preview_active()).then(|| rulers::rulers(app)).flatten(); let canvas_id = ui.scope_id().with("slide_canvas"); let strips_clip = ui.clip_rect().intersect(room); @@ -71,7 +71,6 @@ pub(crate) fn show(app: &mut SlideApp, ui: &mut Ui) { .document(ViewTarget::new(size).fit_ceiling(scheme.max_device())) .view(Bind::given(view)) .navigation(navigation()) - .queued(queued) .keys_to_body(true) .with_rulers(strips) .rulers(rulers::ruler_text(app.session.prefs.units), rulers::ruler_text(app.session.prefs.units)); diff --git a/crates/ui-egui/src/canvas/tests.rs b/crates/ui-egui/src/canvas/tests.rs index 6f3523b14..998090684 100644 --- a/crates/ui-egui/src/canvas/tests.rs +++ b/crates/ui-egui/src/canvas/tests.rs @@ -6,7 +6,7 @@ use craft::behavior::CursorIcon; use deckcraft_engine::Session; use deckcraft_geom::Point; use egui::accesskit::Role; -use egui::{Event, Modifiers, Pos2, pos2, vec2}; +use egui::{Event, Modifiers, MouseWheelUnit, Pos2, TouchPhase, pos2, vec2}; use egui_kittest::Harness; use egui_kittest::kittest::{NodeT, Queryable}; use serde_json::{Value, json}; @@ -98,6 +98,106 @@ fn slide_point(h: &Harness<'_, SlideApp>, p: Pos2) -> Pos2 { pos2((p.x - rect.min.x) / scale, (p.y - rect.min.y) / scale) } +/// A platform-command line wheel notch, then enough frames for egui to spread it out. +fn ctrl_wheel(h: &mut Harness<'_, SlideApp>, lines: f32) { + h.event(Event::MouseWheel { + unit: MouseWheelUnit::Line, + delta: vec2(0.0, lines), + phase: TouchPhase::Move, + source: egui::MouseWheelSource::Wheel, + modifiers: Modifiers::COMMAND, + }); + h.run_steps(30); +} + +/// Within half a slide point, plus the half device pixel the slide moves to sit on the pixel grid (#184). +fn assert_near(a: Pos2, b: Pos2, what: &str) { + assert!((a - b).length() < 1.25, "{what}: {a:?} moved to {b:?}"); +} + +/// #106: wheel and pinch zoom zoomed toward the slide's corner instead of the pointer. +#[test] +fn wheel_and_pinch_zoom_keep_the_point_under_the_pointer() { + let mut h = harness(); + let fit = h.state().canvas_scale(); + let r = h.state().slide_rect().unwrap(); + let p = r.min + vec2(r.width() * 0.8, r.height() * 0.7); + h.event(Event::PointerMoved(p)); + h.run_steps(1); + let under = slide_point(&h, p); + + h.event(Event::Zoom(2.0)); + h.run_steps(2); + let pinched = h.state().canvas_scale(); + assert!(pinched > fit * 1.5, "a pinch zooms in: {fit} -> {pinched}"); + assert_near(under, slide_point(&h, p), "pinch in"); + + ctrl_wheel(&mut h, 1.0); + assert!(h.state().canvas_scale() > pinched, "Ctrl/Cmd + wheel up zooms in"); + assert_near(under, slide_point(&h, p), "wheel in"); + + ctrl_wheel(&mut h, -1.0); + ctrl_wheel(&mut h, -1.0); + assert!(h.state().canvas_scale() < pinched, "Ctrl/Cmd + wheel down zooms out"); + assert_near(under, slide_point(&h, p), "wheel out"); + + h.event(Event::Zoom(0.8)); + h.run_steps(2); + assert_near(under, slide_point(&h, p), "pinch out"); + + // Zoomed out below the window size, the slide is centred again. + h.event(Event::Zoom(0.25)); + h.run_steps(2); + assert!(h.state().canvas_scale() < fit); + let (canvas, slide) = (h.state().canvas_rect().unwrap(), h.state().slide_rect().unwrap()); + assert!((canvas.center() - slide.center()).length() < 1.0, "{canvas:?} {slide:?}"); +} + +/// Zoom In/Out (keys, buttons, slider) keeps the middle of the view where it is. +#[test] +fn zoom_commands_keep_the_middle_of_the_view() { + let mut h = harness(); + let c = h.state().canvas_rect().unwrap().center(); + let mid = slide_point(&h, c); + for _ in 0..4 { + let before = h.state().canvas_scale(); + h.state_mut().run("view.zoomIn", json!({})).unwrap(); + h.run_steps(2); + assert!(h.state().canvas_scale() > before, "zoom in zooms"); + assert_near(mid, slide_point(&h, c), "zoom in"); + } + let before = h.state().canvas_scale(); + h.state_mut().run("view.zoomOut", json!({})).unwrap(); + h.run_steps(2); + assert!(h.state().canvas_scale() < before, "zoom out zooms"); + assert_near(mid, slide_point(&h, c), "zoom out"); +} + +/// #830: when the Format or Animation Painter is armed, the cursor shows the copy icon so the user can see the tool is +/// waiting for a click. +#[test] +fn format_painter_sets_the_cursor() { + let mut h = harness(); + h.hover_at(pos2(600.0, 400.0)); + h.run_steps(2); + assert_ne!(h.output().platform_output.cursor_icon, egui::CursorIcon::Copy, "no painter is armed yet"); + h.state_mut().session.painter = Some((Default::default(), false)); + h.run_steps(3); + assert_eq!(h.output().platform_output.cursor_icon, egui::CursorIcon::Copy, "the format painter arms the Copy cursor"); +} + +/// #830: the Animation Painter arms the same cursor. +#[test] +fn animation_painter_sets_the_cursor() { + let mut h = harness(); + h.hover_at(pos2(600.0, 400.0)); + h.run_steps(2); + assert_ne!(h.output().platform_output.cursor_icon, egui::CursorIcon::Copy, "no painter is armed yet"); + h.state_mut().session.anim_painter = Some((vec![], false)); + h.run_steps(3); + assert_eq!(h.output().platform_output.cursor_icon, egui::CursorIcon::Copy, "the animation painter arms the Copy cursor"); +} + fn app_with_table() -> (Harness<'static, SlideApp>, deckcraft_model::ShapeId) { let mut s = Session::with_new(); let data = json!([["Hello world", "b"], ["c", ""]]);