PhotoCraft: An open-source, photo editor written by scratch in pure Rust
fix(type): the Type tool's canvas pick skips text in hidden groups (#4320) (#4332)
FredApps96f4256ced26
diff --git a/crates/doc/src/lib.rs b/crates/doc/src/lib.rsindex e2a46e407..a5c5381ee 100644--- a/crates/doc/src/lib.rs+++ b/crates/doc/src/lib.rs@@ -1047,6 +1047,11 @@ impl Document {(1..=path.len()).filter_map(|n| self.layer_at(path.get(..n)?)).fold(Locks::default(), |a, l| a.union(l.locks))}+ /// Whether the layer at `path` is shown: it and every group around it are visible.+ pub fn shown_at(&self, path: &[usize]) -> bool {+ (1..=path.len()).all(|n| path.get(..n).and_then(|p| self.layer_at(p)).is_some_and(|l| l.visible))+ }+/// [`Self::locks_at`] for the layer `id`.pub fn effective_locks(&self, id: LayerId) -> Locks {self.path_of(id).map_or_else(Locks::default, |p| self.locks_at(&p))diff --git a/crates/engine/src/type_caret_cmds.rs b/crates/engine/src/type_caret_cmds.rsindex 528472cc2..fbc9322c6 100644--- a/crates/engine/src/type_caret_cmds.rs+++ b/crates/engine/src/type_caret_cmds.rs@@ -106,9 +106,7 @@ fn topmost(doc: &Document, x: f64, y: f64) -> Option<Frame> {let mut ids: Vec<LayerId> = doc.walk().into_iter()- .filter(|(path, _, layer)| {- matches!(layer.content, LayerContent::Text(_)) && (1..=path.len()).all(|n| doc.layer_at(&path[..n]).is_some_and(|ancestor| ancestor.visible))- })+ .filter(|(path, _, layer)| matches!(layer.content, LayerContent::Text(_)) && doc.shown_at(path)).map(|(_, _, layer)| layer.id).collect();ids.reverse();diff --git a/crates/ui-egui/src/type_tool.rs b/crates/ui-egui/src/type_tool.rsindex 6a801b338..291fdde78 100644--- a/crates/ui-egui/src/type_tool.rs+++ b/crates/ui-egui/src/type_tool.rs@@ -52,7 +52,8 @@ fn to_text(aff: &Affine, x: f64, y: f64) -> (f32, f32) {(p.x as f32, p.y as f32)}-/// Topmost visible type layer whose laid-out text contains the document point.+/// Topmost shown type layer (it and every group around it visible, #4320) whose laid-out text+/// contains the document point.fn hit_layer(app: &mut PhotocraftApp, x: f64, y: f64) -> Option<LayerId> {let doc = app.session.active()?.doc.clone();let slop = 6.0 / app.point_zoom().max(0.01);@@ -61,7 +62,7 @@ fn hit_layer(app: &mut PhotocraftApp, x: f64, y: f64) -> Option<LayerId> {let mut ids: Vec<LayerId> = doc.walk().into_iter()- .filter(|(_, _, l)| (l.visible || editing == Some(l.id)) && matches!(l.content, LayerContent::Text(_)))+ .filter(|(path, _, l)| matches!(l.content, LayerContent::Text(_)) && (editing == Some(l.id) || doc.shown_at(path))).map(|(_, _, l)| l.id).collect();ids.reverse(); // walk() is bottom-up; hit the topmost first@@ -2227,6 +2228,86 @@ mod tests {current_text(app, LayerId(ed.layer)).unwrap()}+ /// A committed type layer reading `s` at the same spot as the others (stacked variants, #4320).+ fn stacked_variant(app: &mut PhotocraftApp, s: &str) -> LayerId {+ app.ui.text_edit = None;+ pointer_up(app, [50.0, 100.0], [50.0, 100.0]);+ insert(app, s);+ commit(app);+ app.session.active().unwrap().active_layer.unwrap()+ }++ fn hide(app: &mut PhotocraftApp, id: LayerId) {+ app.run("layer.setProps", json!({"layer": id.0, "visible": false})).unwrap();+ }++ /// Wraps `id` in a new group and returns the group.+ fn group_of(app: &mut PhotocraftApp, id: LayerId) -> LayerId {+ app.run("layer.select", json!({"layer": id.0, "mode": "replace"})).unwrap();+ app.run("layer.groupLayers", json!({})).unwrap();+ let doc = &app.session.active().unwrap().doc;+ let path = doc.path_of(id).unwrap();+ doc.layer_at(&path[..path.len() - 1]).unwrap().id+ }++ /// Three type variants at one spot, bottom to top: A, B, C.+ fn variants() -> (PhotocraftApp, [LayerId; 3]) {+ let mut app = app();+ let ids = ["Variant A", "Variant B", "Variant C"].map(|s| stacked_variant(&mut app, s));+ (app, ids)+ }++ const ON_TEXT: (f64, f64) = (60.0, 95.0);++ /// #4320: variants hidden through their groups above the shown one are not picked.+ #[test]+ fn type_click_skips_text_in_hidden_groups_above_the_shown_variant() {+ let (mut app, [a, b, c]) = variants();+ for id in [b, c] {+ let g = group_of(&mut app, id);+ hide(&mut app, g);+ }+ assert_eq!(hit_layer(&mut app, ON_TEXT.0, ON_TEXT.1), Some(a));+ assert!(pointer_down(&mut app, ON_TEXT.0, ON_TEXT.1, egui::Modifiers::NONE));+ assert_eq!(app.ui.text_edit.as_ref().map(|e| e.layer), Some(a.0), "the click edits the shown variant");+ }++ #[test]+ fn type_click_skips_text_in_a_nested_hidden_group() {+ let (mut app, [a, b, c]) = variants();+ hide(&mut app, b);+ let inner = group_of(&mut app, c);+ let outer = group_of(&mut app, inner);+ hide(&mut app, outer);+ assert!(app.session.active().unwrap().doc.layer(c).unwrap().visible, "only the outer group is hidden");+ assert_eq!(hit_layer(&mut app, ON_TEXT.0, ON_TEXT.1), Some(a));+ }++ #[test]+ fn type_click_skips_own_hidden_variants_above_and_below() {+ let (mut app, [a, b, c]) = variants();+ hide(&mut app, b);+ hide(&mut app, c);+ assert_eq!(hit_layer(&mut app, ON_TEXT.0, ON_TEXT.1), Some(a), "hidden variants above");+ let (mut app, [a, b, c]) = variants();+ hide(&mut app, a);+ hide(&mut app, b);+ assert_eq!(hit_layer(&mut app, ON_TEXT.0, ON_TEXT.1), Some(c), "hidden variants below");+ }++ /// The Move tool's double-click on type (#1132) shares the pick.+ #[test]+ fn move_double_click_edits_the_shown_variant_not_one_in_a_hidden_group() {+ let (mut app, [a, b, c]) = variants();+ for id in [b, c] {+ let g = group_of(&mut app, id);+ hide(&mut app, g);+ }+ app.ui.tool = crate::state::Tool::Move;+ assert!(edit_at(&mut app, ON_TEXT.0, ON_TEXT.1));+ assert_eq!(app.ui.text_edit.as_ref().map(|e| e.layer), Some(a.0));+ }+/// Photoshop: a click on existing type edits it; a ⇧-click there starts a new type layer.#[test]fn shift_click_on_existing_type_starts_new_text() {