Merge pull request #10658 from dotCooCoo/fix/layer-selection-scans

Scripts: hasSelectedArtwork selects inside an isolated group and charges its scans
This commit is contained in:
.CooCoo authored and GitHub committed 2026-12-03 07:35:25 -08:00
commit a64c923e04
3 files changed
+91 -4

No files matched your search

+32 -4
View File
@@ -309,7 +309,14 @@ fn layer_z_position(h: &Host, a: &[Data]) -> Answer {
fn layer_has_selected(h: &Host, a: &[Data]) -> Answer {
let (doc, id) = this_layer(h, a)?;
Ok(Data::Bool(with_doc(h, doc, |_, st| st.selection.objects.iter().any(|o| st.doc.ancestry(*o).is_some_and(|a| a.contains(&id))))))
let selected = with_doc(h, doc, |_, st| {
// The scan is charged by every selected object it looks at.
let objects = &st.selection.objects;
let found = objects.iter().position(|o| st.doc.ancestry(*o).is_some_and(|a| a.contains(&id)));
h.charge_call(found.map_or(objects.len(), |i| i + 1) as u64)?;
Ok::<_, HostError>(found.is_some())
})?;
Ok(Data::Bool(selected))
}
/// Collects visible, unlocked art through non-template sublayers, bottom first, and counts every
@@ -336,7 +343,7 @@ fn collect_editable_art(n: &crate::doc::Node, out: &mut Vec<NodeId>) -> u64 {
fn layer_set_has_selected(h: &Host, a: &[Data]) -> Answer {
let on = boolean(arg(a, 1), "hasSelectedArtwork")?;
let (doc, id) = this_layer(h, a)?;
let (ids, over) = with_doc(h, doc, |_, st| {
let (ids, over) = with_doc(h, doc, |s, st| {
let inside = |o: &NodeId| st.doc.ancestry(*o).is_some_and(|a| a.contains(&id));
let selected = &st.selection.objects;
h.charge_call(selected.len() as u64)?;
@@ -349,10 +356,31 @@ fn layer_set_has_selected(h: &Host, a: &[Data]) -> Answer {
// The walk is charged by every node it looks at, hidden and locked ones included, whether
// or not anything is added.
h.charge_call(count)?;
// While an object is isolated, the art that holds it is replaced by the isolated object,
// or by what an isolated group or other container holds, as Select All takes it.
let isolated = st.isolation.and_then(|i| st.doc.node(i));
let path = isolated.and_then(|c| st.doc.ancestry(c.id)).unwrap_or_default();
let editable = path.iter().all(|i| st.doc.node(*i).is_some_and(|n| n.visible && !n.locked && !n.is_template()));
let mut candidates = Vec::with_capacity(art.len());
for o in art {
match isolated {
Some(_) if path.contains(&o) && !editable => {}
Some(c) if path.contains(&o) && c.is_container() => {
// The same walk as the layer's, through any sublayer the container holds.
let visited = collect_editable_art(c, &mut candidates);
h.charge_call(visited)?;
}
Some(c) if path.contains(&o) => candidates.push(c.id),
_ => candidates.push(o),
}
}
let mut have: std::collections::HashSet<NodeId> = selected.iter().copied().collect();
let mut ids = Vec::new();
for o in art {
if have.contains(&o) || !crate::cmd::select::is_selectable(&st.doc, st.isolation, &locks, o) {
for o in candidates {
if have.contains(&o)
|| !crate::cmd::select::is_selectable(&st.doc, st.isolation, &locks, o)
|| (s.guides_locked() && crate::is_guide_path(&st.doc, o))
{
continue;
}
// Only art that would be added counts toward the limit.
+56
View File
@@ -4276,6 +4276,62 @@ fn has_selected_artwork_charges_the_walk_when_nothing_is_added() {
assert!(error.contains("passed more data"), "{error}");
}
/// Reading `hasSelectedArtwork` is charged for every selected object it looks at.
#[test]
fn has_selected_artwork_charges_the_selection_it_reads() {
let repeated = |n: usize| {
let mut s = Session::new();
s.execute("file.new", &json!({"width": 400, "height": 300})).unwrap();
let ids: Vec<u64> = (0..n).map(|i| rect(&mut s, i as f64, 0.0, 1.0, 1.0).0).collect();
s.execute("layer.new", &json!({})).unwrap();
s.execute("select.set", &json!({"ids": ids})).unwrap();
crate::script::TEST_BRIDGE_BYTES.with(|b| b.set(Some(1 << 13)));
let r = run(&mut s, "var l = app.activeDocument.layers[0]; for (var i = 0; i < 50; i++) l.hasSelectedArtwork;");
crate::script::TEST_BRIDGE_BYTES.with(|b| b.set(None));
r
};
repeated(1).unwrap();
let error = repeated(200).unwrap_err().to_string();
assert!(error.contains("passed more data"), "{error}");
}
/// While a group is isolated, `hasSelectedArtwork = true` selects the art inside the group, as
/// Select All does.
#[test]
fn has_selected_artwork_selects_inside_an_isolated_group() {
let (mut s, a, b) = doc();
s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap();
let g = s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap();
s.execute("object.isolate", &json!({"id": g})).unwrap();
s.execute("select.none", &json!({})).unwrap();
let out = log(&mut s, "var l = app.activeDocument.layers[0]; l.hasSelectedArtwork = true; vc.log(l.hasSelectedArtwork);");
assert_eq!(out, [json!(true)]);
let sel = &s.active().unwrap().selection.objects;
assert!(sel.contains(&a) && sel.contains(&b), "{sel:?}");
}
/// While Lock Guides is on, a path made into a guide is not counted as art to add, so a selection
/// at the limit does not refuse a layer that holds only such a path.
#[test]
fn has_selected_artwork_skips_locked_guide_paths() {
let mut s = Session::new();
s.execute("file.new", &json!({"width": 400, "height": 300})).unwrap();
let a = rect(&mut s, 10.0, 20.0, 30.0, 40.0);
s.execute("select.set", &json!({"ids": [a.0]})).unwrap();
s.execute("view.guides.make", &json!({})).unwrap();
s.execute("view.guides.lock", &json!({"locked": true})).unwrap();
s.execute("layer.new", &json!({})).unwrap();
let other = rect(&mut s, 220.0, 30.0, 10.0, 10.0);
s.active_mut().unwrap().selection.objects = vec![other; 10_000];
run(&mut s, "app.activeDocument.layers.getByName('Layer 1').hasSelectedArtwork = true;").unwrap_or_else(|e| panic!("{e}"));
assert_eq!(s.active().unwrap().selection.objects.len(), 10_000);
s.execute("select.none", &json!({})).unwrap();
let steps = undo_steps(&s);
run(&mut s, "app.activeDocument.layers.getByName('Layer 1').hasSelectedArtwork = true;").unwrap();
assert_eq!(undo_steps(&s), steps);
assert!(s.active().unwrap().selection.objects.is_empty());
}
#[test]
fn the_report_gives_the_active_artboard_the_run_leaves() {
let (mut s, _, _) = doc();
+3
View File
@@ -3326,6 +3326,9 @@ through `$`:
deselects only the layer's art (`select.toggle`); the rest of the selection, ruler guides included, stays. A layer
with nothing to add or deselect runs no command. Each assignment counts the current selection toward the script's
operation budget, and `true` also counts every node of the layer it looks at, hidden and locked ones included.
While an object in the layer is isolated, `true` adds what Select All takes in isolation: the art inside an
isolated group, or the isolated object itself. Reading `hasSelectedArtwork` counts each selected object it checks
toward the same budget.
`clipped = true` makes the group's topmost object, which must be a path, a compound path or type, its clipping
path; `false` releases the mask; the group keeps its id. Changes are refused when the group or its layer is
hidden or locked, or its artboard is locked, and `true` is also refused when the topmost object is hidden or