PhotoCraft: An open-source, photo editor written by scratch in pure Rust
fix(layers): Ctrl+Alt+G releases the clipping mask when pressed again (#4927)
OctaNebula026de5e2a3f5
diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rsindex 1e86407af..403994ac1 100644--- a/crates/ui-egui/src/menus.rs+++ b/crates/ui-egui/src/menus.rs@@ -1118,6 +1118,17 @@ pub fn menu_items(app: &PhotocraftApp) -> Vec<MenuItem> {it.color = prefs.menus.colors.get(&it.id).cloned();}}+ // Layer › Create Clipping Mask is Photoshop's one Create / Release item: when its shortcut+ // would release ([`crate::shortcut_dispatch::clip_key_releases`]) it reads Release Clipping+ // Mask and runs it, keeping that shortcut. The engine's own Release item lives in that slot.+ items.retain(|i| i.id != "layer.releaseClippingMask");+ if crate::shortcut_dispatch::clip_key_releases(app)+ && let Some(item) = items.iter_mut().find(|i| i.id == "layer.createClippingMask")+ {+ item.id = "layer.releaseClippingMask".into();+ item.label = "Release Clipping Mask".into();+ item.enabled = is_enabled(app, &item.id);+ }items}diff --git a/crates/ui-egui/src/shortcut_dispatch.rs b/crates/ui-egui/src/shortcut_dispatch.rsindex 1a6980077..0a6443721 100644--- a/crates/ui-egui/src/shortcut_dispatch.rs+++ b/crates/ui-egui/src/shortcut_dispatch.rs@@ -241,7 +241,7 @@ pub fn dispatch_pressed(app: &mut PhotocraftApp, ctx: &egui::Context, focus: Foc// panels instead, #1313): the shortcut took it, so cancel that move.ctx.memory_mut(|m| m.move_focus(egui::FocusDirection::None));}- let id = delete_key_command(app, id);+ let id = clip_key_command(app, delete_key_command(app, id));let owner = key_owner(app, ctx);dispatch(app, ctx, &id);ran = true;@@ -284,6 +284,26 @@ fn delete_key_command(app: &PhotocraftApp, id: String) -> String {"layer.delete".into()}+/// Create Clipping Mask's key (⌥⌘G) releases when there is nothing left to clip, as in Photoshop,+/// whose Layer menu has one item that reads Create or Release Clipping Mask with the same+/// shortcut: pressing it again takes the clip off. That is when the layers Create Clipping Mask+/// would clip are all clipped already: the selected layer, or with several selected, every one+/// above the lowest.+pub(crate) fn clip_key_releases(app: &PhotocraftApp) -> bool {+ let Some(st) = app.session.active() else { return false };+ let selected = st.selected_layers();+ let clips = match selected.as_slice() {+ [] => return false,+ [one] => std::slice::from_ref(one),+ [_lowest, above @ ..] => above,+ };+ clips.iter().all(|id| st.doc.layer(*id).is_some_and(|l| l.clipped))+}++fn clip_key_command(app: &PhotocraftApp, id: String) -> String {+ if id == "layer.createClippingMask" && clip_key_releases(app) { "layer.releaseClippingMask".into() } else { id }+}+/// Why `id` can't run now (the engine's reason when it has one).pub fn disabled_reason(app: &PhotocraftApp, id: &str) -> String {photocraft_engine::commands::find(id).and_then(|_| app.session.disabled_reason_with(id, &app.with_mask_target(id, serde_json::Value::Null))).unwrap_or_else(diff --git a/crates/ui-egui/src/shortcut_dispatch/tests.rs b/crates/ui-egui/src/shortcut_dispatch/tests.rsindex 71a7bc385..458f22c05 100644--- a/crates/ui-egui/src/shortcut_dispatch/tests.rs+++ b/crates/ui-egui/src/shortcut_dispatch/tests.rs@@ -135,9 +135,13 @@ fn put_focus(h: &mut Harness<'_, PhotocraftApp>, place: Place) {}/// Is `bound` the same action as the menu item `shown`? (Window › Layers is `window.panel.layers`-/// in the catalogue and `window.toggle.layers` in the shell.)+/// in the catalogue and `window.toggle.layers` in the shell; Release Clipping Mask shows Create+/// Clipping Mask's key, which releases while it reads so.)fn same(shown: &str, bound: &str) -> bool {- shown == bound || crate::menus::panel_alias(shown) == Some(bound) || crate::menus::panel_alias(bound) == Some(shown)+ shown == bound+ || crate::menus::panel_alias(shown) == Some(bound)+ || crate::menus::panel_alias(bound) == Some(shown)+ || (shown == "layer.releaseClippingMask" && bound == "layer.createClippingMask")}/// Every shortcut shown in the menus (and the shell's unlisted ones), with whether it is live.@@ -566,6 +570,65 @@ fn delete_with_a_selection_clears_it() {assert_eq!(names(&h), before, "no layer was deleted");}+fn is_clipped(h: &Harness<'_, PhotocraftApp>, id: photocraft_doc::LayerId) -> bool {+ h.state().session.active().unwrap().doc.layer(id).unwrap().clipped+}++/// The Layer menu's clipping mask items: (id, label, shortcut, enabled).+fn clip_items(h: &Harness<'_, PhotocraftApp>) -> Vec<(String, String, Option<String>, bool)> {+ crate::menus::menu_items(h.state()).into_iter().filter(|i| i.id.ends_with("ClippingMask")).map(|i| (i.id, i.label, i.shortcut, i.enabled)).collect()+}++fn clip_item(id: &str, label: &str) -> Vec<(String, String, Option<String>, bool)> {+ vec![(id.into(), label.into(), Some("Cmd+Alt+G".into()), true)]+}++/// ⌥⌘G clips the selected layer and pressing it again releases it, as Photoshop's one Create /+/// Release Clipping Mask item does; the Layer menu reads Release Clipping Mask, with the same+/// shortcut, while the key would release.+#[test]+fn clip_key_toggles_the_clipping_mask() {+ let mut h = harness();+ put_focus(&mut h, Place::Canvas);+ let paint = h.state().session.active().unwrap().active_layer.unwrap();+ assert_eq!(clip_items(&h), clip_item("layer.createClippingMask", "Create Clipping Mask"));+ press(&mut h, "Cmd+Alt+G");+ assert_eq!(logged(&h), ["layer.createClippingMask"]);+ assert!(is_clipped(&h, paint));+ assert_eq!(clip_items(&h), clip_item("layer.releaseClippingMask", "Release Clipping Mask"));+ press(&mut h, "Cmd+Alt+G");+ assert_eq!(logged(&h), ["layer.releaseClippingMask"]);+ assert!(!is_clipped(&h, paint), "the second press releases it");+ assert_eq!(clip_items(&h), clip_item("layer.createClippingMask", "Create Clipping Mask"));+ press(&mut h, "Cmd+Z");+ assert!(is_clipped(&h, paint), "one undo puts the clip back");+}++/// With several layers selected the key clips every one above the lowest, and releases them once+/// they all are: a selection where only some are clipped clips the rest first.+#[test]+fn clip_key_toggles_several_selected_layers() {+ let mut h = harness();+ put_focus(&mut h, Place::Canvas);+ let s = &mut h.state_mut().session;+ let paint = s.active().unwrap().active_layer.unwrap();+ let base = s.active().unwrap().doc.walk().into_iter().find(|(_, _, l)| l.name == "Hello").unwrap().2.id;+ let top = photocraft_doc::LayerId(s.execute("layer.new.layer", json!({"name": "top"})).unwrap()["layer"].as_u64().unwrap());+ s.execute("layer.createClippingMask", json!({"layer": paint.0})).unwrap();+ s.execute("layer.select", json!({"layer": base.0, "mode": "replace"})).unwrap();+ for id in [paint, top] {+ s.execute("layer.select", json!({"layer": id.0, "mode": "add"})).unwrap();+ }+ assert_eq!(s.active().unwrap().selected_layers().len(), 3);+ h.run_steps(2);+ press(&mut h, "Cmd+Alt+G");+ assert_eq!(logged(&h), ["layer.createClippingMask"], "the top layer isn't clipped yet");+ assert!(is_clipped(&h, paint) && is_clipped(&h, top) && !is_clipped(&h, base));+ press(&mut h, "Cmd+Alt+G");+ assert_eq!(logged(&h), ["layer.releaseClippingMask"]);+ assert!(!is_clipped(&h, paint) && !is_clipped(&h, top) && !is_clipped(&h, base));+}+#[test]fn named_action_function_key_replays_and_survives_preferences_roundtrip() {let mut h = harness();