From 54dda6fd2faf19ebebd7ad133ed4c0a43362dd63 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 20 Nov 2026 20:41:58 -0800 Subject: [PATCH 1/7] Canvas: a tool drag ends with the modifiers held at its button release The canvas sent a tool's Up event the release event's position but the frame's modifiers. A click released with Shift held, and Shift let go in the same frame, counted as a release without Shift: the Tangent Circle Tool, for one, then drew a circle where Shift at the release draws none. The Up event, and the Zoom tool's click, now take the modifiers from the button release event, and the frame's modifiers when the frame has no release event. --- crates/ui-egui/src/canvas/input.rs | 16 ++++++++++++++-- crates/ui-egui/src/canvas/tests.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/crates/ui-egui/src/canvas/input.rs b/crates/ui-egui/src/canvas/input.rs index a6a0509de5..67391b2e6e 100644 --- a/crates/ui-egui/src/canvas/input.rs +++ b/crates/ui-egui/src/canvas/input.rs @@ -139,8 +139,10 @@ pub(super) fn handle_input(app: &mut VectorcraftApp, ui: &Ui, resp: &egui::Respo if held { drag_held(app, ui, &f, d, p); } else { - // The drag ends where its button went up: the moves after the release in this frame are hover. - drag_released(app, ui, &f, d, release_pos(ui, button, false).unwrap_or(p)); + // The drag ends where its button went up, with the modifiers held then: the moves and + // modifier changes after the release in this frame do not count. + let up = Pointing { m: release_mods(ui, button).unwrap_or(f.m), ..f }; + drag_released(app, ui, &up, d, release_pos(ui, button, false).unwrap_or(p)); } } else if let Some(p) = hover_tool // A press on an artboard's name starts no drag: the moves while it is held are not hover. @@ -744,6 +746,16 @@ fn release_pos(ui: &Ui, button: egui::PointerButton, last: bool) -> Option }) } +/// The modifiers held when `button` first went up in this frame, the release that ends a drag. +fn release_mods(ui: &Ui, button: egui::PointerButton) -> Option { + ui.input(|i| { + i.events.iter().find_map(|e| match e { + egui::Event::PointerButton { button: b, pressed: false, modifiers, .. } if *b == button => Some(*modifiers), + _ => None, + }) + }) +} + /// Whether Space moves the Zoom tool's marquee in the drag under way ([`zoom_marquee_start`]). fn zoom_space_free_id() -> egui::Id { egui::Id::unique("canvas-zoom-space-free") diff --git a/crates/ui-egui/src/canvas/tests.rs b/crates/ui-egui/src/canvas/tests.rs index d5244aa234..cc4660f7bf 100644 --- a/crates/ui-egui/src/canvas/tests.rs +++ b/crates/ui-egui/src/canvas/tests.rs @@ -1421,6 +1421,36 @@ fn shift_clicking_canvas_artboard_label_toggles_multi_selection() { assert_eq!(app.session.tool_options()["selected"], json!([2]), "Plain click selects that artboard alone"); } +/// A tool drag ends with the modifiers held when its button went up. Shift let go after the +/// release in the same frame still constrains the Rectangle tool's last rectangle to a square, and +/// Shift pressed after the release does not constrain it. +#[test] +fn tool_drag_ends_with_the_modifiers_of_its_button_release() { + let draw = |held: egui::Modifiers, after: egui::Modifiers| { + let mut app = VectorcraftApp::new(Session::new(), Default::default()); + app.session.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); + app.select_tool("rectangle"); + let ctx = egui::Context::default(); + frame(&mut app, &ctx, vec![]); + let from = app.canvas_rect.unwrap().center() - vec2(30.0, 15.0); + let to = from + vec2(60.0, 30.0); + let b = |pos, pressed| egui::Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed, modifiers: held }; + frame(&mut app, &ctx, vec![egui::Event::ModifiersChanged(held), egui::Event::PointerMoved(from), b(from, true)]); + frame(&mut app, &ctx, vec![egui::Event::PointerMoved(from + vec2(30.0, 15.0))]); + frame(&mut app, &ctx, vec![egui::Event::PointerMoved(to), b(to, false), egui::Event::ModifiersChanged(after)]); + frame(&mut app, &ctx, vec![egui::Event::ModifiersChanged(egui::Modifiers::NONE)]); + let st = app.session.active().unwrap(); + let kids = st.doc.layers[0].children().unwrap(); + assert_eq!(kids.len(), 1, "the drag draws one rectangle"); + let b = kids[0].geometric_bounds().unwrap(); + (b.width(), b.height()) + }; + let (w, h) = draw(egui::Modifiers::SHIFT, egui::Modifiers::NONE); + assert!((w - h).abs() < 1e-6, "Shift held at the release draws a square, got {w} x {h}"); + let (w, h) = draw(egui::Modifiers::NONE, egui::Modifiers::SHIFT); + assert!((w - 2.0 * h).abs() < 1e-6, "Shift pressed after the release does not constrain, got {w} x {h}"); +} + /// A press on an artboard's name starts no drag. Moving with the button still held is not hover, /// so Shift over a gridline intersection does not position the Perspective Selection tool's plane /// until the button is up. From 45286143243518c81f356495ee009aa7a0dc5843 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 22 Nov 2026 04:14:12 -0800 Subject: [PATCH 2/7] Canvas: a still release sends the tool its changed modifiers and Space A tool drag released without moving since its last frame sent only the up, so Shift pressed in the release's frame did not constrain the shape it committed. The release now also sends a drag when Shift, Alt, Cmd/Ctrl, Space or Backtick changed since the drag's last frame, as a held drag does. Space at the release is now read at the button-up, as the other modifiers are: Space pressed after the release in the same frame no longer moves the shape, and a focus loss after the release keeps Space as it was held. --- crates/ui-egui/src/canvas/drag.rs | 24 +++++++- crates/ui-egui/src/canvas/input.rs | 30 ++++++---- crates/ui-egui/src/canvas/tests.rs | 92 ++++++++++++++++++++++++++++++ 3 files changed, 133 insertions(+), 13 deletions(-) diff --git a/crates/ui-egui/src/canvas/drag.rs b/crates/ui-egui/src/canvas/drag.rs index 512df6e9a9..45b474c6d4 100644 --- a/crates/ui-egui/src/canvas/drag.rs +++ b/crates/ui-egui/src/canvas/drag.rs @@ -109,6 +109,18 @@ pub(super) fn backquote_seen(ui: &Ui) -> (bool, Option) { (now, before) } +/// Whether Space was held at the end of the frame before (`None` before the first), recording +/// whether it is held now. The canvas records it once a frame, as it does Backtick ([`backquote_seen`]). +pub(super) fn space_seen(ui: &Ui) -> Option { + let id = egui::Id::unique("canvas-space-seen"); + let now = ui.input(|i| i.key_down(egui::Key::Space)); + ui.data_mut(|d| { + let before = d.get_temp::(id); + d.insert_temp(id, now); + before + }) +} + /// Whether Backtick was held when `button` went up in this frame, with `before` its state at the end /// of the frame before ([`backquote_seen`]). Its state as the frame began is the opposite of its /// first press or release in the frame; when the window lost the focus first (which lets go of every @@ -116,11 +128,17 @@ pub(super) fn backquote_seen(ui: &Ui) -> (bool, Option) { /// releases and focus losses before the button-up then apply in the order they came. Without a /// button-up in this frame, its state now. pub(super) fn backquote_at_release(ui: &Ui, button: egui::PointerButton, before: Option) -> bool { + key_at_release(ui, egui::Key::Backtick, button, before) +} + +/// Whether `key` was held when `button` went up in this frame, by the rules of +/// [`backquote_at_release`], with `before` its state at the end of the frame before. +pub(super) fn key_at_release(ui: &Ui, key: egui::Key, button: egui::PointerButton, before: Option) -> bool { ui.input(|i| { - let now = i.key_down(egui::Key::Backtick); - // A press or release of Backtick (`Some(pressed)`), or a focus loss (`None`). + let now = i.key_down(key); + // A press or release of `key` (`Some(pressed)`), or a focus loss (`None`). let step = |e: &egui::Event| match e { - egui::Event::Key { key: egui::Key::Backtick, pressed, repeat: false, .. } => Some(Some(*pressed)), + egui::Event::Key { key: k, pressed, repeat: false, .. } if *k == key => Some(Some(*pressed)), egui::Event::WindowFocused(false) => Some(None), _ => None, }; diff --git a/crates/ui-egui/src/canvas/input.rs b/crates/ui-egui/src/canvas/input.rs index 67391b2e6e..5be38cd994 100644 --- a/crates/ui-egui/src/canvas/input.rs +++ b/crates/ui-egui/src/canvas/input.rs @@ -1,8 +1,8 @@ use super::artboards::{artboard_name_at, begin_artboard_name_edit, name_press_id, select_named_artboard}; use super::drag::{ FrameMove, HoverContext, apply_requests, backquote_at_release, backquote_seen, dispatch, drag_art_out, drag_id, drag_layer_id, drag_pos_id, - drop_press, mods, move_pose, on_canvas, pen_pose, pen_pressure, space_lends_view_tools, tool_drag_doc_id, tool_drag_mods_changed, - tool_drag_moved, tool_frame_moves, tool_holds, tool_press_doc_id, + drop_press, key_at_release, mods, move_pose, on_canvas, pen_pose, pen_pressure, space_lends_view_tools, space_seen, tool_drag_doc_id, + tool_drag_mods_changed, tool_drag_moved, tool_frame_moves, tool_holds, tool_press_doc_id, }; use super::view::{ HOLD_DELAY, HOLD_ZOOM, SCRUB_SLOP, SCRUB_ZOOM, WheelTurn, animated_zoom, edge_scroll, edge_scroll_pixels, pan_pixels, pan_ppp, rotate_about, @@ -30,7 +30,8 @@ pub(super) fn handle_input(app: &mut VectorcraftApp, ui: &Ui, resp: &egui::Respo let at = hover.or_else(|| pointer.interact_pos().filter(|p| rect.contains(*p))); let view = app.view_info(); let (backquote_now, backquote_before) = backquote_seen(ui); - let f = Pointing { resp, rect, pointer: &pointer, m, space, xf, v, view, backquote_before, modal }; + let space_before = space_seen(ui); + let f = Pointing { resp, rect, pointer: &pointer, m, space, xf, v, view, backquote_before, space_before, modal }; touch_gestures(app, ui, rect, view, modal); // Pressed and held still, a pen or a finger opens the context menu (egui's long touch): the // tool's press is dropped, as a right-click presses nothing. Under a modal dialog it drops nothing. @@ -139,9 +140,11 @@ pub(super) fn handle_input(app: &mut VectorcraftApp, ui: &Ui, resp: &egui::Respo if held { drag_held(app, ui, &f, d, p); } else { - // The drag ends where its button went up, with the modifiers held then: the moves and - // modifier changes after the release in this frame do not count. - let up = Pointing { m: release_mods(ui, button).unwrap_or(f.m), ..f }; + // The drag ends where its button went up, with the modifiers and Space held then: the + // moves and key changes after the release in this frame do not count. + let m = release_mods(ui, button).unwrap_or(f.m); + let space = space_lends_view_tools(app, m, key_at_release(ui, egui::Key::Space, button, f.space_before)); + let up = Pointing { m, space, ..f }; drag_released(app, ui, &up, d, release_pos(ui, button, false).unwrap_or(p)); } } else if let Some(p) = hover_tool @@ -224,6 +227,8 @@ struct Pointing<'a> { view: vectorcraft_engine::ViewInfo, /// Whether Backtick was held at the end of the frame before ([`backquote_seen`]). backquote_before: Option, + /// Whether Space was held at the end of the frame before ([`space_seen`]). + space_before: Option, /// Whether a modal dialog is open ([`crate::panel_docking::modal_open`]). modal: bool, } @@ -596,14 +601,17 @@ fn drag_released(app: &mut VectorcraftApp, ui: &Ui, f: &Pointing, d: Drag, p: Po let last_drag: Option = ui.data(|dd| dd.get_temp(tool_drag_doc_id())); // Where the pointer was in the frame before: a zoom marquee moved with Space follows it. let before: Option = ui.data(|dd| dd.get_temp(drag_pos_id())); + // Backtick as it was at the button-up: a key pressed or released later in the frame does not count. + let backquote = backquote_at_release(ui, egui::PointerButton::Primary, f.backquote_before); + // Whether Shift, Alt, Cmd/Ctrl, Space or Backtick changed since the drag's last frame, read + // before the drag's state is cleared below. + let mods_changed = matches!(d, Drag::Tool) && tool_drag_mods_changed(ui, mods(m, space), backquote); ui.data_mut(|dd| { dd.remove::(drag_id()); dd.remove::(drag_pos_id()); dd.remove::(tool_drag_doc_id()); dd.remove::(tool_press_doc_id()); }); - // Backtick as it was at the button-up: a key pressed or released later in the frame does not count. - let backquote = backquote_at_release(ui, egui::PointerButton::Primary, f.backquote_before); let ui_zoom = app.canvas_ui_zoom; match d { Drag::ZoomBox { start } => { @@ -640,12 +648,14 @@ fn drag_released(app: &mut VectorcraftApp, ui: &Ui, f: &Pointing, d: Drag, p: Po // (#834). Only once the press has dragged, so a click's jitter stays a click // (the Pen's corner anchor, no nudge with Direct Selection). A tool that builds a // stroke from the drag's samples gets the frame's other moves before it, up to the - // button-up: the moves after it are hover. + // button-up: the moves after it are hover. A modifier that changed at the release + // with the pointer held still reshapes the preview too, as it does while held + // (#4711): Shift pressed in the release's frame still draws a square. let (moves, last_pose) = tool_frame_moves(ui, app, true); let pen = move_pose(app, &xf, last_pose); if let Some(last) = last_drag { dispatch_frame_moves(app, ui, f, backquote, moves); - if last != xf.to_doc(p) { + if last != xf.to_doc(p) || mods_changed { let ev = PointerEvent { kind: PointerKind::Drag, pos: xf.to_doc(p), diff --git a/crates/ui-egui/src/canvas/tests.rs b/crates/ui-egui/src/canvas/tests.rs index cc4660f7bf..73b2d37547 100644 --- a/crates/ui-egui/src/canvas/tests.rs +++ b/crates/ui-egui/src/canvas/tests.rs @@ -1451,6 +1451,98 @@ fn tool_drag_ends_with_the_modifiers_of_its_button_release() { assert!((w - 2.0 * h).abs() < 1e-6, "Shift pressed after the release does not constrain, got {w} x {h}"); } +/// Shift pressed only in the frame of a tool drag's release, with the pointer held still since +/// the drag's last frame, still constrains the Rectangle tool's rectangle to a square. +#[test] +fn shift_pressed_at_a_still_release_constrains_the_tool_drag() { + let mut app = VectorcraftApp::new(Session::new(), Default::default()); + app.session.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); + app.select_tool("rectangle"); + let ctx = egui::Context::default(); + frame(&mut app, &ctx, vec![]); + let from = app.canvas_rect.unwrap().center() - vec2(30.0, 15.0); + let to = from + vec2(60.0, 30.0); + let b = |pos, pressed, modifiers| egui::Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed, modifiers }; + frame(&mut app, &ctx, vec![egui::Event::PointerMoved(from), b(from, true, egui::Modifiers::NONE)]); + frame(&mut app, &ctx, vec![egui::Event::PointerMoved(from + vec2(30.0, 15.0))]); + frame(&mut app, &ctx, vec![egui::Event::PointerMoved(to)]); + frame(&mut app, &ctx, vec![egui::Event::ModifiersChanged(egui::Modifiers::SHIFT), b(to, false, egui::Modifiers::SHIFT)]); + frame(&mut app, &ctx, vec![egui::Event::ModifiersChanged(egui::Modifiers::NONE)]); + let st = app.session.active().unwrap(); + let kids = st.doc.layers[0].children().unwrap(); + assert_eq!(kids.len(), 1, "the drag draws one rectangle"); + let r = kids[0].geometric_bounds().unwrap(); + let (w, h) = (r.width(), r.height()); + assert!((w - h).abs() < 1e-6, "Shift pressed at a still release draws a square, got {w} x {h}"); +} + +/// Space pressed after a tool drag's release, in the same frame, does not count for the drag: the +/// Rectangle tool's last move still resizes the rectangle instead of moving it. +#[test] +fn space_pressed_after_the_release_does_not_move_the_tool_drag() { + let draw = |space_after: bool| { + let mut app = VectorcraftApp::new(Session::new(), Default::default()); + app.session.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); + app.select_tool("rectangle"); + let ctx = egui::Context::default(); + frame(&mut app, &ctx, vec![]); + let from = app.canvas_rect.unwrap().center() - vec2(30.0, 15.0); + let to = from + vec2(60.0, 30.0); + let b = |pos, pressed| egui::Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed, modifiers: egui::Modifiers::NONE }; + let space = + |pressed| egui::Event::Key { key: egui::Key::Space, physical_key: None, pressed, repeat: false, modifiers: egui::Modifiers::NONE }; + frame(&mut app, &ctx, vec![egui::Event::PointerMoved(from), b(from, true)]); + frame(&mut app, &ctx, vec![egui::Event::PointerMoved(from + vec2(30.0, 15.0))]); + let mut release = vec![egui::Event::PointerMoved(to), b(to, false)]; + if space_after { + release.push(space(true)); + } + frame(&mut app, &ctx, release); + frame(&mut app, &ctx, if space_after { vec![space(false)] } else { vec![] }); + let st = app.session.active().unwrap(); + let kids = st.doc.layers[0].children().unwrap(); + assert_eq!(kids.len(), 1, "the drag draws one rectangle"); + let r = kids[0].geometric_bounds().unwrap(); + (r.width(), r.height()) + }; + let plain = draw(false); + let (w, h) = draw(true); + assert!((w - plain.0).abs() < 1e-6 && (h - plain.1).abs() < 1e-6, "Space after the release keeps the size {plain:?}, got {w} x {h}"); +} + +/// Space held through a tool drag still counts at its release when the window loses the focus +/// after the release in the same frame: the Rectangle tool's last move still moves the rectangle. +#[test] +fn space_held_at_the_release_counts_when_the_focus_goes_after_it() { + let draw = |focus_lost: bool| { + let mut app = VectorcraftApp::new(Session::new(), Default::default()); + app.session.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); + app.select_tool("rectangle"); + let ctx = egui::Context::default(); + frame(&mut app, &ctx, vec![]); + let from = app.canvas_rect.unwrap().center() - vec2(30.0, 15.0); + let to = from + vec2(60.0, 30.0); + let b = |pos, pressed| egui::Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed, modifiers: egui::Modifiers::NONE }; + let space = + |pressed| egui::Event::Key { key: egui::Key::Space, physical_key: None, pressed, repeat: false, modifiers: egui::Modifiers::NONE }; + frame(&mut app, &ctx, vec![egui::Event::PointerMoved(from), b(from, true)]); + frame(&mut app, &ctx, vec![egui::Event::PointerMoved(from + vec2(30.0, 15.0))]); + frame(&mut app, &ctx, vec![space(true)]); + let mut release = vec![egui::Event::PointerMoved(to), b(to, false)]; + release.push(if focus_lost { egui::Event::WindowFocused(false) } else { space(false) }); + frame(&mut app, &ctx, release); + frame(&mut app, &ctx, vec![]); + let st = app.session.active().unwrap(); + let kids = st.doc.layers[0].children().unwrap(); + assert_eq!(kids.len(), 1, "the drag draws one rectangle"); + let r = kids[0].geometric_bounds().unwrap(); + (r.x0, r.y0, r.width(), r.height()) + }; + let released = draw(false); + let lost = draw(true); + assert_eq!(lost, released, "a focus loss after the release keeps Space as held at the release"); +} + /// A press on an artboard's name starts no drag. Moving with the button still held is not hover, /// so Shift over a gridline intersection does not position the Perspective Selection tool's plane /// until the button is up. From b7f2eacc6aad0196a8dcd4ffbce0dd490f62da40 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 22 Nov 2026 05:58:13 -0800 Subject: [PATCH 3/7] Canvas: a click pressed and released in one frame ends with its release's modifiers A press whose release came in the same frame started the gesture and left the release for the next frame, which read that frame's modifiers, Space and position. Alt pressed before the release and let go after it in the frame then opened the Rectangle dialog without centering the shape on the click. The press now ends the gesture in its own frame, through the same release path a held drag takes, with the modifiers, Space and position of the button-up. --- crates/ui-egui/src/canvas/input.rs | 24 ++++++++++++++++------ crates/ui-egui/src/canvas/tests.rs | 33 ++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/crates/ui-egui/src/canvas/input.rs b/crates/ui-egui/src/canvas/input.rs index 5be38cd994..b996462561 100644 --- a/crates/ui-egui/src/canvas/input.rs +++ b/crates/ui-egui/src/canvas/input.rs @@ -121,6 +121,13 @@ pub(super) fn handle_input(app: &mut VectorcraftApp, ui: &Ui, resp: &egui::Respo ui.data_mut(|dd| dd.insert_temp(name_press_id(), (i, press_shift))); } else { begin_drag(app, ui, &f, p); + // Pressed and released in this frame (a quick click or tap): the press ends now, with + // the modifiers, Space and position of its release, not those of the next frame. + if !pointer.primary_down() + && let Some(d) = ui.data(|dd| dd.get_temp::(drag_id())) + { + end_drag(app, ui, &f, d, egui::PointerButton::Primary, p); + } } } else if drag.is_none() && resp.hovered() @@ -140,12 +147,7 @@ pub(super) fn handle_input(app: &mut VectorcraftApp, ui: &Ui, resp: &egui::Respo if held { drag_held(app, ui, &f, d, p); } else { - // The drag ends where its button went up, with the modifiers and Space held then: the - // moves and key changes after the release in this frame do not count. - let m = release_mods(ui, button).unwrap_or(f.m); - let space = space_lends_view_tools(app, m, key_at_release(ui, egui::Key::Space, button, f.space_before)); - let up = Pointing { m, space, ..f }; - drag_released(app, ui, &up, d, release_pos(ui, button, false).unwrap_or(p)); + end_drag(app, ui, &f, d, button, p); } } else if let Some(p) = hover_tool // A press on an artboard's name starts no drag: the moves while it is held are not hover. @@ -594,6 +596,16 @@ fn dispatch_frame_moves(app: &mut VectorcraftApp, ui: &Ui, f: &Pointing, backquo } } +/// A gesture whose `button` went up ends where it went up, with the modifiers and Space held then: +/// the moves and key changes after the release in this frame do not count. Without a release in +/// this frame, it ends at `p` with the modifiers and Space held now. +fn end_drag(app: &mut VectorcraftApp, ui: &Ui, f: &Pointing, d: Drag, button: egui::PointerButton, p: Pos2) { + let m = release_mods(ui, button).unwrap_or(f.m); + let space = space_lends_view_tools(app, m, key_at_release(ui, egui::Key::Space, button, f.space_before)); + let up = Pointing { m, space, ..*f }; + drag_released(app, ui, &up, d, release_pos(ui, button, false).unwrap_or(p)); +} + /// A gesture released at `p`: the view settles, or the tool gets its up. fn drag_released(app: &mut VectorcraftApp, ui: &Ui, f: &Pointing, d: Drag, p: Pos2) { let Pointing { resp, rect, m, space, xf, view, .. } = *f; diff --git a/crates/ui-egui/src/canvas/tests.rs b/crates/ui-egui/src/canvas/tests.rs index 73b2d37547..a72dd436ff 100644 --- a/crates/ui-egui/src/canvas/tests.rs +++ b/crates/ui-egui/src/canvas/tests.rs @@ -1476,6 +1476,39 @@ fn shift_pressed_at_a_still_release_constrains_the_tool_drag() { assert!((w - h).abs() < 1e-6, "Shift pressed at a still release draws a square, got {w} x {h}"); } +/// A click whose press and release come in one frame ends with the modifiers held at its release, +/// not those of the next frame: Alt pressed before the release and let go after it in that frame +/// opens the Rectangle dialog centered on the click, and Alt pressed after the release does not. +#[test] +fn a_click_within_one_frame_ends_with_the_modifiers_of_its_release() { + let click = |held: egui::Modifiers, after: egui::Modifiers| { + let mut app = VectorcraftApp::new(Session::new(), Default::default()); + app.session.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); + app.select_tool("rectangle"); + let ctx = egui::Context::default(); + frame(&mut app, &ctx, vec![]); + let at = app.canvas_rect.unwrap().center(); + let b = |pressed, modifiers| egui::Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed, modifiers }; + frame( + &mut app, + &ctx, + vec![ + egui::Event::PointerMoved(at), + b(true, egui::Modifiers::NONE), + egui::Event::ModifiersChanged(held), + b(false, held), + egui::Event::ModifiersChanged(after), + ], + ); + frame(&mut app, &ctx, vec![egui::Event::ModifiersChanged(egui::Modifiers::NONE)]); + let d = app.ui.dialog.as_ref().expect("the click opens the Rectangle dialog"); + assert_eq!(d.kind, "rectangle"); + d.fields.get("alt").cloned() + }; + assert_eq!(click(egui::Modifiers::ALT, egui::Modifiers::NONE), Some(json!(true)), "Alt held at the release centers the rectangle"); + assert_eq!(click(egui::Modifiers::NONE, egui::Modifiers::ALT), Some(json!(false)), "Alt pressed after the release does not"); +} + /// Space pressed after a tool drag's release, in the same frame, does not count for the drag: the /// Rectangle tool's last move still resizes the rectangle instead of moving it. #[test] From e1b15ecea6193262a7ba4ddd4a7f0eee4f86a287 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 22 Nov 2026 06:59:03 -0800 Subject: [PATCH 4/7] Canvas: a click pressed and released in one frame starts where it was pressed A press and release that come in one frame now start the gesture at the press's own position, not where the pointer moved after the release in that frame. With Animated Zoom off, such a Zoom tool click whose pointer then moves away steps the zoom instead of zooming to the area between the release and the new hover position, and a drag pressed, moved and released in one frame zooms to the area dragged across. The Hand, Rotate View and tool presses start at the press position the same way. --- crates/ui-egui/src/canvas/input.rs | 23 +++++++++++++----- crates/ui-egui/src/canvas/tests.rs | 38 ++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 6 deletions(-) diff --git a/crates/ui-egui/src/canvas/input.rs b/crates/ui-egui/src/canvas/input.rs index b996462561..a4ca1863c0 100644 --- a/crates/ui-egui/src/canvas/input.rs +++ b/crates/ui-egui/src/canvas/input.rs @@ -120,13 +120,14 @@ pub(super) fn handle_input(app: &mut VectorcraftApp, ui: &Ui, resp: &egui::Respo .unwrap_or(m.shift); ui.data_mut(|dd| dd.insert_temp(name_press_id(), (i, press_shift))); } else { - begin_drag(app, ui, &f, p); - // Pressed and released in this frame (a quick click or tap): the press ends now, with + // Pressed and released in this frame (a quick click or tap): the press starts where the + // button went down, not where the pointer moved to after the release, and ends now, with // the modifiers, Space and position of its release, not those of the next frame. - if !pointer.primary_down() - && let Some(d) = ui.data(|dd| dd.get_temp::(drag_id())) - { - end_drag(app, ui, &f, d, egui::PointerButton::Primary, p); + let released = !pointer.primary_down(); + let start = if released { press_pos(ui, egui::PointerButton::Primary).filter(|q| rect.contains(*q)).unwrap_or(p) } else { p }; + begin_drag(app, ui, &f, start); + if released && let Some(d) = ui.data(|dd| dd.get_temp::(drag_id())) { + end_drag(app, ui, &f, d, egui::PointerButton::Primary, start); } } } else if drag.is_none() @@ -768,6 +769,16 @@ fn release_pos(ui: &Ui, button: egui::PointerButton, last: bool) -> Option }) } +/// Where `button` first went down in this frame. +fn press_pos(ui: &Ui, button: egui::PointerButton) -> Option { + ui.input(|i| { + i.events.iter().find_map(|e| match e { + egui::Event::PointerButton { pos, button: b, pressed: true, .. } if *b == button => Some(*pos), + _ => None, + }) + }) +} + /// The modifiers held when `button` first went up in this frame, the release that ends a drag. fn release_mods(ui: &Ui, button: egui::PointerButton) -> Option { ui.input(|i| { diff --git a/crates/ui-egui/src/canvas/tests.rs b/crates/ui-egui/src/canvas/tests.rs index a72dd436ff..e1a4260f03 100644 --- a/crates/ui-egui/src/canvas/tests.rs +++ b/crates/ui-egui/src/canvas/tests.rs @@ -1509,6 +1509,44 @@ fn a_click_within_one_frame_ends_with_the_modifiers_of_its_release() { assert_eq!(click(egui::Modifiers::NONE, egui::Modifiers::ALT), Some(json!(false)), "Alt pressed after the release does not"); } +/// With Animated Zoom off, a Zoom tool click whose press and release come in one frame steps the +/// zoom though the pointer then moves away in that frame, and a drag from the press to the release +/// zooms to the area dragged across, in one frame or over several. +#[test] +fn a_zoom_click_within_one_frame_starts_where_it_was_pressed() { + use egui::Event; + let mut app = VectorcraftApp::new(Session::new(), Default::default()); + app.session.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); + app.run("prefs.set", json!({"key": "animatedZoom", "value": false})).unwrap(); + app.select_tool("zoom"); + let ctx = egui::Context::default(); + frame(&mut app, &ctx, vec![]); + let rect = app.canvas_rect.unwrap(); + let at = rect.center() - vec2(100.0, 50.0); + let to = at + vec2(100.0, 50.0); + let button = |pos, pressed| Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed, modifiers: Default::default() }; + let area = |z0: f64| z0 * f64::from((rect.width() / 100.0).min(rect.height() / 50.0)); + let z0 = app.view().unwrap().zoom; + frame(&mut app, &ctx, vec![Event::PointerMoved(at)]); + frame(&mut app, &ctx, vec![button(at, true), button(at, false), Event::PointerMoved(to)]); + frame(&mut app, &ctx, vec![]); + assert_eq!(app.view().unwrap().zoom, crate::state::next_zoom(z0, true), "a click steps the zoom"); + for one_frame in [true, false] { + app.view_mut().unwrap().zoom = z0; + frame(&mut app, &ctx, vec![Event::PointerMoved(at)]); + if one_frame { + frame(&mut app, &ctx, vec![button(at, true), Event::PointerMoved(to), button(to, false)]); + } else { + frame(&mut app, &ctx, vec![button(at, true)]); + frame(&mut app, &ctx, vec![Event::PointerMoved(to)]); + frame(&mut app, &ctx, vec![button(to, false)]); + } + frame(&mut app, &ctx, vec![]); + let z = app.view().unwrap().zoom; + assert!((z - area(z0)).abs() < 1e-6, "a drag (one frame: {one_frame}) zooms to its area: {z0} → {z}, not {}", area(z0)); + } +} + /// Space pressed after a tool drag's release, in the same frame, does not count for the drag: the /// Rectangle tool's last move still resizes the rectangle instead of moving it. #[test] From 2e9040d01b38840f7522b26bc4442d204eed9677 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 22 Nov 2026 11:09:43 -0800 Subject: [PATCH 5/7] Canvas: a press moved and released in one frame drags the tool A tool press that moves and goes up in the same frame now gets a Drag at the release position before its Up, as a held press does, so the Rectangle tool draws the rectangle instead of opening its dialog at the press point. A press that does not move stays a click. - A tool that builds a stroke from the drag's samples gets the frame's moves between the press and the button-up, not the hover moves before the press. - The Zoom tool's marquee for such a press reads only the frame's events between the press and the button-up, with Space as it was at the press: Space and moves after the release no longer shift it. - The press starts its gesture, and its Down goes, with the modifiers, Space and Backtick held at the press, not those held at the end of the frame. Space held at the press and let go before the release pans. - A pan or a turn of the view pressed and released in one frame follows the pointer to the release, and Shift held at the release snaps the turn. --- crates/ui-egui/src/canvas/drag.rs | 44 +++++- crates/ui-egui/src/canvas/input.rs | 136 ++++++++++++++---- crates/ui-egui/src/canvas/tests.rs | 217 ++++++++++++++++++++++++++++- 3 files changed, 365 insertions(+), 32 deletions(-) diff --git a/crates/ui-egui/src/canvas/drag.rs b/crates/ui-egui/src/canvas/drag.rs index 45b474c6d4..9aadc14f3d 100644 --- a/crates/ui-egui/src/canvas/drag.rs +++ b/crates/ui-egui/src/canvas/drag.rs @@ -134,6 +134,18 @@ pub(super) fn backquote_at_release(ui: &Ui, button: egui::PointerButton, before: /// Whether `key` was held when `button` went up in this frame, by the rules of /// [`backquote_at_release`], with `before` its state at the end of the frame before. pub(super) fn key_at_release(ui: &Ui, key: egui::Key, button: egui::PointerButton, before: Option) -> bool { + key_at_button(ui, key, button, false, before) +} + +/// Whether `key` was held when `button` went down in this frame, by the rules of +/// [`backquote_at_release`] with the button-down in place of the button-up. +pub(super) fn key_at_press(ui: &Ui, key: egui::Key, button: egui::PointerButton, before: Option) -> bool { + key_at_button(ui, key, button, true, before) +} + +/// Whether `key` was held when `button` first went down (`pressed`) or up in this frame +/// ([`key_at_release`], [`key_at_press`]). +fn key_at_button(ui: &Ui, key: egui::Key, button: egui::PointerButton, pressed: bool, before: Option) -> bool { ui.input(|i| { let now = i.key_down(key); // A press or release of `key` (`Some(pressed)`), or a focus loss (`None`). @@ -142,7 +154,9 @@ pub(super) fn key_at_release(ui: &Ui, key: egui::Key, button: egui::PointerButto egui::Event::WindowFocused(false) => Some(None), _ => None, }; - let Some(up) = i.events.iter().position(|e| matches!(e, egui::Event::PointerButton { button: b, pressed: false, .. } if *b == button)) else { + let Some(up) = + i.events.iter().position(|e| matches!(e, egui::Event::PointerButton { button: b, pressed: p, .. } if *b == button && *p == pressed)) + else { return now; }; let start = match i.events.iter().find_map(step) { @@ -218,9 +232,30 @@ pub(super) type FrameMove = (Pos2, Option, Option); /// button's button-up in this frame, if it came: the moves after it are hover, and the button-up's /// position is the pointer's. The last move before it is left out only when it is at that /// position; its tilt and rotation come second either way. +#[cfg(test)] pub(super) fn frame_moves(ui: &Ui, stylus: Option<&StylusFeed>, release: Option) -> (Vec, Option) { + frame_moves_since(ui, stylus, release, false) +} + +/// [`frame_moves`], and with `since_press` only the moves after the primary button first went down +/// in this frame: a press that began in this frame did not pass the positions before it. +pub(super) fn frame_moves_since( + ui: &Ui, + stylus: Option<&StylusFeed>, + release: Option, + since_press: bool, +) -> (Vec, Option) { let samples = stylus.map(StylusFeed::take_moves).unwrap_or_default(); ui.input(|i| { + // How many of the frame's moves came before the press, when only those after it count. + let before_press = if since_press { + i.events + .iter() + .position(|e| matches!(e, egui::Event::PointerButton { button: egui::PointerButton::Primary, pressed: true, .. })) + .map_or(0, |k| i.events.iter().take(k).filter(|e| matches!(e, egui::Event::PointerMoved(_))).count()) + } else { + 0 + }; // How many of the frame's moves came before the button-up, and where it came, if it came. let before_up = release.and_then(|b| { i.events.iter().enumerate().find_map(|(k, e)| match e { @@ -264,6 +299,7 @@ pub(super) fn frame_moves(ui: &Ui, stylus: Option<&StylusFeed>, release: Option< } None => moves.pop().and_then(|(_, _, q)| q), }; + moves.drain(..before_press.min(moves.len())); // A burst of events (a stalled frame) is thinned to a bounded count. const MAX: usize = 64; if moves.len() > MAX { @@ -279,10 +315,10 @@ pub(super) fn frame_moves(ui: &Ui, stylus: Option<&StylusFeed>, release: Option< /// for the others, which get one drag a frame. The moves are read either way, so the host's feed /// pairs the next frame's with its own. The tilt and rotation the feed kept for the pointer's /// position come second, for every tool. With `released`, only the moves before the primary -/// button-up count. -pub(super) fn tool_frame_moves(ui: &Ui, app: &VectorcraftApp, released: bool) -> (Vec, Option) { +/// button-up count, and with `pressed_here` too, only those after the press in this frame. +pub(super) fn tool_frame_moves(ui: &Ui, app: &VectorcraftApp, released: bool, pressed_here: bool) -> (Vec, Option) { let release = released.then_some(egui::PointerButton::Primary); - let (moves, last_pose) = frame_moves(ui, app.services.stylus.as_ref(), release); + let (moves, last_pose) = frame_moves_since(ui, app.services.stylus.as_ref(), release, pressed_here); (if app.session.tool_wants_samples() { moves } else { vec![] }, last_pose) } diff --git a/crates/ui-egui/src/canvas/input.rs b/crates/ui-egui/src/canvas/input.rs index a4ca1863c0..20550efe25 100644 --- a/crates/ui-egui/src/canvas/input.rs +++ b/crates/ui-egui/src/canvas/input.rs @@ -1,8 +1,8 @@ use super::artboards::{artboard_name_at, begin_artboard_name_edit, name_press_id, select_named_artboard}; use super::drag::{ FrameMove, HoverContext, apply_requests, backquote_at_release, backquote_seen, dispatch, drag_art_out, drag_id, drag_layer_id, drag_pos_id, - drop_press, key_at_release, mods, move_pose, on_canvas, pen_pose, pen_pressure, space_lends_view_tools, space_seen, tool_drag_doc_id, - tool_drag_mods_changed, tool_drag_moved, tool_frame_moves, tool_holds, tool_press_doc_id, + drop_press, key_at_press, key_at_release, mods, move_pose, on_canvas, pen_pose, pen_pressure, space_lends_view_tools, space_seen, + tool_drag_doc_id, tool_drag_mods_changed, tool_drag_moved, tool_frame_moves, tool_holds, tool_press_doc_id, }; use super::view::{ HOLD_DELAY, HOLD_ZOOM, SCRUB_SLOP, SCRUB_ZOOM, WheelTurn, animated_zoom, edge_scroll, edge_scroll_pixels, pan_pixels, pan_ppp, rotate_about, @@ -122,12 +122,29 @@ pub(super) fn handle_input(app: &mut VectorcraftApp, ui: &Ui, resp: &egui::Respo } else { // Pressed and released in this frame (a quick click or tap): the press starts where the // button went down, not where the pointer moved to after the release, and ends now, with - // the modifiers, Space and position of its release, not those of the next frame. + // the modifiers, Space and position of its release, not those of the next frame. It + // starts, and its Down goes, with the modifiers, Space and Backtick held at the press, + // not those held at the release or after it. let released = !pointer.primary_down(); - let start = if released { press_pos(ui, egui::PointerButton::Primary).filter(|q| rect.contains(*q)).unwrap_or(p) } else { p }; - begin_drag(app, ui, &f, start); + let primary = egui::PointerButton::Primary; + let start = if released { press_pos(ui, primary).filter(|q| rect.contains(*q)).unwrap_or(p) } else { p }; + let (press, backquote) = if released { + let m = press_mods(ui, primary).unwrap_or(m); + let space = space_lends_view_tools(app, m, key_at_press(ui, egui::Key::Space, primary, f.space_before)); + (Pointing { m, space, ..f }, key_at_press(ui, egui::Key::Backtick, primary, backquote_before)) + } else { + (Pointing { ..f }, backquote_now) + }; + begin_drag(app, ui, &press, start, backquote); if released && let Some(d) = ui.data(|dd| dd.get_temp::(drag_id())) { - end_drag(app, ui, &f, d, egui::PointerButton::Primary, start); + // A pan or a turn of the view follows the pointer to where the button went up, with + // the modifiers held then (Shift snaps the turn). + if matches!(d, Drag::Pan { .. } | Drag::RotateView { .. }) + && let Some(up) = release_pos(ui, primary, false) + { + drag_held(app, ui, &Pointing { m: release_mods(ui, primary).unwrap_or(m), ..press }, d, up); + } + end_drag(app, ui, &f, d, egui::PointerButton::Primary, start, true); } } } else if drag.is_none() @@ -148,7 +165,7 @@ pub(super) fn handle_input(app: &mut VectorcraftApp, ui: &Ui, resp: &egui::Respo if held { drag_held(app, ui, &f, d, p); } else { - end_drag(app, ui, &f, d, button, p); + end_drag(app, ui, &f, d, button, p, false); } } else if let Some(p) = hover_tool // A press on an artboard's name starts no drag: the moves while it is held are not hover. @@ -330,8 +347,9 @@ fn present_step(app: &mut VectorcraftApp, ui: &Ui, f: &Pointing, drag: Option { - let start = zoom_marquee_start(ui, start, before, p); + let start = zoom_marquee_start(ui, start, before, p, None); ui.data_mut(|dd| dd.insert_temp(drag_id(), Drag::ZoomBox { start })); marquee(ui.painter(), egui::Rect::from_two_pos(start, p)); } @@ -483,7 +501,7 @@ fn drag_held(app: &mut VectorcraftApp, ui: &Ui, f: &Pointing, d: Drag, p: Pos2) // or the web, a finger) and a macOS pen's moves carry their own pressure; a // mouse's, a pen's on X11 among them, take the host's latest. Other tools get // the last position only, so a costly drag preview runs once a frame. - let (moves, last_pose) = tool_frame_moves(ui, app, false); + let (moves, last_pose) = tool_frame_moves(ui, app, false, false); // Held near an edge of the canvas, the drag scrolls the view so it can go on past // what shows (#8551). The moves above came before the scroll and keep the view they // were made in; the pointer's position takes the scrolled one, so the tool gets a @@ -599,16 +617,21 @@ fn dispatch_frame_moves(app: &mut VectorcraftApp, ui: &Ui, f: &Pointing, backquo /// A gesture whose `button` went up ends where it went up, with the modifiers and Space held then: /// the moves and key changes after the release in this frame do not count. Without a release in -/// this frame, it ends at `p` with the modifiers and Space held now. -fn end_drag(app: &mut VectorcraftApp, ui: &Ui, f: &Pointing, d: Drag, button: egui::PointerButton, p: Pos2) { +/// this frame, it ends at `p` with the modifiers and Space held now. `pressed_here` says that the +/// gesture was pressed in this frame too ([`drag_released`]). +fn end_drag(app: &mut VectorcraftApp, ui: &Ui, f: &Pointing, d: Drag, button: egui::PointerButton, p: Pos2, pressed_here: bool) { let m = release_mods(ui, button).unwrap_or(f.m); let space = space_lends_view_tools(app, m, key_at_release(ui, egui::Key::Space, button, f.space_before)); let up = Pointing { m, space, ..*f }; - drag_released(app, ui, &up, d, release_pos(ui, button, false).unwrap_or(p)); + let cut = if pressed_here { press_span(ui, button) } else { None }; + drag_released(app, ui, &up, d, release_pos(ui, button, false).unwrap_or(p), cut); } -/// A gesture released at `p`: the view settles, or the tool gets its up. -fn drag_released(app: &mut VectorcraftApp, ui: &Ui, f: &Pointing, d: Drag, p: Pos2) { +/// A gesture released at `p`: the view settles, or the tool gets its up. With `cut`, the gesture +/// was pressed in this frame, and `cut` holds the indexes of the frame's events between its press +/// and its button-up ([`press_span`]): a press that moved away from where it went down dragged, and the +/// Zoom tool's marquee takes only those events. +fn drag_released(app: &mut VectorcraftApp, ui: &Ui, f: &Pointing, d: Drag, p: Pos2, cut: Option>) { let Pointing { resp, rect, m, space, xf, view, .. } = *f; // The last position the tool's drag got: none if the press never dragged. let last_drag: Option = ui.data(|dd| dd.get_temp(tool_drag_doc_id())); @@ -628,7 +651,7 @@ fn drag_released(app: &mut VectorcraftApp, ui: &Ui, f: &Pointing, d: Drag, p: Po let ui_zoom = app.canvas_ui_zoom; match d { Drag::ZoomBox { start } => { - let start = zoom_marquee_start(ui, start, before, p); + let start = zoom_marquee_start(ui, start, before, p, cut); ui.data_mut(|dd| dd.remove::(zoom_space_free_id())); let r = egui::Rect::from_two_pos(start, p); if let Some(vm) = app.view_mut() { @@ -664,8 +687,17 @@ fn drag_released(app: &mut VectorcraftApp, ui: &Ui, f: &Pointing, d: Drag, p: Po // button-up: the moves after it are hover. A modifier that changed at the release // with the pointer held still reshapes the preview too, as it does while held // (#4711): Shift pressed in the release's frame still draws a square. - let (moves, last_pose) = tool_frame_moves(ui, app, true); + let (moves, last_pose) = tool_frame_moves(ui, app, true, cut.is_some()); let pen = move_pose(app, &xf, last_pose); + // Pressed and released in this frame: the press dragged when it went up, or moved + // before it went up, away from the press, as a held press drags on any move + // ([`tool_drag_moved`]). The tools keep a click's jitter a click themselves. + let dragged_here = cut.as_ref().is_some_and(|r| { + ui.input(|i| { + let Some(egui::Event::PointerButton { pos: q, .. }) = r.start.checked_sub(1).and_then(|k| i.events.get(k)) else { return false }; + *q != p || i.events.get(r.clone()).unwrap_or_default().iter().any(|e| matches!(e, egui::Event::PointerMoved(at) if at != q)) + }) + }); if let Some(last) = last_drag { dispatch_frame_moves(app, ui, f, backquote, moves); if last != xf.to_doc(p) || mods_changed { @@ -679,6 +711,21 @@ fn drag_released(app: &mut VectorcraftApp, ui: &Ui, f: &Pointing, d: Drag, p: Po }; dispatch(app, &ev, view); } + } else if dragged_here { + // Pressed, moved and released in one frame: the press dragged to where it went + // up, so a Shape tool draws the shape instead of opening its dialog, and a tool + // that builds a stroke from the drag's samples gets the moves after the press + // first. + dispatch_frame_moves(app, ui, f, backquote, moves); + let ev = PointerEvent { + kind: PointerKind::Drag, + pos: xf.to_doc(p), + mods: mods(m, space), + backquote, + pressure: pen_pressure(ui, app, false), + pen, + }; + dispatch(app, &ev, view); } let ev = PointerEvent { kind: PointerKind::Up, @@ -779,6 +826,27 @@ fn press_pos(ui: &Ui, button: egui::PointerButton) -> Option { }) } +/// The modifiers held when `button` first went down in this frame ([`press_pos`]). +fn press_mods(ui: &Ui, button: egui::PointerButton) -> Option { + ui.input(|i| { + i.events.iter().find_map(|e| match e { + egui::Event::PointerButton { button: b, pressed: true, modifiers, .. } if *b == button => Some(*modifiers), + _ => None, + }) + }) +} + +/// The indexes of this frame's events after `button` first went down and before it next went up: +/// the events of a press that began and ended in this frame. `None` without both. +fn press_span(ui: &Ui, button: egui::PointerButton) -> Option> { + ui.input(|i| { + let is = |e: &egui::Event, down: bool| matches!(e, egui::Event::PointerButton { button: b, pressed, .. } if *b == button && *pressed == down); + let press = i.events.iter().position(|e| is(e, true))?; + let up = i.events.iter().skip(press).position(|e| is(e, false))?; + Some(press + 1..press + up) + }) +} + /// The modifiers held when `button` first went up in this frame, the release that ends a drag. fn release_mods(ui: &Ui, button: egui::PointerButton) -> Option { ui.input(|i| { @@ -802,29 +870,43 @@ fn zoom_space_free_id() -> egui::Id { /// moves and Space presses and releases are taken in the order they came, so each move follows the /// Space state it came with. The Space key counts as held whatever the modifiers, so a marquee that /// Cmd+Space started while the Type tool edits text follows the same rules once Cmd is let go. -fn zoom_marquee_start(ui: &Ui, start: Pos2, before: Option, p: Pos2) -> Pos2 { +/// With `cut`, the indexes of the frame's events between a press and a release that both came in +/// this frame ([`press_span`]), only those events count: Space pressed or released and moves made +/// after the button-up do not move the marquee. +fn zoom_marquee_start(ui: &Ui, start: Pos2, before: Option, p: Pos2, cut: Option>) -> Pos2 { enum Step { Space(bool), Move(Pos2), } let id = zoom_space_free_id(); - let (steps, space): (Vec, bool) = ui.input(|i| { + let (steps, space): (Vec<(usize, Step)>, bool) = ui.input(|i| { let steps = i .events .iter() - .filter_map(|e| match e { - egui::Event::Key { key: egui::Key::Space, pressed, repeat: false, .. } => Some(Step::Space(*pressed)), - egui::Event::PointerMoved(q) => Some(Step::Move(*q)), + .enumerate() + .filter_map(|(k, e)| match e { + egui::Event::Key { key: egui::Key::Space, pressed, repeat: false, .. } => Some((k, Step::Space(*pressed))), + egui::Event::PointerMoved(q) => Some((k, Step::Move(*q))), _ => None, }) .collect(); (steps, i.key_down(egui::Key::Space)) }); // Space as it was when the frame began: the opposite of its first press or release, else as it is now. - let mut held = steps.iter().find_map(|s| if let Step::Space(down) = s { Some(!down) } else { None }).unwrap_or(space); - let mut free = !held || ui.data(|d| d.get_temp::(id)).unwrap_or(true); + let mut held = steps.iter().find_map(|(_, s)| if let Step::Space(down) = s { Some(!down) } else { None }).unwrap_or(space); + // With `cut`, Space as it was at the press, and only the steps between the press and the button-up. + let (from, to) = cut.as_ref().map_or((0, usize::MAX), |r| (r.start, r.end)); + for (_, s) in steps.iter().filter(|(k, _)| *k < from) { + if let Step::Space(down) = s { + held = *down; + } + } + let steps = steps.into_iter().filter(|(k, _)| (from..to).contains(k)).map(|(_, s)| s); + // Space held at the press sizes the marquee until it is let go; the press set the flag with + // Space as it was at the end of the frame, so a press in this frame takes it from the press. + let mut free = !held || (cut.is_none() && ui.data(|d| d.get_temp::(id)).unwrap_or(true)); let (mut start, mut at) = (start, before); - for s in steps.into_iter().chain(std::iter::once(Step::Move(p))) { + for s in steps.chain(std::iter::once(Step::Move(p))) { match s { Step::Space(down) => { held = down; diff --git a/crates/ui-egui/src/canvas/tests.rs b/crates/ui-egui/src/canvas/tests.rs index e1a4260f03..9646093f6f 100644 --- a/crates/ui-egui/src/canvas/tests.rs +++ b/crates/ui-egui/src/canvas/tests.rs @@ -1614,6 +1614,220 @@ fn space_held_at_the_release_counts_when_the_focus_goes_after_it() { assert_eq!(lost, released, "a focus loss after the release keeps Space as held at the release"); } +/// A Rectangle tool press that moves and goes up in one frame drags to where it went up: it draws +/// the rectangle across the drag and opens no Rectangle dialog. One that moves no farther than the +/// tool's own click distance is a click, which opens the dialog. +#[test] +fn a_press_moved_and_released_within_one_frame_drags_the_tool() { + { + let mut app = VectorcraftApp::new(Session::new(), Default::default()); + app.session.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); + app.select_tool("rectangle"); + let ctx = egui::Context::default(); + frame(&mut app, &ctx, vec![]); + let at = app.canvas_rect.unwrap().center(); + let jitter = at + vec2(2.0, 1.0); + let b = |pos, pressed| egui::Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed, modifiers: egui::Modifiers::NONE }; + frame(&mut app, &ctx, vec![egui::Event::PointerMoved(at)]); + frame(&mut app, &ctx, vec![b(at, true), egui::Event::PointerMoved(jitter), b(jitter, false)]); + frame(&mut app, &ctx, vec![]); + assert_eq!(app.ui.dialog.as_ref().map(|d| d.kind.as_str()), Some("rectangle"), "a click's jitter opens the dialog"); + assert!(app.session.active().unwrap().doc.layers[0].children().unwrap().is_empty(), "a click's jitter draws nothing"); + } + let mut app = VectorcraftApp::new(Session::new(), Default::default()); + app.session.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); + app.select_tool("rectangle"); + let ctx = egui::Context::default(); + frame(&mut app, &ctx, vec![]); + let from = app.canvas_rect.unwrap().center() - vec2(30.0, 15.0); + let to = from + vec2(60.0, 30.0); + let b = |pos, pressed| egui::Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed, modifiers: egui::Modifiers::NONE }; + frame(&mut app, &ctx, vec![egui::Event::PointerMoved(from)]); + frame(&mut app, &ctx, vec![b(from, true), egui::Event::PointerMoved(to), b(to, false)]); + frame(&mut app, &ctx, vec![]); + assert!(app.ui.dialog.is_none(), "the drag opens no dialog: {:?}", app.ui.dialog.as_ref().map(|d| &d.kind)); + let st = app.session.active().unwrap(); + let kids = st.doc.layers[0].children().unwrap(); + assert_eq!(kids.len(), 1, "the drag draws one rectangle"); + let r = kids[0].geometric_bounds().unwrap(); + let (w, h) = (r.width(), r.height()); + assert!(w > 0.0 && (w - 2.0 * h).abs() < 1e-6, "the rectangle spans the drag, got {w} x {h}"); +} + +/// With Animated Zoom off, a Zoom tool drag pressed and released in one frame zooms to the area +/// dragged across, though Space goes down, the pointer moves and Space goes up after the release in +/// that frame. +#[test] +fn space_after_a_zoom_drag_within_one_frame_does_not_move_its_marquee() { + use egui::Event; + let mut app = VectorcraftApp::new(Session::new(), Default::default()); + app.session.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); + app.run("prefs.set", json!({"key": "animatedZoom", "value": false})).unwrap(); + app.select_tool("zoom"); + let ctx = egui::Context::default(); + frame(&mut app, &ctx, vec![]); + let rect = app.canvas_rect.unwrap(); + let at = rect.center() - vec2(100.0, 50.0); + let to = at + vec2(100.0, 50.0); + let button = |pos, pressed| Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed, modifiers: Default::default() }; + let space = |pressed| Event::Key { key: egui::Key::Space, physical_key: None, pressed, repeat: false, modifiers: Default::default() }; + let z0 = app.view().unwrap().zoom; + let area = z0 * f64::from((rect.width() / 100.0).min(rect.height() / 50.0)); + frame(&mut app, &ctx, vec![Event::PointerMoved(at)]); + frame( + &mut app, + &ctx, + vec![button(at, true), Event::PointerMoved(to), button(to, false), space(true), Event::PointerMoved(to + vec2(40.0, 20.0)), space(false)], + ); + frame(&mut app, &ctx, vec![]); + let z = app.view().unwrap().zoom; + assert!((z - area).abs() < 1e-6, "the drag zooms to its area: {z0} → {z}, not {area}"); + // Cmd+Space held at the press and Space let go before the release, in the press's frame: the + // moves while Space was held size the marquee, as they do over several frames. + app.view_mut().unwrap().zoom = z0; + let cmd = egui::Modifiers::COMMAND; + let button = |pos, pressed| Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed, modifiers: cmd }; + let space = |pressed| Event::Key { key: egui::Key::Space, physical_key: None, pressed, repeat: false, modifiers: cmd }; + frame(&mut app, &ctx, vec![Event::ModifiersChanged(cmd), space(true), Event::PointerMoved(at)]); + frame( + &mut app, + &ctx, + vec![button(at, true), Event::PointerMoved(at + vec2(50.0, 25.0)), Event::PointerMoved(to), space(false), button(to, false)], + ); + frame(&mut app, &ctx, vec![Event::ModifiersChanged(egui::Modifiers::NONE)]); + let z = app.view().unwrap().zoom; + assert!((z - area).abs() < 1e-6, "Space held at the press sizes the marquee: {z0} → {z}, not {area}"); +} + +/// A Selection tool click pressed and released in one frame presses with the modifiers held at +/// its press: Shift held at the press and let go before the release adds the clicked object to +/// the selection. +#[test] +fn a_click_within_one_frame_presses_with_the_modifiers_of_its_press() { + let mut app = VectorcraftApp::new(Session::new(), Default::default()); + app.run("file.new", json!({"width": 400, "height": 300})).unwrap(); + let a = app.run("shape.rectangle", json!({"x": 20, "y": 20, "width": 60, "height": 60})).unwrap()["id"].clone(); + let b = app.run("shape.rectangle", json!({"x": 200, "y": 100, "width": 60, "height": 60})).unwrap()["id"].clone(); + app.select_tool("selection"); + app.run("select.set", json!({"ids": [a]})).unwrap(); + let ctx = egui::Context::default(); + frame(&mut app, &ctx, vec![]); + let xf = Xf::new(app.canvas_rect.unwrap(), app.view().unwrap()); + let at = xf.to_screen(Point::new(230.0, 130.0)); + let press = |pressed, modifiers| egui::Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed, modifiers }; + frame(&mut app, &ctx, vec![egui::Event::PointerMoved(at)]); + frame( + &mut app, + &ctx, + vec![ + egui::Event::ModifiersChanged(egui::Modifiers::SHIFT), + press(true, egui::Modifiers::SHIFT), + egui::Event::ModifiersChanged(egui::Modifiers::NONE), + press(false, egui::Modifiers::NONE), + ], + ); + frame(&mut app, &ctx, vec![]); + let id = |v: &serde_json::Value| vectorcraft_doc::NodeId(v.as_u64().unwrap()); + let mut selected = app.session.active().unwrap().selection.objects.clone(); + selected.sort_by_key(|n| n.0); + assert_eq!(selected, vec![id(&a), id(&b)], "Shift held at the press adds the clicked object"); +} + +/// A press and release in one frame takes Space as it was at the press: Space held at the press +/// and let go before the release pans the view to the release instead of drawing with the +/// Rectangle tool, and Space pressed after the release leaves a Rectangle tool click, which opens +/// its dialog. +#[test] +fn a_press_within_one_frame_takes_space_as_it_was_at_the_press() { + let run = |events: &dyn Fn(Pos2, Pos2) -> Vec| { + let mut app = VectorcraftApp::new(Session::new(), Default::default()); + app.session.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); + app.select_tool("rectangle"); + let ctx = egui::Context::default(); + frame(&mut app, &ctx, vec![]); + let at = app.canvas_rect.unwrap().center(); + frame(&mut app, &ctx, vec![egui::Event::PointerMoved(at)]); + let center = app.view().unwrap().center; + frame(&mut app, &ctx, events(at, at + vec2(60.0, 30.0))); + frame(&mut app, &ctx, vec![]); + let drawn = app.session.active().unwrap().doc.layers[0].children().unwrap().len(); + let panned = app.view().unwrap().center != center; + (drawn, app.ui.dialog.as_ref().map(|d| d.kind.clone()), panned) + }; + let b = |pos, pressed| egui::Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed, modifiers: egui::Modifiers::NONE }; + let space = |pressed| egui::Event::Key { key: egui::Key::Space, physical_key: None, pressed, repeat: false, modifiers: egui::Modifiers::NONE }; + let held = run(&|a, to| vec![space(true), b(a, true), egui::Event::PointerMoved(to), space(false), b(to, false)]); + assert_eq!(held, (0, None, true), "Space held at the press pans"); + let after = run(&|a, _| vec![b(a, true), b(a, false), space(true)]); + assert_eq!(after, (0, Some("rectangle".to_string()), false), "Space after the release leaves a click"); +} + +/// A Rotate View drag pressed and released in one frame turns the view to the release, snapped +/// to 15° when Shift is held at the release, though it was not held at the press nor at the end +/// of the frame. +#[test] +fn a_rotate_view_drag_within_one_frame_turns_with_the_release_modifiers() { + let turn = |shift_at_release: bool| { + let mut app = VectorcraftApp::new(Session::new(), Default::default()); + app.session.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); + app.select_tool("rotateView"); + let ctx = egui::Context::default(); + frame(&mut app, &ctx, vec![]); + let c = app.canvas_rect.unwrap().center(); + let (from, to) = (c + vec2(100.0, 0.0), c + vec2(100.0, 36.4)); + let up = if shift_at_release { egui::Modifiers::SHIFT } else { egui::Modifiers::NONE }; + let b = |pos, pressed, modifiers| egui::Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed, modifiers }; + frame(&mut app, &ctx, vec![egui::Event::PointerMoved(from)]); + frame( + &mut app, + &ctx, + vec![ + b(from, true, egui::Modifiers::NONE), + egui::Event::PointerMoved(to), + egui::Event::ModifiersChanged(up), + b(to, false, up), + egui::Event::ModifiersChanged(egui::Modifiers::NONE), + ], + ); + frame(&mut app, &ctx, vec![]); + app.view().unwrap().rotation + }; + let free = turn(false); + assert!(free.rem_euclid(15.0) > 1.0 && free.rem_euclid(15.0) < 14.0, "without Shift the turn is not snapped: {free}"); + let snapped = turn(true); + assert!((snapped.rem_euclid(15.0) % 15.0).abs() < 1e-9, "Shift held at the release snaps the turn: {snapped}"); +} + +/// A Pencil stroke pressed and released in one frame takes the moves between its press and its +/// release, not the moves before the press: it bends through the point it passed, and it starts +/// at the press. +#[test] +fn a_stroke_within_one_frame_takes_the_moves_after_its_press() { + use egui::{Event, Modifiers}; + let mut app = VectorcraftApp::new(Session::new(), Default::default()); + app.session.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); + app.ui.view.smart_guides = false; + app.ui.view.snap_to_point = false; + let ctx = egui::Context::default(); + frame(&mut app, &ctx, vec![]); + let xf = Xf::new(app.canvas_rect.unwrap(), app.view().unwrap()); + let button = |pos, pressed| Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed, modifiers: Modifiers::NONE }; + let a = app.canvas_rect.unwrap().center() - vec2(100.0, 0.0); + let end = a + vec2(120.0, 0.0); + app.select_tool("pencil"); + frame(&mut app, &ctx, vec![Event::PointerMoved(a - vec2(60.0, 0.0))]); + let mut events = vec![Event::PointerMoved(a - vec2(30.0, 0.0)), Event::PointerMoved(a), button(a, true)]; + events.extend([20.0, 40.0, 60.0, 80.0, 100.0].map(|x: f32| Event::PointerMoved(a + vec2(x, 40.0 - (x - 60.0).abs() * 0.5)))); + events.extend([Event::PointerMoved(end), button(end, false)]); + frame(&mut app, &ctx, events); + frame(&mut app, &ctx, vec![]); + let st = app.session.active().unwrap(); + let r = st.doc.node(*st.selection.objects.first().expect("the stroke is drawn and selected")).unwrap().geometric_bounds().unwrap(); + let (left, right) = (xf.to_doc(a), xf.to_doc(end)); + assert!((r.x0 - left.x).abs() < 1.0 && (r.x1 - right.x).abs() < 1.0, "the stroke runs from the press to the release: {r:?}"); + assert!(r.height() > 10.0, "the stroke bends through the moves after the press: {r:?}"); +} + /// A press on an artboard's name starts no drag. Moving with the button still held is not hover, /// so Shift over a gridline intersection does not position the Perspective Selection tool's plane /// until the button is up. @@ -12280,7 +12494,8 @@ fn only_the_freehand_tools_get_every_move_of_a_frame() { { app.select_tool(tool); let mut moves = vec![]; - let mut out = ctx.run_ui(egui::RawInput { events: events.clone(), ..Default::default() }, |ui| moves = tool_frame_moves(ui, &app, false).0); + let mut out = + ctx.run_ui(egui::RawInput { events: events.clone(), ..Default::default() }, |ui| moves = tool_frame_moves(ui, &app, false, false).0); out.textures_delta.clear(); assert_eq!(moves.len(), if every { 19 } else { 0 }, "{tool}"); } From b16377d22358e0bef3c994767a38f5a5463430f7 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 22 Nov 2026 14:24:15 -0800 Subject: [PATCH 6/7] Canvas: a press in one frame ends at its own release, and a one-frame Animated Zoom drag scrubs When a frame holds the release of an earlier press, then a new press and its release, the new press now ends at the release after it: its Up, the pan or turn of the view, the modifiers, Space and Backtick all come from that release, and a stroke's moves stop there. Before, they came from the first release in the frame, which belongs to the earlier press. With Animated Zoom on, a Zoom tool press that moves more than the scrub slop and goes up in the same frame now zooms with the pointer about the press, as it does over several frames, instead of stepping as a click. Only the moves before the button-up count; a press that stays within the slop still steps. --- crates/ui-egui/src/canvas/drag.rs | 58 ++++++++------------- crates/ui-egui/src/canvas/input.rs | 79 ++++++++++++++++------------ crates/ui-egui/src/canvas/tests.rs | 82 ++++++++++++++++++++++++++++++ 3 files changed, 151 insertions(+), 68 deletions(-) diff --git a/crates/ui-egui/src/canvas/drag.rs b/crates/ui-egui/src/canvas/drag.rs index 9aadc14f3d..4064b68c39 100644 --- a/crates/ui-egui/src/canvas/drag.rs +++ b/crates/ui-egui/src/canvas/drag.rs @@ -121,31 +121,13 @@ pub(super) fn space_seen(ui: &Ui) -> Option { }) } -/// Whether Backtick was held when `button` went up in this frame, with `before` its state at the end -/// of the frame before ([`backquote_seen`]). Its state as the frame began is the opposite of its -/// first press or release in the frame; when the window lost the focus first (which lets go of every -/// key without a release), it is `before`; with neither, it is its state now. The frame's presses, -/// releases and focus losses before the button-up then apply in the order they came. Without a -/// button-up in this frame, its state now. -pub(super) fn backquote_at_release(ui: &Ui, button: egui::PointerButton, before: Option) -> bool { - key_at_release(ui, egui::Key::Backtick, button, before) -} - -/// Whether `key` was held when `button` went up in this frame, by the rules of -/// [`backquote_at_release`], with `before` its state at the end of the frame before. -pub(super) fn key_at_release(ui: &Ui, key: egui::Key, button: egui::PointerButton, before: Option) -> bool { - key_at_button(ui, key, button, false, before) -} - -/// Whether `key` was held when `button` went down in this frame, by the rules of -/// [`backquote_at_release`] with the button-down in place of the button-up. -pub(super) fn key_at_press(ui: &Ui, key: egui::Key, button: egui::PointerButton, before: Option) -> bool { - key_at_button(ui, key, button, true, before) -} - -/// Whether `key` was held when `button` first went down (`pressed`) or up in this frame -/// ([`key_at_release`], [`key_at_press`]). -fn key_at_button(ui: &Ui, key: egui::Key, button: egui::PointerButton, pressed: bool, before: Option) -> bool { +/// Whether `key` (Backtick, Space) was held at the frame's event at index `at`, a button going down +/// or up, with `before` its state at the end of the frame before ([`backquote_seen`], [`space_seen`]). +/// Its state as the frame began is the opposite of its first press or release in the frame; when +/// the window lost the focus first (which lets go of every key without a release), it is `before`; +/// with neither, it is its state now. The frame's presses, releases and focus losses before `at` +/// then apply in the order they came. Without `at`, its state now. +pub(super) fn key_at_event(ui: &Ui, key: egui::Key, at: Option, before: Option) -> bool { ui.input(|i| { let now = i.key_down(key); // A press or release of `key` (`Some(pressed)`), or a focus loss (`None`). @@ -154,9 +136,7 @@ fn key_at_button(ui: &Ui, key: egui::Key, button: egui::PointerButton, pressed: egui::Event::WindowFocused(false) => Some(None), _ => None, }; - let Some(up) = - i.events.iter().position(|e| matches!(e, egui::Event::PointerButton { button: b, pressed: p, .. } if *b == button && *p == pressed)) - else { + let Some(up) = at.map(|k| k.min(i.events.len())) else { return now; }; let start = match i.events.iter().find_map(step) { @@ -168,6 +148,13 @@ fn key_at_button(ui: &Ui, key: egui::Key, button: egui::PointerButton, pressed: }) } +/// Whether `key` was held when `button` first went down in this frame, by the rules of +/// [`key_at_event`]. +pub(super) fn key_at_press(ui: &Ui, key: egui::Key, button: egui::PointerButton, before: Option) -> bool { + let at = ui.input(|i| i.events.iter().position(|e| matches!(e, egui::Event::PointerButton { button: b, pressed: true, .. } if *b == button))); + key_at_event(ui, key, at, before) +} + /// The pen pressure (0..1) of the press in progress: the force of this frame's pen or touch /// input, else the pressure in the host's feed while a pen is the pointer that moved last /// ([`crate::Services::stylus`]: [`StylusFeed::press`] for a new press, [`StylusFeed::held`] @@ -238,7 +225,8 @@ pub(super) fn frame_moves(ui: &Ui, stylus: Option<&StylusFeed>, release: Option< } /// [`frame_moves`], and with `since_press` only the moves after the primary button first went down -/// in this frame: a press that began in this frame did not pass the positions before it. +/// in this frame: a press that began in this frame did not pass the positions before it. The moves +/// then stop at the first button-up after that press, not at a release that came before it. pub(super) fn frame_moves_since( ui: &Ui, stylus: Option<&StylusFeed>, @@ -248,17 +236,15 @@ pub(super) fn frame_moves_since( let samples = stylus.map(StylusFeed::take_moves).unwrap_or_default(); ui.input(|i| { // How many of the frame's moves came before the press, when only those after it count. - let before_press = if since_press { - i.events - .iter() - .position(|e| matches!(e, egui::Event::PointerButton { button: egui::PointerButton::Primary, pressed: true, .. })) - .map_or(0, |k| i.events.iter().take(k).filter(|e| matches!(e, egui::Event::PointerMoved(_))).count()) + let press = if since_press { + i.events.iter().position(|e| matches!(e, egui::Event::PointerButton { button: egui::PointerButton::Primary, pressed: true, .. })) } else { - 0 + None }; + let before_press = press.map_or(0, |k| i.events.iter().take(k).filter(|e| matches!(e, egui::Event::PointerMoved(_))).count()); // How many of the frame's moves came before the button-up, and where it came, if it came. let before_up = release.and_then(|b| { - i.events.iter().enumerate().find_map(|(k, e)| match e { + i.events.iter().enumerate().skip(press.map_or(0, |k| k + 1)).find_map(|(k, e)| match e { egui::Event::PointerButton { pos, button, pressed: false, .. } if *button == b => { Some((i.events.iter().take(k).filter(|e| matches!(e, egui::Event::PointerMoved(_))).count(), *pos)) } diff --git a/crates/ui-egui/src/canvas/input.rs b/crates/ui-egui/src/canvas/input.rs index 20550efe25..785f1225ac 100644 --- a/crates/ui-egui/src/canvas/input.rs +++ b/crates/ui-egui/src/canvas/input.rs @@ -1,8 +1,8 @@ use super::artboards::{artboard_name_at, begin_artboard_name_edit, name_press_id, select_named_artboard}; use super::drag::{ - FrameMove, HoverContext, apply_requests, backquote_at_release, backquote_seen, dispatch, drag_art_out, drag_id, drag_layer_id, drag_pos_id, - drop_press, key_at_press, key_at_release, mods, move_pose, on_canvas, pen_pose, pen_pressure, space_lends_view_tools, space_seen, - tool_drag_doc_id, tool_drag_mods_changed, tool_drag_moved, tool_frame_moves, tool_holds, tool_press_doc_id, + FrameMove, HoverContext, apply_requests, backquote_seen, dispatch, drag_art_out, drag_id, drag_layer_id, drag_pos_id, drop_press, key_at_event, + key_at_press, mods, move_pose, on_canvas, pen_pose, pen_pressure, space_lends_view_tools, space_seen, tool_drag_doc_id, tool_drag_mods_changed, + tool_drag_moved, tool_frame_moves, tool_holds, tool_press_doc_id, }; use super::view::{ HOLD_DELAY, HOLD_ZOOM, SCRUB_SLOP, SCRUB_ZOOM, WheelTurn, animated_zoom, edge_scroll, edge_scroll_pixels, pan_pixels, pan_ppp, rotate_about, @@ -136,13 +136,33 @@ pub(super) fn handle_input(app: &mut VectorcraftApp, ui: &Ui, resp: &egui::Respo (Pointing { ..f }, backquote_now) }; begin_drag(app, ui, &press, start, backquote); - if released && let Some(d) = ui.data(|dd| dd.get_temp::(drag_id())) { - // A pan or a turn of the view follows the pointer to where the button went up, with - // the modifiers held then (Shift snaps the turn). - if matches!(d, Drag::Pan { .. } | Drag::RotateView { .. }) - && let Some(up) = release_pos(ui, primary, false) - { - drag_held(app, ui, &Pointing { m: release_mods(ui, primary).unwrap_or(m), ..press }, d, up); + if released && let Some(mut d) = ui.data(|dd| dd.get_temp::(drag_id())) { + let span = press_span(ui, primary); + if let Some((_, up, up_mods)) = ending_release(ui, primary, span.as_ref()) { + match d { + // A pan or a turn of the view follows the pointer to where the button went + // up, with the modifiers held then (Shift snaps the turn). + Drag::Pan { .. } | Drag::RotateView { .. } => drag_held(app, ui, &Pointing { m: up_mods, ..press }, d, up), + // An Animated Zoom press that moved more than the scrub's slop before it went + // up zooms with the pointer as a held one does, about where it was pressed; + // one that stayed within it is a click. Only the moves before the button-up count. + Drag::ZoomScrub { start: from, held, .. } => { + let moved = ui.input(|i| { + let span = i.events.get(span.clone().unwrap_or_default()).unwrap_or_default(); + std::iter::once(up) + .chain(span.iter().filter_map(|e| if let egui::Event::PointerMoved(q) = e { Some(*q) } else { None })) + .any(|q| (q - from).length() > SCRUB_SLOP) + }); + if moved { + let ui_zoom = app.canvas_ui_zoom; + if let Some(vm) = app.view_mut() { + zoom_about(vm, rect, from, vm.zoom * (f64::from(up.x - from.x) * SCRUB_ZOOM).exp(), ui_zoom); + } + d = Drag::ZoomScrub { start: from, held, moved: true }; + } + } + _ => {} + } } end_drag(app, ui, &f, d, egui::PointerButton::Primary, start, true); } @@ -618,13 +638,15 @@ fn dispatch_frame_moves(app: &mut VectorcraftApp, ui: &Ui, f: &Pointing, backquo /// A gesture whose `button` went up ends where it went up, with the modifiers and Space held then: /// the moves and key changes after the release in this frame do not count. Without a release in /// this frame, it ends at `p` with the modifiers and Space held now. `pressed_here` says that the -/// gesture was pressed in this frame too ([`drag_released`]). +/// gesture was pressed in this frame too ([`drag_released`]), and then the release that ends it is +/// the one after that press ([`ending_release`]). fn end_drag(app: &mut VectorcraftApp, ui: &Ui, f: &Pointing, d: Drag, button: egui::PointerButton, p: Pos2, pressed_here: bool) { - let m = release_mods(ui, button).unwrap_or(f.m); - let space = space_lends_view_tools(app, m, key_at_release(ui, egui::Key::Space, button, f.space_before)); - let up = Pointing { m, space, ..*f }; let cut = if pressed_here { press_span(ui, button) } else { None }; - drag_released(app, ui, &up, d, release_pos(ui, button, false).unwrap_or(p), cut); + let release = ending_release(ui, button, cut.as_ref()); + let m = release.map_or(f.m, |(_, _, m)| m); + let space = space_lends_view_tools(app, m, key_at_event(ui, egui::Key::Space, release.map(|(k, ..)| k), f.space_before)); + let up = Pointing { m, space, ..*f }; + drag_released(app, ui, &up, d, release.map_or(p, |(_, q, _)| q), cut); } /// A gesture released at `p`: the view settles, or the tool gets its up. With `cut`, the gesture @@ -638,7 +660,8 @@ fn drag_released(app: &mut VectorcraftApp, ui: &Ui, f: &Pointing, d: Drag, p: Po // Where the pointer was in the frame before: a zoom marquee moved with Space follows it. let before: Option = ui.data(|dd| dd.get_temp(drag_pos_id())); // Backtick as it was at the button-up: a key pressed or released later in the frame does not count. - let backquote = backquote_at_release(ui, egui::PointerButton::Primary, f.backquote_before); + let release = ending_release(ui, egui::PointerButton::Primary, cut.as_ref()).map(|(k, ..)| k); + let backquote = key_at_event(ui, egui::Key::Backtick, release, f.backquote_before); // Whether Shift, Alt, Cmd/Ctrl, Space or Backtick changed since the drag's last frame, read // before the drag's state is cleared below. let mods_changed = matches!(d, Drag::Tool) && tool_drag_mods_changed(ui, mods(m, space), backquote); @@ -805,14 +828,16 @@ fn triple_click(app: &mut VectorcraftApp, ui: &Ui, f: &Pointing, p: Pos2) { dispatch(app, &ev, view); } -/// Where `button` went up: the first release ends a drag, the last classifies this frame's click. -fn release_pos(ui: &Ui, button: egui::PointerButton, last: bool) -> Option { +/// The release of `button` that ends a gesture, with its index among the frame's events, where it +/// came and the modifiers held then. With `cut`, the gesture was pressed in this frame, and its +/// release is the one that ends `cut` ([`press_span`]), not one that came before the press; else +/// it is the first release in this frame. `None` without one. +fn ending_release(ui: &Ui, button: egui::PointerButton, cut: Option<&std::ops::Range>) -> Option<(usize, Pos2, egui::Modifiers)> { ui.input(|i| { - let mut releases = i.events.iter().filter_map(|e| match e { - egui::Event::PointerButton { pos, button: b, pressed: false, .. } if *b == button => Some(*pos), + i.events.iter().enumerate().skip(cut.map_or(0, |r| r.end)).find_map(|(k, e)| match e { + egui::Event::PointerButton { pos, button: b, pressed: false, modifiers } if *b == button => Some((k, *pos, *modifiers)), _ => None, - }); - if last { releases.next_back() } else { releases.next() } + }) }) } @@ -847,16 +872,6 @@ fn press_span(ui: &Ui, button: egui::PointerButton) -> Option Option { - ui.input(|i| { - i.events.iter().find_map(|e| match e { - egui::Event::PointerButton { button: b, pressed: false, modifiers, .. } if *b == button => Some(*modifiers), - _ => None, - }) - }) -} - /// Whether Space moves the Zoom tool's marquee in the drag under way ([`zoom_marquee_start`]). fn zoom_space_free_id() -> egui::Id { egui::Id::unique("canvas-zoom-space-free") diff --git a/crates/ui-egui/src/canvas/tests.rs b/crates/ui-egui/src/canvas/tests.rs index 9646093f6f..42ba46dfc6 100644 --- a/crates/ui-egui/src/canvas/tests.rs +++ b/crates/ui-egui/src/canvas/tests.rs @@ -1547,6 +1547,88 @@ fn a_zoom_click_within_one_frame_starts_where_it_was_pressed() { } } +/// With Animated Zoom on, a Zoom tool press dragged sideways and released in one frame zooms with +/// the pointer about the press, as it does over several frames, instead of stepping as a click. A +/// press released where it went down, with a move after the release in that frame, still steps. +#[test] +fn an_animated_zoom_drag_within_one_frame_scrubs() { + use egui::Event; + let mut app = VectorcraftApp::new(Session::new(), Default::default()); + app.session.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); + app.select_tool("zoom"); + let ctx = egui::Context::default(); + frame(&mut app, &ctx, vec![]); + let rect = app.canvas_rect.unwrap(); + let at = rect.center() - vec2(100.0, 50.0); + let to = at + vec2(100.0, 0.0); + let button = |pos, pressed| Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed, modifiers: Default::default() }; + let doc_at = |app: &VectorcraftApp, p: Pos2| Xf::new(rect, app.view().unwrap()).to_doc(p); + frame(&mut app, &ctx, vec![Event::PointerMoved(at)]); + let (z0, pinned) = (app.view().unwrap().zoom, doc_at(&app, at)); + frame(&mut app, &ctx, vec![button(at, true), Event::PointerMoved(to), button(to, false)]); + frame(&mut app, &ctx, vec![]); + let z1 = app.view().unwrap().zoom; + assert!((z1 / z0 - 2.0).abs() < 1e-6, "100 points right doubles the zoom: {z0} → {z1}"); + assert!(doc_at(&app, at).distance(pinned) < 1e-6, "about the press"); + app.view_mut().unwrap().zoom = z0; + frame(&mut app, &ctx, vec![Event::PointerMoved(at)]); + frame(&mut app, &ctx, vec![button(at, true), button(at, false), Event::PointerMoved(to)]); + frame(&mut app, &ctx, vec![]); + assert_eq!(app.view().unwrap().zoom, crate::state::next_zoom(z0, true), "a click steps the zoom"); +} + +/// A press and release in one frame that follow the release of an earlier press in that frame end +/// at their own release, with its modifiers: with Animated Zoom off, a Zoom tool click steps the +/// zoom in though Alt was held at the earlier release, and a drag zooms to the area it dragged +/// across, not to the earlier release. +#[test] +fn a_press_within_one_frame_after_an_earlier_release_ends_at_its_own_release() { + use egui::Event; + let mut app = VectorcraftApp::new(Session::new(), Default::default()); + app.session.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); + app.run("prefs.set", json!({"key": "animatedZoom", "value": false})).unwrap(); + app.select_tool("zoom"); + let ctx = egui::Context::default(); + frame(&mut app, &ctx, vec![]); + let rect = app.canvas_rect.unwrap(); + let at = rect.center() - vec2(100.0, 50.0); + let to = at + vec2(100.0, 50.0); + let (near, far) = (rect.center() + vec2(110.0, 90.0), rect.center() + vec2(150.0, 120.0)); + let button = |pos, pressed, modifiers| Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed, modifiers }; + let none = egui::Modifiers::NONE; + let z0 = app.view().unwrap().zoom; + // A press dragged in earlier frames, then in one frame its release, a new press and that + // press's release. + let run = |app: &mut VectorcraftApp, events: Vec| { + app.view_mut().unwrap().zoom = z0; + frame(app, &ctx, vec![Event::PointerMoved(near)]); + frame(app, &ctx, vec![button(near, true, none)]); + frame(app, &ctx, vec![Event::PointerMoved(far)]); + frame(app, &ctx, events); + frame(app, &ctx, vec![Event::ModifiersChanged(none)]); + app.view().unwrap().zoom + }; + let alt = egui::Modifiers::ALT; + let z = run( + &mut app, + vec![ + Event::ModifiersChanged(alt), + button(far, false, alt), + Event::ModifiersChanged(none), + Event::PointerMoved(at), + button(at, true, none), + button(at, false, none), + ], + ); + assert_eq!(z, crate::state::next_zoom(z0, true), "the click zooms in with the modifiers of its own release"); + let z = run( + &mut app, + vec![button(far, false, none), Event::PointerMoved(at), button(at, true, none), Event::PointerMoved(to), button(to, false, none)], + ); + let area = z0 * f64::from((rect.width() / 100.0).min(rect.height() / 50.0)); + assert!((z - area).abs() < 1e-6, "the drag zooms to its own area: {z0} → {z}, not {area}"); +} + /// Space pressed after a tool drag's release, in the same frame, does not count for the drag: the /// Rectangle tool's last move still resizes the rectangle instead of moving it. #[test] From 8c39b153a96bc563791208ad29f6531109508271 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 25 Nov 2026 20:04:33 -0800 Subject: [PATCH 7/7] Canvas: a tool drag's release modifiers come from the craft pointer frame The release modifiers are read from the pointer frame's pick_mods, the modifiers of the pass's button release with the latched ones added, in place of raw egui pointer events. The release-modifier tests drive the whole app through testing::app_frame. --- crates/ui-egui/src/canvas/tests.rs | 44 +++++++++++++---------- crates/ui-egui/src/canvas/tool_adapter.rs | 29 ++++++--------- 2 files changed, 36 insertions(+), 37 deletions(-) diff --git a/crates/ui-egui/src/canvas/tests.rs b/crates/ui-egui/src/canvas/tests.rs index 8b6ac373af..0a0ad98faa 100644 --- a/crates/ui-egui/src/canvas/tests.rs +++ b/crates/ui-egui/src/canvas/tests.rs @@ -533,8 +533,14 @@ fn cmd_with_another_tool_drags_with_the_selection_tool_used_last() { assert_eq!(cmd_drag(&mut app, inside, inside), "selection"); } -/// A Rectangle tool on a new 400 × 300 document, after one frame, with Smart Guides and Snap to -/// Point off → (the app, its context, the canvas view). +/// One frame of the whole app on `ctx` with `events`, in a 1600 × 850 window, as the hosts run it. +fn app_step(app: &mut VectorcraftApp, ctx: &egui::Context, events: Vec) { + let screen = egui::Rect::from_min_size(Pos2::ZERO, vec2(1600.0, 850.0)); + crate::testing::app_frame(app, ctx, egui::RawInput { screen_rect: Some(screen), events, ..Default::default() }); +} + +/// A Rectangle tool on a new 400 × 300 document in the whole app, after three frames, with Smart +/// Guides and Snap to Point off → (the app, its context, the canvas view). fn rectangle_tool() -> (VectorcraftApp, egui::Context, craft::behavior::CanvasView) { let mut app = VectorcraftApp::new(Session::new(), Default::default()); app.session.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); @@ -542,7 +548,9 @@ fn rectangle_tool() -> (VectorcraftApp, egui::Context, craft::behavior::CanvasVi app.ui.view.snap_to_point = false; app.select_tool("rectangle"); let ctx = crate::testing::ui_context(); - frame(&mut app, &ctx, vec![]); + for _ in 0..3 { + app_step(&mut app, &ctx, vec![]); + } let xf = app.canvas_view().unwrap(); (app, ctx, xf) } @@ -578,15 +586,15 @@ fn tool_drag_ends_with_the_modifiers_of_its_button_release() { let (mut app, ctx, xf) = rectangle_tool(); let (from, to) = (xf.to_screen(Point::new(100.0, 100.0)), xf.to_screen(Point::new(160.0, 130.0))); let none = egui::Modifiers::NONE; - frame(&mut app, &ctx, vec![egui::Event::PointerMoved(from)]); - frame(&mut app, &ctx, vec![primary(from, true, none)]); - frame(&mut app, &ctx, vec![egui::Event::PointerMoved(from.lerp(to, 0.5))]); - frame( + app_step(&mut app, &ctx, vec![egui::Event::PointerMoved(from)]); + app_step(&mut app, &ctx, vec![primary(from, true, none)]); + app_step(&mut app, &ctx, vec![egui::Event::PointerMoved(from.lerp(to, 0.5))]); + app_step( &mut app, &ctx, vec![egui::Event::PointerMoved(to), egui::Event::ModifiersChanged(held), primary(to, false, held), egui::Event::ModifiersChanged(after)], ); - frame(&mut app, &ctx, vec![egui::Event::ModifiersChanged(none)]); + app_step(&mut app, &ctx, vec![egui::Event::ModifiersChanged(none)]); let paths = drawn_paths(&app); assert_eq!(paths.len(), 1, "the drag draws one rectangle"); paths[0] @@ -606,11 +614,11 @@ fn tool_drag_ends_with_the_modifiers_of_its_button_release() { fn shift_pressed_at_a_still_release_constrains_the_tool_drag() { let (mut app, ctx, xf) = rectangle_tool(); let (from, to) = (xf.to_screen(Point::new(100.0, 100.0)), xf.to_screen(Point::new(160.0, 130.0))); - frame(&mut app, &ctx, vec![egui::Event::PointerMoved(from), primary(from, true, egui::Modifiers::NONE)]); - frame(&mut app, &ctx, vec![egui::Event::PointerMoved(from.lerp(to, 0.5))]); - frame(&mut app, &ctx, vec![egui::Event::PointerMoved(to)]); - frame(&mut app, &ctx, vec![egui::Event::ModifiersChanged(egui::Modifiers::SHIFT), primary(to, false, egui::Modifiers::SHIFT)]); - frame(&mut app, &ctx, vec![egui::Event::ModifiersChanged(egui::Modifiers::NONE)]); + app_step(&mut app, &ctx, vec![egui::Event::PointerMoved(from), primary(from, true, egui::Modifiers::NONE)]); + app_step(&mut app, &ctx, vec![egui::Event::PointerMoved(from.lerp(to, 0.5))]); + app_step(&mut app, &ctx, vec![egui::Event::PointerMoved(to)]); + app_step(&mut app, &ctx, vec![egui::Event::ModifiersChanged(egui::Modifiers::SHIFT), primary(to, false, egui::Modifiers::SHIFT)]); + app_step(&mut app, &ctx, vec![egui::Event::ModifiersChanged(egui::Modifiers::NONE)]); let paths = drawn_paths(&app); assert_eq!(paths.len(), 1, "the drag draws one rectangle"); assert!((paths[0].width() - paths[0].height()).abs() < 1e-3, "Shift pressed at a still release draws a square: {:?}", paths[0]); @@ -625,13 +633,13 @@ fn the_move_of_a_releasing_frame_counts_for_the_tool_drag() { let (mut app, ctx, xf) = rectangle_tool(); let (from, to) = (xf.to_screen(Point::new(100.0, 100.0)), xf.to_screen(Point::new(160.0, 130.0))); let none = egui::Modifiers::NONE; - frame(&mut app, &ctx, vec![egui::Event::PointerMoved(from), primary(from, true, none)]); - frame(&mut app, &ctx, vec![egui::Event::PointerMoved(from.lerp(to, 0.5))]); + app_step(&mut app, &ctx, vec![egui::Event::PointerMoved(from), primary(from, true, none)]); + app_step(&mut app, &ctx, vec![egui::Event::PointerMoved(from.lerp(to, 0.5))]); if space { - frame(&mut app, &ctx, vec![space_key(true)]); + app_step(&mut app, &ctx, vec![space_key(true)]); } - frame(&mut app, &ctx, vec![egui::Event::PointerMoved(to), primary(to, false, none)]); - frame(&mut app, &ctx, if space { vec![space_key(false)] } else { vec![] }); + app_step(&mut app, &ctx, vec![egui::Event::PointerMoved(to), primary(to, false, none)]); + app_step(&mut app, &ctx, if space { vec![space_key(false)] } else { vec![] }); let paths = drawn_paths(&app); assert_eq!(paths.len(), 1, "the drag draws one rectangle"); paths[0] diff --git a/crates/ui-egui/src/canvas/tool_adapter.rs b/crates/ui-egui/src/canvas/tool_adapter.rs index 8f177c1968..b857973dfd 100644 --- a/crates/ui-egui/src/canvas/tool_adapter.rs +++ b/crates/ui-egui/src/canvas/tool_adapter.rs @@ -133,17 +133,6 @@ pub(crate) fn tool_enter(app: &mut VectorcraftApp, mods: Mods, view: ViewInfo) - opened } -/// The modifiers held when the primary button went up in this pass: its first release, as a pass carries a press only -/// as its last pointer event. `None` without a release in the pass. -fn release_mods(ctx: &egui::Context) -> Option { - ctx.input(|i| { - i.events.iter().find_map(|e| match e { - egui::Event::PointerButton { button: egui::PointerButton::Primary, pressed: false, modifiers, .. } => Some(*modifiers), - _ => None, - }) - }) -} - /// The session as a tool for one pass. The host's trait takes `&self` for the cursor and the claims, which the /// session answers through `&mut`, so the app sits in a cell. struct SessionTool<'a> { @@ -161,12 +150,14 @@ impl<'a> SessionTool<'a> { Self { app: RefCell::new(app), view, carrying: Cell::new(false), ctx: ui.ctx().clone(), layer: ui.layer_id() } } - /// `ev` with Shift, Alt, Cmd and Ctrl as they were held when the primary button went up in this pass to end the - /// gesture, instead of as they are at the end of the pass: a modifier pressed or let go after the release does not - /// count for it. Space and Backtick stay as the host holds them. Unchanged without a release in the pass. - fn at_release(&self, mut ev: PointerEvent) -> PointerEvent { - if let Some(m) = release_mods(&self.ctx) { - ev.mods = tool_mods(craft::keymap::Mods::from_egui(&m, crate::keymap::platform()), ev.mods.space); + /// `ev` with Shift, Alt, Cmd and Ctrl as they were held when the gesture's button went up in this pass, latched ones + /// included (the pointer frame's `pick_mods`), instead of as they are at the end of the pass: a modifier pressed or + /// let go after the release does not count for it. Space and Backtick stay as the host holds them. Unchanged in a + /// pass where that button did not come up. + fn at_release(&self, pointer: &ToolPointer, mut ev: PointerEvent) -> PointerEvent { + let frame = craft::ui::runtime::pointer::frame(&self.ctx); + if frame.release == Some(pointer.button) { + ev.mods = tool_mods(frame.pick_mods, ev.mods.space); } ev } @@ -344,7 +335,7 @@ impl CanvasTool for SessionTool<'_> { let carried = super::drops::carry_out(&mut self.app(), cx, pointer, covered); if !carried { // The moves of the pass that releases the press came before its release, with the keys held then. - let ev = self.at_release(pointer_event(PointerKind::Drag, pointer, cx)); + let ev = self.at_release(pointer, pointer_event(PointerKind::Drag, pointer, cx)); dispatch(&mut self.app(), &ev, self.view); self.app().canvas.last_drag = Some((pointer.pos, ev)); } @@ -363,7 +354,7 @@ impl CanvasTool for SessionTool<'_> { } drop(app); // The press ends where it went up, with the keys held at its release. - let up = self.at_release(pointer_event(PointerKind::Up, pointer, cx)); + let up = self.at_release(pointer, pointer_event(PointerKind::Up, pointer, cx)); let covered = self.covered(pointer); let carried = super::drops::carried(&self.app(), cx, pointer, covered); self.app().canvas.panes.released_from = carried.as_ref().map(|art| (art.document, art.revision));