Files
figcraft/crates/engine/src/selection_behaviour_tests.rs
T
Oleksii Sukholita 2e7a168a91 M1: a Cmd/Ctrl press through a selected unfilled frame onto nothing starts a marquee
Where the deep pick under a selected unfilled nested frame found nothing at all (a frame in a
group on the page, with nothing under it), the press still fell back to dragging the selection,
so it moved the frame, while the same press with nothing selected started a marquee. A press that
reaches nothing now counts as being on the selection only for a selected layer it wasn't passed
through. Plain presses keep the fallback. Raised in review.

`a_cmd_press_through_a_selected_unfilled_frame_onto_nothing_starts_a_marquee` fails without the
change: the frame moves.
2026-11-21 14:56:42 +01:00

778 lines
41 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! Selection behaviour suite, written from the public documentation of design tools: each rule
//! is a table row with the selection it implies and a one-line derivation. Where the
//! documentation is silent the row says so and pins today's behaviour.
// The tables read best as tuples.
#![allow(clippy::type_complexity)]
use serde_json::{Value, json};
use crate::Session;
use crate::params::id_of;
use crate::tools::{PointerEvent, PointerKind, handle_at};
use figcraft_doc::NodeId;
use figcraft_geom::Point;
fn ex(s: &mut Session, cmd: &str, p: Value) -> Value {
s.execute(cmd, &p).unwrap_or_else(|e| panic!("{cmd}: {e}"))
}
fn id(v: &Value) -> NodeId {
id_of(&v["id"]).unwrap()
}
fn rect(s: &mut Session, x: f64, y: f64, parent: Option<NodeId>) -> NodeId {
let mut p = json!({"x": x, "y": y, "width": 40, "height": 40});
if let Some(parent) = parent {
p["parent"] = json!(parent);
}
id(&ex(s, "shape.rectangle", p))
}
/// The layers of the scene, by name.
#[allow(dead_code)]
struct Scene {
/// Top-level frame (0,0 400×300) holding: group `g` of `a` (20,20) and `b` (80,20); `c`
/// (20,120); nested frame `n` (200,20 150×150) with `d` (220,40); boolean `u` of two
/// rectangles at (20,200) and (40,200).
f: NodeId,
g: NodeId,
a: NodeId,
b: NodeId,
c: NodeId,
n: NodeId,
d: NodeId,
u: NodeId,
/// On the page: `r` (500,0); `r2` (500,100) under the locked `lk` (same place); the hidden
/// `hid` (500,200); the empty frame `empty` (600,0 80×80).
r: NodeId,
r2: NodeId,
lk: NodeId,
hid: NodeId,
empty: NodeId,
}
fn scene(s: &mut Session) -> Scene {
let f = id(&ex(s, "frame.create", json!({"x": 0, "y": 0, "width": 400, "height": 300})));
let a = rect(s, 20.0, 20.0, Some(f));
let b = rect(s, 80.0, 20.0, Some(f));
let g = id(&ex(s, "object.group", json!({"ids": [a, b]})));
let c = rect(s, 20.0, 120.0, Some(f));
let n = id(&ex(s, "frame.create", json!({"x": 200, "y": 20, "width": 150, "height": 150, "parent": f})));
// Positions are in the parent's space.
let d = rect(s, 20.0, 20.0, Some(n));
let e1 = rect(s, 20.0, 200.0, Some(f));
let e2 = rect(s, 40.0, 200.0, Some(f));
let u = id(&ex(s, "bool.union", json!({"ids": [e1, e2]})));
let r = rect(s, 500.0, 0.0, None);
let r2 = rect(s, 500.0, 100.0, None);
let lk = rect(s, 500.0, 100.0, None);
ex(s, "object.toggleLock", json!({"ids": [lk], "locked": true}));
let hid = rect(s, 500.0, 200.0, None);
ex(s, "object.toggleVisible", json!({"ids": [hid], "visible": false}));
let empty = id(&ex(s, "frame.create", json!({"x": 600, "y": 0, "width": 80, "height": 80})));
ex(s, "select.none", json!({}));
Scene { f, g, a, b, c, n, d, u, r, r2, lk, hid, empty }
}
#[derive(Clone, Copy, Default)]
struct Mods {
shift: bool,
cmd: bool,
}
const NONE: Mods = Mods { shift: false, cmd: false };
const SHIFT: Mods = Mods { shift: true, cmd: false };
const CMD: Mods = Mods { shift: false, cmd: true };
fn ev(kind: PointerKind, x: f64, y: f64, m: Mods) -> PointerEvent {
let mut e = PointerEvent::at(kind, x, y, 1.0);
(e.shift, e.cmd) = (m.shift, m.cmd);
e
}
/// A press and release without moving.
fn click(s: &mut Session, x: f64, y: f64, m: Mods) {
s.pointer(&ev(PointerKind::Down, x, y, m)).unwrap();
s.pointer(&ev(PointerKind::Up, x, y, m)).unwrap();
}
fn drag(s: &mut Session, a: (f64, f64), b: (f64, f64), m: Mods) {
s.pointer(&ev(PointerKind::Down, a.0, a.1, m)).unwrap();
s.pointer(&ev(PointerKind::Move, b.0, b.1, m)).unwrap();
s.pointer(&ev(PointerKind::Up, b.0, b.1, m)).unwrap();
}
fn sorted(mut v: Vec<NodeId>) -> Vec<NodeId> {
v.sort();
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();
let k = scene(&mut s);
// (point, modifiers, selection before, selection after, derivation)
let rows: Vec<((f64, f64), Mods, Vec<NodeId>, Vec<NodeId>, &str)> = vec![
((520.0, 20.0), NONE, vec![], vec![k.r], "a click selects the layer under the pointer"),
((30.0, 30.0), NONE, vec![], vec![k.g], "a group is selected as a whole"),
((30.0, 130.0), NONE, vec![], vec![k.c], "a layer in a top-level frame is selected directly (frames hold their layers like the canvas)"),
((230.0, 50.0), NONE, vec![], vec![k.n], "silent: a nested frame selects as a whole, like a group (pinned)"),
((30.0, 210.0), NONE, vec![], vec![k.u], "a boolean group is selected as a whole"),
((30.0, 30.0), CMD, vec![], vec![k.a], "Cmd/Ctrl-click selects the deepest layer"),
((230.0, 50.0), CMD, vec![], vec![k.d], "Cmd/Ctrl-click reaches into nested frames"),
((30.0, 210.0), CMD, vec![], vec![sel_operand(&s, k.u, 30.0)], "Cmd/Ctrl-click reaches a boolean's operand"),
((90.0, 30.0), NONE, vec![k.a], vec![k.b], "with a layer selected, its siblings select at its level"),
((520.0, 20.0), SHIFT, vec![k.c], vec![k.c, k.r], "Shift-click adds to the selection"),
((30.0, 130.0), SHIFT, vec![k.c, k.r], vec![k.r], "Shift-click on a selected layer removes it"),
((520.0, 20.0), NONE, vec![k.r, k.empty], vec![k.r], "a click on one of several selected layers selects just it"),
((520.0, 20.0), CMD, vec![k.r, k.empty], vec![k.r, k.empty], "silent: Cmd/Ctrl-click on a selected layer keeps the whole selection (pinned)"),
((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"),
];
for (p, m, before, after, why) in rows {
ex(&mut s, "select.set", json!({"ids": before}));
click(&mut s, p.0, p.1, m);
assert_eq!(sorted(s.selection()), sorted(after), "{why}");
}
}
/// A screen with a device chrome overlay: the top-level frame `screen` (0,0 390×844) holds a text
/// at (40,200), the rectangle `content` (40,300) and, above them, `inst`, an unfilled instance of
/// a component the size of the screen with a status bar (`status`) at the top and a home
/// indicator at the bottom, nothing in between.
struct Chrome {
screen: NodeId,
text: NodeId,
content: NodeId,
inst: NodeId,
status: NodeId,
}
fn device_chrome(s: &mut Session) -> Chrome {
let screen = id(&ex(s, "frame.create", json!({"x": 0, "y": 0, "width": 390, "height": 844})));
let text = id(&ex(s, "text.create", json!({"text": "Hello", "x": 40, "y": 200, "parent": screen})));
let content = rect(s, 40.0, 300.0, Some(screen));
let chrome = id(&ex(s, "frame.create", json!({"x": 600, "y": 0, "width": 390, "height": 844})));
ex(s, "shape.rectangle", json!({"x": 0, "y": 0, "width": 390, "height": 47, "parent": chrome}));
ex(s, "shape.rectangle", json!({"x": 128, "y": 830, "width": 134, "height": 5, "parent": chrome}));
ex(s, "node.set", json!({"ids": [chrome], "props": {"fills": []}}));
let comp = id(&ex(s, "component.create", json!({"ids": [chrome]})));
let inst = id(&ex(s, "instance.create", json!({"component": comp, "x": 0, "y": 0, "parent": screen})));
let status = s.doc().unwrap().node(inst).unwrap().children[0];
ex(s, "select.none", json!({}));
Chrome { screen, text, content, inst, status }
}
/// Observed in the reference app with synthetic files: with Cmd/Ctrl held, a frame, component
/// or instance nested in another layer is hit only where it has a visible fill (its stroke and
/// its empty box let the pick through to the layers under it), while a plain click still picks
/// it by its box. A device chrome overlay shows it: a status bar and a home indicator in an
/// unfilled instance the size of the screen, over the screen's content.
#[test]
fn cmd_click_reaches_through_an_unfilled_nested_frame() {
let mut s = Session::new();
let Chrome { screen, content, inst, status, .. } = device_chrome(&mut s);
let unfilled = json!([]);
let filled = json!([{"type": "solid", "color": "#FFFFFF"}]);
let hidden = json!([{"type": "solid", "color": "#FFFFFF", "visible": false}]);
let clear = json!([{"type": "solid", "color": "#FFFFFF", "opacity": 0}]);
// (the instance's fills, point, modifiers, selection after, derivation)
let rows: Vec<(&Value, (f64, f64), Mods, NodeId, &str)> = vec![
(&unfilled, (60.0, 320.0), CMD, content, "Cmd/Ctrl-click reaches the layer under the unfilled instance"),
(&unfilled, (200.0, 600.0), CMD, screen, "with nothing under it there, the top-level frame is picked"),
(&unfilled, (200.0, 20.0), CMD, status, "a layer inside the instance is still the deepest layer"),
(&unfilled, (60.0, 320.0), NONE, inst, "a plain click still picks the instance by its box"),
(&filled, (200.0, 600.0), CMD, inst, "a visible fill makes the instance the deepest layer there"),
(&filled, (60.0, 320.0), CMD, inst, "and hides the layer under it"),
(&hidden, (60.0, 320.0), CMD, content, "a hidden fill paints nothing"),
(&clear, (60.0, 320.0), CMD, content, "a fill at 0% paints nothing"),
];
for (fills, p, m, after, why) in rows {
ex(&mut s, "node.set", json!({"ids": [inst], "props": {"fills": fills}}));
ex(&mut s, "select.none", json!({}));
click(&mut s, p.0, p.1, m);
assert_eq!(s.selection(), vec![after], "{why}");
}
ex(&mut s, "node.set", json!({"ids": [inst], "props": {"fills": []}}));
ex(&mut s, "stroke.add", json!({"ids": [inst]}));
ex(&mut s, "select.none", json!({}));
click(&mut s, 0.5, 600.0, CMD);
assert_eq!(s.selection(), vec![screen], "a frame's stroke doesn't count: the pick goes through it too");
s.pointer(&ev(PointerKind::Hover, 60.0, 320.0, CMD)).unwrap();
assert_eq!(s.ds().unwrap().hover, Some(content), "hover with Cmd/Ctrl held shows what a Cmd/Ctrl-click picks");
s.pointer(&ev(PointerKind::Hover, 60.0, 320.0, NONE)).unwrap();
assert_eq!(s.ds().unwrap().hover, Some(inst), "and without it, what a click picks");
// The instance being selected doesn't stop the pick: the press isn't on it there.
for (p, after, why) in [
((60.0, 320.0), content, "with the instance selected, Cmd/Ctrl-click still reaches the layer under it"),
((200.0, 600.0), screen, "and on an empty spot there, the top-level frame"),
] {
ex(&mut s, "select.set", json!({"ids": [inst]}));
click(&mut s, p.0, p.1, CMD);
assert_eq!(s.selection(), vec![after], "{why}");
}
// A plain nested frame does the same. A frame directly in a section is a board, like a
// top-level frame: it keeps its box.
let card = id(&ex(&mut s, "frame.create", json!({"x": 1000, "y": 0, "width": 300, "height": 300})));
let inside = rect(&mut s, 20.0, 20.0, Some(card));
let veil = id(&ex(&mut s, "frame.create", json!({"x": 0, "y": 0, "width": 300, "height": 300, "parent": card})));
ex(&mut s, "node.set", json!({"ids": [veil], "props": {"fills": []}}));
let sec = id(&ex(&mut s, "node.create", json!({"type": "section", "x": 2000, "y": 0, "width": 400, "height": 400})));
let board = id(&ex(&mut s, "node.create", json!({"type": "frame", "x": 50, "y": 50, "width": 100, "height": 100, "parent": sec})));
ex(&mut s, "node.set", json!({"ids": [board], "props": {"fills": []}}));
for (p, after, why) in [
((1040.0, 40.0), inside, "an unfilled nested frame lets the pick through like an instance"),
((2100.0, 100.0), board, "Cmd/Ctrl-click on the background of a frame in a section picks the frame"),
] {
ex(&mut s, "select.none", json!({}));
click(&mut s, p.0, p.1, CMD);
assert_eq!(s.selection(), vec![after], "{why}");
}
}
/// With no board under it (a frame in a group on the page), a Cmd/Ctrl press in an unfilled
/// frame's empty part reaches nothing, whether the frame is selected or not: it starts a marquee
/// there and doesn't move the frame.
#[test]
fn a_cmd_press_through_a_selected_unfilled_frame_onto_nothing_starts_a_marquee() {
let gesture = |selected: bool| {
let mut s = Session::new();
let veil = id(&ex(&mut s, "frame.create", json!({"x": 0, "y": 0, "width": 200, "height": 200})));
ex(&mut s, "node.set", json!({"ids": [veil], "props": {"fills": []}}));
let dot = rect(&mut s, 300.0, 0.0, None);
ex(&mut s, "object.group", json!({"ids": [veil, dot]}));
let before: Vec<NodeId> = if selected { vec![veil] } else { vec![] };
ex(&mut s, "select.set", json!({"ids": before}));
drag(&mut s, (100.0, 100.0), (150.0, 150.0), CMD);
let at = s.doc().unwrap().world_bounds(veil).unwrap();
(s.selection(), (at.x0, at.y0))
};
let (unselected, at) = gesture(false);
assert_eq!(at, (0.0, 0.0), "unselected, the press starts a marquee");
let (selected, at) = gesture(true);
assert_eq!(at, (0.0, 0.0), "selected, the frame doesn't move either: the press isn't on it");
assert_eq!(selected, unselected, "and the marquee picks the same layers");
}
/// A click and a double-click with `m` held on the middle of the layer `pick` chooses, in a fresh
/// device chrome scene.
fn double_click_on(m: Mods, pick: fn(&Chrome) -> NodeId) -> (Session, Chrome) {
let mut s = Session::new();
let k = device_chrome(&mut s);
let p = s.doc().unwrap().world_bounds(pick(&k)).unwrap().center();
click(&mut s, p.x, p.y, m);
s.pointer(&ev(PointerKind::DoubleClick, p.x, p.y, m)).unwrap();
(s, k)
}
/// Observed in the reference app with synthetic files: Cmd/Ctrl + double-click reaches a layer
/// under an unfilled overlay like a Cmd/Ctrl-click and edits it at once (a text goes into text
/// editing, a shape into vector edit mode). A plain double-click picks the overlay, which is
/// caught by its box and has nothing inside there to enter.
#[test]
fn cmd_double_click_edits_the_layer_under_an_unfilled_instance() {
let (s, k) = double_click_on(CMD, |k| k.text);
assert_eq!(s.ds().unwrap().text_edit.as_ref().map(|t| t.id), Some(k.text), "Cmd/Ctrl + double-click on a text edits it");
let (s, k) = double_click_on(CMD, |k| k.content);
assert_eq!(s.ds().unwrap().vector_edit.as_ref().map(|v| v.id), Some(k.content), "on a shape, it edits the shape's points");
let (s, k) = double_click_on(NONE, |k| k.text);
assert_eq!(s.selection(), vec![k.inst], "a plain double-click picks the overlay");
assert!(s.ds().unwrap().text_edit.is_none(), "and edits nothing");
}
/// The operand of boolean `u` under x (its children are the two rectangles).
fn sel_operand(s: &Session, u: NodeId, x: f64) -> NodeId {
let doc = s.doc().unwrap();
let kids = &doc.node(u).unwrap().children;
// The later child paints on top: (40,200) covers x ≥ 40, else the first.
if x >= 40.0 { kids[1] } else { kids[0] }
}
#[test]
fn double_click_enters_one_level() {
let mut s = Session::new();
let k = scene(&mut s);
let comp_child = rect(&mut s, 800.0, 0.0, None);
let comp = id(&ex(&mut s, "component.create", json!({"ids": [comp_child]})));
let inst = id(&ex(&mut s, "instance.create", json!({"component": comp, "x": 300, "y": 200, "parent": k.f})));
let inst_child = s.doc().unwrap().node(inst).unwrap().children[0];
// (point, selection before, selection after, derivation)
let rows: Vec<((f64, f64), Vec<NodeId>, Vec<NodeId>, &str)> = vec![
((30.0, 30.0), vec![k.g], vec![k.a], "double-click on a group selects the layer inside it"),
((230.0, 50.0), vec![k.n], vec![k.d], "double-click on a nested frame selects the layer inside it"),
((30.0, 210.0), vec![k.u], vec![sel_operand(&s, k.u, 30.0)], "double-click on a boolean group selects its operand"),
((320.0, 220.0), vec![inst], vec![inst_child], "double-click on an instance selects the layer inside it"),
((30.0, 30.0), vec![], vec![k.a], "a double-click's first click selects the group, the second enters it"),
];
for (p, before, after, why) in rows {
ex(&mut s, "select.set", json!({"ids": before}));
click(&mut s, p.0, p.1, NONE);
s.pointer(&ev(PointerKind::DoubleClick, p.0, p.1, NONE)).unwrap();
assert_eq!(s.selection(), after, "{why}");
}
// A selected shape: vector edit mode.
ex(&mut s, "select.set", json!({"ids": [k.r]}));
s.pointer(&ev(PointerKind::DoubleClick, 520.0, 20.0, NONE)).unwrap();
assert_eq!(s.ds().unwrap().vector_edit.as_ref().map(|v| v.id), Some(k.r), "double-click on a shape edits its points");
}
#[test]
fn a_double_click_anywhere_on_the_selected_shape_edits_its_points() {
let mut s = Session::new();
let r = id(&ex(&mut s, "shape.rectangle", json!({"x": 0, "y": 0, "width": 100, "height": 100})));
ex(&mut s, "node.set", json!({"ids": [r], "props": {"fills": []}}));
ex(&mut s, "stroke.add", json!({"ids": [r]}));
// Selected by its outline; the empty middle is no hit at all (the shape has no fill).
click(&mut s, 0.0, 50.0, NONE);
assert_eq!(s.selection(), vec![r]);
s.pointer(&ev(PointerKind::DoubleClick, 50.0, 50.0, NONE)).unwrap();
assert_eq!(s.ds().unwrap().vector_edit.as_ref().map(|v| v.id), Some(r), "a double-click on the shape edits its points");
}
#[test]
fn a_double_click_on_an_unselected_shape_edits_it_without_a_selecting_click() {
let mut s = Session::new();
let r = id(&ex(&mut s, "shape.rectangle", json!({"x": 0, "y": 0, "width": 100, "height": 100})));
ex(&mut s, "select.none", json!({}));
// The press and release of the click that selects it, and the double-click, came in one frame:
// only the double-click reached the tool, with nothing selected.
s.pointer(&ev(PointerKind::DoubleClick, 50.0, 50.0, NONE)).unwrap();
assert_eq!(s.ds().unwrap().vector_edit.as_ref().map(|v| v.id), Some(r));
}
#[test]
fn marquee_selects_what_it_touches() {
let mut s = Session::new();
let k = scene(&mut s);
// (from, to, modifiers, selection before, selection after, derivation)
let rows: Vec<((f64, f64), (f64, f64), Mods, Vec<NodeId>, Vec<NodeId>, &str)> = vec![
((480.0, -20.0), (505.0, 10.0), NONE, vec![], vec![k.r], "a marquee selects layers it touches, not only those it contains"),
((480.0, -20.0), (560.0, 260.0), NONE, vec![], vec![k.r, k.r2], "locked and hidden layers aren't selected by a marquee"),
(
(-10.0, -10.0),
(450.0, 40.0),
NONE,
vec![],
vec![k.g, k.n],
"from the canvas, a partly covered top-level frame gives the layers in it the marquee touches (measured); the nested frame comes whole",
),
((-10.0, -10.0), (450.0, 310.0), NONE, vec![], vec![k.f], "a marquee covering a top-level frame selects the frame"),
(
(390.0, 290.0),
(25.0, 125.0),
NONE,
vec![],
vec![k.c, k.n, k.u],
"starting on a top-level frame's background selects the layers in it the marquee touches",
),
(
(450.0, 290.0),
(25.0, 25.0),
CMD,
vec![],
vec![k.a, k.b, k.c, k.d, k.u],
"silent: Cmd/Ctrl-marquee selects the innermost layers it touches (pinned)",
),
((480.0, -20.0), (505.0, 10.0), SHIFT, vec![k.c], vec![k.c, k.r], "Shift-marquee adds to the selection"),
((480.0, -20.0), (505.0, 10.0), SHIFT, vec![k.c, k.r], vec![k.c], "Shift-marquee takes out what was selected and is touched again"),
];
for (a, b, m, before, after, why) in rows {
ex(&mut s, "select.set", json!({"ids": before}));
drag(&mut s, a, b, m);
assert_eq!(sorted(s.selection()), sorted(after), "{why}");
}
}
#[test]
fn keyboard_selection() {
let mut s = Session::new();
let k = scene(&mut s);
let page_layers = |s: &Session| s.doc().unwrap().node(s.page().unwrap()).unwrap().children.clone();
let visible_unlocked: Vec<NodeId> = page_layers(&s).into_iter().filter(|i| ![k.lk, k.hid].contains(i)).collect();
// (command, selection before, selection after, derivation)
let rows: Vec<(&str, Vec<NodeId>, Vec<NodeId>, &str)> = vec![
("select.children", vec![k.g], vec![k.a, k.b], "Enter selects the children"),
("select.parent", vec![k.a], vec![k.g], "Shift+Enter selects the parent"),
("select.parent", vec![k.f], vec![k.f], "silent: Shift+Enter on a top-level layer keeps it (pinned)"),
("select.next", vec![k.b], vec![k.a], "Tab selects the next layer down the layers list"),
("select.previous", vec![k.a], vec![k.b], "Shift+Tab selects the previous one"),
("select.all", vec![k.a], vec![k.a, k.b], "Select all selects the selection's siblings"),
("select.all", vec![], visible_unlocked.clone(), "with nothing selected, all layers on the page (hidden and locked ones can't be selected)"),
(
"select.inverse",
vec![k.r],
visible_unlocked.iter().copied().filter(|i| *i != k.r).collect(),
"Select inverse: the other layers Select all would select",
),
];
for (cmd, before, after, why) in rows {
ex(&mut s, "select.set", json!({"ids": before}));
ex(&mut s, cmd, json!({}));
assert_eq!(sorted(s.selection()), sorted(after), "{why}");
}
// Esc clears the selection at once, whatever the nesting (measured in Figma).
ex(&mut s, "select.set", json!({"ids": [k.a]}));
s.escape().unwrap();
assert_eq!(s.selection(), Vec::<NodeId>::new(), "Esc from a nested layer selects nothing, not the parent");
s.escape().unwrap();
assert_eq!(s.selection(), Vec::<NodeId>::new());
// A top-level layer, and several layers, nested or not: one Esc selects nothing.
let cases: Vec<(Vec<NodeId>, &str)> = vec![
(vec![k.f], "a top-level frame"),
(vec![k.r], "a top-level layer"),
(vec![k.a, k.b], "two layers in one group (not their group)"),
(vec![k.a, k.d], "layers in different parents (not the parents)"),
(vec![k.a, k.r], "a nested layer and a top-level one"),
];
for (ids, why) in cases {
ex(&mut s, "select.set", json!({"ids": ids}));
assert_eq!(sorted(s.selection()), sorted(ids.clone()), "{why}: selected");
s.escape().unwrap();
assert_eq!(s.selection(), Vec::<NodeId>::new(), "Esc on {why} selects nothing");
}
}
#[test]
fn locked_layers_selected_in_the_layers_panel_stay_put() {
let mut s = Session::new();
let k = scene(&mut s);
// Selectable from the layers panel (select.set), but the canvas offers no handles and a drag
// that starts on another selected layer leaves it where it is.
ex(&mut s, "select.set", json!({"ids": [k.lk]}));
let doc = s.doc().unwrap().clone();
let corner = Point::new(540.0, 140.0);
assert_eq!(handle_at(&doc, &[k.lk], corner, 1.0), None, "a locked layer has no resize handles");
assert!(handle_at(&doc, &[k.r2], corner, 1.0).is_some(), "the same box unlocked has");
ex(&mut s, "select.set", json!({"ids": [k.lk, k.r]}));
drag(&mut s, (520.0, 20.0), (530.0, 30.0), NONE);
let doc = s.doc().unwrap();
assert_eq!(doc.world_bounds(k.r).unwrap().origin(), Point::new(510.0, 10.0), "the unlocked layer moves");
assert_eq!(doc.world_bounds(k.lk).unwrap().origin(), Point::new(500.0, 100.0), "the locked one doesn't");
// Layers inside a locked layer are locked too.
ex(&mut s, "object.toggleLock", json!({"ids": [k.g], "locked": true}));
assert_eq!(handle_at(s.doc().unwrap(), &[k.a], Point::new(60.0, 60.0), 1.0), None);
}
#[test]
fn dragging_one_of_several_selected_layers_moves_them_all() {
let mut s = Session::new();
let k = scene(&mut s);
let at = |s: &Session, i: NodeId| s.doc().unwrap().world_bounds(i).unwrap().origin();
ex(&mut s, "select.set", json!({"ids": [k.r, k.empty]}));
drag(&mut s, (520.0, 20.0), (540.0, 20.0), NONE);
assert_eq!(sorted(s.selection()), sorted(vec![k.r, k.empty]), "a drag on one of several selected layers keeps them all selected");
assert_eq!(at(&s, k.r), Point::new(520.0, 0.0), "the dragged layer moves");
assert_eq!(at(&s, k.empty), Point::new(620.0, 0.0), "and so do the others");
}
#[test]
fn shift_drag_moves_the_selection() {
let mut s = Session::new();
let k = scene(&mut s);
let at = |s: &Session, i: NodeId| s.doc().unwrap().world_bounds(i).unwrap().origin();
let home = (Point::new(500.0, 0.0), Point::new(600.0, 0.0));
// (selection before, Shift-drag to (from r at 520,20), selection after, where r and empty end up, derivation)
let rows: Vec<(Vec<NodeId>, (f64, f64), Vec<NodeId>, (Point, Point), &str)> = vec![
(
vec![k.r],
(560.0, 22.0),
vec![k.r],
(Point::new(540.0, 0.0), Point::new(600.0, 0.0)),
"Shift-drag on the selected layer moves it along one axis and keeps it selected",
),
(
vec![k.r, k.empty],
(560.0, 22.0),
vec![k.r, k.empty],
(Point::new(540.0, 0.0), Point::new(640.0, 0.0)),
"Shift-drag on one of several selected layers moves them all",
),
(
vec![k.empty],
(560.0, 22.0),
vec![k.r, k.empty],
(Point::new(540.0, 0.0), Point::new(640.0, 0.0)),
"Shift-drag on an unselected layer adds it and moves the selection",
),
(vec![k.r, k.empty], (521.0, 21.0), vec![k.empty], home, "a Shift-press that wobbles under the drag threshold is a click: the layer leaves"),
];
for (before, to, after, end, why) in rows {
ex(&mut s, "select.set", json!({"ids": before}));
drag(&mut s, (520.0, 20.0), to, SHIFT);
assert_eq!(sorted(s.selection()), sorted(after), "{why}");
assert_eq!((at(&s, k.r), at(&s, k.empty)), end, "{why}");
if end != home {
assert!(s.undo().unwrap(), "{why}: one undo step");
assert_eq!((at(&s, k.r), at(&s, k.empty)), home, "{why}: undone");
}
}
// Shift let go before the button: still a Shift-click.
ex(&mut s, "select.set", json!({"ids": [k.r, k.empty]}));
s.pointer(&ev(PointerKind::Down, 520.0, 20.0, SHIFT)).unwrap();
s.pointer(&ev(PointerKind::Up, 520.0, 20.0, NONE)).unwrap();
assert_eq!(s.selection(), vec![k.empty], "Shift let go before the button still takes the layer out");
}
#[test]
fn a_drag_inside_the_selection_box_moves_it_where_nothing_is_hit() {
let mut s = Session::new();
let k = scene(&mut s);
for i in [k.r, k.c] {
ex(&mut s, "node.set", json!({"ids": [i], "props": {"fills": [], "strokes": ["#000000"], "strokeWeight": 1}}));
}
let at = |s: &Session, i: NodeId| s.doc().unwrap().world_bounds(i).unwrap().origin();
// (selection before, drag from, selection after, the layers that move by (10, 10), derivation)
let rows: Vec<(Vec<NodeId>, (f64, f64), Vec<NodeId>, Vec<NodeId>, &str)> = vec![
(vec![k.r], (520.0, 20.0), vec![k.r], vec![k.r], "a selected unfilled shape drags from its inside, not only its stroke"),
(vec![k.c], (40.0, 140.0), vec![k.c], vec![k.c], "the same in a top-level frame, whose background is under the inside"),
(vec![k.r, k.empty], (570.0, 20.0), vec![k.r, k.empty], vec![k.r, k.empty], "a gap inside a multiple selection's box drags them all"),
(vec![], (520.0, 20.0), vec![k.r], vec![], "an unselected unfilled shape is hit on its stroke only: the drag is a marquee, which touches it"),
];
for (before, from, after, moved, why) in rows {
let home: Vec<Point> = [k.r, k.c, k.empty].iter().map(|i| at(&s, *i)).collect();
ex(&mut s, "select.set", json!({"ids": before}));
drag(&mut s, from, (from.0 + 10.0, from.1 + 10.0), NONE);
assert_eq!(sorted(s.selection()), sorted(after), "{why}");
for (i, h) in [k.r, k.c, k.empty].iter().zip(&home) {
let d = if moved.contains(i) { 10.0 } else { 0.0 };
assert_eq!(at(&s, *i), Point::new(h.x + d, h.y + d), "{why}");
}
if !moved.is_empty() {
assert!(s.undo().unwrap(), "{why}: one undo step");
}
}
// A turned layer's box is turned with it: the corners of its page-aligned bounds are outside.
ex(&mut s, "node.set", json!({"ids": [k.r], "props": {"rotation": 45}}));
let b = s.doc().unwrap().world_bounds(k.r).unwrap();
ex(&mut s, "select.set", json!({"ids": [k.r]}));
drag(&mut s, (b.x0 + 2.0, b.y0 + 2.0), (b.x0 + 12.0, b.y0 + 12.0), NONE);
assert_eq!(at(&s, k.r), b.origin(), "a press outside a turned layer's box is a marquee: it doesn't move the layer");
ex(&mut s, "select.set", json!({"ids": [k.r]}));
let c = b.center();
drag(&mut s, (c.x, c.y), (c.x + 10.0, c.y + 10.0), NONE);
assert_eq!(s.selection(), vec![k.r], "inside the turned box it drags");
// Pixel snapping rounds the turned box's origin, so the move is about the pointer's.
let d = at(&s, k.r) - b.origin();
assert!((d.x - 10.0).abs() < 2.0 && (d.y - 10.0).abs() < 2.0, "it moves with the pointer: {d:?}");
}
#[test]
fn a_layer_stacked_below_the_selection_doesnt_take_a_drag_from_inside_it() {
// #1370: inside a selected unfilled shape, a filled layer behind it doesn't take the press (the
// selection drags); a filled layer above it still does.
let mut s = Session::new();
let at = |s: &Session, i: NodeId| s.doc().unwrap().world_bounds(i).unwrap().origin();
let frame = id(&ex(&mut s, "frame.create", json!({"x": 300, "y": 0, "width": 200, "height": 200})));
for (parent, dx, where_) in [(None, 0.0, "on the canvas"), (Some(frame), 300.0, "in a top-level frame")] {
// `back` (0,0) filled, `front` (20,20) stroke only, `top` (60,60) filled; each 100×100, so
// the presses are well clear of the stroke.
let square = |s: &mut Session, x: f64| {
let mut p = json!({"x": x, "y": x, "width": 100, "height": 100});
if let Some(parent) = parent {
p["parent"] = json!(parent);
}
id(&ex(s, "shape.rectangle", p))
};
let back = square(&mut s, 0.0);
let front = square(&mut s, 20.0);
ex(&mut s, "node.set", json!({"ids": [front], "props": {"fills": [], "strokes": ["#000000"], "strokeWeight": 1}}));
let top = square(&mut s, 60.0);
// (drag from, selection after (the layer that moves by (10, 10)), derivation)
let rows = [
((dx + 40.0, 40.0), front, "a filled layer behind the selected shape doesn't take the press: the selection drags"),
((dx + 90.0, 90.0), top, "a filled layer above the selected shape still takes the press"),
];
for (from, moved, why) in rows {
let home: Vec<Point> = [back, front, top].iter().map(|i| at(&s, *i)).collect();
ex(&mut s, "select.set", json!({"ids": [front]}));
drag(&mut s, from, (from.0 + 10.0, from.1 + 10.0), NONE);
assert_eq!(s.selection(), vec![moved], "{why} ({where_})");
for (i, h) in [back, front, top].iter().zip(&home) {
let d = if *i == moved { 10.0 } else { 0.0 };
assert_eq!(at(&s, *i), Point::new(h.x + d, h.y + d), "{why} ({where_})");
}
assert!(s.undo().unwrap(), "{why}: one undo step");
}
}
// A filled layer below two selected shapes, in the gap between them (inside the selection's
// box but neither shape's), is still hit there.
let rect_at = |s: &mut Session, x: f64, w: f64| id(&ex(s, "shape.rectangle", json!({"x": x, "y": 400, "width": w, "height": 100})));
let gap = rect_at(&mut s, 720.0, 60.0);
let (a, b) = (rect_at(&mut s, 600.0, 100.0), rect_at(&mut s, 800.0, 100.0));
ex(&mut s, "node.set", json!({"ids": [a, b], "props": {"fills": [], "strokes": ["#000000"], "strokeWeight": 1}}));
ex(&mut s, "select.set", json!({"ids": [a, b]}));
let home = at(&s, gap);
drag(&mut s, (750.0, 450.0), (760.0, 460.0), NONE);
assert_eq!(s.selection(), vec![gap], "a layer in the gap between selected layers takes the press");
assert_eq!(at(&s, gap), Point::new(home.x + 10.0, home.y + 10.0));
}
#[test]
fn a_click_where_nothing_is_hit_inside_a_multiple_selection_keeps_it() {
let mut s = Session::new();
let a = rect(&mut s, 100.0, 300.0, None);
let b = rect(&mut s, 220.0, 300.0, None);
for i in [a, b] {
ex(&mut s, "node.set", json!({"ids": [i], "props": {"fills": [], "strokes": ["#000000"], "strokeWeight": 1}}));
}
ex(&mut s, "select.set", json!({"ids": [a, b]}));
// The press is inside `a` but its unfilled inside hits nothing: `a` is only the drag fallback,
// not a clicked layer, so a click keeps the selection.
click(&mut s, 120.0, 320.0, NONE);
assert_eq!(sorted(s.selection()), sorted(vec![a, b]));
}
#[test]
fn alt_shift_drag_on_a_selected_layer_duplicates_it() {
let mut s = Session::new();
let k = scene(&mut s);
let layers = |s: &Session| s.doc().unwrap().node(s.page().unwrap()).unwrap().children.len();
let n = layers(&s);
let alt_shift = |kind, x, y| {
let mut e = ev(kind, x, y, SHIFT);
e.alt = true;
e
};
ex(&mut s, "select.set", json!({"ids": [k.r]}));
s.pointer(&alt_shift(PointerKind::Down, 520.0, 20.0)).unwrap();
s.pointer(&alt_shift(PointerKind::Up, 520.0, 20.0)).unwrap();
assert_eq!(s.selection(), Vec::<NodeId>::new(), "an Alt+Shift-click is a Shift-click: the layer leaves the selection");
assert_eq!(layers(&s), n, "and nothing is copied");
ex(&mut s, "select.set", json!({"ids": [k.r]}));
s.pointer(&alt_shift(PointerKind::Down, 520.0, 20.0)).unwrap();
s.pointer(&alt_shift(PointerKind::Move, 560.0, 22.0)).unwrap();
s.pointer(&alt_shift(PointerKind::Up, 560.0, 22.0)).unwrap();
let sel = s.selection();
assert_eq!(layers(&s), n + 1, "an Alt+Shift-drag copies the layer");
assert!(sel.len() == 1 && sel[0] != k.r, "the copy is selected");
let doc = s.doc().unwrap();
assert_eq!(doc.world_bounds(sel[0]).unwrap().origin(), Point::new(540.0, 0.0), "the copy moves along one axis");
assert_eq!(doc.world_bounds(k.r).unwrap().origin(), Point::new(500.0, 0.0), "the original stays");
}
#[test]
fn shift_click_takes_a_layer_out_where_nothing_can_move() {
let mut s = Session::new();
let k = scene(&mut s);
// A version being viewed is read-only: the press can't start a move.
let v = ex(&mut s, "file.saveVersion", json!({"name": "v"}));
ex(&mut s, "version.preview", json!({"id": v["id"]}));
ex(&mut s, "select.set", json!({"ids": [k.r, k.empty]}));
assert!(s.pointer(&ev(PointerKind::Down, 520.0, 20.0, SHIFT)).is_err(), "a version being viewed can't be edited");
s.pointer(&ev(PointerKind::Up, 520.0, 20.0, SHIFT)).unwrap();
assert_eq!(s.selection(), vec![k.empty], "Shift-click still takes the layer out");
}
/// A top-level 300 x 300 frame with a filled 100 x 100 rectangle at (50, 50) and a nested 100 x 100
/// frame at (180, 180) holding a 40 x 40 rectangle at (200, 200).
fn frame_with_child(s: &mut Session) -> (NodeId, NodeId, NodeId, NodeId) {
let f = id(&ex(s, "frame.create", json!({"x": 0, "y": 0, "width": 300, "height": 300})));
let c = id(&ex(s, "shape.rectangle", json!({"x": 50, "y": 50, "width": 100, "height": 100, "parent": f})));
let n = id(&ex(s, "frame.create", json!({"x": 180, "y": 180, "width": 100, "height": 100, "parent": f})));
let gc = id(&ex(s, "shape.rectangle", json!({"x": 200, "y": 200, "width": 40, "height": 40, "parent": n})));
(f, c, n, gc)
}
#[test]
fn a_drag_from_a_child_of_a_selected_frame_moves_the_frame() {
let mut s = Session::new();
let (f, c, _n, gc) = frame_with_child(&mut s);
let sib = id(&ex(&mut s, "shape.rectangle", json!({"x": 90, "y": 90, "width": 30, "height": 30})));
let at = |s: &Session, i: NodeId| s.doc().unwrap().world_bounds(i).unwrap().origin();
let local = |s: &Session, i: NodeId| {
let d = s.doc().unwrap();
let n = d.node(i).unwrap();
let c = n.transform.as_coeffs();
(c[4], c[5])
};
let c_local = local(&s, c);
// (selection before, press point, modifiers, selection after, the layers that move by (10, 10), derivation)
let rows: Vec<(Vec<NodeId>, (f64, f64), Mods, Vec<NodeId>, Vec<NodeId>, &str)> = vec![
(vec![f], (60.0, 60.0), NONE, vec![f], vec![f, c, gc], "a drag from a child of the selected frame moves the frame"),
(vec![f], (220.0, 220.0), NONE, vec![f], vec![f, c, gc], "a drag from a grandchild moves the top-level frame"),
(vec![], (60.0, 60.0), NONE, vec![c], vec![c], "with nothing selected the child is picked and moved"),
(vec![f], (60.0, 60.0), CMD, vec![c], vec![c], "Cmd picks the deepest layer and moves it"),
(vec![f], (100.0, 100.0), NONE, vec![sib], vec![sib], "a sibling painted above the frame takes the press"),
];
for (before, from, m, after, moved, why) in rows {
let all = [f, c, gc, sib];
let home: Vec<Point> = all.iter().map(|i| at(&s, *i)).collect();
ex(&mut s, "select.set", json!({"ids": before}));
drag(&mut s, from, (from.0 + 10.0, from.1 + 10.0), m);
assert_eq!(sorted(s.selection()), sorted(after), "{why}");
for (i, h) in all.iter().zip(&home) {
// The frame carries its children: they move in the world, not in their local space.
let d = if moved.contains(i) { 10.0 } else { 0.0 };
assert_eq!(at(&s, *i), Point::new(h.x + d, h.y + d), "{why}");
}
assert_eq!(local(&s, c), if moved == [c] { (c_local.0 + 10.0, c_local.1 + 10.0) } else { c_local }, "{why}: the child's local position");
assert!(s.undo().unwrap(), "{why}: one undo step");
for (i, h) in all.iter().zip(&home) {
assert_eq!(at(&s, *i), *h, "{why}: undo restores");
}
}
}
#[test]
fn a_click_on_a_child_of_a_selected_frame_selects_the_child() {
let mut s = Session::new();
let (f, c, n, gc) = frame_with_child(&mut s);
let home = s.doc().unwrap().world_bounds(c).unwrap().origin();
for (p, want) in [((60.0, 60.0), c), ((190.0, 190.0), n)] {
ex(&mut s, "select.set", json!({"ids": [f]}));
click(&mut s, p.0, p.1, NONE);
assert_eq!(s.selection(), vec![want], "a click selects the layer inside the selected frame");
}
assert_eq!(s.doc().unwrap().world_bounds(c).unwrap().origin(), home, "nothing moved");
let _ = gc;
s.tool.dev_mode = true;
ex(&mut s, "select.set", json!({"ids": [f]}));
click(&mut s, 60.0, 60.0, NONE);
assert_eq!(s.selection(), vec![c], "in Dev Mode too");
}