Arrange: a drop uses the position and Alt of the button release

A frame can carry the primary button's release and then a pointer move or
a change of modifiers. The tiled pane, the floating window and the canvas
read the pointer position and Alt at the end of the frame, so a move after
the release could change where the artwork landed or which pane took it,
and Alt let go in the same frame moved the originals instead of copying
them. The drop now takes its position and Alt from the release event, as
the canvas already does for its own drags (canvas/input.rs release_pos).
This commit is contained in:
dotCooCoo
2026-11-16 04:47:18 -08:00
parent 72b14d6aaa
commit 7b04069d1e
5 changed files with 102 additions and 7 deletions
+4 -4
View File
@@ -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<Over> {
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);
+32
View File
@@ -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();
+17
View File
@@ -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();
+24 -3
View File
@@ -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::<Artwork>()
&& 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<egui::Pos2> {
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))
}
+25
View File
@@ -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]