mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-04 13:02:50 -05:00
The canvas kept the selection's outlines, anchors and dots only while the document revision stayed the same, so every move command, nudge and drag step built them again from every selected object. With 10,000 selected rectangles one build took about 26 ms in a release build. A move of whole objects now records the documents before and after it and the distance (DocState::moved). The record is made only when each moved object is still the node the transform left (no later pass, such as the pixel grid snap, changed it), none is a member of a blend, and every object the selection shows, the key object and the key anchor's object included, is a moved object or inside one. Copies, scales, moves of patterns alone and moves that resize type areas make no record. The canvas moves its kept marks by that distance when the record names the document they show, or the document a drag began with. It also keeps them through a new revision of the same document and selection, as at the end of a drag. Every other change builds them as before.
128 lines
5.0 KiB
Rust
128 lines
5.0 KiB
Rust
//! [`DocState::moved`]: a move of whole objects names the documents before and after it and how far
|
||
//! it went, and only when every object the selection shows is the one before it, moved that far.
|
||
|
||
use std::sync::Arc;
|
||
|
||
use serde_json::{Value, json};
|
||
use vectorcraft_geom::Vec2;
|
||
|
||
use crate::*;
|
||
|
||
fn session() -> Session {
|
||
let mut s = Session::new();
|
||
s.execute("file.new", &json!({"width": 400, "height": 300})).unwrap();
|
||
s
|
||
}
|
||
|
||
fn id_of(v: &Value) -> NodeId {
|
||
NodeId(v["id"].as_u64().unwrap())
|
||
}
|
||
|
||
fn rect(s: &mut Session, x: f64) -> NodeId {
|
||
id_of(&s.execute("shape.rectangle", &json!({"x": x, "y": 20, "width": 30, "height": 20})).unwrap())
|
||
}
|
||
|
||
fn select(s: &mut Session, ids: &[NodeId]) {
|
||
s.execute("select.set", &json!({"ids": ids.iter().map(|i| i.0).collect::<Vec<_>>()})).unwrap();
|
||
}
|
||
|
||
fn doc(s: &Session) -> Arc<Document> {
|
||
s.doc().unwrap().doc.clone()
|
||
}
|
||
|
||
/// The move [`DocState::moved`] names, when it took `from` to the document now.
|
||
fn moved_from(s: &Session, from: &Arc<Document>) -> Option<Vec2> {
|
||
let st = s.doc().unwrap();
|
||
let m = st.moved.as_ref()?;
|
||
(m.from.as_ptr() == Arc::as_ptr(from) && m.to.as_ptr() == Arc::as_ptr(&st.doc) && m.selection == st.selection).then_some(m.by)
|
||
}
|
||
|
||
// A move of the selection, a nudge and each step of a drag name the move; the drag's steps all start from the
|
||
// document the drag began with.
|
||
#[test]
|
||
fn a_move_of_whole_objects_names_the_documents_and_the_distance() {
|
||
let mut s = session();
|
||
let (a, b) = (rect(&mut s, 20.0), rect(&mut s, 80.0));
|
||
select(&mut s, &[a, b]);
|
||
let before = doc(&s);
|
||
s.execute("object.move", &json!({"dx": 12, "dy": -5})).unwrap();
|
||
assert_eq!(moved_from(&s, &before), Some(Vec2::new(12.0, -5.0)), "Object › Transform › Move");
|
||
let before = doc(&s);
|
||
s.execute("object.nudge", &json!({"dx": 1, "dy": 0})).unwrap();
|
||
assert_eq!(moved_from(&s, &before), Some(Vec2::new(s.prefs.keyboard_increment, 0.0)), "a nudge");
|
||
|
||
let start = doc(&s);
|
||
s.begin_interaction("Move").unwrap();
|
||
for dx in [3.0, 7.5] {
|
||
s.preview("object.transform", &json!({"matrix": [1, 0, 0, 1, dx, 2], "copy": false})).unwrap();
|
||
assert_eq!(moved_from(&s, &start), Some(Vec2::new(dx, 2.0)), "a drag's step by {dx}");
|
||
}
|
||
}
|
||
|
||
// A copy, a transform that is not a move, a move of patterns alone, a move that also resizes type areas, and a move
|
||
// of part of what the selection shows name no move.
|
||
#[test]
|
||
fn a_copy_a_scale_and_a_move_of_part_of_the_selection_name_no_move() {
|
||
let mut s = session();
|
||
let (a, b) = (rect(&mut s, 20.0), rect(&mut s, 80.0));
|
||
select(&mut s, &[a, b]);
|
||
let cases = [
|
||
("object.move", json!({"dx": 12, "dy": 0, "copy": true})),
|
||
("object.transform", json!({"matrix": [2, 0, 0, 1, 0, 0]})),
|
||
("object.move", json!({"dx": 12, "dy": 0, "objects": false, "patterns": true})),
|
||
("object.move", json!({"dx": 12, "dy": 0, "typeAreas": true})),
|
||
("object.transform", json!({"matrix": [1, 0, 0, 1, 5, 0], "ids": [a.0]})),
|
||
];
|
||
for (cmd, p) in cases {
|
||
select(&mut s, &[a, b]);
|
||
let before = doc(&s);
|
||
s.execute(cmd, &p).unwrap();
|
||
assert_eq!(moved_from(&s, &before), None, "{cmd} {p}");
|
||
}
|
||
}
|
||
|
||
// The key object is shown too: a move that leaves it where it was names no move.
|
||
#[test]
|
||
fn a_key_object_left_where_it_was_names_no_move() {
|
||
let mut s = session();
|
||
let (a, b) = (rect(&mut s, 20.0), rect(&mut s, 80.0));
|
||
select(&mut s, &[a]);
|
||
s.active_mut().unwrap().selection.key = Some(b);
|
||
let before = doc(&s);
|
||
s.execute("object.move", &json!({"dx": 12, "dy": 0})).unwrap();
|
||
assert_eq!(moved_from(&s, &before), None);
|
||
}
|
||
|
||
// A pixel-aligned path snaps to the pixel grid after the transform, and an instance of a symbol aligned to the pixel
|
||
// grid snaps after the edit: neither is the object before the move moved by its distance.
|
||
#[test]
|
||
fn objects_that_snap_to_the_pixel_grid_name_no_move() {
|
||
let mut s = session();
|
||
let a = rect(&mut s, 20.0);
|
||
select(&mut s, &[a]);
|
||
s.execute("object.setPixelAligned", &json!({"on": true})).unwrap();
|
||
let before = doc(&s);
|
||
s.execute("object.move", &json!({"dx": 0.3, "dy": 0})).unwrap();
|
||
assert_eq!(moved_from(&s, &before), None, "a pixel-aligned path");
|
||
|
||
let mut s = session();
|
||
let art = rect(&mut s, 20.0);
|
||
let made = s.execute("symbol.new", &json!({"name": "Box", "ids": [art.0], "alignToPixelGrid": true})).unwrap();
|
||
select(&mut s, &[id_of(&made)]);
|
||
let before = doc(&s);
|
||
s.execute("object.move", &json!({"dx": 0.3, "dy": 0})).unwrap();
|
||
assert_eq!(moved_from(&s, &before), None, "an instance of a symbol aligned to the pixel grid");
|
||
}
|
||
|
||
// A member of a blend moved on its own changes the blend's spine.
|
||
#[test]
|
||
fn a_member_of_a_blend_names_no_move() {
|
||
let mut s = session();
|
||
let (a, b) = (rect(&mut s, 20.0), rect(&mut s, 200.0));
|
||
s.execute("object.blend.make", &json!({"ids": [a.0, b.0], "steps": 1})).unwrap();
|
||
select(&mut s, &[a]);
|
||
let before = doc(&s);
|
||
s.execute("object.move", &json!({"dx": 12, "dy": 0})).unwrap();
|
||
assert_eq!(moved_from(&s, &before), None);
|
||
}
|