diff --git a/crates/ui-egui/src/tools/move_distance.rs b/crates/ui-egui/src/tools/move_distance.rs index 471a04c0a1..82a09ffc04 100644 --- a/crates/ui-egui/src/tools/move_distance.rs +++ b/crates/ui-egui/src/tools/move_distance.rs @@ -2,7 +2,8 @@ //! › Smart Guides, Extras on) and the Move tool, holding ⌘ (Ctrl off the Mac) with no button down //! and hovering another layer shows the distances between the selected layers' bounds and that //! layer's, as lines labelled with the value in the ruler unit. Over empty canvas (or the -//! Background, or the selection itself) the distances run to the canvas edges instead. While an +//! Background) the distances run to the canvas edges instead; over the selection itself (or a +//! layer in a selected group) nothing shows (#16861). While an //! ⌥-drag duplicates the layers, the distance between the original and the copy shows. //! //! It's an overlay only: nothing changes in the document and there is no command or History @@ -34,6 +35,7 @@ pub(crate) struct Cache { /// The selected layers' box and the box to measure it to with the pointer at `at` (document px): /// the layer a click there would Auto-Select, or the canvas where that is no other layer. +/// `None` when that layer is part of the selection. fn boxes(app: &mut PhotocraftApp, at: [f64; 2]) -> Option<([f64; 4], [f64; 4])> { if !at[0].is_finite() || !at[1].is_finite() { return None; @@ -49,11 +51,10 @@ fn boxes(app: &mut PhotocraftApp, at: [f64; 2]) -> Option<([f64; 4], [f64; 4])> let d = &st.doc; let a = union(selected.iter().filter(|id| !d.is_background(**id)).filter_map(|id| crate::snap_ui::tree_rect(d, *id))); let canvas = [0.0, 0.0, d.size.width as f64, d.size.height as f64]; - let hovered = photocraft_engine::pick_cmds::move_auto_select_target(d, at[0], at[1], group).filter(|id| { - // The selection itself (or a layer in a selected group) isn't another layer. - !d.is_background(*id) && !selected.iter().any(|s| *s == *id || d.path_of(*id).zip(d.path_of(*s)).is_some_and(|(p, q)| p.starts_with(&q))) - }); - let boxes = a.map(|a| (a, hovered.and_then(|id| crate::snap_ui::tree_rect(d, id)).unwrap_or(canvas))); + let hovered = photocraft_engine::pick_cmds::move_auto_select_target(d, at[0], at[1], group).filter(|id| !d.is_background(*id)); + // The selection itself (or a layer in a selected group) isn't another layer: nothing to measure. + let on_selection = hovered.is_some_and(|id| selected.iter().any(|s| *s == id || d.path_of(id).zip(d.path_of(*s)).is_some_and(|(p, q)| p.starts_with(&q)))); + let boxes = a.filter(|_| !on_selection).map(|a| (a, hovered.and_then(|id| crate::snap_ui::tree_rect(d, id)).unwrap_or(canvas))); app.move_distance = Some(Cache { doc, revision, at, group, selected: selected.to_vec(), boxes }); boxes } @@ -148,8 +149,6 @@ mod tests { assert_eq!((a, b), (mover, Rect::new(300.0, 200.0, 350.0, 260.0))); // Over empty canvas (the Background): to the canvas. assert_eq!(current(&mut app, Some([200.0, 150.0]), true, false), Some((mover, canvas))); - // Over the selected layer itself: also the canvas. - assert_eq!(current(&mut app, Some([60.0, 60.0]), true, false), Some((mover, canvas))); // Only an overlay: the document didn't change. assert_eq!(app.session.active().unwrap().revision, revision); let target = app.session.active().unwrap().doc.layers.iter().find(|l| l.name == "target").unwrap().id; @@ -199,3 +198,7 @@ mod tests { assert_eq!(current(&mut app, None, false, false), None); } } + +#[cfg(test)] +#[path = "move_distance_self_tests.rs"] +mod move_distance_self_tests; diff --git a/crates/ui-egui/src/tools/move_distance_self_tests.rs b/crates/ui-egui/src/tools/move_distance_self_tests.rs new file mode 100644 index 0000000000..7cfb98c130 --- /dev/null +++ b/crates/ui-egui/src/tools/move_distance_self_tests.rs @@ -0,0 +1,55 @@ +//! ⌘-hover over the selection itself measures nothing (#16861). + +use craft::behavior::kurbo::Rect; +use serde_json::json; + +use super::current; +use crate::PhotocraftApp; +use crate::state::Tool; + +/// The issue's document: 1000×800 with a white Background and layers A (100,100 to 300,260), +/// C (400,100 to 500,200) and B (600,400 to 750,520, top and selected). +fn app() -> Box { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 1000, "height": 800})).unwrap(); + for (name, x, y, width, height) in [("A", 100, 100, 200, 160), ("C", 400, 100, 100, 100), ("B", 600, 400, 150, 120)] { + app.run("layer.new.layer", json!({"name": name})).unwrap(); + app.run("select.rect", json!({"x": x, "y": y, "width": width, "height": height})).unwrap(); + app.run("edit.fill", json!({"color": "#ff0000"})).unwrap(); + } + app.run("select.deselect", json!({})).unwrap(); + app.ui.tool = Tool::Move; + app.ui.view.extras = true; + app.ui.view.show.smart_guides = true; + app +} + +const CANVAS: Rect = Rect::new(0.0, 0.0, 1000.0, 800.0); + +#[test] +fn command_over_the_selected_layer_shows_nothing() { + let mut app = app(); + assert_eq!(current(&mut app, Some([675.0, 460.0]), true, false), None, "over B, B selected"); + // Empty canvas (the Background) still measures to the canvas edges, and another layer to it. + let b = Rect::new(600.0, 400.0, 750.0, 520.0); + assert_eq!(current(&mut app, Some([900.0, 100.0]), true, false), Some((b, CANVAS))); + assert_eq!(current(&mut app, Some([450.0, 150.0]), true, false), Some((b, Rect::new(400.0, 100.0, 500.0, 200.0)))); + // The cached answer for the same pixel doesn't outlive a selection change. + app.run("layer.select", json!({"name": "A"})).unwrap(); + assert_eq!(current(&mut app, Some([675.0, 460.0]), true, false).map(|(_, to)| to), Some(b), "B is another layer now"); +} + +#[test] +fn command_over_a_layer_in_a_selected_group_shows_nothing() { + let mut app = app(); + app.run("layer.select", json!({"name": "A"})).unwrap(); + app.run("layer.select", json!({"name": "C", "mode": "add"})).unwrap(); + app.run("layer.groupLayers", json!({"name": "G"})).unwrap(); + for target in ["layer", "group"] { + app.ui.tool_options.move_target = target.into(); + assert_eq!(current(&mut app, Some([200.0, 180.0]), true, false), None, "over A, {target}"); + assert_eq!(current(&mut app, Some([450.0, 150.0]), true, false), None, "over C, {target}"); + let g = Rect::new(100.0, 100.0, 500.0, 260.0); + assert_eq!(current(&mut app, Some([675.0, 460.0]), true, false), Some((g, Rect::new(600.0, 400.0, 750.0, 520.0))), "over B, {target}"); + } +}