diff --git a/crates/ui-egui/src/canvas/tools/selection.rs b/crates/ui-egui/src/canvas/tools/selection.rs index f9fc4a6bc11..f4e2098cd90 100644 --- a/crates/ui-egui/src/canvas/tools/selection.rs +++ b/crates/ui-egui/src/canvas/tools/selection.rs @@ -516,6 +516,15 @@ fn piece_offset(app: &PhotocraftApp) -> Option<(i32, i32)> { /// The engine command of a selection a tool made: the one `SelectEvent::Commit` mapping. `tool` names the History /// step (Single Row, Polygonal Lasso, Patch Tool) and picks the options the bar has for it. pub(crate) fn run_commit(app: &mut PhotocraftApp, tool: Tool, shape: SelectionShape, op: SelectionOperation) { + // With no selection to intersect, a single-line marquee starts a new selection (#18176). + let op = if matches!(tool, Tool::SingleRowMarquee | Tool::SingleColumnMarquee) + && op == SelectionOperation::Intersect + && app.session.active().is_some_and(|state| state.doc.selection.is_none()) + { + SelectionOperation::New + } else { + op + }; let mode = crate::tool_feedback::mode_name(op); let (anti_alias, feather) = (app.ui.tool_options.anti_alias, app.ui.tool_options.feather); let r = |v: f64| v.round() as i64; @@ -694,6 +703,10 @@ pub(crate) fn shown_offset(app: &PhotocraftApp) -> Option<(i32, i32)> { .or_else(|| crate::move_ui::carries_selection(app).then(|| app.canvas_tools.mover.offset()).flatten()) } +#[cfg(test)] +#[path = "../../single_line_marquee_intersect_empty_tests.rs"] +mod single_line_marquee_intersect_empty_tests; + #[cfg(test)] mod tests { use egui::Modifiers; diff --git a/crates/ui-egui/src/single_line_marquee_intersect_empty_tests.rs b/crates/ui-egui/src/single_line_marquee_intersect_empty_tests.rs new file mode 100644 index 00000000000..fabe804d36d --- /dev/null +++ b/crates/ui-egui/src/single_line_marquee_intersect_empty_tests.rs @@ -0,0 +1,53 @@ +use egui::Modifiers; +use photocraft_geom::Rect; +use serde_json::json; + +use crate::PhotocraftApp; +use crate::canvas::{ToolEvent, tool_event}; +use crate::state::Tool; + +fn app(tool: Tool, mode: u8) -> Box { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 120, "height": 80})).unwrap(); + app.ui.extras.snap = false; + app.ui.tool = tool; + app.ui.selection_mode = mode; + app +} + +fn click_line(app: &mut PhotocraftApp) { + tool_event(app, ToolEvent::Down { x: 18.5, y: 24.5, pressure: 1.0 }, Modifiers::NONE); + tool_event(app, ToolEvent::Up { x: 18.5, y: 24.5 }, Modifiers::NONE); +} + +#[test] +fn intersecting_without_a_selection_picks_the_row_or_column() { + for (tool, line, label) in [ + (Tool::SingleRowMarquee, Rect::new(0, 24, 120, 25), "Single Row Marquee"), + (Tool::SingleColumnMarquee, Rect::new(18, 0, 19, 80), "Single Column Marquee"), + ] { + let mut app = app(tool, 3); + click_line(&mut app); + let state = app.session.active().unwrap(); + assert_eq!(state.doc.selection.as_ref().unwrap().content_bounds(), line); + assert_eq!(state.history.undo_label(), Some(label)); + assert_eq!(app.ui.selection_mode, 3, "the options bar stays in Intersect mode"); + } +} + +#[test] +fn intersecting_a_selection_restricts_the_row_to_that_selection() { + let mut app = app(Tool::SingleRowMarquee, 3); + app.run("select.rect", json!({"x": 10, "y": 10, "width": 20, "height": 20})).unwrap(); + click_line(&mut app); + let state = app.session.active().unwrap(); + assert_eq!(state.doc.selection.as_ref().unwrap().content_bounds(), Rect::new(10, 24, 30, 25)); + assert_eq!(state.history.undo_label(), Some("Single Row Marquee")); +} + +#[test] +fn subtracting_without_a_selection_leaves_it_empty() { + let mut app = app(Tool::SingleRowMarquee, 2); + click_line(&mut app); + assert!(app.session.active().unwrap().doc.selection.is_none()); +}