mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-04 13:02:50 -05:00
Scripts: a loop over groups and their page items lists each collection once
The object model kept only the last list of art it built, so a loop that read doc.groupItems[i] and then that group's pageItems rebuilt the document-wide list of groups on every step. It now keeps the last four lists, each while its document is unchanged, and drops a document's older lists when it builds one for a newer version of that document.
This commit is contained in:
3 files changed
+47
-12
No files matched your search
@@ -94,9 +94,9 @@ pub(super) struct State {
|
||||
coords: Cell<Coords>,
|
||||
items: RefCell<Option<ItemList>>,
|
||||
saving: Cell<bool>,
|
||||
/// The last list of art one of [`layers`]' collections gave, kept while its document is
|
||||
/// unchanged.
|
||||
art: RefCell<Option<layers::ArtList>>,
|
||||
/// The last [`layers::ART_LISTS`] lists of art [`layers`]' collections gave, least recently
|
||||
/// used first, each kept while its document is unchanged.
|
||||
art: RefCell<Vec<layers::ArtList>>,
|
||||
/// The uids of the documents whose active artboard `setActiveArtboardIndex` changed.
|
||||
pub(super) active_artboards: RefCell<Vec<u64>>,
|
||||
presets: RefCell<Vec<Vec<(String, Data)>>>,
|
||||
|
||||
@@ -572,17 +572,23 @@ fn art_scope(h: &Host, d: &Data, class: &str) -> Result<(Doc, Option<NodeId>)> {
|
||||
/// and the ids it listed then.
|
||||
pub(super) type ArtList = (u64, Arc<Document>, u8, Option<NodeId>, Rc<Vec<NodeId>>);
|
||||
|
||||
/// How many lists of art [`super::State`] keeps at once.
|
||||
pub(super) const ART_LISTS: usize = 4;
|
||||
|
||||
/// The objects of `kind` in `container` (directly), or in the whole document at any depth, in
|
||||
/// paint order. The last list is kept while the document is unchanged, and each new one is charged
|
||||
/// by the objects it looks at.
|
||||
/// paint order. The last [`ART_LISTS`] lists are kept while their documents are unchanged, so a
|
||||
/// loop over groups that reads each group's `pageItems` builds each list once. Each new list is
|
||||
/// charged by the objects it looks at.
|
||||
fn art_list(h: &Host, st: &DocState, kind: Kind, container: Option<NodeId>) -> Result<Rc<Vec<NodeId>>> {
|
||||
if let Some((uid, doc, k, c, ids)) = &*h.dom.art.borrow()
|
||||
&& *uid == st.uid
|
||||
&& Arc::ptr_eq(doc, &st.doc)
|
||||
&& *k == kind as u8
|
||||
&& *c == container
|
||||
{
|
||||
return Ok(ids.clone());
|
||||
let mut lists = h.dom.art.borrow_mut();
|
||||
let at = lists.iter().position(|(uid, doc, k, c, _)| *uid == st.uid && Arc::ptr_eq(doc, &st.doc) && *k == kind as u8 && *c == container);
|
||||
if let Some(at) = at {
|
||||
let list = lists.remove(at);
|
||||
let ids = list.4.clone();
|
||||
lists.push(list);
|
||||
return Ok(ids);
|
||||
}
|
||||
}
|
||||
let ids: Vec<NodeId> = match container {
|
||||
None => {
|
||||
@@ -597,7 +603,12 @@ fn art_list(h: &Host, st: &DocState, kind: Kind, container: Option<NodeId>) -> R
|
||||
}
|
||||
};
|
||||
let ids = Rc::new(ids);
|
||||
*h.dom.art.borrow_mut() = Some((st.uid, st.doc.clone(), kind as u8, container, ids.clone()));
|
||||
let mut lists = h.dom.art.borrow_mut();
|
||||
lists.retain(|(uid, doc, ..)| *uid != st.uid || Arc::ptr_eq(doc, &st.doc));
|
||||
if lists.len() >= ART_LISTS {
|
||||
lists.remove(0);
|
||||
}
|
||||
lists.push((st.uid, st.doc.clone(), kind as u8, container, ids.clone()));
|
||||
Ok(ids)
|
||||
}
|
||||
|
||||
|
||||
@@ -1250,6 +1250,30 @@ fn art_collections_follow_edits_between_reads() {
|
||||
assert_eq!(out, [json!(2), json!(0), json!(3), json!("GroupItem"), json!(1), json!(2), json!("PathItem")]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_loop_over_groups_and_their_items_lists_each_collection_once() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"width": 400, "height": 300})).unwrap();
|
||||
for i in 0..5 {
|
||||
let x = f64::from(i) * 20.0;
|
||||
let (a, b) = (rect(&mut s, x, 0.0, 10.0, 10.0), rect(&mut s, x, 20.0, 10.0, 10.0));
|
||||
s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap();
|
||||
s.execute("object.group", &json!({})).unwrap();
|
||||
}
|
||||
// Both scripts make the same calls. The first reads the groups and each group's items in
|
||||
// turn; the second reads every group before any group's items, so it builds each list once.
|
||||
let nested = "var gs = app.activeDocument.groupItems, n = 0;\
|
||||
for (var i = 0; i < gs.length; i++) { var p = gs[i].pageItems; for (var j = 0; j < p.length; j++) { p[j]; n++; } }\
|
||||
vc.log(n);";
|
||||
let in_order = "var gs = app.activeDocument.groupItems, all = [], n = 0;\
|
||||
for (var i = 0; i < gs.length; i++) { all.push(gs[i]); }\
|
||||
for (var k = 0; k < all.length; k++) { var p = all[k].pageItems; for (var j = 0; j < p.length; j++) { p[j]; n++; } }\
|
||||
vc.log(n);";
|
||||
let (a, b) = (run(&mut s, nested).unwrap(), run(&mut s, in_order).unwrap());
|
||||
assert_eq!((a["log"].clone(), b["log"].clone()), (json!([10]), json!([10])));
|
||||
assert_eq!(a["hostCalls"], b["hostCalls"], "{a}\n{b}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn group_items_remove_all_refuses_locked_groups() {
|
||||
let (mut s, a, b) = doc();
|
||||
|
||||
Reference in new issue
Block a user