diff --git a/crates/ui-egui/src/canvas/canvas_tool_menu.rs b/crates/ui-egui/src/canvas/canvas_tool_menu.rs index ba6cf181f71..38ef019fb0e 100644 --- a/crates/ui-egui/src/canvas/canvas_tool_menu.rs +++ b/crates/ui-egui/src/canvas/canvas_tool_menu.rs @@ -2065,7 +2065,8 @@ pub(crate) fn run( let doc = view.to_doc(at); open_at(app, tool, mods, at, [doc.x, doc.y], None); } - if let Some(request) = host.context { + // A right press that ended a Gradient drag made the gradient instead (#18703). + if let Some(request) = host.context.filter(|_| !app.canvas_tools.gradient.ended_by_right_press()) { // A Space-held Hand opens the Hand's menu over the current tool. A momentary Zoom opens no menu of its own, but // with ⌘ held the right-click is still the one that lists the layers under the pointer (#307). let tool = match request.momentary { diff --git a/crates/ui-egui/src/tools/gradient_ui.rs b/crates/ui-egui/src/tools/gradient_ui.rs index 82577cbeeb0..42e59bb1653 100644 --- a/crates/ui-egui/src/tools/gradient_ui.rs +++ b/crates/ui-egui/src/tools/gradient_ui.rs @@ -34,6 +34,8 @@ mod editor_tests; #[cfg(test)] mod live_overlay_tests; #[cfg(test)] +mod right_click_during_drag_tests; +#[cfg(test)] mod tests; /// Id of the temporary layer previewing a new gradient while it is drawn. diff --git a/crates/ui-egui/src/tools/gradient_ui/right_click_during_drag_tests.rs b/crates/ui-egui/src/tools/gradient_ui/right_click_during_drag_tests.rs new file mode 100644 index 00000000000..8356f5ec48c --- /dev/null +++ b/crates/ui-egui/src/tools/gradient_ui/right_click_during_drag_tests.rs @@ -0,0 +1,107 @@ +//! #18703: a right press while a Gradient drag runs ends the drag there, as in the reference application. The gradient +//! (classic pixels, or a new Gradient Fill layer) is made at once from the press to the right press, as one History +//! step and with no menu; moving on and releasing the left button afterwards changes nothing. A drag still too short +//! makes nothing, and with a modifier held (not measured) the right press keeps today's behaviour. + +use egui::Modifiers; +use photocraft_doc::{Layer, LayerId}; +use serde_json::json; + +use crate::PhotocraftApp; +use crate::canvas::testing::{move_to, press, release, secondary_click}; +use crate::state::Tool; + +const A: [f64; 2] = [40.0, 60.0]; +const B: [f64; 2] = [160.0, 100.0]; +/// Where the left button goes after the right press, away from B. +const C: [f64; 2] = [180.0, 30.0]; + +/// A 200×150 document with the Gradient tool in classic or Gradient (fill layer) mode. +fn app(classic: bool) -> Box { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.session.execute("file.new", json!({"width": 200, "height": 150})).unwrap(); + app.sync_views(); + app.ui.tool = Tool::Gradient; + app.ui.tool_options.gradient_classic = classic; + app.ui.extras.snap = false; + app +} + +fn steps(app: &PhotocraftApp) -> usize { + app.session.active().unwrap().history.past_len() +} + +fn newest(app: &PhotocraftApp) -> Option { + app.session.active().unwrap().history.entries().pop() +} + +/// The document's layers, top to bottom, with their ids left out: ids are numbered across the process, so two +/// documents made the same way get different ones. +fn layers(app: &PhotocraftApp) -> Vec { + let mut layers: Vec = app.session.active().unwrap().doc.layers.iter().cloned().collect(); + for layer in &mut layers { + layer.id = LayerId(0); + } + layers +} + +/// The left button down at A and dragged to B, still held. +fn drag_to_b(app: &mut PhotocraftApp) { + press(app, A, Modifiers::NONE); + move_to(app, [100.0, 80.0], Modifiers::NONE); + move_to(app, B, Modifiers::NONE); +} + +/// The layers and newest step of a plain drag from A released at B. +fn released_at_b(classic: bool) -> (Vec, Option) { + let mut app = app(classic); + drag_to_b(&mut app); + release(&mut app, B, Modifiers::NONE); + (layers(&app), newest(&app)) +} + +#[test] +fn a_right_press_mid_drag_makes_the_gradient_there_and_the_release_adds_nothing() { + for classic in [true, false] { + let mut app = app(classic); + let before = steps(&app); + drag_to_b(&mut app); + secondary_click(&mut app, B, Modifiers::NONE); + assert_eq!(steps(&app), before + 1, "the right press makes the gradient as one step (classic: {classic})"); + assert!(app.ui.canvas_tool_menu.is_none(), "no menu opens (classic: {classic})"); + let made = layers(&app); + let (reference, step) = released_at_b(classic); + assert_eq!(newest(&app), step, "the step a release at B makes (classic: {classic})"); + assert!(made == reference, "the gradient a release at B makes (classic: {classic})"); + + move_to(&mut app, C, Modifiers::NONE); + release(&mut app, C, Modifiers::NONE); + assert_eq!(steps(&app), before + 1, "the left release adds no step (classic: {classic})"); + assert!(layers(&app) == made, "nor moves the end point (classic: {classic})"); + } +} + +#[test] +fn a_right_press_before_the_drag_is_long_enough_makes_nothing() { + let mut app = app(true); + let (before, shown) = (steps(&app), layers(&app)); + press(&mut app, A, Modifiers::NONE); + secondary_click(&mut app, A, Modifiers::NONE); + move_to(&mut app, B, Modifiers::NONE); + release(&mut app, B, Modifiers::NONE); + assert_eq!(steps(&app), before, "no step"); + assert!(layers(&app) == shown, "the document is unchanged"); + assert!(app.ui.canvas_tool_menu.is_none(), "no menu opens"); +} + +/// Not measured with a modifier held: the drag goes on and its release makes the gradient, as before. +#[test] +fn with_a_modifier_held_the_right_press_leaves_the_drag_going() { + let mut app = app(true); + let before = steps(&app); + drag_to_b(&mut app); + secondary_click(&mut app, B, Modifiers::SHIFT); + assert_eq!(steps(&app), before, "the right press makes nothing yet"); + release(&mut app, B, Modifiers::NONE); + assert_eq!(steps(&app), before + 1, "the release makes the gradient"); +} diff --git a/crates/ui-egui/src/tools/gradient_ui/tool.rs b/crates/ui-egui/src/tools/gradient_ui/tool.rs index 98b80cb6195..079918dbdf6 100644 --- a/crates/ui-egui/src/tools/gradient_ui/tool.rs +++ b/crates/ui-egui/src/tools/gradient_ui/tool.rs @@ -32,12 +32,21 @@ pub(crate) struct GradientDraw { pub(crate) redraw: Option, /// Whether the last sample snapped to 45°. pub(crate) shift: bool, + /// Whether the drag has gone at least [`MIN_DRAG`] on screen, for a right press that ends it. + far: bool, + /// Whether this pass's right press ended the drag, so the canvas menu stays closed for it. + ended_by_right_press: bool, } impl GradientDraw { fn clear(&mut self) { *self = GradientDraw::default(); } + + /// Whether this pass's right press ended the drag: the canvas menu does not open for it. + pub(crate) fn ended_by_right_press(&self) -> bool { + self.ended_by_right_press + } } /// The Gradient tool for one pass. @@ -47,9 +56,28 @@ pub(crate) fn tool<'a>(scope: &'a ToolScope<'a>, state: &'a mut GradientDraw, _h /// Ends the drag when the host reports its end. The tool clears it at release and cancel itself; this covers a gesture /// the host ended without a call, so none is left behind. -pub(crate) fn drain(_app: &mut PhotocraftApp, state: &mut GradientDraw, response: &ToolHostResponse) { - if matches!(response.gesture, Some(GesturePhase::Commit | GesturePhase::Cancel)) { +/// +/// A right press during the drag reaches the app as the host's context request while the left press goes on. With no +/// modifier held it ends the drag there (#18703): the gradient is made at once, as a release would make it, and the +/// rest of the left press changes nothing. A drag still shorter than [`MIN_DRAG`] makes nothing, as a short release. +pub(crate) fn drain(app: &mut PhotocraftApp, state: &mut GradientDraw, response: &ToolHostResponse) { + state.ended_by_right_press = false; + if let Some(request) = response.context + && request.momentary.is_none() + && request.mods.is_none() + && let Some(from) = state.from + { + let (to, redraw, far) = (state.to, state.redraw, state.far); state.clear(); + state.ended_by_right_press = true; + if far { + commit(app, from, to, redraw); + } + } + if matches!(response.gesture, Some(GesturePhase::Commit | GesturePhase::Cancel)) { + let ended = state.ended_by_right_press; + state.clear(); + state.ended_by_right_press = ended; } } @@ -135,6 +163,10 @@ impl CanvasTool for GradientTool<'_> { let (Some(from), Some(at)) = (self.state.from, doc_at(cx, pointer.pos)) else { return }; self.state.shift = pointer.mods.shift; self.state.to = snapped(from, at, pointer.mods.shift); + if let Some(space) = cx.space() { + let screen = |p: [f32; 2]| space.view.to_screen(Point::new(f64::from(p[0]), f64::from(p[1]))); + self.state.far = screen(from).distance(screen(self.state.to)) >= MIN_DRAG; + } } fn release(&mut self, pointer: &ToolPointer, cx: &mut ToolContext<'_>) {