Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Masks: \ toggles the layer mask's rubylith overlay (#1809)

David Manndee737313a56
diff --git a/crates/engine/src/mask_view_cmds.rs b/crates/engine/src/mask_view_cmds.rs
index 104bcf235..2beb03612 100644
--- a/crates/engine/src/mask_view_cmds.rs
+++ b/crates/engine/src/mask_view_cmds.rs
@@ -17,6 +17,9 @@ use crate::{DocState, EngineError, Result, Session};
pub const ID: &str = "view.layerMask";
+/// `\` (#1765): show or hide the active layer's mask as a rubylith, Photoshop's key for it.
+pub const OVERLAY_ID: &str = "layer.toggleMaskOverlay";
+
/// How a shown layer mask is drawn.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
@@ -121,17 +124,46 @@ fn set_view(s: &mut Session, p: &Value) -> Result<Value> {
Ok(json!({"layer": layer.0, "mode": mode_name(v.filter(|v| v.layer == layer))}))
}
+/// `\` needs a layer mask on the active layer; without one Photoshop's key does nothing.
+fn active_has_mask(s: &Session) -> std::result::Result<(), String> {
+ let st = s.active().ok_or("no document open")?;
+ let layer = st.active_layer.and_then(|id| st.doc.layer(id)).ok_or("no active layer")?;
+ if layer.mask.is_some() { Ok(()) } else { Err(format!("layer \"{}\" has no layer mask", layer.name)) }
+}
+
+/// `{"layer":id?}`: [`ID`] with mode `toggleOverlay` (a `mode` param is ignored).
+fn toggle_overlay(s: &mut Session, p: &Value) -> Result<Value> {
+ let layer = match p {
+ Value::Object(m) => m.get("layer").cloned().unwrap_or(Value::Null),
+ Value::Null => Value::Null,
+ _ => return Err(EngineError::BadParams { cmd: OVERLAY_ID.into(), msg: "expected an object".into() }),
+ };
+ set_view(s, &json!({"layer": layer, "mode": "toggleOverlay"}))
+}
+
pub fn specs() -> Vec<CommandSpec> {
- vec![CommandSpec {
- id: ID,
- label: "View Layer Mask",
- menu: &[],
- shortcut: None,
- params: r##"{"layer":id?,"mode":"off"|"gray"|"overlay"|"toggleGray"|"toggleOverlay"="toggleGray"} (gray: the mask alone, ⌥-click its thumbnail; overlay: red rubylith over the composite, ⇧⌥-click; view state, not an undo step; painting while shown paints the mask)"##,
- enabled: has_doc,
- run: set_view,
- journal: false,
- }]
+ vec![
+ CommandSpec {
+ id: ID,
+ label: "View Layer Mask",
+ menu: &[],
+ shortcut: None,
+ params: r##"{"layer":id?,"mode":"off"|"gray"|"overlay"|"toggleGray"|"toggleOverlay"="toggleGray"} (gray: the mask alone, ⌥-click its thumbnail; overlay: red rubylith over the composite, ⇧⌥-click or `\`; view state, not an undo step; painting while shown paints the mask)"##,
+ enabled: has_doc,
+ run: set_view,
+ journal: false,
+ },
+ CommandSpec {
+ id: OVERLAY_ID,
+ label: "Toggle Layer Mask Overlay",
+ menu: &[],
+ shortcut: Some("\\"),
+ params: r##"{"layer":id?} (shows or hides the active layer's mask as a 50% red rubylith over the composite, Photoshop's `\` key; view state, not an undo step; painting while shown paints the mask)"##,
+ enabled: active_has_mask,
+ run: toggle_overlay,
+ journal: false,
+ },
+ ]
}
#[cfg(test)]
diff --git a/crates/engine/src/mask_view_cmds/tests.rs b/crates/engine/src/mask_view_cmds/tests.rs
index c9e47ec6f..33e36b142 100644
--- a/crates/engine/src/mask_view_cmds/tests.rs
+++ b/crates/engine/src/mask_view_cmds/tests.rs
@@ -135,3 +135,34 @@ fn load_selection_from_a_vector_mask() {
s.execute("layer.vectorMask.delete", json!({})).unwrap();
assert!(s.execute("select.loadSelection", json!({"channel": "vectorMask"})).is_err());
}
+
+/// #1765: `\` (`layer.toggleMaskOverlay`) shows the active layer's mask as a rubylith and hides it
+/// again; without a mask (or a document) it is disabled and errors instead of panicking.
+#[test]
+fn backslash_command_toggles_the_overlay() {
+ let (mut s, masked, bg) = session();
+ let steps = s.active().unwrap().history.entries().len();
+ assert!(s.is_enabled(OVERLAY_ID));
+ assert_eq!(s.execute(OVERLAY_ID, json!({})).unwrap()["mode"], "overlay");
+ assert_eq!(view(&s), json!({"layer": masked, "mode": "overlay"}));
+ assert_eq!(s.execute(OVERLAY_ID, Value::Null).unwrap()["mode"], "off", "again: back to the composite");
+ assert_eq!(view(&s), Value::Null);
+ // From the grayscale view it switches to the overlay.
+ s.execute(ID, json!({"mode": "gray"})).unwrap();
+ assert_eq!(s.execute(OVERLAY_ID, json!({"mode": "gray"})).unwrap()["mode"], "overlay", "`mode` is ignored");
+ s.execute(OVERLAY_ID, json!({})).unwrap();
+ let st = s.active().unwrap();
+ assert_eq!(st.history.entries().len(), steps, "not a history step");
+ assert!(!st.is_dirty());
+ // No mask on the active layer, bad params, no document: errors, nothing changes.
+ s.execute("layer.select", json!({"layer": bg})).unwrap();
+ assert!(!s.is_enabled(OVERLAY_ID));
+ assert!(s.execute(OVERLAY_ID, json!({})).is_err());
+ assert!(s.execute(OVERLAY_ID, json!({"layer": bg})).is_err());
+ assert!(s.execute(OVERLAY_ID, json!({"layer": "x"})).is_err());
+ assert!(s.execute(OVERLAY_ID, json!([1])).is_err());
+ assert_eq!(view(&s), Value::Null);
+ let mut empty = Session::new();
+ assert!(!empty.is_enabled(OVERLAY_ID));
+ assert!(empty.execute(OVERLAY_ID, json!({})).is_err());
+}
diff --git a/crates/ui-egui/src/mask_thumbs_ui/tests.rs b/crates/ui-egui/src/mask_thumbs_ui/tests.rs
index 4c1d8bc3f..12f535162 100644
--- a/crates/ui-egui/src/mask_thumbs_ui/tests.rs
+++ b/crates/ui-egui/src/mask_thumbs_ui/tests.rs
@@ -390,6 +390,34 @@ fn mask_view_gestures_fail_gracefully_without_a_mask() {
assert_eq!(crate::canvas::paint_target(h.state()), json!("pixels"));
}
+/// #1765: `\` shows the active layer's mask as a rubylith and hides it again, as in Photoshop;
+/// the mask stays the paint target meanwhile. On a layer without a mask it does nothing, and a
+/// focused text field keeps the key.
+#[test]
+fn backslash_toggles_the_rubylith() {
+ let (s, masked, shape) = dotted();
+ let mut h = harness(s, 0, 1.0, 290.0);
+ let doc = h.state().session.active().unwrap().doc.clone();
+ h.key_press(egui::Key::Backslash);
+ h.run_steps(2);
+ assert_eq!(mask_view(&h), json!({"layer": masked, "mode": "overlay"}), "\\ shows the overlay");
+ assert_eq!(crate::canvas::paint_target(h.state()), json!("mask"), "painting paints the mask");
+ let px = canvas_px(&h).unwrap();
+ assert_eq!(px[50 * 200 + 50], egui::Color32::from_rgba_premultiplied(128, 0, 0, 128), "50% red over hidden areas");
+ h.key_press(egui::Key::Backslash);
+ h.run_steps(2);
+ assert_eq!(mask_view(&h), serde_json::Value::Null, "\\ again hides it");
+ assert!(std::sync::Arc::ptr_eq(&doc, &h.state().session.active().unwrap().doc), "a view toggle, not an edit");
+ // A layer without a layer mask: nothing happens.
+ h.state_mut().run("layer.select", json!({"layer": shape})).unwrap();
+ h.run_steps(2);
+ h.key_press(egui::Key::Backslash);
+ h.run_steps(2);
+ assert_eq!(mask_view(&h), serde_json::Value::Null);
+ let sc = crate::shortcuts::parse("\\").unwrap();
+ assert!(!crate::shortcut_dispatch::Focus::Text.allows(&sc), "typing \\ in a text field stays text");
+}
+
/// #780: clicking the mask thumbnail targets the mask, and ⌘I (Image › Adjustments › Invert)
/// then inverts the mask instead of the layer; on an adjustment layer the menu item is live.
#[test]