diff --git a/crates/engine/src/canvas/tools.rs b/crates/engine/src/canvas/tools.rs index f876885ef..86418b814 100644 --- a/crates/engine/src/canvas/tools.rs +++ b/crates/engine/src/canvas/tools.rs @@ -431,6 +431,9 @@ pub(crate) enum Gesture { additive: bool, base: Vec, within: Option, + /// A click stepping out of a selected descendant picks its frame. Wait for a real drag + /// before replacing that selection with the marquee's contents. + pending_click: bool, }, Move { start: Point, diff --git a/crates/engine/src/canvas/tools/drag.rs b/crates/engine/src/canvas/tools/drag.rs index 2208f74ee..b41613f97 100644 --- a/crates/engine/src/canvas/tools/drag.rs +++ b/crates/engine/src/canvas/tools/drag.rs @@ -77,8 +77,8 @@ impl Session { } // A frame's name label picks the frame itself. The background of a top-level frame with // layers in it is like the canvas (a press starts a marquee inside it) unless the frame is - // selected; an empty frame (or a widget, or a board object such as a sticky) is picked and - // dragged. + // selected; stepping out from a selected descendant is handled by the marquee below. + // An empty frame (or a widget, or a board object such as a sticky) is picked and dragged. let board_background = |leaf: NodeId| { !sel.contains(&leaf) && doc.node(leaf).is_some_and(|n| { @@ -86,11 +86,8 @@ impl Session { }) }; let label = label_at(&doc, page, &self.tool.labels, ev.p()); - let target = label.or_else(|| { - self.hit(&doc, page, ev.p(), ev.tol() * 2.0) - .filter(|leaf| ev.cmd || !board_background(*leaf)) - .map(|leaf| select_target(&doc, leaf, &sel, ev.cmd)) - }); + let hit = label.or_else(|| self.hit(&doc, page, ev.p(), ev.tol() * 2.0)); + let target = label.or_else(|| hit.filter(|leaf| ev.cmd || !board_background(*leaf)).map(|leaf| select_target(&doc, leaf, &sel, ev.cmd))); // A press inside the selection's box that reaches nothing above it (an unfilled shape's // inside, the frame or canvas behind it, or a layer stacked below the selected layer the // press is in) drags the selection. In a gap between selected layers, what's there is hit. @@ -172,10 +169,22 @@ impl Session { } None => { let within = self.marquee_within(ev.p())?; - if !ev.shift { + // Clicking the background of the frame containing the current selection steps + // out to that frame. Keep the marquee gesture (without a Move interaction), so + // dragging here still selects its contents and cannot leave a stale snapshot. + let parent = hit.filter(|f| !ev.shift && !ev.cmd && within == Some(*f) && sel.iter().any(|s| doc.is_ancestor(*f, *s))); + if let Some(f) = parent { + self.set_selection(vec![f]); + } else if !ev.shift { self.set_selection(vec![]); } - self.tool.gesture = Gesture::Marquee { start: ev.p(), additive: ev.shift, base: if ev.shift { sel } else { vec![] }, within }; + self.tool.gesture = Gesture::Marquee { + start: ev.p(), + additive: ev.shift, + base: if ev.shift { sel } else { vec![] }, + within, + pending_click: parent.is_some(), + }; } } Ok(()) @@ -196,9 +205,18 @@ impl Session { it.label = if ev.alt { "Duplicate" } else { "Move" }.into(); } } + if let Gesture::Marquee { start, pending_click, .. } = &mut g + && *pending_click + { + if within_slop(ev, *start) { + self.tool.gesture = g; + return Ok(()); + } + *pending_click = false; + } let res = match &g { Gesture::None => return self.hover(ev), - Gesture::Marquee { start, additive, base, within } => { + Gesture::Marquee { start, additive, base, within, .. } => { let r = Rect::from_points(*start, ev.p()); self.tool.overlay.marquee = Some([r.x0, r.y0, r.x1, r.y1]); // Cmd/Ctrl: the nested layers it touches. diff --git a/crates/engine/src/selection_behaviour_tests.rs b/crates/engine/src/selection_behaviour_tests.rs index 2e447c5de..0499996ff 100644 --- a/crates/engine/src/selection_behaviour_tests.rs +++ b/crates/engine/src/selection_behaviour_tests.rs @@ -108,6 +108,42 @@ fn sorted(mut v: Vec) -> Vec { v } +#[test] +fn clicking_a_device_background_after_its_nested_button_selects_the_device() { + let mut s = Session::new(); + let device = id(&ex(&mut s, "frame.create", json!({"x": 132, "y": -148, "width": 440, "height": 956}))); + let button = id(&ex(&mut s, "frame.create", json!({"parent": device, "x": 155, "y": 30, "width": 130, "height": 34}))); + rect(&mut s, 10.0, 5.0, Some(button)); + ex(&mut s, "layout.add", json!({"ids": [button], "direction": "horizontal"})); + ex(&mut s, "select.set", json!({"ids": [button]})); + let before = s.doc().unwrap().clone(); + let history = s.undo_depth(); + s.pointer(&ev(PointerKind::Down, 352.0, 330.0, NONE)).unwrap(); + assert_eq!(s.selection(), vec![device], "the parent is selected on press"); + // Small pointer jitter must not turn the click into a zero-sized marquee. + s.pointer(&ev(PointerKind::Move, 353.0, 330.0, NONE)).unwrap(); + s.pointer(&ev(PointerKind::Up, 353.0, 330.0, NONE)).unwrap(); + s.pointer(&ev(PointerKind::Hover, 353.0, 330.0, NONE)).unwrap(); + assert_eq!(s.selection(), vec![device], "the parent stays selected after release and hover"); + assert_eq!(*s.doc().unwrap(), before); + assert_eq!(s.undo_depth(), history, "selection adds no edit history"); +} + +#[test] +fn dragging_a_device_background_after_its_child_still_marquees_and_closes_cleanly() { + let mut s = Session::new(); + let k = scene(&mut s); + ex(&mut s, "select.set", json!({"ids": [k.d]})); + drag(&mut s, (390.0, 290.0), (190.0, 10.0), NONE); + assert_eq!(s.selection(), vec![k.n], "a background drag selects the nested frame"); + assert!(s.ds().unwrap().interaction.is_none(), "a marquee leaves no move snapshot behind"); + ex(&mut s, "node.set", json!({"id": k.n, "props": {"opacity": 0.5}})); + click(&mut s, 30.0, 130.0, NONE); + assert_eq!(s.doc().unwrap().node(k.n).unwrap().opacity, 0.5, "the next click keeps the edit"); + ex(&mut s, "edit.undo", json!({})); + assert_eq!(s.doc().unwrap().node(k.n).unwrap().opacity, 1.0, "the edit retains its undo step"); +} + #[test] fn clicking_selects_by_the_documented_rules() { let mut s = Session::new(); @@ -130,6 +166,9 @@ fn clicking_selects_by_the_documented_rules() { ((520.0, 120.0), NONE, vec![], vec![k.r2], "a locked layer can't be selected on the canvas: the click goes to what is under it"), ((520.0, 220.0), NONE, vec![k.r], vec![], "a hidden layer can't be selected on the canvas (empty canvas deselects)"), ((390.0, 290.0), NONE, vec![k.r], vec![], "the background of a top-level frame with layers is like the canvas: a click deselects"), + ((390.0, 290.0), NONE, vec![k.c], vec![k.f], "clicking the containing frame's background steps out from its child"), + ((390.0, 290.0), NONE, vec![k.d], vec![k.f], "stepping out works from a deeper descendant too"), + ((390.0, 290.0), NONE, vec![], vec![], "without a selected descendant the background remains canvas-like"), ((390.0, 290.0), NONE, vec![k.f], vec![k.f], "a selected top-level frame stays selected when its background is clicked"), ((390.0, 290.0), CMD, vec![], vec![k.f], "silent: Cmd/Ctrl-click on a frame's background selects the frame (the deepest layer there)"), ((640.0, 40.0), NONE, vec![], vec![k.empty], "an empty top-level frame is selected by a click on it"),