Merge pull request #2453 from storytold/carry/2330

M8: a Shift-click range in the Layers panel keeps a frame, not its covered children, as in Figma
This commit is contained in:
BFlat
2026-11-19 01:19:30 +07:00
committed by GitHub
4 changed files with 113 additions and 4 deletions
@@ -951,6 +951,64 @@ fn search_fields_keep_their_text_in_the_middle() {
assert!(off < 1.0, "Search assets is {off} points off the middle");
}
#[test]
fn a_shift_range_across_frames_keeps_a_frame_and_not_its_covered_children() {
let mut s = Session::new();
let frame1 = id(s.execute("frame.create", &json!({"x": 0, "y": 0, "width": 100, "height": 100, "name": "Frame 1"})).unwrap());
let rect2 = rect(&mut s, "Rect 2", 0.0, Some(frame1));
let rect3 = rect(&mut s, "Rect 3", 30.0, Some(frame1));
let frame2 = id(s.execute("frame.create", &json!({"x": 200, "y": 0, "width": 100, "height": 100, "name": "Frame 2"})).unwrap());
let rect4 = rect(&mut s, "Rect 4", 0.0, Some(frame2));
s.execute("select.none", &json!({})).unwrap();
let mut u = Ui::new(s);
u.app.ui.expanded.extend([frame1, frame2]);
u.frame(vec![]);
let (row4, row2) = (u.row_at(rect4), u.row_at(rect2));
click(&mut u, row4, Modifiers::NONE);
click(&mut u, row2, SHIFT);
assert_eq!(
sorted(u.app.session.selection()),
sorted(vec![rect4, frame1]),
"the range keeps Frame 1 and drops its covered children {rect3:?} and {rect2:?}"
);
}
#[test]
fn a_second_shift_click_after_a_reverse_cross_frame_range_extends_the_range() {
let mut s = Session::new();
let frame1 = id(s.execute("frame.create", &json!({"x": 0, "y": 0, "width": 100, "height": 100, "name": "Frame 1"})).unwrap());
let rect2 = rect(&mut s, "Rect 2", 0.0, Some(frame1));
let rect3 = rect(&mut s, "Rect 3", 30.0, Some(frame1));
let frame2 = id(s.execute("frame.create", &json!({"x": 200, "y": 0, "width": 100, "height": 100, "name": "Frame 2"})).unwrap());
let rect4 = rect(&mut s, "Rect 4", 0.0, Some(frame2));
s.execute("select.none", &json!({})).unwrap();
let mut u = Ui::new(s);
u.app.ui.expanded.extend([frame1, frame2]);
u.frame(vec![]);
let (row2, row4, row3) = (u.row_at(rect2), u.row_at(rect4), u.row_at(rect3));
click(&mut u, row2, Modifiers::NONE);
click(&mut u, row4, SHIFT);
assert_eq!(sorted(u.app.session.selection()), sorted(vec![rect4, frame1]));
// Rows run Frame 2, Rect 4, Frame 1, Rect 3, Rect 2: the range from Rect 2 to Rect 3 is those two.
click(&mut u, row3, SHIFT);
assert_eq!(sorted(u.app.session.selection()), sorted(vec![rect3, rect2]), "the range still runs from Rect 2, not a toggle");
}
#[test]
fn a_shift_range_inside_one_frame_is_unchanged() {
let mut s = Session::new();
let frame = id(s.execute("frame.create", &json!({"x": 0, "y": 0, "width": 100, "height": 100, "name": "F"})).unwrap());
let kids = [0.0, 30.0, 60.0].map(|x| rect(&mut s, "k", x, Some(frame)));
s.execute("select.none", &json!({})).unwrap();
let mut u = Ui::new(s);
u.app.ui.expanded.insert(frame);
u.frame(vec![]);
let (first, last) = (u.row_at(kids[0]), u.row_at(kids[2]));
click(&mut u, first, Modifiers::NONE);
click(&mut u, last, SHIFT);
assert_eq!(sorted(u.app.session.selection()), sorted(kids.to_vec()));
}
/// The centre of `id`'s icon, a top-level row: the row's left edge is the first point from the
/// left at which the pointer is over it, and the icon's centre is 8 (margin) + 4 + 22 points
/// further right.
+36
View File
@@ -76,6 +76,42 @@ fn up_and_down_walk_the_rows_and_shift_extends() {
assert_eq!(doc.node(c).unwrap().transform.translation().x, 200.0);
}
#[test]
fn a_keyboard_shift_range_keeps_an_anchor_covered_by_a_selected_frame() {
let mut s = Session::new();
let frame1 = id(s.execute("frame.create", &json!({"x": 0, "y": 0, "width": 100, "height": 100, "name": "Frame 1"})).unwrap());
let mk = |s: &mut Session, name: &str, parent: NodeId| {
id(s.execute("shape.rectangle", &json!({"x": 0, "y": 0, "width": 20, "height": 20, "parent": parent, "props": {"name": name}})).unwrap())
};
let rect2 = mk(&mut s, "Rect 2", frame1);
let rect3 = mk(&mut s, "Rect 3", frame1);
let frame2 = id(s.execute("frame.create", &json!({"x": 200, "y": 0, "width": 100, "height": 100, "name": "Frame 2"})).unwrap());
let rect4 = mk(&mut s, "Rect 4", frame2);
s.execute("select.none", &json!({})).unwrap();
let mut u = Ui::new(s);
u.app.ui.expanded.extend([frame1, frame2]);
u.frame(vec![]);
click_row(&mut u, rect2);
let p4 = u.row_at(rect4);
let shift = Modifiers { shift: true, ..Default::default() };
u.frame_with(vec![Event::PointerMoved(p4)], shift);
let button = |pressed| Event::PointerButton { pos: p4, button: egui::PointerButton::Primary, pressed, modifiers: shift };
u.frame_with(vec![button(true)], shift);
u.frame_with(vec![button(false)], shift);
let mut got = sel(&u);
got.sort();
let mut want = vec![rect4, frame1];
want.sort();
assert_eq!(got, want, "the pointer range keeps the frame");
// Rows: Frame 2, Rect 4, Frame 1, Rect 3, Rect 2. Shift+Down runs from Rect 2 (the anchor).
key(&mut u, Key::ArrowDown, true);
let mut got = sel(&u);
got.sort();
let mut want = vec![rect3, rect2];
want.sort();
assert_eq!(got, want, "the keyboard range extends from Rect 2's row, not from the current row");
}
#[test]
fn left_and_right_open_close_and_move_between_levels() {
let (mut u, _, frame, child, _) = setup();
+16 -2
View File
@@ -637,6 +637,14 @@ fn matching(doc: &Document, id: NodeId, q: &str, out: &mut Vec<Row>) {
}
}
/// The range anchor while it is still part of the selection: selected itself, or covered by a
/// selected frame (a range across parents selects the frame and not its children).
fn live_anchor(app: &FigApp, sel: &[NodeId]) -> Option<NodeId> {
let a = app.ui.layer_anchor?;
let covered = || app.session.ds().is_ok_and(|ds| ds.doc.ancestors(a).iter().any(|p| sel.contains(p)));
(sel.contains(&a) || covered()).then_some(a)
}
/// Arrow keys while the layers panel has the keyboard: Up/Down select the row above or below
/// (Shift extends the range from the anchor), Left collapses a row or goes to its parent, Right
/// expands a row or goes to its first child. Returns whether the key was used.
@@ -669,7 +677,7 @@ pub(crate) fn key_nav(app: &mut FigApp, key: egui::Key, shift: bool) -> bool {
let Some((j, next)) = j.and_then(|j| rows.get(j).map(|r| (j, r.id))) else { return true };
if shift {
// The rows from the anchor to the new one, with the moving end last.
let anchor = app.ui.layer_anchor.filter(|a| sel.contains(a)).unwrap_or(cur);
let anchor = live_anchor(app, &sel).unwrap_or(cur);
let a = rows.iter().position(|r| r.id == anchor).unwrap_or(i);
let ids: Vec<NodeId> = if a <= j {
rows.get(a..=j).unwrap_or_default().iter().map(|r| r.id).collect()
@@ -909,7 +917,8 @@ fn select_clicked(app: &mut FigApp, rows: &[Row], sel: &[NodeId], id: NodeId, m:
app.ui.pages_focus = None;
let order: Vec<NodeId> = rows.iter().map(|r| r.id).collect();
let pos = |x: NodeId| order.iter().position(|o| *o == x);
let anchor = app.ui.layer_anchor.filter(|a| sel.contains(a)).and_then(pos);
let live = live_anchor(app, sel);
let anchor = live.and_then(pos);
if m.shift
&& let (Some(ai), Some(bi)) = (anchor, pos(id))
{
@@ -921,6 +930,11 @@ fn select_clicked(app: &mut FigApp, rows: &[Row], sel: &[NodeId], id: NodeId, m:
s.push(*x);
}
}
// A layer under a selected ancestor is covered by it, not a member of its own.
if let Ok(ds) = app.session.ds() {
let all = s.clone();
s.retain(|x| !ds.doc.ancestors(*x).iter().any(|a| all.contains(a)));
}
app.run("select.set", json!({"ids": s}));
} else if m.shift || m.command {
let mut s = sel.to_vec();
+3 -2
View File
File diff suppressed because one or more lines are too long