From 48300cebe30b6b2593a92f1a817de5d4a86418d5 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 28 Nov 2026 16:34:40 -0800 Subject: [PATCH] Layers: the selection squares read held art from the listed rows Each frame with the Layers panel open, every row's selection square walked the art below its node to find whether it held a selected object, so a layer with 10,000 objects walked all of them in every frame. The rows now list the nodes that hold selected art in one pass over the tree when they are listed again (on each revision or selection change), and the square reads that list. --- crates/ui-egui/src/panels/layers/cells.rs | 10 +++----- crates/ui-egui/src/panels/layers/model.rs | 9 +++++++ crates/ui-egui/src/panels/layers/rows.rs | 31 +++++++++++++++++++++++ crates/ui-egui/src/panels/layers/tests.rs | 24 ++++++++++++++++++ 4 files changed, 67 insertions(+), 7 deletions(-) diff --git a/crates/ui-egui/src/panels/layers/cells.rs b/crates/ui-egui/src/panels/layers/cells.rs index ce0de11247..d12f248bb4 100644 --- a/crates/ui-egui/src/panels/layers/cells.rs +++ b/crates/ui-egui/src/panels/layers/cells.rs @@ -357,15 +357,11 @@ impl Component for Cells { /// The marks of node row `n`'s selection square in the panel over `tree`: whether it shows the /// full-sized square (the row is selected, or it is a layer or sublayer whose visible, unlocked art -/// is all selected), and whether it shows the small one (it holds selected art otherwise). +/// is all selected), and whether it shows the small one (it holds selected art otherwise). It reads +/// the nodes that hold selected art from the rows, which list them once per revision. pub(super) fn square_marks(tree: &DocLayers<'_>, n: &Node) -> (bool, bool) { let full = tree.is_selected(n.id) || tree.is_full(n.id); - let holds = !full && { - let mut any = false; - n.walk(&mut |c| any |= c.id != n.id && tree.is_selected(c.id)); - any - }; - (full, holds) + (full, !full && tree.holds_selected(n.id)) } /// The cells of row `row` (shown as `info`) of the panel over `tree`. diff --git a/crates/ui-egui/src/panels/layers/model.rs b/crates/ui-egui/src/panels/layers/model.rs index 57345e5040..c73794b6d0 100644 --- a/crates/ui-egui/src/panels/layers/model.rs +++ b/crates/ui-egui/src/panels/layers/model.rs @@ -193,6 +193,9 @@ pub(super) struct LayerRows { /// and not empty. #[plain_data(assume = "node ids are integers")] full: HashSet, + /// The nodes that hold a selected object below them ([`super::rows::holding`]). + #[plain_data(assume = "node ids are integers")] + holding: HashSet, /// The selected blends, in selection order. #[plain_data(assume = "node ids are integers")] blends: Vec, @@ -252,6 +255,7 @@ impl LayerRows { ruler, guide_rows, full: super::rows::full_layers(doc, &selected), + holding: super::rows::holding(doc, &selected), selected, blends: blends.collect(), root: st.isolation.and_then(|i| isolated_root(doc, i)).map(|(n, ..)| n.id), @@ -310,6 +314,11 @@ impl LayerRows { self.full.contains(&id) } + /// Whether node `id` holds a selected object below it. + pub(super) fn holds_selected(&self, id: NodeId) -> bool { + self.holding.contains(&id) + } + /// The node ids a row menu's command acts on: every highlighted row when the menu's row is one of /// them, else the row's own node. pub(super) fn targets(&self, target: &MenuTarget) -> Vec { diff --git a/crates/ui-egui/src/panels/layers/rows.rs b/crates/ui-egui/src/panels/layers/rows.rs index 8b1ca836fe..c7f140123c 100644 --- a/crates/ui-egui/src/panels/layers/rows.rs +++ b/crates/ui-egui/src/panels/layers/rows.rs @@ -150,6 +150,37 @@ pub(super) fn full_layers(doc: &Document, sel: &HashSet) -> HashSet = const { std::cell::Cell::new(0) }; +} + +/// The nodes of `doc` that hold an object of `sel` below them, found in one pass over the tree: the rows whose +/// selection square shows the small mark when they are not full. +pub(super) fn holding(doc: &Document, sel: &HashSet) -> HashSet { + /// Adds `n` to `out` when an object of `sel` is below it, and returns whether `n` or one below it is in `sel`. + fn visit(n: &Node, sel: &HashSet, out: &mut HashSet) -> bool { + #[cfg(test)] + HOLDING_VISITS.with(|v| v.set(v.get() + 1)); + let mut below = false; + for c in n.children().into_iter().flatten() { + below |= visit(c, sel, out); + } + if below { + out.insert(n.id); + } + below || sel.contains(&n.id) + } + let mut out = HashSet::new(); + if !sel.is_empty() { + for n in &doc.layers { + visit(n, sel, &mut out); + } + } + out +} + /// The `layer.target` of a click with `mods` on the target circle of `id`, by the rules of a row click (#8639): plain /// targets it, Ctrl/Cmd adds it to the selection or takes it out, Shift selects the art of every row `filter` lists /// from `anchor` (the last circle clicked, else the targeted row) to it, and Shift+Ctrl/Cmd adds that art. With it, diff --git a/crates/ui-egui/src/panels/layers/tests.rs b/crates/ui-egui/src/panels/layers/tests.rs index 2cd61e7083..5eeca21a8e 100644 --- a/crates/ui-egui/src/panels/layers/tests.rs +++ b/crates/ui-egui/src/panels/layers/tests.rs @@ -1328,3 +1328,27 @@ fn a_target_circle_drop_after_file_revert_leaves_the_restored_appearance_in_plac panel.drag("Target A", "Target B", Modifiers::NONE); assert_eq!(fill(&panel.app(), b), red, "a target circle drag started after the revert still moves the appearance"); } + +/// The rows find which nodes hold selected art once per revision: frames that change nothing walk no node for the +/// selection squares, and a new selection walks the tree once. +#[test] +fn idle_frames_walk_no_node_for_the_selection_squares() { + use crate::tests::panel_harness::Panel; + let visits = || super::rows::HOLDING_VISITS.with(std::cell::Cell::get); + let (mut app, [layer, sub, g, b, a, c]) = nested(); + app.session.execute("select.set", &json!({"ids": [b.0]})).unwrap(); + let mut panel = Panel::new(app, show); + panel.frame(); + assert_eq!(squares(&panel.app(), &[layer, sub, g, b, a, c]), (vec![b], vec![layer, sub, g]), "the layers and the group hold B"); + let before = visits(); + for _ in 0..10 { + panel.frame(); + } + assert_eq!(visits() - before, 0, "frames that change nothing walk no node"); + panel.app().session.execute("select.set", &json!({"ids": [a.0]})).unwrap(); + let before = visits(); + for _ in 0..5 { + panel.frame(); + } + assert!((1..=12).contains(&(visits() - before)), "a new selection walks the tree once: {}", visits() - before); +}