From 27e10129bdbd83d22dd5edb95c7a5e680e5cfca7 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 2 Dec 2026 18:44:27 -0800 Subject: [PATCH] Scripts: hasSelectedArtwork selects inside an isolated group and charges its scans While a group is isolated, `layer.hasSelectedArtwork = true` looked only at the layer's top-level art, which is not selectable in isolation, so it selected nothing. It now takes what Select All takes in isolation: the art inside the isolated group (through any sublayer it holds), or the isolated object itself, and nothing when the isolated object or what holds it is hidden or locked. While Lock Guides is on, paths made into guides are not counted as art to add, so they no longer trip the selection limit. Reading `hasSelectedArtwork` now counts each selected object it checks toward the script's operation budget, as the setter already did. Follows #9048, which added the property. --- crates/engine/src/script/dom/layers.rs | 36 +++++++++++++++-- crates/engine/src/tests/script_dom.rs | 56 ++++++++++++++++++++++++++ docs/mcp.md | 3 ++ 3 files changed, 91 insertions(+), 4 deletions(-) diff --git a/crates/engine/src/script/dom/layers.rs b/crates/engine/src/script/dom/layers.rs index 4328c1bad2..bd0503ae81 100644 --- a/crates/engine/src/script/dom/layers.rs +++ b/crates/engine/src/script/dom/layers.rs @@ -305,7 +305,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 @@ -332,7 +339,7 @@ fn collect_editable_art(n: &crate::doc::Node, out: &mut Vec) -> 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)?; @@ -345,10 +352,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 = 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. diff --git a/crates/engine/src/tests/script_dom.rs b/crates/engine/src/tests/script_dom.rs index 73e44c2af7..325b67e8f6 100644 --- a/crates/engine/src/tests/script_dom.rs +++ b/crates/engine/src/tests/script_dom.rs @@ -3792,6 +3792,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 = (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(); diff --git a/docs/mcp.md b/docs/mcp.md index b3c7372984..631108be31 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -3309,6 +3309,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. A scoped `removeAll()` that leaves a clipping group or layer