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.
This commit is contained in:
dotCooCoo committed 2026-11-22 05:58:13 -08:00
1 parent 4528614324
commit b7f2eacc6a
2 files changed
+51 -6

No files matched your search

+18 -6
View File
@@ -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>(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;
+33
View File
@@ -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]