diff --git a/crates/automation/src/workspace/tests.rs b/crates/automation/src/workspace/tests.rs index 252a687d7af..c8aa5b55bfa 100644 --- a/crates/automation/src/workspace/tests.rs +++ b/crates/automation/src/workspace/tests.rs @@ -732,6 +732,7 @@ fn documents_filesystem_path_params(id: &str, params: &str) -> bool { | "path.info" | "path.addToSelection" | "path.subtractFromSelection" + | "path.intersectWithSelection" | "path.set" | "path.transform" | "path.moveAnchors" diff --git a/crates/engine/src/cmd/vector_cmds.rs b/crates/engine/src/cmd/vector_cmds.rs index c4cb0d94715..d03180525b2 100644 --- a/crates/engine/src/cmd/vector_cmds.rs +++ b/crates/engine/src/cmd/vector_cmds.rs @@ -230,6 +230,17 @@ pub fn specs() -> Vec { run: |s, p| path_to_selection_with_mode(s, p, "subtract"), journal: true, }, + // ⇧⌥⌘↩ / Ctrl+Alt+Shift+Enter intersects the path with the selection (#7726). + CommandSpec { + id: "path.intersectWithSelection", + label: "Intersect Path with Selection", + menu: &[], + shortcut: Some("Cmd+Alt+Shift+Enter"), + params: r##"{"name":str|"work"|"layer"="work","feather":px=0,"antiAlias":bool=true} (path.toSelection with mode "intersect")"##, + enabled: has_doc, + run: |s, p| path_to_selection_with_mode(s, p, "intersect"), + journal: true, + }, spec!("select.toWorkPath", "Make Work Path", [], r##"{"tolerance":0.5..10 px=2} → {subpaths,knots}"##, has_selection, select_to_work_path), spec!( "path.fill", diff --git a/crates/engine/src/cmd/vector_cmds/paths.rs b/crates/engine/src/cmd/vector_cmds/paths.rs index 7c7f550a298..e54c38fd198 100644 --- a/crates/engine/src/cmd/vector_cmds/paths.rs +++ b/crates/engine/src/cmd/vector_cmds/paths.rs @@ -371,7 +371,8 @@ pub(super) fn path_to_selection(s: &mut Session, p: &Value) -> Result { Ok(json!({ "selected": selected })) } -/// `path.toSelection` with the combine mode fixed by the command (⌘⇧↩ adds, ⌘⌥↩ subtracts); +/// `path.toSelection` with the combine mode fixed by the command (⌘⇧↩ adds, ⌘⌥↩ subtracts, +/// ⇧⌥⌘↩ intersects); /// a `mode` in the params is overridden, the rest is forwarded and validated as usual. pub(super) fn path_to_selection_with_mode(s: &mut Session, p: &Value, mode: &str) -> Result { let mut q = match p { diff --git a/crates/engine/src/cmd/vector_cmds/tests.rs b/crates/engine/src/cmd/vector_cmds/tests.rs index b659cbd656d..263ee6785da 100644 --- a/crates/engine/src/cmd/vector_cmds/tests.rs +++ b/crates/engine/src/cmd/vector_cmds/tests.rs @@ -460,6 +460,26 @@ fn open_pen_paths_make_the_same_selection_as_closed_paths() { } } +/// ⇧⌘↩, ⌥⌘↩ and ⇧⌥⌘↩'s commands are Make Selection from Path with their mode (#7726), whatever +/// `mode` the params ask for. +#[test] +fn the_fixed_mode_path_commands_combine_as_their_mode() { + let mut s = session(80, 80, 8); + let square = |x: f64| path_json(&vector::shapes::rect(x, 20.0, 30.0, 30.0)); + s.execute("path.set", json!({"name": "Sq", "path": square(30.0)})).unwrap(); + for (id, mode) in [("path.addToSelection", "add"), ("path.subtractFromSelection", "subtract"), ("path.intersectWithSelection", "intersect")] { + s.execute("select.rect", json!({"x": 10, "y": 20, "width": 30, "height": 30, "antiAlias": false})).unwrap(); + s.execute("path.toSelection", json!({"name": "Sq", "mode": mode, "antiAlias": false})).unwrap(); + let want = selection_mask(&s); + s.execute("select.rect", json!({"x": 10, "y": 20, "width": 30, "height": 30, "antiAlias": false})).unwrap(); + s.execute(id, json!({"name": "Sq", "mode": "replace", "antiAlias": false})).unwrap(); + assert_eq!(selection_mask(&s), want, "{id} is mode {mode}"); + } + // Intersecting keeps only the 10 px both squares cover. + let mask = selection_mask(&s); + assert_eq!(mask.iter().filter(|v| **v > 0.5).count(), 10 * 30); +} + #[test] fn selection_to_work_path_roundtrip_iou() { let mut s = session(240, 180, 8); diff --git a/crates/ui-egui/src/app/commands.rs b/crates/ui-egui/src/app/commands.rs index df4bc779865..48d892542c6 100644 --- a/crates/ui-egui/src/app/commands.rs +++ b/crates/ui-egui/src/app/commands.rs @@ -264,6 +264,7 @@ impl PhotocraftApp { | "path.toSelection" | "path.addToSelection" | "path.subtractFromSelection" + | "path.intersectWithSelection" ) && self.session.active().is_some_and(|st| st.doc.selection.is_some()) { diff --git a/crates/ui-egui/src/menus/invoke.rs b/crates/ui-egui/src/menus/invoke.rs index e1f52d934c2..639b840bd2c 100644 --- a/crates/ui-egui/src/menus/invoke.rs +++ b/crates/ui-egui/src/menus/invoke.rs @@ -207,10 +207,16 @@ fn invoke_file_commands(app: &mut PhotocraftApp, ctx: &egui::Context, id: &str, // ⌘↩ / Ctrl+Enter (#306): load the path selected in the Paths panel (or the one being // drawn) as a selection. "path.toSelection" if params.get("name").is_none() => crate::vector_ui::path_to_selection(app, params.clone()), - // ⌘⇧↩ / ⌘⌥↩ load the same path, adding it to or subtracting it from the selection. - "path.addToSelection" | "path.subtractFromSelection" if params.get("name").is_none() => { + // ⌘⇧↩ / ⌘⌥↩ / ⇧⌥⌘↩ load the same path, adding it to, subtracting it from or intersecting + // it with the selection. + "path.addToSelection" | "path.subtractFromSelection" | "path.intersectWithSelection" if params.get("name").is_none() => { let mut p = params.as_object().cloned().unwrap_or_default(); - p.insert("mode".into(), json!(if id == "path.addToSelection" { "add" } else { "subtract" })); + let mode = match id { + "path.addToSelection" => "add", + "path.subtractFromSelection" => "subtract", + _ => "intersect", + }; + p.insert("mode".into(), json!(mode)); crate::vector_ui::path_to_selection(app, Value::Object(p)) } // Layer › Rename Layer from a menu starts in-place renaming in the Layers panel. diff --git a/crates/ui-egui/src/tools/vector_ui/path_selection_tests.rs b/crates/ui-egui/src/tools/vector_ui/path_selection_tests.rs index d49eeaae497..5b73b9430d3 100644 --- a/crates/ui-egui/src/tools/vector_ui/path_selection_tests.rs +++ b/crates/ui-egui/src/tools/vector_ui/path_selection_tests.rs @@ -537,3 +537,28 @@ fn cmd_shift_and_alt_enter_use_the_path_selected_in_the_paths_panel() { press(&mut h, Key::Enter, Modifiers { alt: true, ..command() }); assert_eq!(selection(&h), Some([40, 40, 20, 20]), "{}", h.state().ui.status); } + +/// The selected area in pixels (partly selected pixels count in part). +fn selected_area(h: &Harness<'_, PhotocraftApp>) -> f32 { + let d = &h.state().session.active().unwrap().doc; + d.selection.as_ref().map_or(0.0, |s| s.read_region(d.bounds()).iter().sum()) +} + +/// #7726: as in Photoshop, ⇧⌘↩ adds the path to the selection, ⌥⌘↩ subtracts it and ⇧⌥⌘↩ +/// intersects them. A 100×60 selection and a 100×20 path overlapping it by 40×20; the areas are +/// Photoshop 2025's for the same document. +#[test] +fn shift_and_option_with_cmd_enter_combine_the_path_with_the_selection() { + let alt = Modifiers { alt: true, ..command() }; + let shift = Modifiers { shift: true, ..command() }; + let both = Modifiers { shift: true, ..alt }; + for (mods, area) in [(shift, 7200.0), (alt, 5200.0), (both, 800.0), (command(), 2000.0)] { + let mut h = harness(); + h.state_mut().run("path.set", json!({"name": "work", "path": rect_path(80.0, 40.0, 100.0, 20.0)})).unwrap(); + h.state_mut().run("select.rect", json!({"x": 20, "y": 20, "width": 100, "height": 60, "antiAlias": false})).unwrap(); + h.state_mut().ui.tool = Tool::Brush; + h.run_steps(2); + press(&mut h, Key::Enter, mods); + assert!((selected_area(&h) - area).abs() < 0.5, "{mods:?}: {} px, wanted {area}: {}", selected_area(&h), h.state().ui.status); + } +}