PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Layers: Add vector mask makes the mask from the selected path and targets it (#4740)
Marco Arnulfo95a9a6b29ce6
diff --git a/crates/ui-egui/src/canvas_tool_menu.rs b/crates/ui-egui/src/canvas_tool_menu.rsindex 37ee58b0a..e1466f09a 100644--- a/crates/ui-egui/src/canvas_tool_menu.rs+++ b/crates/ui-egui/src/canvas_tool_menu.rs@@ -1466,6 +1466,9 @@ mod tests {choose_open(&mut app, "layer.vectorMask.fromPath");let st = app.session.active().unwrap();assert!(st.active_layer.and_then(|id| st.doc.layer(id)).is_some_and(|l| l.vector_mask.is_some()));+ // The new vector mask's path becomes the active one (#4728); pick the Work Path again.+ assert_eq!(app.ui.selected_path.as_deref(), Some("layer"));+ app.ui.selected_path = Some("work".into());choose_open(&mut app, "edit.defineCustomShape");let id = app.ui.dialogs.last().map(|d| d.id).unwrap();let fields = &app.ui.dialogs.last().unwrap().fields;diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rsindex 131a670ee..9792297eb 100644--- a/crates/ui-egui/src/lib.rs+++ b/crates/ui-egui/src/lib.rs@@ -992,12 +992,17 @@ impl PhotocraftApp {let params = self.with_mask_target(id, params);let params = vector_ui::with_active_path(self, id, params);let path_mask = vector_ui::takes_path_mask(id) && params.get("path").is_some_and(|v| !v.is_null());+ let on_active_layer = self+ .session+ .active()+ .is_some_and(|st| st.active_layer.is_some_and(|layer| params.get("layer").and_then(Value::as_u64).is_none_or(|target| target == layer.0)));let creates_active_mask =matches!(id, "layer.layerMask.revealAll" | "layer.layerMask.hideAll" | "layer.layerMask.revealSelection" | "layer.layerMask.hideSelection")- && self- .session- .active()- .is_some_and(|st| st.active_layer.is_some_and(|layer| params.get("layer").and_then(Value::as_u64).is_none_or(|target| target == layer.0)));+ && on_active_layer;+ let creates_active_vector_mask = matches!(+ id,+ "layer.vectorMask.add" | "layer.vectorMask.fromPath" | "layer.vectorMask.revealAll" | "layer.vectorMask.hideAll" | "layer.vectorMask.currentPath"+ ) && on_active_layer;let r = if id == "actions.play" {actions::play(self, ¶ms)} else if photocraft_engine::actions_cmds::shell_view_command(id) {@@ -1038,6 +1043,15 @@ impl PhotocraftApp {self.ui.mask_target = true;self.ui.vector_mask_target = false;}+ if r.is_ok() && creates_active_vector_mask {+ // Likewise a new vector mask (#4728): Properties shows its Density and Feather, and a+ // mask made from a path becomes the active path.+ self.ui.vector_mask_target = true;+ self.ui.mask_target = false;+ if matches!(id, "layer.vectorMask.currentPath" | "layer.vectorMask.fromPath") {+ self.ui.selected_path = Some("layer".into());+ }+ }if r.is_ok() && matches!(id, "edit.copy" | "edit.cut" | "edit.copyMerged") {self.clip_external = false;self.export_os_clipboard();diff --git a/crates/ui-egui/src/mask_thumbs_ui/tests.rs b/crates/ui-egui/src/mask_thumbs_ui/tests.rsindex e18cb0e9a..50ff96f08 100644--- a/crates/ui-egui/src/mask_thumbs_ui/tests.rs+++ b/crates/ui-egui/src/mask_thumbs_ui/tests.rs@@ -169,6 +169,29 @@ fn a_new_mask_is_targeted_so_the_next_footer_trash_keeps_its_layer() {assert!(h.state().session.active().unwrap().doc.layer(photocraft_doc::LayerId(shape)).is_some());}+/// #4728: with a path selected in the Paths panel, the footer's "Add vector mask" makes the vector+/// mask from that path (Layer › Vector Mask › Current Path) and targets it, so Properties shows+/// its Density and Feather.+#[test]+fn add_vector_mask_uses_the_selected_path_and_targets_the_new_mask() {+ let (s, _, _) = session();+ let mut h = harness(s, 0, 1.0, 290.0);+ h.state_mut().run("layer.new.layer", json!({"name": "Spock"})).unwrap();+ h.state_mut().run("layer.layerMask.revealAll", json!({})).unwrap();+ let id = h.state().session.active().unwrap().active_layer.unwrap();+ let path = json!({"subpaths": [{"closed": true, "knots": [[30, 20], [90, 20], [90, 80], [30, 80]]}]});+ h.state_mut().run("path.set", json!({"name": "work", "path": path})).unwrap();+ h.state_mut().ui.selected_path = Some("work".into());+ let work = h.state().session.active().unwrap().doc.work_path.clone().unwrap();+ h.run_steps(3);+ let at = h.get_by_label("Add vector mask").rect().center();+ click_with(&mut h, at, Modifiers::NONE);+ let l = h.state().session.active().unwrap().doc.layer(id).unwrap().clone();+ assert_eq!(l.vector_mask.map(|v| v.path), Some(work), "the vector mask is the selected path, not Reveal All");+ assert!(h.state().ui.vector_mask_target && !h.state().ui.mask_target, "the new vector mask is targeted");+ assert_eq!(h.state().ui.selected_path.as_deref(), Some("layer"), "the mask's path becomes the active path");+}+#[test]fn dragging_a_layer_row_to_trash_still_deletes_the_layer_with_a_mask_targeted() {let (s, masked, _) = session();diff --git a/crates/ui-egui/src/panels.rs b/crates/ui-egui/src/panels.rsindex 07653150e..ce9437da5 100644--- a/crates/ui-egui/src/panels.rs+++ b/crates/ui-egui/src/panels.rs@@ -2448,7 +2448,12 @@ fn layers(app: &mut PhotocraftApp, ui: &mut egui::Ui) {if mask_btn.clicked()&& let Some(cmd) = mask_cmd{- actions.push((cmd.into(), json!({})));+ // A path selected in the Paths panel becomes the vector mask, as in Photoshop (#4728);+ // Alt still inverts it.+ match crate::vector_ui::selected_document_path(app).filter(|_| cmd.starts_with("layer.vectorMask.")) {+ Some(name) => actions.push(("layer.vectorMask.currentPath".into(), json!({"name": name, "hide": alt}))),+ None => actions.push((cmd.into(), json!({}))),+ }}let fx = fx_button(ui, 26.0, tl!("Add a layer style"));crate::press_menu::show(&fx, |ui, menu| {diff --git a/crates/ui-egui/src/vector_ui.rs b/crates/ui-egui/src/vector_ui.rsindex 5e694ff98..b4e6cca16 100644--- a/crates/ui-egui/src/vector_ui.rs+++ b/crates/ui-egui/src/vector_ui.rs@@ -564,6 +564,18 @@ pub fn takes_path_mask(id: &str) -> bool {id.starts_with("layer.newFillLayer.") || id.starts_with("layer.newAdjustmentLayer.")}+/// The work or saved path explicitly selected in the Paths panel (not a layer's own path).+pub fn selected_document_path(app: &PhotocraftApp) -> Option<String> {+ let st = app.session.active()?;+ let sel = app.ui.selected_path.as_deref()?;+ let listed = path_rows(&st.doc, st.active_layer).iter().any(|r| match r.kind {+ PathRow::Work => sel == "work",+ PathRow::Saved => r.name == sel,+ PathRow::Layer => false,+ });+ listed.then(|| sel.to_string())+}+/// New fill and adjustment layers take the path selected in the Paths panel as their vector mask,/// as in Photoshop (#1419): the command gets it as `"path"` unless the caller set one (`null`/// opts out). Only an explicitly selected work or saved path counts; deselect it by clicking the@@ -572,16 +584,7 @@ pub fn with_active_path(app: &PhotocraftApp, id: &str, params: Value) -> Value {if !takes_path_mask(id) || params.get("path").is_some() {return params;}- let Some(st) = app.session.active() else { return params };- let Some(sel) = app.ui.selected_path.as_deref() else { return params };- let listed = path_rows(&st.doc, st.active_layer).iter().any(|r| match r.kind {- PathRow::Work => sel == "work",- PathRow::Saved => r.name == sel,- PathRow::Layer => false,- });- if !listed {- return params;- }+ let Some(sel) = selected_document_path(app) else { return params };match params {Value::Object(mut m) => {m.insert("path".into(), json!(sel));