M8, #2329: use page-aligned selection box and single rotation centre for multi-selection

This commit is contained in:
Aathish committed 2026-11-28 23:16:24 +05:30
1 parent d95c6a91d4
commit bd68da55cc
5 files changed
+68 -54

No files matched your search

+3 -13
View File
@@ -116,25 +116,15 @@ pub fn selection_box(doc: &Document, sel: &[NodeId]) -> Option<[Point; 4]> {
}
/// The selection's handle box: the page transform of its top-left corner (axes along the box) and
/// its size. One layer: its own box; several sharing a rotation: a box turned with them;
/// otherwise the page-aligned bounds.
/// its size. One layer: its own box; several layers: the page-aligned bounds.
pub fn selection_frame(doc: &Document, sel: &[NodeId]) -> Option<(Affine, f64, f64)> {
let ids = roots(doc, sel);
if let [id] = ids.as_slice() {
let n = doc.node(*id)?;
return Some((doc.world_transform(*id), n.width, n.height));
}
let Some(angle) = shared_rotation(doc, &ids) else {
let b = doc.bounds_of(&ids)?;
return Some((Affine::translate((b.x0, b.y0)), b.width(), b.height()));
};
let rot = Affine::rotate(angle);
let inv = rot.inverse();
let b = ids
.iter()
.filter_map(|i| Some(figcraft_geom::transform_rect_bbox(inv * doc.world_transform(*i), doc.node(*i)?.local_rect())))
.reduce(|a, b| a.union(b))?;
Some((rot * Affine::translate((b.x0, b.y0)), b.width(), b.height()))
let b = doc.bounds_of(&ids)?;
Some((Affine::translate((b.x0, b.y0)), b.width(), b.height()))
}
/// The rotation `ids` share on the page (degrees, counter-clockwise positive), else 0 (the handle
+24 -3
View File
@@ -499,11 +499,9 @@ fn a_turned_pair_turned_again_about_its_handle_box_repeats_without_a_move() {
fn several_uneven_layers_turned_together_repeat_about_one_centre_each_time() {
let mut s = Session::new();
let mut copies = uneven_pair_and_copies(&mut s);
// Upright, the pair's handle box is its bounds: both centres are (250, 150), so this is also
// the Rotate handle's turn. Turned, the box keeps that centre; the bounds don't.
ex(&mut s, "transform.rotate", json!({"ids": copies, "degrees": 30}));
let pivot = figcraft_geom::Point::new(250.0, 150.0);
for k in 1..=3 {
let pivot = handle_box_centre(&s, &copies);
let before: Vec<(f64, f64)> = copies.iter().map(|c| centre(&s, *c)).collect();
let again = ex(&mut s, "edit.duplicate", json!({}));
let next: Vec<NodeId> = again["ids"].as_array().unwrap().iter().map(|v| id_of(v).unwrap()).collect();
@@ -519,6 +517,29 @@ fn several_uneven_layers_turned_together_repeat_about_one_centre_each_time() {
}
}
#[test]
fn pre_rotated_uneven_pair_turned_with_no_pivot_repeats_without_a_move() {
let mut s = Session::new();
let wide = id_of(&ex(&mut s, "shape.rectangle", json!({"x": 200, "y": 0, "width": 300, "height": 100}))["id"]).unwrap();
let ids = [rect(&mut s, 0.0, 200.0), wide];
ex(&mut s, "transform.rotate", json!({"ids": ids, "degrees": 60}));
ex(&mut s, "select.set", json!({"ids": ids}));
let out = ex(&mut s, "edit.duplicate", json!({}));
let copies: Vec<NodeId> = out["ids"].as_array().unwrap().iter().map(|v| id_of(v).unwrap()).collect();
ex(&mut s, "transform.rotate", json!({"ids": copies, "degrees": 30}));
let pivot = handle_box_centre(&s, &copies);
let before: Vec<(f64, f64)> = copies.iter().map(|c| centre(&s, *c)).collect();
let again = ex(&mut s, "edit.duplicate", json!({}));
let next: Vec<NodeId> = again["ids"].as_array().unwrap().iter().map(|v| id_of(v).unwrap()).collect();
assert_eq!(next.len(), 2);
for (i, c) in next.iter().enumerate() {
assert!(near(turn(&s, *c), 120.0), "layer {i}: rotation {}", turn(&s, *c));
let want = figcraft_geom::translation(figcraft_geom::rotate_about(figcraft_geom::Affine::translate((before[i].0, before[i].1)), pivot, 30.0));
let got = centre(&s, *c);
assert!(near(got.0, want.x) && near(got.1, want.y), "layer {i}: {got:?} vs {want:?}");
}
}
#[test]
fn a_flipped_copy_does_not_repeat_a_half_turn() {
let mut s = Session::new();
+14 -27
View File
@@ -6,7 +6,7 @@ use crate::Session;
use crate::params::id_of;
use crate::tools::{PointerEvent, PointerKind, selection_box, selection_frame};
use figcraft_doc::NodeId;
use figcraft_geom::{Affine, Point};
use figcraft_geom::Point;
fn rect(s: &mut Session, x: f64, y: f64, rotation: f64) -> NodeId {
let id = id_of(&s.execute("shape.rectangle", &json!({"x": x, "y": y, "width": 100, "height": 50})).unwrap()["id"]).unwrap();
@@ -18,29 +18,18 @@ fn near(a: f64, b: f64) -> bool {
(a - b).abs() < 1e-6
}
/// The layer's corners in the box's axes.
fn corners_in(s: &Session, frame: Affine, id: NodeId) -> Vec<Point> {
let doc = s.doc().unwrap();
let n = doc.node(id).unwrap();
let m = frame.inverse() * doc.world_transform(id);
[(0.0, 0.0), (n.width, 0.0), (n.width, n.height), (0.0, n.height)].iter().map(|(x, y)| m * Point::new(*x, *y)).collect()
}
#[test]
fn shared_rotation_turns_the_box_with_the_layers() {
fn shared_rotation_keeps_the_page_aligned_box() {
let mut s = Session::new();
let ids = [rect(&mut s, 0.0, 0.0, 30.0), rect(&mut s, 40.0, 120.0, 30.0)];
let (frame, w, h) = selection_frame(s.doc().unwrap(), &ids).unwrap();
// The box's axes follow the layers': each sits upright in it, and the box hugs them.
let pts: Vec<Point> = ids.iter().flat_map(|i| corners_in(&s, frame, *i)).collect();
let fold = |f: fn(f64, f64) -> f64, init: f64, k: fn(&Point) -> f64| pts.iter().map(k).fold(init, f);
assert!(near(fold(f64::min, f64::INFINITY, |p| p.x), 0.0) && near(fold(f64::min, f64::INFINITY, |p| p.y), 0.0));
assert!(near(fold(f64::max, f64::NEG_INFINITY, |p| p.x), w) && near(fold(f64::max, f64::NEG_INFINITY, |p| p.y), h));
let c = (frame.inverse() * s.doc().unwrap().world_transform(ids[0])).as_coeffs();
let doc = s.doc().unwrap();
let b = doc.bounds_of(&ids).unwrap();
let (frame, w, h) = selection_frame(doc, &ids).unwrap();
assert!(near(w, b.width()) && near(h, b.height()), "{w} {h} vs {} {}", b.width(), b.height());
let q = selection_box(doc, &ids).unwrap();
assert_eq!(q, [Point::new(b.x0, b.y0), Point::new(b.x1, b.y0), Point::new(b.x1, b.y1), Point::new(b.x0, b.y1)]);
let c = frame.as_coeffs();
assert!(near(c[0], 1.0) && near(c[1], 0.0) && near(c[2], 0.0) && near(c[3], 1.0), "{c:?}");
// Tighter than the page-aligned bounds.
let b = s.doc().unwrap().bounds_of(&ids).unwrap();
assert!(w * h < b.width() * b.height() - 1.0);
}
#[test]
@@ -56,24 +45,22 @@ fn different_or_right_angle_rotations_keep_the_page_aligned_box() {
}
#[test]
fn resizing_a_rotated_multi_selection_scales_along_its_axes() {
fn resizing_a_rotated_multi_selection_scales_along_page_axes() {
let mut s = Session::new();
let ids = [rect(&mut s, 0.0, 0.0, 30.0), rect(&mut s, 40.0, 120.0, 30.0)];
s.execute("select.set", &json!({"ids": ids})).unwrap();
let (frame, w, h) = selection_frame(s.doc().unwrap(), &ids).unwrap();
// Drag the bottom-right handle to make the box 1.5× wider and 2× taller.
// Drag the bottom-right handle to make the page-aligned box 1.5× wider and 2× taller.
let (from, to) = (frame * Point::new(w, h), frame * Point::new(w * 1.5, h * 2.0));
s.pointer(&PointerEvent::at(PointerKind::Down, from.x, from.y, 1.0)).unwrap();
s.pointer(&PointerEvent::at(PointerKind::Move, to.x, to.y, 1.0)).unwrap();
s.pointer(&PointerEvent::at(PointerKind::Up, to.x, to.y, 1.0)).unwrap();
let doc = s.doc().unwrap();
let (f2, w2, h2) = selection_frame(doc, &ids).unwrap();
assert!(near(w2, w * 1.5) && near(h2, h * 2.0), "{w2} {h2}");
// The opposite corner stays put and the layers keep their rotation and grow with the box.
assert!((f2 * Point::ZERO - frame * Point::ZERO).hypot() < 1e-6);
let (_f2, w2, h2) = selection_frame(doc, &ids).unwrap();
assert!(w2 > w && h2 > h, "{w2} {h2} vs {w} {h}");
for i in ids {
let n = doc.node(i).unwrap();
assert!(near(n.width, 150.0) && near(n.height, 100.0), "{} {}", n.width, n.height);
assert!(n.width > 100.0 && n.height > 50.0, "{} {}", n.width, n.height);
assert!(near(figcraft_geom::rotation_degrees(n.transform), 30.0));
}
// One undo step restores both.
+26 -10
View File
@@ -274,22 +274,38 @@ fn cmd_alt_arrows_rotate_a_degree_and_fifteen_with_shift() {
assert_eq!(size(&u), (40.0, 40.0), "no resize");
press(&mut u, Key::ArrowDown, cmd());
assert_eq!(size(&u), (40.0, 41.0), "Cmd/Ctrl+arrows alone still resize");
// Two layers turned together: the turns go about the rotated selection box's centre, so a
// degree down and a degree up leave both where they were.
// Two layers turned together: the turns go about the page-aligned selection box's centre.
let c = id(u.app.session.execute("shape.rectangle", &json!({"x": 200, "y": 0, "width": 40, "height": 40})).unwrap());
let d = id(u.app.session.execute("shape.rectangle", &json!({"x": 300, "y": 60, "width": 30, "height": 20})).unwrap());
u.app.run("select.set", json!({"ids": [c, d]}));
u.app.run("transform.rotate", json!({"degrees": 30}));
let bounds = |u: &Ui| [c, d].map(|n| u.app.session.doc().unwrap().world_bounds(n).unwrap());
let before = bounds(&u);
let box_centre = |u: &Ui| {
let q = figcraft_engine::tools::selection_box(u.app.session.doc().unwrap(), &[c, d]).unwrap();
figcraft_geom::Point::new(q.iter().map(|p| p.x).sum::<f64>() / 4.0, q.iter().map(|p| p.y).sum::<f64>() / 4.0)
};
let layer_centre = |u: &Ui, n: figcraft_doc::NodeId| {
let doc = u.app.session.doc().unwrap();
let b = doc.world_bounds(n).unwrap();
figcraft_geom::Point::new((b.x0 + b.x1) * 0.5, (b.y0 + b.y1) * 0.5)
};
let pivot = box_centre(&u);
let before_centres = [layer_centre(&u, c), layer_centre(&u, d)];
press(&mut u, Key::ArrowDown, cmd() | Modifiers::ALT);
for (i, n) in [c, d].into_iter().enumerate() {
let got_rot = figcraft_geom::rotation_degrees(u.app.session.doc().unwrap().node(n).unwrap().transform);
assert!((got_rot - 31.0).abs() < 1e-6, "layer {i}: {got_rot}");
let want = figcraft_geom::translation(figcraft_geom::rotate_about(
figcraft_geom::Affine::translate((before_centres[i].x, before_centres[i].y)),
pivot,
1.0,
));
let got = layer_centre(&u, n);
assert!((got.x - want.x).abs() < 1e-6 && (got.y - want.y).abs() < 1e-6, "layer {i}: {got:?} vs {want:?}");
}
press(&mut u, Key::ArrowUp, cmd() | Modifiers::ALT);
let after = bounds(&u);
for (x, y) in before.iter().zip(&after) {
assert!(
(x.x0 - y.x0).abs() < 1e-6 && (x.y0 - y.y0).abs() < 1e-6 && (x.x1 - y.x1).abs() < 1e-6 && (x.y1 - y.y1).abs() < 1e-6,
"{x:?} then {y:?}"
);
for (i, n) in [c, d].into_iter().enumerate() {
let got_rot = figcraft_geom::rotation_degrees(u.app.session.doc().unwrap().node(n).unwrap().transform);
assert!((got_rot - 30.0).abs() < 1e-6, "layer {i} back: {got_rot}");
}
// While points are being edited the chord does nothing to the layer.
u.app.run("select.set", json!({"ids": [r]}));
+1 -1
View File
@@ -63,7 +63,7 @@ interaction feel only partly compared: the interactions measured so far, case by
| Escape with layers selected | done | clears the selection at once, whatever the nesting; it never selects the parent. Measured in the Figma web app (2026-11-08) with a rectangle in a group in a frame, selected from the canvas (a double-click into the group), from the Layers panel, and after Tab moved the selection to its sibling: one Escape selects nothing, a second leaves nothing selected. Escape still leaves text and vector editing, ends gestures and the pen first, and ends the focus view once nothing or the focused frame is selected (`selection_behaviour_tests`, `devfocus_tests`) |
| Enter / Shift+Enter (children / parent), Tab / Shift+Tab (siblings) | done | Tab renames the next layer while renaming |
| Resize handles: Shift keeps proportions, Alt from centre, constraints-aware | done | Alt shrinks too; past an edge flips (several layers mirror); groups and mixed rotations scale each layer along its own axes, keeping rotation and flip (no skew: children turned off a right angle are approximate); Cmd/Ctrl ignores a frame's constraints (`transform_behaviour_tests`); Shift keeps the ratio (a corner by the axis dragged most, an edge scaling the other axis about the opposite edge's middle), Alt keeps the centre, Shift+Alt does both, each read afresh on every move (`handle_drags_resize_by_the_documented_rules`; measured in Figma's web app, 2026-11-08; Ctrl/Cmd while dragging not measurable) |
| Rotate just outside a corner; Shift in 15° steps | done | rotated handle box when layers share a rotation; Shift lands the rotation on multiples of 15° (measured in Figma: a layer at 7° or -4.27° ends at 15°), not 15° steps from where the drag began; layers with different rotations step the turn from 0; the Rotation field shows and sets the rotation on the page (its own angle plus its parents'), in frames and groups alike, measured in Figma's web app 2026-11-10 (#2196): a rectangle drawn inside a turned frame reads 0, typing 7 makes it 7 on the page; the panel sends `node.set absoluteRotation`, while `rotation` stays the angle to the parent for scripts, inspect, codegen and the formats. Pinned, not measured: several layers read one value when their page angles agree (typing sets them all, one undo step); a flipped layer reads in the same form as before (a horizontal flip reads 180 plus the angle) and typing keeps the flip; a flipped parent is handled by the world math. Tests: `absolute_rotation_sets_the_rotation_on_the_page_keeping_the_centre`, `the_absolute_rotation_key_turns_about_a_moved_origin_too`, `the_rotation_field_shows_and_sets_the_rotation_on_the_page` |
| Rotate just outside a corner; Shift in 15° steps | done | page-aligned handle box for several layers (also when they share a rotation, matching Figma); Shift lands the rotation on multiples of 15° (measured in Figma: a layer at 7° or -4.27° ends at 15°), not 15° steps from where the drag began; layers with different rotations step the turn from 0; the Rotation field shows and sets the rotation on the page (its own angle plus its parents'), in frames and groups alike, measured in Figma's web app 2026-11-10 (#2196): a rectangle drawn inside a turned frame reads 0, typing 7 makes it 7 on the page; the panel sends `node.set absoluteRotation`, while `rotation` stays the angle to the parent for scripts, inspect, codegen and the formats. Measured in Figma (2026-11-13): typing a rotation with several layers selected turns each layer about its own centre; pinned, not measured: several layers read one value when their page angles agree (typing sets them all, one undo step); a flipped layer reads in the same form as before (a horizontal flip reads 180 plus the angle) and typing keeps the flip; a flipped parent is handled by the world math. Tests: `absolute_rotation_sets_the_rotation_on_the_page_keeping_the_centre`, `the_absolute_rotation_key_turns_about_a_moved_origin_too`, `the_rotation_field_shows_and_sets_the_rotation_on_the_page` |
| Flip, rotate 90° | done | |
| Rotation origin (Alt+R) | done | a handle at the selection's centre to drag (snaps to the box's corners, edge middles and centre), double-click puts it back, Escape or Alt+R hides it; the corner handles, the rotation field, Rotate 90°/180° and `transform.rotate` turn about it; it follows the layers and lasts as long as the selection, not saved (the documentation doesn't say; pinned) |
| Align and Distribute (Design panel and shortcuts) | done | single and multi-layer Align and Distribute were measured in Figma's web app on 2026-11-12 and match: one layer aligns to its frame, several to their combined bounds, Distribute spaces three or more. Align is disabled for a child in an auto layout frame's flow (Alt+A and Alt+S do nothing, the frame is untouched), as measured; the panel's tip says the frame places the layer, and an absolutely positioned child still aligns to its frame (not measured, so today's behaviour is kept). Tidy up was measured to differ (Figma keeps the order and sizes and evens one gap per axis): not yet. Tests: `align_is_unavailable_for_a_child_in_an_auto_layout_flow`, `an_absolute_child_of_an_auto_layout_frame_still_aligns_to_it`, `the_align_buttons_are_off_for_a_layer_in_an_auto_layout_flow` |