diff --git a/crates/ui-egui/src/canvas/panes.rs b/crates/ui-egui/src/canvas/panes.rs index 473bc53839..e6e16ad271 100644 --- a/crates/ui-egui/src/canvas/panes.rs +++ b/crates/ui-egui/src/canvas/panes.rs @@ -295,7 +295,7 @@ enum Over { /// artwork before the canvas sees the release ([`crate::document_drag::take_for_pane`]). fn artwork_over(app: &mut VectorcraftApp, ui: &Ui, w: Win, area: egui::Rect) -> Option { let (uid, _) = slot(app, w)?; - let p = ui.input(|i| i.pointer.interact_pos())?; + let p = ui.input(crate::document_drag::released_at)?; if !area.contains(p) || ui.ctx().layer_id_at(p).is_some_and(|l| l != ui.layer_id()) { return None; } @@ -327,13 +327,13 @@ fn noted_at(app: &VectorcraftApp, ctx: &egui::Context, p: egui::Pos2) -> Option< } /// A Selection drag's artwork released over a pane the last frame noted ([`note_drop`]) drops into -/// its document ([`crate::document_drag::drop_on_pane`]), at the document point under the pointer -/// in that pane's own view. Runs at the start of a frame, before any canvas sees the release. +/// its document ([`crate::document_drag::drop_on_pane`]), at the document point where the button +/// went up, in that pane's own view. Runs at the start of a frame, before any canvas sees the release. pub(crate) fn release_on_noted(app: &mut VectorcraftApp, ctx: &egui::Context) { if app.canvas.windows.drops.is_empty() || !ctx.input(|i| i.pointer.primary_released()) { return; } - let Some(p) = ctx.input(|i| i.pointer.interact_pos()) else { return }; + let Some(p) = ctx.input(crate::document_drag::released_at) else { return }; let Some((w, area)) = noted_at(app, ctx, p) else { return }; let Some(v) = crate::doc_windows::view(app, w).copied() else { return }; let at = Xf::at(area, &v, ctx.zoom_factor()).to_doc(p); diff --git a/crates/ui-egui/src/canvas/panes/tests.rs b/crates/ui-egui/src/canvas/panes/tests.rs index 85301cd22b..03c0ce7bed 100644 --- a/crates/ui-egui/src/canvas/panes/tests.rs +++ b/crates/ui-egui/src/canvas/panes/tests.rs @@ -316,6 +316,38 @@ fn a_selection_drag_into_another_view_of_its_document_moves_the_art() { assert_eq!(app.session.documents()[1].doc, target, "the other document is untouched"); } +#[test] +fn a_pane_drop_lands_where_the_button_went_up_and_copies_when_alt_was_held_then() { + // A pointer move later in the frame of the release does not move the drop. + let (mut app, ctx) = tiled_pair(); + grab(&mut app, &ctx, Point::new(100.0, 100.0)); + let (drop, _) = in_pane(&app, &ctx, (1, 1), Point::new(170.0, 120.0)); + let (later, _) = in_pane(&app, &ctx, (1, 1), Point::new(80.0, 60.0)); + app_frame(&mut app, &ctx, vec![egui::Event::PointerMoved(drop), button(drop, false), egui::Event::PointerMoved(later)]); + app_frame(&mut app, &ctx, vec![]); + assert_eq!(app.session.active_index(), Some(1)); + let placed = selected_bounds(&app); + assert!((placed.center().x - 170.0).abs() < 0.01 && (placed.center().y - 120.0).abs() < 0.01, "{placed:?}"); + // Alt held at the release and let go later in that frame copies into another view of the source. + let (mut app, ctx) = tiled_pair(); + app.run("window.newWindow", json!({})).unwrap(); + app.run("window.arrange.tile", json!({})).unwrap(); + app_frame(&mut app, &ctx, vec![]); + app_frame(&mut app, &ctx, vec![]); + let current = crate::doc_windows::current(&app).unwrap(); + let other = app.canvas.panes.last.iter().map(|(w, _)| *w).find(|w| w.0 == 0 && *w != current).unwrap(); + grab(&mut app, &ctx, Point::new(100.0, 100.0)); + let (drop, _) = in_pane(&app, &ctx, other, Point::new(250.0, 200.0)); + let alt = egui::Modifiers::ALT; + let cursor = app_frame(&mut app, &ctx, vec![egui::Event::ModifiersChanged(alt), egui::Event::PointerMoved(drop)]); + assert_eq!(cursor, egui::CursorIcon::Copy, "Alt shows a copy"); + let up = egui::Event::PointerButton { pos: drop, button: egui::PointerButton::Primary, pressed: false, modifiers: alt }; + app_frame(&mut app, &ctx, vec![up, egui::Event::ModifiersChanged(egui::Modifiers::NONE)]); + app_frame(&mut app, &ctx, vec![]); + assert_eq!(crate::doc_windows::current(&app), Some(other)); + assert_eq!(objects(&app, 0), 2, "the square was copied, not moved"); +} + #[test] fn art_carried_through_a_tab_drops_into_a_third_documents_pane() { let (mut app, ctx) = tiled_pair(); diff --git a/crates/ui-egui/src/doc_windows/tests.rs b/crates/ui-egui/src/doc_windows/tests.rs index 02c764f5f9..3ecacda490 100644 --- a/crates/ui-egui/src/doc_windows/tests.rs +++ b/crates/ui-egui/src/doc_windows/tests.rs @@ -1047,6 +1047,23 @@ fn a_release_in_the_first_frame_over_a_floating_window_still_copies_the_art() { assert_eq!(app.session.documents()[0].doc, source); } +#[test] +fn a_floating_window_drop_lands_where_the_button_went_up() { + let ctx = egui::Context::default(); + let mut app = floating_pair(&ctx); + grab(&mut app, &ctx, vectorcraft_geom::Point::new(325.0, 255.0)); + let drop = in_noted(&app, (1, 1), vectorcraft_geom::Point::new(200.0, 150.0)); + let later = in_noted(&app, (1, 1), vectorcraft_geom::Point::new(120.0, 220.0)); + app_frame(&mut app, &ctx, vec![egui::Event::PointerMoved(drop)]); + // A pointer move later in the frame of the release does not move the drop. + app_frame(&mut app, &ctx, vec![button(drop, false), egui::Event::PointerMoved(later)]); + app_frame(&mut app, &ctx, vec![]); + assert_eq!(app.session.active_index(), Some(1)); + assert_eq!(objects(&app, 1), 2); + let placed = selected_center(&app); + assert!((placed.x - 200.0).abs() < 0.01 && (placed.y - 150.0).abs() < 0.01, "{placed:?}"); +} + #[test] fn art_dragged_from_a_floating_window_drops_into_the_main_windows_document() { let ctx = egui::Context::default(); diff --git a/crates/ui-egui/src/document_drag.rs b/crates/ui-egui/src/document_drag.rs index a975732972..53a509e265 100644 --- a/crates/ui-egui/src/document_drag.rs +++ b/crates/ui-egui/src/document_drag.rs @@ -93,7 +93,7 @@ pub(crate) fn cleanup(app: &VectorcraftApp, ctx: &Context) { }); // The canvas and the other windows' tiled panes take a release ([`drop_on_pane`]). let target = |p: egui::Pos2| app.canvas_rect.is_some_and(|r| r.contains(p)) || app.canvas.panes.last.iter().any(|(_, r)| r.contains(p)); - let outside_release = ctx.input(|i| i.pointer.primary_released() && !i.pointer.interact_pos().is_some_and(target)); + let outside_release = ctx.input(|i| i.pointer.primary_released() && !released_at(i).is_some_and(target)); if !valid || cancel || outside_release || app.ui.dialog.is_some() { egui::DragAndDrop::clear_payload(ctx); } @@ -197,10 +197,11 @@ pub(crate) fn drop_on_canvas(app: &mut VectorcraftApp, resp: &Response, xf: &can resp.ctx.set_cursor_icon(if same && !copy { egui::CursorIcon::Move } else { egui::CursorIcon::Copy }); } if let Some(art) = resp.dnd_release_payload::() - && let Some(at) = resp.ctx.input(|i| i.pointer.interact_pos()).map(|p| xf.to_doc(p)) + && let Some(at) = resp.ctx.input(released_at).map(|p| xf.to_doc(p)) { let result = if same { // Back in the source tab, resume the ordinary move (Alt copies). + let copy = release_alt(&resp.ctx); app.run("object.move", json!({"ids": art.ids, "dx": at.x-art.origin.grab.x, "dy": at.y-art.origin.grab.y, "copy": copy})) } else { app.run( @@ -283,7 +284,7 @@ pub(crate) fn drop_on_pane(app: &mut VectorcraftApp, ctx: &Context, w: crate::do app.ui.home = None; crate::doc_windows::show_window(app, w); let result = if uid == origin.uid { - let copy = ctx.input(|i| i.modifiers.alt); + let copy = release_alt(ctx); app.run("object.move", json!({"ids": ids, "dx": at.x - origin.grab.x, "dy": at.y - origin.grab.y, "copy": copy})) } else { app.run("edit.dropSelection", json!({"sourceDocument": origin.uid, "ids": ids, "grab": [origin.grab.x, origin.grab.y], "at": [at.x, at.y]})) @@ -296,3 +297,23 @@ pub(crate) fn drop_on_pane(app: &mut VectorcraftApp, ctx: &Context, w: crate::do } ctx.request_repaint(); } + +/// The primary button's release in this frame: where it went up, and the modifiers held then. +fn primary_release(i: &egui::InputState) -> Option<(egui::Pos2, egui::Modifiers)> { + i.events.iter().find_map(|e| match e { + egui::Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed: false, modifiers } => Some((*pos, *modifiers)), + _ => None, + }) +} + +/// Where the primary button went up in this frame, else where the pointer is. A pointer move later +/// in the frame of the release does not change where the artwork is released. +pub(crate) fn released_at(i: &egui::InputState) -> Option { + primary_release(i).map(|(p, _)| p).or(i.pointer.interact_pos()) +} + +/// Whether Alt was held when the primary button went up in this frame, else whether it is held now. +/// Alt let go later in the frame of the release still copies. +fn release_alt(ctx: &Context) -> bool { + ctx.input(|i| primary_release(i).map_or(i.modifiers.alt, |(_, m)| m.alt)) +} diff --git a/crates/ui-egui/src/tests/document_drag.rs b/crates/ui-egui/src/tests/document_drag.rs index cefe441020..dba75e316c 100644 --- a/crates/ui-egui/src/tests/document_drag.rs +++ b/crates/ui-egui/src/tests/document_drag.rs @@ -291,6 +291,31 @@ fn hovering_back_to_source_resumes_move_instead_of_copying() { assert!((bounds.x0 - 150.0).abs() < 0.001 && (bounds.y0 - 150.0).abs() < 0.001, "{bounds:?}"); } +/// Verify that a drop back in the source tab lands where the button went up and copies when Alt was +/// held then, though the pointer moves and Alt is let go later in the frame of the release. +#[test] +fn a_drop_back_in_the_source_uses_the_release_position_and_alt() { + let (mut app, ctx, _, tabs) = drag_fixture(); + frame(&mut app, &ctx, 0.2, vec![Event::PointerMoved(tabs[1].center())]); + frame(&mut app, &ctx, 0.25, vec![]); + frame(&mut app, &ctx, 0.8, vec![]); + frame(&mut app, &ctx, 0.9, vec![Event::PointerMoved(tabs[0].center())]); + frame(&mut app, &ctx, 1.4, vec![]); + assert_eq!(app.session.active_index(), Some(0)); + let xf = canvas::Xf::new(app.canvas_rect.unwrap(), app.view().unwrap()); + let drop = xf.to_screen(Point::new(175.0, 180.0)); + let later = xf.to_screen(Point::new(300.0, 250.0)); + let alt = egui::Modifiers::ALT; + frame(&mut app, &ctx, 1.5, vec![Event::ModifiersChanged(alt), Event::PointerMoved(drop)]); + let up = Event::PointerButton { pos: drop, button: PointerButton::Primary, pressed: false, modifiers: alt }; + frame(&mut app, &ctx, 1.6, vec![up, Event::PointerMoved(later), Event::ModifiersChanged(egui::Modifiers::NONE)]); + let st = app.session.active().unwrap(); + assert_eq!(st.doc.layers[0].children().map_or(0, Vec::len), 2, "Alt at the release copies the original"); + assert_eq!(st.selection.objects.len(), 1); + let bounds = st.doc.node(st.selection.objects[0]).unwrap().geometric_bounds().unwrap(); + assert!((bounds.x0 - 150.0).abs() < 0.001 && (bounds.y0 - 150.0).abs() < 0.001, "{bounds:?}"); +} + /// Back in the source tab, the move resumes even when coming to the front measured the source's /// type again in its own composite fonts, which replaces the document without editing it. #[test]