From 88a997db16dda8b42c3f056b10846ed9a2642ec6 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 4 Dec 2026 21:17:22 -0800 Subject: [PATCH 1/4] Canvas: pans move the view by whole pixels of the art's frame A pan by part of a device pixel (a trackpad, an eased wheel line, a middle-button drag at a fractional display scale) moved the view so that the art's frame no longer differed from the last one by whole pixels. The render worker then rendered the whole canvas again for every step instead of moving the frame it kept. The canvas now moves each pan by the whole number of frame pixels nearest to it and keeps the part of a pixel left over for the next pan, so steps under half a pixel still add up. A Hand, scroll bar or compass drag sets the view from where the drag started, and is rounded without the earlier remainder. A zoom, a turn, a resize or a view the app sets is not rounded. This is a temporary change in VectorCraft's canvas code until storytold/craft-libs#5411, which makes the framework's pans move by whole device pixels, reaches VectorCraft's craft-libs pin. The pin bump that brings it in removes this code. --- crates/ui-egui/src/canvas.rs | 114 ++++++++++++++- crates/ui-egui/src/canvas/tests.rs | 221 +++++++++++++++++++++++++++++ crates/ui-egui/src/state.rs | 5 + 3 files changed, 334 insertions(+), 6 deletions(-) diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs index 73b97bce0f..584137c1c4 100644 --- a/crates/ui-egui/src/canvas.rs +++ b/crates/ui-egui/src/canvas.rs @@ -146,6 +146,8 @@ struct Body { edited: Option, /// The Quick Pen tool took this pass's wheel for its arc size ([`quick_pen_wheel::arc_size`]). arc_view: Option, + /// What this pass's pan by whole device pixels left ([`pan_on_device_pixels`]). + pan: Option, } /// Window `win`'s document in the framework canvas, filling the rest of `ui`. True when the canvas @@ -265,8 +267,14 @@ pub(crate) fn show_document(app: &mut VectorcraftApp, ui: &mut Ui, win: Win, kin pass.view = Some(arc_view); } } + // The canvas's own pans (the wheel, a middle-button drag, keys) move the view by a step from where it was. + let pan = (!measuring).then(|| { + let rest = doc_windows::view(app, win).and_then(|v| v.pan_rest.filter(|rest| rest.kept == Some((v.center, v.zoom, v.rotation)))); + pan_on_device_pixels(rest, ui.ctx().input(|i| i.max_texture_side), &mut space, pass.as_mut()) + }); body = with_canvas_space(ui, space, |ui| show_body(app, ui, &space, pass.as_ref(), win, kind, bar_shapes)); body.arc_view = arc_turn.then_some(arc_view); + body.pan = pan; } }); let output = shown.output; @@ -279,7 +287,7 @@ pub(crate) fn show_document(app: &mut VectorcraftApp, ui: &mut Ui, win: Win, kin rulers::apply(app, ui, out); } let pressed = matches!(output.press.phase, PressPhase::Down { .. }); - let Body { reply, context_at, context_key, edited, arc_view } = body; + let Body { reply, context_at, context_key, edited, arc_view, pan } = body; let request = reply .as_ref() .and_then(|r| r.context.as_ref()) @@ -288,13 +296,17 @@ pub(crate) fn show_document(app: &mut VectorcraftApp, ui: &mut Ui, win: Win, kin let jumped = edited.is_some(); // Presentation Mode keeps its view through a drag of the middle button, which the canvas pans on its own, and a // wheel turn the Quick Pen tool takes for its arc size leaves other view requests in place. - let view_now = if presenting && ui.input(|i| i.pointer.middle_down()) { - given - } else { - edited.or(reply.and_then(|r| r.view)).or(arc_view).or(output.view).unwrap_or(cv) - }; + let keeps_given = presenting && ui.input(|i| i.pointer.middle_down()); + let pan = pan.filter(|_| !keeps_given); + let view_now = + if keeps_given { given } else { edited.or(reply.and_then(|r| r.view)).or(arc_view).or(pan.map(|p| p.view)).or(output.view).unwrap_or(cv) }; + // Pointer drags of the Hand, compass and scroll bars request absolute views without carrying a rounding remainder. + let absolute_drag = (edited.is_some() || app.session.tool_id() == "hand" || tool_adapter::view_tool_held(app)) + && ui.input(|i| i.pointer.any_down() || i.pointer.any_released()); + let (view_now, pan) = settled_on_device_pixels(pan, view_now, !absolute_drag); if let Some(view) = doc_windows::view_mut(app, win) { view.set_canvas(&view_now, scaling); + view.pan_rest = pan.map(|rest| PanRest { kept: Some((view.center, view.zoom, view.rotation)), ..rest }); // Fitted once, the view stays where it is moved. view.fitted |= refit; // An edit of the compass or a scroll bar reaches the next pass, which stops a running ease. @@ -318,6 +330,96 @@ pub(crate) fn show_document(app: &mut VectorcraftApp, ui: &mut Ui, win: Win, kin pressed } +/// What a canvas pan by whole device pixels ([`pan_step`]) left: the view the canvas settled on, the device pixels a +/// point it was shown at, and the part of a device pixel along each screen axis the pan has not moved yet (at most half +/// a pixel each way). +/// +/// Temporary: storytold/craft-libs#5411 moves the canvas's pans and wheel steps by whole device pixels. Remove this, +/// [`crate::state::View::pan_rest`] and their callers with the craft-libs pin bump that brings it in. +#[derive(Clone, Copy, Debug, PartialEq)] +pub(crate) struct PanRest { + view: CanvasView, + pixels_per_point: f32, + owed: (f64, f64), + /// The app's center, zoom and rotation after keeping the settled view. + kept: Option<(vectorcraft_geom::Point, f64, f64)>, +} + +impl PanRest { + /// A rest of nothing after `view`, shown at `pixels_per_point`. + fn none(view: CanvasView, pixels_per_point: f32) -> Self { + Self { view, pixels_per_point, owed: (0.0, 0.0), kept: None } + } +} + +/// `view`, when it differs from `from` only in its center: the view that moves the document from where `from` draws it +/// by a whole number of device pixels at `pixels_per_point` along each screen axis, the number nearest to how far +/// `view` moves it plus `owed` device pixels, with what is left of a pixel. Exactly half a pixel left stays owed. None +/// when `view` differs from `from` in more than its center, or `pixels_per_point` or the views are not usable. +pub(crate) fn pan_step(from: CanvasView, view: CanvasView, owed: (f64, f64), pixels_per_point: f32) -> Option<(CanvasView, (f64, f64))> { + let ppp = f64::from(pixels_per_point); + let mut moved = from; + moved.center = view.center; + if moved != view || !(ppp.is_finite() && ppp > 0.0 && view.zoom.is_finite() && view.zoom > 0.0) { + return None; + } + let (a, b) = (from.affine() * vectorcraft_geom::Point::ZERO, view.affine() * vectorcraft_geom::Point::ZERO); + let total = ((b.x - a.x) * ppp + owed.0, (b.y - a.y) * ppp + owed.1); + let nearest = |t: f64| t.signum() * (t.abs() - 0.5).ceil().max(0.0); + let whole = (nearest(total.0), nearest(total.1)); + let shift = egui::vec2((a.x + whole.0 / ppp - b.x) as f32, (a.y + whole.1 / ppp - b.y) as f32); + if !(shift.x.is_finite() && shift.y.is_finite()) { + return None; + } + let mut shown = view; + if shift != egui::Vec2::ZERO { + shown.center = view.center - view.unmap_vec(shift); + } + (shown.center.x.is_finite() && shown.center.y.is_finite()).then_some((shown, (total.0 - whole.0, total.1 - whole.1))) +} + +/// Moves the view the canvas settled on in this pass (`space`'s target) from `rest.view` by whole pixels of the art's +/// frame ([`pan_step`], [`raster::frame_ppp`] with the display's largest texture side `max_side`), adding the part of a +/// pixel `rest` owes, and gives the tool host the same view in `pass`. The render worker then moves the frame it kept +/// ([`raster::render_job`]) instead of rendering the whole canvas for a pan by part of a pixel. Nothing is rounded while +/// a zoom eases, or when the app changed the view since `rest`. Returns the rest to keep. +fn pan_on_device_pixels(rest: Option, max_side: usize, space: &mut CanvasSpace, pass: Option<&mut ToolPass>) -> PanRest { + let target = space.target(); + // The pixels of the art's frame, which are fewer than the device's when the frame would be wider than `max_side`. + let ppp = raster::frame_ppp(target.rect.size(), space.pixels_per_point, max_side); + let step = rest.filter(|rest| !space.easing() && rest.pixels_per_point == ppp).and_then(|rest| { + let mut from = rest.view; + // Match zooms separated only by the UI scaling round trip. + if (from.zoom - target.zoom).abs() <= 1e-12 * from.zoom.abs().max(target.zoom.abs()) { + from.zoom = target.zoom; + } + pan_step(from, target, rest.owed, ppp) + }); + let Some((view, owed)) = step else { + return PanRest::none(target, ppp); + }; + if view != target { + space.set_view(view); + if let Some(pass) = pass { + pass.space = Some(*space); + pass.view = Some(view); + } + } + PanRest { view, pixels_per_point: ppp, owed, kept: None } +} + +/// `view`, the view the app keeps after this pass, moved from the view of `rest` by whole device pixels when it differs +/// from it in its center alone (a tool, a scroll bar or the compass panned it after [`pan_on_device_pixels`]), and the +/// rest to keep. +fn settled_on_device_pixels(rest: Option, view: CanvasView, carry: bool) -> (CanvasView, Option) { + let Some(rest) = rest.filter(|rest| rest.view != view) else { return (view, rest) }; + let ppp = rest.pixels_per_point; + match pan_step(rest.view, view, if carry { rest.owed } else { (0.0, 0.0) }, ppp) { + Some((view, owed)) => (view, Some(PanRest { view, owed, ..rest })), + None => (view, Some(PanRest::none(view, ppp))), + } +} + /// The body of the canvas in order: controls that take presses first, the tool host, the selection's marks and the /// display-only widgets over it, the picture last. fn show_body( diff --git a/crates/ui-egui/src/canvas/tests.rs b/crates/ui-egui/src/canvas/tests.rs index af2755f7ea..8660fcc476 100644 --- a/crates/ui-egui/src/canvas/tests.rs +++ b/crates/ui-egui/src/canvas/tests.rs @@ -6247,6 +6247,227 @@ fn a_whole_pixel_pan() { } } +/// Wheel steps of part of a device pixel, at a zoom and center that put the document's origin between device pixels, +/// move the frame the render worker kept: every frame that may copy from the last one does. +#[test] +fn wheel_steps_of_part_of_a_pixel_move_the_kept_frame() { + keeping_color_settings(|| { + for rulers in [false, true] { + for scaling in [1.0, 1.75] { + wheel_steps_of_part_of_a_pixel(rulers, scaling); + } + } + }); +} + +fn wheel_steps_of_part_of_a_pixel(rulers: bool, scaling: f64) { + let mut app = VectorcraftApp::new(Session::new(), Default::default()); + app.session.prefs.ui_scaling = scaling; + app.ui.view.rulers = rulers; + app.session.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); + for x in [20, 160, 300] { + app.session.execute("shape.rectangle", &json!({"x": x, "y": 40, "width": 80, "height": 200})).unwrap(); + } + app.canvas.worker_started = true; + let ctx = crate::testing::ui_context(); + for _ in 0..3 { + app_step(&mut app, &ctx, vec![]); + } + { + let v = app.view_mut().unwrap(); + let zoom = 1.37; + let scale = f64::from(scaling as f32); + if scaling != 1.0 { + assert_ne!((zoom * scale) / scale, zoom, "the UI scaling round trip changes the canvas zoom"); + } + v.zoom = zoom * scale; + v.center = Point::new(200.3, 150.7); + } + app_step(&mut app, &ctx, vec![]); + let (worker, jobs, done) = crate::render_worker::Worker::test_channel(); + app.canvas.worker = Some(worker); + app.canvas.last_ms = 50.0; + let at = app.canvas_view().unwrap().rect.center(); + let wheel = egui::Event::MouseWheel { + unit: egui::MouseWheelUnit::Point, + delta: vec2(0.3, -0.4), + phase: egui::TouchPhase::Move, + modifiers: egui::Modifiers::default(), + source: egui::MouseWheelSource::Unknown, + }; + let start = app.canvas_view().unwrap().center; + let mut r = vectorcraft_render::Renderer::new(); + let (mut kept, mut copying) = (None, 0); + for _ in 0..10 { + app_step(&mut app, &ctx, vec![egui::Event::PointerMoved(at), wheel.clone()]); + while let Ok(job) = jobs.try_recv() { + let may = job.may_copy && kept.is_some(); + let frame = render_job(&mut r, &mut kept, &job); + copying += usize::from(may); + if may { + assert!(frame.reused, "each new frame of the pan copies the kept frame's pixels, rulers: {rulers}, UI scaling: {scaling}"); + } + let (img, reused) = (frame.img, frame.reused); + done.send(crate::render_worker::Done { key: job.key, doc: job.doc, opts: job.opts, img, ms: 50.0, moved: 0, reused, view_sized: false }) + .unwrap(); + } + } + assert_ne!(app.canvas_view().unwrap().center, start, "the wheel pans the view"); + assert!(copying >= 3, "the pan asks for new frames: {copying}"); +} + +/// Pans by part of a device pixel, each from the view the last one settled on, move the document by whole device pixels +/// at the view's zoom and rotation, and the parts left over add up: after each step the document is within half a device +/// pixel of where the pans together put it. A change of more than the center is not a pan. +#[test] +fn pans_by_part_of_a_pixel_move_by_whole_device_pixels_and_add_up() { + let rect = egui::Rect::from_min_size(pos2(10.5, 7.25), vec2(640.0, 480.0)); + for (zoom, rotation, ppp) in [(1.37, 0.0, 1.0), (0.613, 30.0, 2.0), (7.9, -75.0, 1.25)] { + let start = CanvasView::new(rect, Point::new(203.17, -48.6), zoom).rotation(rotation); + let origin = |v: &CanvasView| v.affine() * Point::ZERO; + let (mut view, mut owed) = (start, (0.0, 0.0)); + // Each step moves the document 0.3 points right and 0.45 points up on screen. + let step = vec2(0.3, -0.45); + for n in 1..=12 { + let mut next = view; + next.center = view.center - view.unmap_vec(step); + let (shown, left) = super::pan_step(view, next, owed, ppp).expect("a pan"); + let ppp = f64::from(ppp); + let (a, b) = (origin(&view), origin(&shown)); + let wanted = origin(&start) + vectorcraft_geom::Vec2::new(f64::from(step.x), f64::from(step.y)) * f64::from(n); + for (from, to, want) in [(a.x, b.x, wanted.x), (a.y, b.y, wanted.y)] { + let moved = (to - from) * ppp; + assert!((moved - moved.round()).abs() < 1e-6, "{zoom} {rotation}° step {n}: moved {moved} device pixels"); + assert!(((to - want) * ppp).abs() <= 0.5 + 1e-6, "{zoom} {rotation}° step {n}: {} device pixels short", (want - to) * ppp); + } + assert_eq!((shown.zoom, shown.rotation, shown.rect), (start.zoom, start.rotation, start.rect)); + (view, owed) = (shown, left); + } + let mut zoomed = view; + zoomed.zoom = zoom * 1.1; + assert_eq!(super::pan_step(view, zoomed, owed, ppp), None, "a zoom is not a pan"); + } +} + +/// A drag (a button held) sets where the view goes: each pass's view stays within half a device pixel of the view the +/// drag asked for, however many passes it lasts, and moves by whole device pixels from the last one. +#[test] +fn a_drag_s_views_stay_within_half_a_pixel_of_the_pointer() { + let rect = egui::Rect::from_min_size(pos2(0.0, 0.0), vec2(640.0, 480.0)); + let start = CanvasView::new(rect, Point::new(100.0, 80.0), 1.37); + let mut rest = Some(super::PanRest::none(start, 1.0)); + for n in 1..=20 { + // The drag asks for the view at its press moved 0.35 points a pass. + let mut asked = start; + asked.center = start.center - start.unmap_vec(vec2(0.35, 0.35) * n as f32); + let last = rest.unwrap().view; + let (view, next) = super::settled_on_device_pixels(rest, asked, false); + let [a, b, c] = [last, view, asked].map(|v| v.affine() * Point::ZERO); + for (from, to, want) in [(a.x, b.x, c.x), (a.y, b.y, c.y)] { + assert!(((to - from) - (to - from).round()).abs() < 1e-6, "pass {n}: whole pixels from the last view"); + assert!((to - want).abs() <= 0.5 + 1e-6, "pass {n}: {} device pixels from the drag", want - to); + } + rest = next; + } +} + +/// Steps a tool takes from where the view was (a drag's autoscroll, the Hand's glide) keep the parts of a pixel they +/// leave: after each pass the view is within half a device pixel of all the steps together. +#[test] +fn a_tool_s_steps_keep_the_parts_of_a_pixel_they_leave() { + let rect = egui::Rect::from_min_size(pos2(0.0, 0.0), vec2(640.0, 480.0)); + let start = CanvasView::new(rect, Point::new(100.0, 80.0), 1.37); + let mut rest = Some(super::PanRest::none(start, 1.25)); + for n in 1..=12 { + let last = rest.unwrap().view; + // Each pass the tool moves the view it was given 2.4 device pixels left. + let mut asked = last; + asked.center = last.center - last.unmap_vec(vec2(-2.4 / 1.25, 0.0)); + let (view, next) = super::settled_on_device_pixels(rest, asked, true); + let moved = (view.affine() * Point::ZERO).x - (start.affine() * Point::ZERO).x; + assert!((moved * 1.25 + 2.4 * f64::from(n)).abs() <= 0.5 + 1e-5, "pass {n}: moved {} device pixels", moved * 1.25); + rest = next; + } +} + +/// A Hand drag by parts of a device pixel, at a zoom and center that put the document's origin between device pixels, +/// ends where the pointer let go: the document is within half a device pixel of where the drag puts it, after the +/// release too. +#[test] +fn a_hand_drag_by_parts_of_a_pixel_ends_where_the_pointer_lets_go() { + let mut app = VectorcraftApp::new(Session::new(), Default::default()); + app.session.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); + app.select_tool("hand"); + let ctx = crate::testing::ui_context(); + for _ in 0..3 { + app_step(&mut app, &ctx, vec![]); + } + { + let v = app.view_mut().unwrap(); + v.zoom = 1.37; + v.center = Point::new(200.3, 150.7); + } + app_step(&mut app, &ctx, vec![]); + let start = app.canvas_view().unwrap(); + let at = start.rect.center(); + let end = at + vec2(17.7, 12.35); + app_step(&mut app, &ctx, vec![egui::Event::PointerMoved(at), press(at, true)]); + for to in [at + vec2(13.7, 9.35), end] { + app_step(&mut app, &ctx, vec![egui::Event::PointerMoved(to)]); + } + let origin = |v: &CanvasView| v.affine() * Point::ZERO; + let wanted = origin(&start) + vectorcraft_geom::Vec2::new(f64::from(end.x - at.x), f64::from(end.y - at.y)); + let near = |app: &VectorcraftApp, when: &str| { + let got = origin(&app.canvas_view().unwrap()); + assert!((got.x - wanted.x).abs() <= 0.5 + 1e-6 && (got.y - wanted.y).abs() <= 0.5 + 1e-6, "{when}: {got:?}, the drag puts it at {wanted:?}"); + }; + near(&app, "dragging"); + app_step(&mut app, &ctx, vec![press(end, false)]); + app_step(&mut app, &ctx, vec![]); + near(&app, "released"); +} + +/// A middle-button drag at 1.25 device pixels a point moves the view by whole device pixels and keeps the parts of a +/// pixel each move leaves: after moves of one point each the document is within half a device pixel of the pointer. +#[test] +fn a_middle_button_drag_keeps_the_parts_of_a_pixel_it_moves() { + let mut app = VectorcraftApp::new(Session::new(), Default::default()); + app.session.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); + let ctx = crate::testing::ui_context(); + let step = |app: &mut VectorcraftApp, events: Vec| { + let screen = egui::Rect::from_min_size(Pos2::ZERO, vec2(1600.0, 850.0)); + let mut raw = egui::RawInput { screen_rect: Some(screen), events, ..Default::default() }; + raw.viewports.entry(egui::ViewportId::ROOT).or_default().native_pixels_per_point = Some(1.25); + crate::testing::app_frame(app, &ctx, raw); + }; + for _ in 0..3 { + step(&mut app, vec![]); + } + { + let v = app.view_mut().unwrap(); + v.zoom = 1.37; + v.center = Point::new(200.3, 150.7); + } + step(&mut app, vec![]); + assert_eq!(ctx.pixels_per_point(), 1.25); + let start = app.canvas_view().unwrap(); + let at = start.rect.center(); + let middle = + |at: Pos2, pressed: bool| egui::Event::PointerButton { pos: at, button: egui::PointerButton::Middle, pressed, modifiers: Default::default() }; + step(&mut app, vec![egui::Event::PointerMoved(at), middle(at, true)]); + let mut to = at; + for _ in 0..8 { + to += vec2(1.0, 0.0); + step(&mut app, vec![egui::Event::PointerMoved(to)]); + } + let origin = |v: &CanvasView| v.affine() * Point::ZERO; + let (got, from) = (origin(&app.canvas_view().unwrap()), origin(&start)); + let moved = (got.x - from.x) * 1.25; + assert!((moved - f64::from(to.x - at.x) * 1.25).abs() <= 0.5 + 1e-6, "moved {moved} device pixels for {} points", to.x - at.x); + assert!(((got.y - from.y) * 1.25).abs() <= 0.5 + 1e-6, "no move down"); + step(&mut app, vec![middle(to, false)]); +} + /// Installing, replacing or removing a plug-in draws a document with a plug-in effect again, and a /// pan from the frame drawn before the change renders whole instead of copying its pixels. The key /// of a document without a plug-in effect does not follow the plug-ins. The other tests share the diff --git a/crates/ui-egui/src/state.rs b/crates/ui-egui/src/state.rs index fda6c31677..fbf40de0de 100644 --- a/crates/ui-egui/src/state.rs +++ b/crates/ui-egui/src/state.rs @@ -104,6 +104,10 @@ pub struct View { /// How the canvas reaches the view the app last set ([`Self::set_canvas`] callers). #[serde(skip)] pub moved: Option, + /// What the canvas's last pan left to move ([`crate::canvas::PanRest`]). Temporary, removed with the craft-libs pin + /// bump that brings in storytold/craft-libs#5411. + #[serde(skip)] + pub(crate) pan_rest: Option, } impl Default for View { @@ -122,6 +126,7 @@ impl Default for View { rect: egui::Rect::NOTHING, request: None, moved: None, + pan_rest: None, } } } From a8a8cf8e1101012a331adc2677f7d471cdc58e90 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 4 Dec 2026 22:59:43 -0800 Subject: [PATCH 2/4] Canvas: a view the app asks for is not rounded to whole device pixels A fit or another view request that keeps the zoom and rotation changes only the center, so the pan's rounding still matched its kept view and moved the fitted view by up to half a device pixel. The canvas now applies the pan remainder only on passes without a view request. --- crates/ui-egui/src/canvas.rs | 9 ++++-- crates/ui-egui/src/canvas/tests.rs | 46 ++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs index 584137c1c4..66b055c874 100644 --- a/crates/ui-egui/src/canvas.rs +++ b/crates/ui-egui/src/canvas.rs @@ -186,6 +186,8 @@ pub(crate) fn show_document(app: &mut VectorcraftApp, ui: &mut Ui, win: Win, kin if refit && queued.is_empty() { queued.push(ViewRequest::Place(ViewCommands::FitArtboard)); } + // A view the app asks for this pass (a fit, a zoom command) keeps its exact center. + let requested = !queued.is_empty(); let mut cv = view.canvas(scaling, area); let given = cv; // A zoom percentage (Actual Size) counts from the density of the display that shows this @@ -269,7 +271,9 @@ pub(crate) fn show_document(app: &mut VectorcraftApp, ui: &mut Ui, win: Win, kin } // The canvas's own pans (the wheel, a middle-button drag, keys) move the view by a step from where it was. let pan = (!measuring).then(|| { - let rest = doc_windows::view(app, win).and_then(|v| v.pan_rest.filter(|rest| rest.kept == Some((v.center, v.zoom, v.rotation)))); + let rest = doc_windows::view(app, win) + .filter(|_| !requested) + .and_then(|v| v.pan_rest.filter(|rest| rest.kept == Some((v.center, v.zoom, v.rotation)))); pan_on_device_pixels(rest, ui.ctx().input(|i| i.max_texture_side), &mut space, pass.as_mut()) }); body = with_canvas_space(ui, space, |ui| show_body(app, ui, &space, pass.as_ref(), win, kind, bar_shapes)); @@ -382,7 +386,8 @@ pub(crate) fn pan_step(from: CanvasView, view: CanvasView, owed: (f64, f64), pix /// frame ([`pan_step`], [`raster::frame_ppp`] with the display's largest texture side `max_side`), adding the part of a /// pixel `rest` owes, and gives the tool host the same view in `pass`. The render worker then moves the frame it kept /// ([`raster::render_job`]) instead of rendering the whole canvas for a pan by part of a pixel. Nothing is rounded while -/// a zoom eases, or when the app changed the view since `rest`. Returns the rest to keep. +/// a zoom eases, when the app changed the view since `rest`, or when the app asks for a view this pass (`rest` is None). +/// Returns the rest to keep. fn pan_on_device_pixels(rest: Option, max_side: usize, space: &mut CanvasSpace, pass: Option<&mut ToolPass>) -> PanRest { let target = space.target(); // The pixels of the art's frame, which are fewer than the device's when the frame would be wider than `max_side`. diff --git a/crates/ui-egui/src/canvas/tests.rs b/crates/ui-egui/src/canvas/tests.rs index 8660fcc476..c44a3934dc 100644 --- a/crates/ui-egui/src/canvas/tests.rs +++ b/crates/ui-egui/src/canvas/tests.rs @@ -6468,6 +6468,52 @@ fn a_middle_button_drag_keeps_the_parts_of_a_pixel_it_moves() { step(&mut app, vec![middle(to, false)]); } +/// A fit the app asks for after a pan by parts of a device pixel, at the zoom the pan kept, puts the view at the fitted +/// center exactly: the pan's leftover part of a pixel does not round it. +#[test] +fn a_fit_after_a_pan_by_part_of_a_pixel_keeps_its_exact_center() { + let mut app = VectorcraftApp::new(Session::new(), Default::default()); + app.session.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); + let ctx = crate::testing::ui_context(); + let step = |app: &mut VectorcraftApp, events: Vec| { + let screen = egui::Rect::from_min_size(Pos2::ZERO, vec2(1600.0, 850.0)); + let mut raw = egui::RawInput { screen_rect: Some(screen), events, ..Default::default() }; + raw.viewports.entry(egui::ViewportId::ROOT).or_default().native_pixels_per_point = Some(1.25); + crate::testing::app_frame(app, &ctx, raw); + }; + for _ in 0..3 { + step(&mut app, vec![]); + } + let fitted = app.canvas_view().unwrap(); + let at = fitted.rect.center(); + // The pans start from a view whose document origin is between device pixels of the fitted one. + { + let v = app.view_mut().unwrap(); + v.center = Point::new(v.center.x + 0.37, v.center.y + 0.21); + } + step(&mut app, vec![]); + let wheel = egui::Event::MouseWheel { + unit: egui::MouseWheelUnit::Point, + delta: vec2(0.3, -0.45), + phase: egui::TouchPhase::Move, + modifiers: egui::Modifiers::default(), + source: egui::MouseWheelSource::Unknown, + }; + for _ in 0..3 { + step(&mut app, vec![egui::Event::PointerMoved(at), wheel.clone()]); + } + let panned = app.canvas_view().unwrap(); + assert_ne!(panned.center, fitted.center, "the wheel pans the view"); + assert_eq!(panned.zoom, fitted.zoom, "the pan keeps the zoom"); + assert!(app.view().unwrap().pan_rest.is_some(), "the pan keeps a rest"); + app.view_mut().unwrap().fitted = false; + step(&mut app, vec![]); + let refit = app.canvas_view().unwrap(); + assert_eq!(refit.zoom, fitted.zoom, "the fit gives the same zoom"); + let (got, want) = (refit.affine() * Point::ZERO, fitted.affine() * Point::ZERO); + assert!((got.x - want.x).abs() < 1e-6 && (got.y - want.y).abs() < 1e-6, "the fit is not rounded: {got:?}, fitted at {want:?}"); +} + /// Installing, replacing or removing a plug-in draws a document with a plug-in effect again, and a /// pan from the frame drawn before the change renders whole instead of copying its pixels. The key /// of a document without a plug-in effect does not follow the plug-ins. The other tests share the From 3a00fd8f4e0b6653a1f1f82782e468824adaef5d Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 4 Dec 2026 23:56:29 -0800 Subject: [PATCH 3/4] Canvas: absolute drags leave no part of a pixel for later pans An absolute drag (Hand, compass, scroll bars) rounds each pass's view from the last one without adding the remainder kept from earlier, but it still stored the remainder pan_step returned. A drag by 0.4 device pixels then made a later wheel step of 0.2 device pixels move the view by a whole pixel. settled_on_device_pixels now keeps nothing owed after an absolute drag. --- crates/ui-egui/src/canvas.rs | 4 ++-- crates/ui-egui/src/canvas/tests.rs | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs index 66b055c874..1490c35a89 100644 --- a/crates/ui-egui/src/canvas.rs +++ b/crates/ui-egui/src/canvas.rs @@ -415,12 +415,12 @@ fn pan_on_device_pixels(rest: Option, max_side: usize, space: &mut Canv /// `view`, the view the app keeps after this pass, moved from the view of `rest` by whole device pixels when it differs /// from it in its center alone (a tool, a scroll bar or the compass panned it after [`pan_on_device_pixels`]), and the -/// rest to keep. +/// rest to keep. Without `carry` (an absolute drag) the rest neither adds nor keeps a part of a pixel. fn settled_on_device_pixels(rest: Option, view: CanvasView, carry: bool) -> (CanvasView, Option) { let Some(rest) = rest.filter(|rest| rest.view != view) else { return (view, rest) }; let ppp = rest.pixels_per_point; match pan_step(rest.view, view, if carry { rest.owed } else { (0.0, 0.0) }, ppp) { - Some((view, owed)) => (view, Some(PanRest { view, owed, ..rest })), + Some((view, owed)) => (view, Some(PanRest { view, owed: if carry { owed } else { (0.0, 0.0) }, ..rest })), None => (view, Some(PanRest::none(view, ppp))), } } diff --git a/crates/ui-egui/src/canvas/tests.rs b/crates/ui-egui/src/canvas/tests.rs index c266b780c2..cb3083be8e 100644 --- a/crates/ui-egui/src/canvas/tests.rs +++ b/crates/ui-egui/src/canvas/tests.rs @@ -6371,6 +6371,23 @@ fn a_drag_s_views_stay_within_half_a_pixel_of_the_pointer() { } } +/// A drag's rounding is not owed to the pans after it: a drag by 0.4 device pixels leaves the view where it was and +/// nothing owed, so a later step of 0.2 device pixels does not move the view either. +#[test] +fn a_drag_leaves_nothing_owed_to_later_pans() { + let rect = egui::Rect::from_min_size(pos2(0.0, 0.0), vec2(640.0, 480.0)); + let start = CanvasView::new(rect, Point::new(100.0, 80.0), 1.0); + let mut asked = start; + asked.center = start.center - start.unmap_vec(vec2(0.4, 0.0)); + let (view, rest) = super::settled_on_device_pixels(Some(super::PanRest::none(start, 1.0)), asked, false); + assert_eq!(view.center, start.center, "a drag by 0.4 device pixels rounds to no move"); + assert_eq!(rest.map(|rest| rest.owed), Some((0.0, 0.0)), "the drag leaves nothing owed"); + let mut stepped = view; + stepped.center = view.center - view.unmap_vec(vec2(0.2, 0.0)); + let (after, _) = super::settled_on_device_pixels(rest, stepped, true); + assert_eq!(after.center, view.center, "a step of 0.2 device pixels alone does not move the view"); +} + /// Steps a tool takes from where the view was (a drag's autoscroll, the Hand's glide) keep the parts of a pixel they /// leave: after each pass the view is within half a device pixel of all the steps together. #[test] From 8ed783706be7714615756cc75f9d20a3f126870a Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 5 Dec 2026 00:54:44 -0800 Subject: [PATCH 4/4] Canvas: an absolute drag that keeps the view clears what is owed settled_on_device_pixels returned the rest unchanged when the view after the pass matched the rest's view, so an absolute drag pass that left the view in place kept the part of a pixel earlier pans owed. A later wheel step could then move the view by a whole pixel the drag had left out. That branch now clears the owed part when the pass does not carry it, and keeps it when it does. --- crates/ui-egui/src/canvas.rs | 5 ++++- crates/ui-egui/src/canvas/tests.rs | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs index 1490c35a89..facbde7d15 100644 --- a/crates/ui-egui/src/canvas.rs +++ b/crates/ui-egui/src/canvas.rs @@ -417,7 +417,10 @@ fn pan_on_device_pixels(rest: Option, max_side: usize, space: &mut Canv /// from it in its center alone (a tool, a scroll bar or the compass panned it after [`pan_on_device_pixels`]), and the /// rest to keep. Without `carry` (an absolute drag) the rest neither adds nor keeps a part of a pixel. fn settled_on_device_pixels(rest: Option, view: CanvasView, carry: bool) -> (CanvasView, Option) { - let Some(rest) = rest.filter(|rest| rest.view != view) else { return (view, rest) }; + let Some(rest) = rest else { return (view, None) }; + if rest.view == view { + return (view, Some(PanRest { owed: if carry { rest.owed } else { (0.0, 0.0) }, ..rest })); + } let ppp = rest.pixels_per_point; match pan_step(rest.view, view, if carry { rest.owed } else { (0.0, 0.0) }, ppp) { Some((view, owed)) => (view, Some(PanRest { view, owed: if carry { owed } else { (0.0, 0.0) }, ..rest })), diff --git a/crates/ui-egui/src/canvas/tests.rs b/crates/ui-egui/src/canvas/tests.rs index cb3083be8e..8e2281effb 100644 --- a/crates/ui-egui/src/canvas/tests.rs +++ b/crates/ui-egui/src/canvas/tests.rs @@ -6388,6 +6388,25 @@ fn a_drag_leaves_nothing_owed_to_later_pans() { assert_eq!(after.center, view.center, "a step of 0.2 device pixels alone does not move the view"); } +/// A drag pass that leaves the view where it was clears what earlier pans left owed, so a later step of 0.2 device +/// pixels after 0.4 owed does not move the view by a whole pixel. +#[test] +fn a_drag_that_leaves_the_view_in_place_clears_what_is_owed() { + let rect = egui::Rect::from_min_size(pos2(0.0, 0.0), vec2(640.0, 480.0)); + let start = CanvasView::new(rect, Point::new(100.0, 80.0), 1.0); + let owing = super::PanRest { owed: (0.4, 0.0), ..super::PanRest::none(start, 1.0) }; + let (view, rest) = super::settled_on_device_pixels(Some(owing), start, false); + assert_eq!(view, start, "the drag keeps the view"); + assert_eq!(rest.map(|rest| rest.owed), Some((0.0, 0.0)), "the drag leaves nothing owed"); + let mut stepped = view; + stepped.center = view.center - view.unmap_vec(vec2(0.2, 0.0)); + let (after, _) = super::settled_on_device_pixels(rest, stepped, true); + assert_eq!(after.center, view.center, "a step of 0.2 device pixels alone does not move the view"); + // A pass that carries the rest keeps what is owed when the view stays where it was. + let (_, kept) = super::settled_on_device_pixels(Some(owing), start, true); + assert_eq!(kept.map(|rest| rest.owed), Some((0.4, 0.0)), "a carrying pass keeps what is owed"); +} + /// Steps a tool takes from where the view was (a drag's autoscroll, the Hand's glide) keep the parts of a pixel they /// leave: after each pass the view is within half a device pixel of all the steps together. #[test]