PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Address CodeRabbit review: a Zoom hold belongs to its own press and canvas
David Mann23e9b9ccf86b
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rsindex c26e65ba8..0f4d9c9fe 100644--- a/crates/ui-egui/src/canvas.rs+++ b/crates/ui-egui/src/canvas.rs@@ -3731,7 +3731,9 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect:// it owns the press: no scrub, and its release is no click step.if tool == Tool::Zoom && !transform_owns_pointer {let out = zoom_out(ui.input(|i| i.modifiers.alt));- if crate::zoom_hold::update(&ctx, &mut view, &xf, app.session.prefs().general.animated_zoom, out, ppp) {+ let on_canvas = !under_dialog && !over_compass && response.is_pointer_button_down_on();+ let hold = crate::zoom_hold::hold_id(idx, primary, ctx.viewport_id());+ if crate::zoom_hold::update(&ctx, &mut view, &xf, app.session.prefs().general.animated_zoom, out, ppp, on_canvas, hold) {(buttons.started, buttons.dragged, buttons.stopped, buttons.clicked) = (false, false, false, false);}}diff --git a/crates/ui-egui/src/zoom_hold.rs b/crates/ui-egui/src/zoom_hold.rsindex 673ccf6a6..216ee698e 100644--- a/crates/ui-egui/src/zoom_hold.rs+++ b/crates/ui-egui/src/zoom_hold.rs@@ -37,8 +37,10 @@ struct Hold {dragged: bool,}-fn id() -> egui::Id {- egui::Id::new("pc-zoom-tool-hold")+/// Where a canvas keeps its hold: each canvas (document `idx` of the tiled windows, in the main+/// or a secondary window) has its own, so a hold on one never zooms another.+pub fn hold_id(idx: usize, primary: bool, viewport: egui::ViewportId) -> egui::Id {+ egui::Id::new(("pc-zoom-tool-hold", idx, primary, viewport))}/// The zoom after holding for `dt` seconds at `zoom` (out: shrinking), within the range for a@@ -50,31 +52,42 @@ pub fn hold_zoom(zoom: f32, dt: f64, out: bool, size: [u32; 2]) -> f32 {}/// Is a held press zooming right now? (It owns the gesture: no scrub, no click step.)-pub fn active(ctx: &egui::Context) -> bool {- ctx.data(|d| d.get_temp::<Hold>(id())).is_some_and(|h| h.last.is_some())+pub fn active(ctx: &egui::Context, id: egui::Id) -> bool {+ ctx.data(|d| d.get_temp::<Hold>(id)).is_some_and(|h| h.last.is_some())}-/// Advance a Zoom-tool hold by one frame. `enabled` is the Animated Zoom preference, `out` the-/// zoom direction (⌥ or a temporary zoom-out key, read live). Returns true when the press is a-/// hold zoom (on its release frame too), so the canvas skips the scrub and the click step.-pub fn update(ctx: &egui::Context, view: &mut View, xf: &ViewXform, enabled: bool, out: bool, ppp: f32) -> bool {+/// Advance the Zoom-tool hold of the canvas whose [`hold_id`] is `id` by one frame. `enabled` is+/// the Animated Zoom preference, `out` the zoom direction (⌥ or a temporary zoom-out key, read+/// live), `on_canvas` whether the canvas owns the press (not a dialog, a scrollbar or the compass+/// over it); only such a press can become a hold. Returns true when the press is a hold zoom (on+/// its release frame too), so the canvas skips the scrub and the click step.+#[allow(clippy::too_many_arguments)]+pub fn update(ctx: &egui::Context, view: &mut View, xf: &ViewXform, enabled: bool, out: bool, ppp: f32, on_canvas: bool, id: egui::Id) -> bool {let (down, origin, start, pos, now) =ctx.input(|i| (i.pointer.primary_down(), i.pointer.press_origin(), i.pointer.press_start_time(), i.pointer.latest_pos(), i.time));- let held = ctx.data(|d| d.get_temp::<Hold>(id()));+ let held = ctx.data(|d| d.get_temp::<Hold>(id));if !enabled || !down {if held.is_some() {- ctx.data_mut(|d| d.remove::<Hold>(id()));+ ctx.data_mut(|d| d.remove::<Hold>(id));}return enabled && held.is_some_and(|h| h.last.is_some());}- let mut h = match held {+ let start = start.filter(|t| t.is_finite());+ // A hold left from an earlier press (the tool changed before its release, so no frame here+ // saw the button come up) is not this press's.+ let mine = |h: &Hold| origin == Some(h.anchor) && start.is_none_or(|t| t == h.t0);+ let mut h = match held.filter(mine) {Some(h) => h,None => {- // A press that began on the canvas (not one dragged in from a panel).- let Some(anchor) = origin.filter(|p| xf.rect.contains(*p)) else { return false };- let t0 = start.filter(|t| t.is_finite()).unwrap_or(now);+ // A press that began on the canvas (not one dragged in from a panel) and that the+ // canvas owns.+ let Some(anchor) = origin.filter(|p| on_canvas && xf.rect.contains(*p)) else {+ ctx.data_mut(|d| d.remove::<Hold>(id));+ return false;+ };+ let t0 = start.unwrap_or(now);let h = Hold { anchor, doc: xf.to_doc(anchor), t0, last: None, dragged: false };- ctx.data_mut(|d| d.insert_temp(id(), h));+ ctx.data_mut(|d| d.insert_temp(id, h));h}};@@ -85,7 +98,7 @@ pub fn update(ctx: &egui::Context, view: &mut View, xf: &ViewXform, enabled: boo// Moved before the hold began: a drag (scrubby zoom or rectangle), not a hold.if pos.is_some_and(|p| p.distance(h.anchor) > SLOP) {h.dragged = true;- ctx.data_mut(|d| d.insert_temp(id(), h));+ ctx.data_mut(|d| d.insert_temp(id, h));return false;}let begin = h.t0 + DELAY;@@ -99,7 +112,7 @@ pub fn update(ctx: &egui::Context, view: &mut View, xf: &ViewXform, enabled: boolet zoom = hold_zoom(view.zoom, dt, out, view.doc_size);crate::zoom_tool::anchor_view(view, xf, h.anchor, h.doc, zoom, ppp);h.last = Some(now);- ctx.data_mut(|d| d.insert_temp(id(), h));+ ctx.data_mut(|d| d.insert_temp(id, h));ctx.request_repaint();true}@@ -114,6 +127,11 @@ mod tests {const DOC: [u32; 2] = [400, 300];+ /// The hold of the harness's one canvas.+ fn hid() -> egui::Id {+ hold_id(0, true, egui::ViewportId::ROOT)+ }+#[test]fn hold_zoom_is_frame_rate_independent_and_safe() {// One second in one step or in 60 steps: two doublings either way.@@ -259,6 +277,65 @@ mod tests {button(&mut h, p + vec2(50.0, 0.0), false, false);h.run_steps(2);assert!((h.state().current_zoom() - expected).abs() < 1e-4);- assert!(!active(&h.ctx));+ assert!(!active(&h.ctx, hid()));+ }++ #[test]+ fn a_hold_is_only_reused_by_its_own_press_on_its_own_canvas() {+ // The Hold only goes away on a Zoom-tool frame with the button up. When the tool changed+ // before the release, the next Zoom press can be the first such frame: it is a new press.+ let h = harness(true);+ let (xf, view) = (xf(&h), h.state().ui.views[0].clone());+ let p = xf.rect.center();+ let press = |t: f64| egui::RawInput {+ time: Some(t),+ events: vec![Event::PointerMoved(p), Event::PointerButton { pos: p, button: PointerButton::Primary, pressed: true, modifiers: Default::default() }],+ ..Default::default()+ };+ let elsewhere = p + vec2(-100.0, 30.0);+ let outside = xf.rect.right_bottom() + vec2(50.0, 50.0);+ for stale in [+ Hold { anchor: elsewhere, doc: xf.to_doc(elsewhere), t0: 1.0, last: Some(1.6), dragged: false },+ Hold { anchor: elsewhere, doc: xf.to_doc(elsewhere), t0: 1.0, last: None, dragged: true },+ // Same point, earlier press.+ Hold { anchor: p, doc: xf.to_doc(p), t0: 1.0, last: Some(1.6), dragged: false },+ // A hold on another (tiled) canvas.+ Hold { anchor: outside, doc: [0.0, 0.0], t0: 10.0, last: Some(10.0), dragged: false },+ ] {+ let ctx = egui::Context::default();+ ctx.data_mut(|d| d.insert_temp(hid(), stale));+ let mut v = view.clone();+ ctx.begin_pass(press(10.0));+ let zooming = update(&ctx, &mut v, &xf, true, false, 1.0, true, hid());+ ctx.end_pass().textures_delta.clear();+ assert!(!zooming, "{stale:?}");+ assert_eq!(v.zoom, view.zoom, "{stale:?}");+ let fresh = ctx.data(|d| d.get_temp::<Hold>(hid()));+ assert_eq!(fresh.map(|h| (h.anchor, h.last, h.dragged)), Some((p, None, false)), "{stale:?}");+ }+ // Tiled windows: canvas 0's hold zooms canvas 0 only. Canvas 1 (which doesn't own the+ // press) neither picks it up nor ends it.+ let ctx = egui::Context::default();+ let ours = Hold { anchor: p, doc: xf.to_doc(p), t0: 10.0, last: Some(10.0), dragged: false };+ ctx.data_mut(|d| d.insert_temp(hid(), ours));+ let mut v = view.clone();+ ctx.begin_pass(press(10.0));+ let other = hold_id(1, true, egui::ViewportId::ROOT);+ assert!(!update(&ctx, &mut v, &xf, true, false, 1.0, false, other));+ ctx.end_pass().textures_delta.clear();+ assert_eq!(v.zoom, view.zoom);+ assert_eq!(ctx.data(|d| d.get_temp::<Hold>(hid())), Some(ours));+ }++ #[test]+ fn a_press_the_canvas_does_not_own_never_becomes_a_hold() {+ let mut h = harness(true);+ crate::dialogs::open_command_dialog(h.state_mut(), "image.adjustments.brightnessContrast", "Brightness/Contrast…");+ h.run_steps(3);+ assert_eq!(h.state().ui.dialogs.len(), 1);+ let p = h.state().last_canvas_rect.center() + vec2(-120.0, 70.0);+ let zs = hold(&mut h, p, 8, false);+ assert!(zs.iter().all(|&z| z == 1.0), "{zs:?}");+ assert!(!active(&h.ctx, hid()));}}