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}"); }