Merge pull request #2448 from storytold/carry/2376

M5: the arrow keys move a layer along the canvas axes inside a rotated parent (#2372)
This commit is contained in:
BFlat authored and GitHub committed 2026-11-19 00:21:02 +07:00
commit ebb22e9539
4 files changed
+108 -2

No files matched your search

+2
View File
@@ -20,6 +20,8 @@ mod select;
mod vector_clipboard;
pub use vector_clipboard::clipboard_svg;
#[cfg(test)]
mod nudge_rotated_parent_tests;
#[cfg(test)]
mod vector_clipboard_tests;
pub(crate) use clipboard::apply_copied_props;
@@ -0,0 +1,92 @@
//! The arrow keys move a layer along the canvas axes whatever its parent's rotation (#2372).
use serde_json::{Value, json};
use crate::Session;
use crate::params::id_of;
use figcraft_doc::NodeId;
fn frame(s: &mut Session, parent: Option<NodeId>, w: f64, h: f64, rotation: f64) -> NodeId {
let mut p = json!({"x": 10, "y": 20, "width": w, "height": h});
if let (Some(par), Some(o)) = (parent, p.as_object_mut()) {
o.insert("parent".into(), json!(par));
}
let r = s.execute("frame.create", &p).unwrap();
let id = id_of(&r["id"]).unwrap();
if rotation != 0.0 {
s.execute("node.set", &json!({"id": id, "props": {"rotation": rotation}})).unwrap();
}
id
}
fn world(s: &Session, id: NodeId) -> (f64, f64) {
let c = s.doc().unwrap().world_transform(id).as_coeffs();
(c[4], c[5])
}
fn nudge(s: &mut Session, id: NodeId, params: Value) {
let mut p = params;
if let Some(o) = p.as_object_mut() {
o.insert("ids".into(), json!([id]));
}
s.execute("transform.nudge", &p).unwrap();
}
fn moved_by(s: &mut Session, id: NodeId, params: Value) -> (f64, f64) {
let before = world(s, id);
nudge(s, id, params);
let after = world(s, id);
(after.0 - before.0, after.1 - before.1)
}
fn near(got: (f64, f64), want: (f64, f64)) {
assert!((got.0 - want.0).abs() < 1e-6 && (got.1 - want.1).abs() < 1e-6, "moved by {got:?}, wanted {want:?}");
}
fn in_rotated(rotation: f64) -> (Session, NodeId) {
let mut s = Session::new();
let outer = frame(&mut s, None, 400.0, 300.0, rotation);
let child = frame(&mut s, Some(outer), 100.0, 80.0, 0.0);
(s, child)
}
#[test]
fn down_moves_a_child_of_a_frame_rotated_90_down_the_canvas() {
let (mut s, child) = in_rotated(90.0);
let k = s.prefs.nudge_small;
near(moved_by(&mut s, child, json!({"dx": 0, "dy": 1})), (0.0, k));
}
#[test]
fn right_moves_it_right_on_the_canvas() {
let (mut s, child) = in_rotated(90.0);
let k = s.prefs.nudge_small;
near(moved_by(&mut s, child, json!({"dx": 1, "dy": 0})), (k, 0.0));
}
#[test]
fn a_big_nudge_in_a_rotated_parent_follows_the_canvas_too() {
let (mut s, child) = in_rotated(90.0);
let k = s.prefs.nudge_big;
near(moved_by(&mut s, child, json!({"dx": 1, "dy": 0, "big": true})), (k, 0.0));
}
#[test]
fn a_child_of_an_unrotated_frame_moves_as_before() {
let (mut s, child) = in_rotated(0.0);
let k = s.prefs.nudge_small;
let local_before = s.doc().unwrap().node(child).unwrap().transform.as_coeffs();
near(moved_by(&mut s, child, json!({"dx": 1, "dy": 0})), (k, 0.0));
let local_after = s.doc().unwrap().node(child).unwrap().transform.as_coeffs();
assert!((local_after[4] - local_before[4] - k).abs() < 1e-6 && (local_after[5] - local_before[5]).abs() < 1e-6);
}
#[test]
fn a_layer_two_rotated_frames_deep_moves_along_the_canvas() {
let mut s = Session::new();
let outer = frame(&mut s, None, 500.0, 400.0, 45.0);
let inner = frame(&mut s, Some(outer), 300.0, 200.0, 30.0);
let child = frame(&mut s, Some(inner), 100.0, 80.0, 0.0);
let k = s.prefs.nudge_small;
near(moved_by(&mut s, child, json!({"dx": 0, "dy": 1})), (0.0, k));
}
+12 -1
View File
@@ -184,8 +184,19 @@ pub(super) fn nudge(s: &mut Session, p: &Value) -> Result<Value> {
// Children in an auto layout flow move one place instead (see `al_keys`).
for i in &ids {
if crate::al_keys::flowing_parent(doc, *i).is_none() && (dx, dy) != (0.0, 0.0) {
// The step is along the canvas axes; the layer's transform is in its parent's space.
let (lx, ly) = match doc.node(*i).and_then(|n| n.parent) {
Some(par) => {
let inv = doc.world_transform(par).inverse();
let o = inv * figcraft_geom::Point::new(0.0, 0.0);
let q = inv * figcraft_geom::Point::new(dx, dy);
let (lx, ly) = (q.x - o.x, q.y - o.y);
if lx.is_finite() && ly.is_finite() { (lx, ly) } else { (dx, dy) }
}
None => (dx, dy),
};
let n = doc.get_mut(*i)?;
n.transform = Affine::translate((dx, dy)) * n.transform;
n.transform = Affine::translate((lx, ly)) * n.transform;
changed = true;
}
}
+2 -1
View File
@@ -71,7 +71,7 @@ interaction feel only partly compared: the interactions measured so far, case by
| Alt-drag duplicate | done | the copy snaps to the layer it came from too (edges, centres, equal spacing); Duplicate (Cmd+D) then repeats the copy's move and rotation (the move of its centre in the page frame, then the turn about the copy's own centre, or the selection's centre for several layers; not its size), also after a Duplicate whose copy was moved, as long as the copies stay selected and the selection hasn't changed (measured in Figma's web app, 2026-11-12); Alt going down or up mid-drag turns the move into a copy or back to the original, with Shift a copy kept to one axis (`alt_pressed_during_a_drag_turns_the_move_into_a_copy`, `alt_shift_drag_on_a_selected_layer_duplicates_it`; measured in Figma's web app, 2026-11-08) |
| Drag threshold; double-click into groups and text | done | a press moves or resizes only once the pointer has travelled 3 px on screen, so a click or double-click made with an unsteady hand drags nothing; a double-click goes one level into a group and the next one edits the text under it, wherever on the text it lands, with all of it selected (a double-click in the text being edited selects the word) (`double_click_tests`); Enter on a selected text does the same (all of it selected, typing replaces it) and Escape leaves editing with the layer still selected (`text_enter_tests`) |
| Scale tool (K): handles about the opposite handle, live factor, panel | done | Dragging a handle ignores the origin grid and pivots on the opposite handle's point (the opposite corner, or the opposite edge's middle for an edge, which scales both ways), with Alt on the centre; the origin grid (`tool.scaleAnchor`) starts at the centre and sets only what the Scale field and the W and H fields keep fixed (W and H with a non-centre origin are unmeasured). Measured in Figma's web app, 2026-11-09. Tests: `a_scale_handle_drag_pivots_on_the_opposite_handle_whatever_the_origin`, `the_scale_origin_starts_at_the_centre`. Pressing K opens the Scale section with its factor field focused and its text selected; typing 2 (or 2x, 1.5) then Enter scales the selection by it about the panel's origin, which starts at the centre, as one undo step, then closes the section and returns to Move with the selection kept; Escape in the field closes it, back to Move, nothing changed. Measured in Figma's web app, 2026-11-09. Tests: `pressing_k_focuses_the_scale_field_with_its_text_selected`, `k_two_enter_scales_the_selection_by_two`, `escape_in_the_scale_field_closes_it_and_changes_nothing`. |
| Nudge 1 / Shift 10 (preference), Cmd+arrows resize | done | arrows reorder auto layout children |
| Nudge 1 / Shift 10 (preference), Cmd+arrows resize | done | arrows reorder auto layout children; the arrow keys move along the canvas axes inside a rotated parent, as in Figma, #2372 |
| Smart guides: edges, centres, equal spacing (between two layers or continuing a row's gap, labelled), spacing labels, parent, layout grid, ruler guides | done | red, `snap.rs`; a guide dragged out of a ruler or moved snaps to layer edges and centres too (Snap to Objects; Cmd/Ctrl suspends), on its own axis only; page guides see the page's top-level layers, frame guides the frame's box, layout grids and every layer in it, along a rotated frame's own axes; the object snap threshold is 4.5 screen pixels, measured against Figma (snapped at a gap of 4.4 screen px, not at 4.6, at 100% and 200% zoom, 2026-11-08; the equal-spacing snap shares it), but the ruler-guide and rotation-origin thresholds (5 px), the red guide's look and snapping with Snap to pixel grid off are not yet compared with Figma's (`guide_snap_tests`, `transform_behaviour_tests`) |
| Ctrl to suspend snapping while dragging (macOS) | done | Control on macOS (Cmd keeps working), Ctrl elsewhere; moving, resizing and drawing |
| Measure (Alt hover, Cmd+Alt nested, Shift+Alt padding/gaps) | done | labels never overlap; the arrows still nudge while measuring (Shift: the big step) and the distances follow; the selection's handles and size label hide while measuring (`measure_nudge_tests`, #894) |
@@ -210,6 +210,7 @@ This audit records candidates for subsequent fixes; their behaviour is unchanged
| Date | Change | Summary |
|---|---|---|
| 2026-11-18 | patch | M5, #2372: the arrow keys move a layer along the canvas axes inside a rotated parent (the step was applied along the parent's axes, so Down moved a child of a frame rotated 90 degrees sideways); auto layout children are unchanged |
| 2026-11-18 | patch | M1, #2355: Cmd/Ctrl+V with one layer inside a frame selected pasted the copy onto the page on top of the frame; it now goes into the layer's parent just above it (in an auto layout frame, the item after it, so the layout grows), keeping today's behaviour for a page-level layer, several layers, an instance's children and a board atom. Tests: `paste_sibling_tests` (5) |
| 2026-11-08 | minor | M8, #2109: in vector edit mode the Align commands (`align.left` to `align.bottom`, Alt+A/H/D/W/V/S) and Distribute horizontal and vertical spacing act on the selected points when neither `ids` nor `id` is given (either one targets layers), as in Figma (measured in Figma on 2026-11-08): each point moves along one axis only, to its box's left, horizontal centre, right, top, vertical centre or bottom, and distribute spaces the points evenly between the outermost two in order along the axis; handles go with their vertex, a single point stays, with no point selected nothing moves (the layer is not aligned to its frame, as measured in Figma), and one undo restores them. The Design panel draws the alignment row first, with a Distribute menu from three points. Numbers unchanged. |
| 2026-11-08 | patch | Number fields: scrubbing a label moves half a step a point, not a step, as measured in Figma (X from 100: +6 for a 12.2 px drag, +31 for 61.2 px; rotation 6° and opacity 5% for 12.2 px); Shift (x10), Alt (x0.1) and the speed by height are kept on top of it and were not measured, and no field's scrub direction changed |