PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Capture marquee selection modifiers at mouse press (#2558)
Harrison O'Connor-Hooverac90ca2b1afa
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rsindex 6c31678d8..f11a6cb41 100644--- a/crates/ui-egui/src/canvas.rs+++ b/crates/ui-egui/src/canvas.rs@@ -2576,10 +2576,11 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect:if tool == Tool::Crop && response.is_pointer_button_down_on() && ui.input(|i| i.pointer.primary_down()) && crate::crop_ui::press(app) {ctx.request_repaint();}- // Capture temporary Type transforms at the actual press, before egui's drag threshold.- // Releasing Command before the first recognised move must not turn it into text selection.- let type_press = egui::Id::new("type-pointer-press-modifiers");- if tool.is_type()+ // Capture intent at the press, before egui's drag threshold: Type transforms keep+ // Command, and marquee add/subtract must not become a live Shift/Alt constraint.+ let capture_press = tool.is_type() || matches!(tool, Tool::RectMarquee | Tool::EllipseMarquee);+ let tool_press = egui::Id::new(("tool-pointer-press-modifiers", idx));+ if capture_press&& let Some((p, press_mods)) = ui.input(|i| {i.events.iter().find_map(|e| match e {egui::Event::PointerButton { pos, button: PointerButton::Primary, pressed: true, modifiers } if rect.contains(*pos) => {@@ -2590,8 +2591,8 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect:}){let press_mods = crate::workspace_ui::sticky_mods(app, press_mods);- ctx.data_mut(|d| d.insert_temp(type_press, press_mods));- if press_mods.command && app.ui.text_edit.is_some() {+ ctx.data_mut(|d| d.insert_temp(tool_press, press_mods));+ if tool.is_type() && press_mods.command && app.ui.text_edit.is_some() {let d = xf.to_doc(p);tool_event(app, ToolEvent::Down { x: d[0], y: d[1], pressure: app.stylus.pressure() }, press_mods);}@@ -2682,7 +2683,7 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect:begin_transform_controls_at(app, &ctx, &xf, p);}let d = xf.to_doc(p);- let press_mods = if tool.is_type() { ctx.data(|d| d.get_temp(type_press)).unwrap_or(mods) } else { mods };+ let press_mods = if capture_press { ctx.data(|d| d.get_temp(tool_press)).unwrap_or(mods) } else { mods };tool_event(app, ToolEvent::Down { x: d[0], y: d[1], pressure: app.stylus.pressure() }, press_mods);}// A stroke started on the press takes its moves from the press on, not only once eguidiff --git a/crates/ui-egui/src/marquee_tests.rs b/crates/ui-egui/src/marquee_tests.rsindex bfc94bcd1..052702a12 100644--- a/crates/ui-egui/src/marquee_tests.rs+++ b/crates/ui-egui/src/marquee_tests.rs@@ -97,6 +97,37 @@ fn readout(h: &Harness<'static, PhotocraftApp>, w: i32, ht: i32) -> bool {labels && values && h.query_all_by_label_contains(" px").count() == 2}+#[test]+fn marquee_intent_is_captured_before_the_drag_threshold() {+ for tool in [Tool::RectMarquee, Tool::EllipseMarquee] {+ for at_press in [Modifiers::NONE, Modifiers::SHIFT] {+ let mut h = harness(tool);+ h.state_mut().ui.extras.snap = false;+ h.state_mut().run("select.rect", json!({"x": 10, "y": 10, "width": 20, "height": 20})).unwrap();+ mods(&mut h, at_press);+ move_to(&mut h, 100.0, 100.0);+ let start = screen(&h, 100.0, 100.0);+ button(&mut h, start, true, at_press);+ assert!(h.state().drag.is_none(), "still below the drag threshold");+ // Change Shift while the pointer is still at the press, before egui starts a drag.+ let during = if at_press.shift { Modifiers::NONE } else { Modifiers::SHIFT };+ mods(&mut h, during);+ move_to(&mut h, 180.0, 130.0);+ assert!(readout(&h, 80, if at_press.shift { 30 } else { 80 }), "{tool:?}, Shift at press: {}", at_press.shift);+ release_at(&mut h, 180.0, 130.0, during);+ let st = h.state().session.active().unwrap();+ let sel = st.doc.selection.as_ref().unwrap();+ if at_press.shift {+ assert!(sel.sample_channel(20, 20, 0) > 0.99, "Shift at the press keeps Add mode");+ assert_eq!(selection(&h), Rect::new(10, 10, 180, 130));+ } else {+ assert!(sel.sample_channel(20, 20, 0) < 0.01, "Shift after the press constrains a replacement");+ assert_eq!(selection(&h), Rect::new(100, 100, 180, 180));+ }+ }+ }+}+#[test]fn shift_during_the_drag_draws_a_square_and_the_readout_follows() {let mut h = harness(Tool::RectMarquee);