PhotoCraft: An open-source, photo editor written by scratch in pure Rust
fix(layer-picker): include text bounds in right-click layer list (#4288)
Nigel Ihza Firdausyab5a6c5ed20b
diff --git a/crates/engine/src/pick_cmds.rs b/crates/engine/src/pick_cmds.rsindex d3bee1c6a..c40e15e9b 100644--- a/crates/engine/src/pick_cmds.rs+++ b/crates/engine/src/pick_cmds.rs@@ -92,9 +92,9 @@ fn pick(s: &mut Session, p: &Value) -> Result<Value> {let x = p.get("x").and_then(Value::as_f64).ok_or_else(|| EngineError::BadParams { cmd: "layer.pickAt".into(), msg: "missing `x`".into() })?.floor() as i32;let y = p.get("y").and_then(Value::as_f64).ok_or_else(|| EngineError::BadParams { cmd: "layer.pickAt".into(), msg: "missing `y`".into() })?.floor() as i32;let doc = s.active().ok_or(EngineError::NoDocument)?.doc.clone();- // The right-click list names the layers with pixels under the pointer.+ // The right-click list includes type layers between glyphs, like Auto-Select.if p.get("list").and_then(Value::as_bool).unwrap_or(false) {- let hits = layers_hit(&doc, x, y, false);+ let hits = layers_hit(&doc, x, y, true);let names: Vec<Value> = hits.iter().filter_map(|id| doc.layer(*id)).map(|l| json!({"layer": l.id.0, "name": l.name})).collect();return Ok(json!({ "layers": names }));}@@ -190,7 +190,7 @@ pub fn specs() -> Vec<CommandSpec> {label: "Auto-Select Layer",menu: &[],shortcut: None,- params: r##"{"x":px,"y":px,"target":"layer|group"="layer","select":bool=true,"mode":"replace|toggle|add"="replace","list":bool=false (return every layer with pixels there, topmost first)} → {layer} | {layers}"##,+ params: r##"{"x":px,"y":px,"target":"layer|group"="layer","select":bool=true,"mode":"replace|toggle|add"="replace","list":bool=false (return visible layers at the point, including text bounds, topmost first)} → {layer} | {layers}"##,enabled: has_doc,journal: false,run: pick,@@ -372,9 +372,10 @@ mod tests {assert_eq!(pick(&mut s, 12, 15), t.0, "on a glyph");assert_eq!(pick(&mut s, 17, 15), t.0, "between the glyphs: the type layer, not the one under it");assert_eq!(pick(&mut s, 17, 20), bg.0, "below the text bounds");- // The right-click list still names only the layers with pixels under the pointer.+ // The context menu also reaches this type layer between the glyphs (#4259).let list = s.execute("layer.pickAt", json!({"x": 17, "y": 15, "list": true})).unwrap();- assert_eq!(list["layers"].as_array().unwrap().len(), 1);+ assert_eq!(list["layers"][0]["layer"], t.0);+ assert_eq!(list["layers"][1]["layer"], bg.0);}#[test]@@ -398,6 +399,9 @@ mod tests {let Some(x) = gap else { return };assert_eq!(layers_at(&doc, x, y), vec![bg], "no type pixels at the gap");assert_eq!(s.execute("layer.pickAt", json!({"x": x, "y": y, "select": false})).unwrap()["layer"], t);+ let menu = s.execute("layer.pickAt", json!({"x": x, "y": y, "list": true})).unwrap();+ assert_eq!(menu["layers"][0]["layer"], t, "type layer stays above its background in the menu");+ assert_eq!(menu["layers"][1]["layer"], bg.0);}#[test]diff --git a/crates/ui-egui/src/layer_pick_ui.rs b/crates/ui-egui/src/layer_pick_ui.rsindex 654751b95..b75fe2b47 100644--- a/crates/ui-egui/src/layer_pick_ui.rs+++ b/crates/ui-egui/src/layer_pick_ui.rs@@ -2,8 +2,8 @@//! with any tool, lists the layers with visible pixels under the pointer, topmost first, as//! Photoshop does. Choosing one selects it.//!-//! The hit test is `layer.pickAt` with `"list": true` (non-transparent pixels, layer masks, hidden-//! layers and hidden groups skipped), and choosing runs `layer.select`, so the menu is the same+//! The hit test is `layer.pickAt` with `"list": true` (non-transparent pixels, text bounds,+//! layer masks, hidden layers and hidden groups), and choosing runs `layer.select`, so the menu is the same//! two commands an agent would run. Painting tools keep their plain right-click (the Brush//! Preset picker, #268); the ⌘/Ctrl variant reaches the layer list from them too.@@ -27,7 +27,7 @@ pub fn is_gesture(tool: Tool, mods: Modifiers) -> bool {tool == Tool::Move || mods.command}-/// The layers with pixels at document point (x, y), topmost first.+/// The layers at document point (x, y), topmost first, including text between glyphs.pub fn layers_at(app: &mut PhotocraftApp, x: f64, y: f64) -> Vec<(u64, String)> {if !(x.is_finite() && y.is_finite()) {return Vec::new();