From 6aabced7c157dfc9075d2d1d091ae422169d4ac3 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 29 Nov 2026 11:23:42 -0800 Subject: [PATCH] perf(layers): keep the display order through edits that leave the tree's structure as it was The Layers panel keyed its display order by the tree's tracked revision, which every edit moves, so a selection change in an expanded tree walked every layer again: 10,002 children() calls for a 10,000-layer document. LayerTree::order_revision lets the app report a number that changes only when children, open groups, open effects or sub-row counts change. With it, the panel keeps the display order until that number, the tracked source, the filter or a reveal's opens change. While the filter is active the order still follows the tracked revision, since the filter reads what the rows show. Trees that report no order revision behave as before. Trials count the display order builds: a selection change builds none, a new order revision builds one, an edit under an active filter builds one, and another tree with the same order revision builds its own rows. --- crates/layers/src/ui/mock.rs | 7 +++ crates/layers/src/ui/panel/body/rows.rs | 13 +++-- crates/layers/src/ui/tree.rs | 15 +++++- crates/layers/tests/it/panel_memo.rs | 72 +++++++++++++++++++++++++ 4 files changed, 102 insertions(+), 5 deletions(-) diff --git a/crates/layers/src/ui/mock.rs b/crates/layers/src/ui/mock.rs index 6bf23a19ab..7fa5b5fac7 100644 --- a/crates/layers/src/ui/mock.rs +++ b/crates/layers/src/ui/mock.rs @@ -51,6 +51,9 @@ pub struct FakeLayers { /// The linked smart objects whose file is not there, which only the app knows: the Smart Object /// filter's Missing. pub missing: Vec, + /// The [order revision](LayerTree::order_revision) the tree reports; `None` keys the display order by the tracked + /// revision. + pub order: Option, } impl FakeLayers { @@ -411,6 +414,10 @@ impl LayerTree for FakeLayers { self.layer(id).is_some_and(|layer| layer.effects_open) } + fn order_revision(&self) -> Option { + self.order + } + fn info(&self, id: &u32) -> LayerInfo { self.layer(id).map(|layer| layer.info.clone()).unwrap_or_else(|| LayerInfo::new(String::new(), LayerKind::Pixel)) } diff --git a/crates/layers/src/ui/panel/body/rows.rs b/crates/layers/src/ui/panel/body/rows.rs index 8fa70c4cbb..2415106d46 100644 --- a/crates/layers/src/ui/panel/body/rows.rs +++ b/crates/layers/src/ui/panel/body/rows.rs @@ -28,11 +28,14 @@ pub(in crate::ui::panel) struct DisplayRow { pub(super) open: bool, } -/// What the display order derives from: the tree's tracked revision, the filter and the groups a reveal opens. A -/// change of any of them builds the rows again; nothing else does. +/// What the display order derives from: the tree's tracked source, its [order revision](LayerTree::order_revision), +/// or its tracked revision when it has none or the filter is active, the filter and the groups a reveal opens. A change +/// of any of them builds the rows again; nothing else does. #[derive(MemoProps)] pub(in crate::ui::panel) struct RowsKey<'a, Id> { - pub(in crate::ui::panel) tree: Revision, + pub(in crate::ui::panel) source: u64, + pub(in crate::ui::panel) order: Option, + pub(in crate::ui::panel) tree: Option, pub(in crate::ui::panel) filter: &'a LayerFilter, #[memo_props(hash = "tree ids are plain model ids (LayerTree::Id: PlainData); Hash covers them")] pub(in crate::ui::panel) opened: &'a [Id], @@ -72,7 +75,9 @@ pub(in crate::ui::panel) struct LayerRows<'t, T: LayerTree> { impl<'t, T: LayerTree> LayerRows<'t, T> { /// The rows for `filter` and `opened`, derived by the body `cx` from the tracked `tree`. pub(in crate::ui::panel) fn new(cx: &BodyCx<'_>, tree: &'t Tracked, filter: &LayerFilter, opened: &[T::Id]) -> Self { - let key = RowsKey { tree: tree.revision(), filter, opened }; + let order = tree.order_revision(); + let tracked = (order.is_none() || filter.active()).then(|| tree.revision()); + let key = RowsKey { source: tree.source(), order, tree: tracked, filter, opened }; // The low half of the key's hash tells the list when the order changed. let revision = MemoKey::of(&key) as u64; let model = cx.derived(&key, || RowsModel::walk(&**tree, filter, opened, revision)); diff --git a/crates/layers/src/ui/tree.rs b/crates/layers/src/ui/tree.rs index ead1857170..f2ff73ad4f 100644 --- a/crates/layers/src/ui/tree.rs +++ b/crates/layers/src/ui/tree.rs @@ -38,7 +38,8 @@ use super::panel::MenuTarget; /// The panel calls [`info`](Self::info) only for the rows it draws (the rows in view and one /// above and below), so a tree of 10,000 layers costs what its visible rows cost. /// [`children`](Self::children) and [`is_open`](Self::is_open) are walked to build the display order, and only when the -/// tracked revision, the filter or a reveal's opens changed. +/// tracked revision (or the [`order_revision`](Self::order_revision) the app gives), the filter or a reveal's opens +/// changed. pub trait LayerTree: PlainData { /// The app's layer id: the key of a row, of its thumbnails and of every event. It is plain model data /// (`#[derive(PlainData)]`), so the panel keys it by its `Hash`, including in the memo key of the display order @@ -61,6 +62,18 @@ pub trait LayerTree: PlainData { /// Whether a layer shows its effects and Smart Filter rows (one flag for both, PhotoCraft). fn effects_open(&self, id: &Self::Id) -> bool; + /// A number that changes whenever an answer the display order is built from changes: [`children`](Self::children), + /// [`is_open`](Self::is_open), [`effects_open`](Self::effects_open) and the number of each layer's + /// [`sub_rows`](Self::sub_rows). With it, the panel builds the display order again only when this number, the filter + /// or a reveal's opens change, and keeps it when only the selection, a name or a toggle changed. While the filter is + /// [`active`](LayerFilter::active) the order also follows the tracked revision, as [`listed`](Self::listed) reads + /// the rows' contents. The order also follows the [`Tracked`](craft_ui::contract::Tracked) the tree is held in, so an + /// app that lists its tree again writes the new value into the same `Tracked` instead of making a new one. + /// `None`, the default, keys the order by the tracked revision, which every edit moves. + fn order_revision(&self) -> Option { + None + } + /// What the row of `id` shows. Cheap: called only for rows in view. fn info(&self, id: &Self::Id) -> LayerInfo; diff --git a/crates/layers/tests/it/panel_memo.rs b/crates/layers/tests/it/panel_memo.rs index e733658654..a1462d2138 100644 --- a/crates/layers/tests/it/panel_memo.rs +++ b/crates/layers/tests/it/panel_memo.rs @@ -3,6 +3,7 @@ use craft_layers::ui::filter::{FilterBy, LayerFilter}; use craft_layers::ui::mock::FakeLayers; use craft_layers::ui::panel::{LayersEvent, LayersPanel}; +use craft_layers::ui::tree::{LayerPart, LayerSelection}; use craft_ui::contract::{Bind, MemoProps, MemoView, Outbox, Tracked}; use craft_ui::testing::{Fixture, MemoRun, last_frame_derived_builds, last_frame_memo_runs, last_pass_memo_runs, pointer, trial}; use egui::{Ui, Vec2}; @@ -109,6 +110,77 @@ fn one_layer_edit_builds_once() { assert_eq!(rig.host_runs(), (0, 1), "and replays"); } +/// A tree that reports an order revision keeps its display order through a selection change: the view runs for the new +/// selection and builds no rows. +#[trial] +fn a_selection_change_keeps_the_display_order_of_a_tree_with_an_order_revision() { + let mut tree = FakeLayers::fifty(); + tree.order = Some(1); + let mut rig = rig(tree); + let title = rig.tree.borrow().layers.iter().find(|layer| layer.info.name == "Title").map(|layer| layer.id).expect("Title is in the tree"); + rig.tree.borrow_mut().selection = LayerSelection::new(vec![title], Some(title), LayerPart::Pixels); + rig.frame(); + assert_eq!(rig.host_runs().0, 1, "the selection runs the view"); + assert_eq!(rig.builds(), 0, "and keeps the display order"); +} + +/// A change of the order revision builds the display order once, and the rows follow the new structure. +#[trial] +fn an_order_revision_change_builds_once() { + let mut tree = FakeLayers::fifty(); + tree.order = Some(1); + let mut rig = rig(tree); + assert!(rig.fixture.by_label("Title").is_some(), "the open Header group shows Title"); + { + let mut tree = rig.tree.borrow_mut(); + let header = tree.layers.iter_mut().find(|layer| layer.info.name == "Header").expect("Header is in the tree"); + header.open = false; + tree.order = Some(2); + } + rig.frame(); + assert_eq!(rig.builds(), 1, "the new order revision builds the display order once"); + rig.fixture.idle(2); + assert!(rig.fixture.by_label("Title").is_none(), "the closed Header group hides Title"); +} + +/// Another tree in the panel builds its own display order, even when it reports the same order revision. +#[trial] +fn another_tree_with_the_same_order_revision_builds_its_own_rows() { + let mut tree = FakeLayers::fifty(); + tree.order = Some(1); + let mut rig = rig(tree); + assert!(rig.fixture.by_label("Tagline").is_some(), "the first tree shows Tagline"); + let mut other = FakeLayers::sample(); + other.order = Some(1); + *rig.tree.borrow_mut() = Tracked::new(other); + rig.frame(); + assert_eq!(rig.builds(), 1, "the other tree builds its display order once"); + rig.fixture.idle(2); + assert!(rig.fixture.by_label("Levels 1").is_some(), "the other tree's rows show"); + assert!(rig.fixture.by_label("Tagline").is_none(), "and the first tree's rows are gone"); +} + +/// While the filter is active the display order follows the tracked revision too: a selection change builds it again, +/// as the filter reads what the rows show. +#[trial] +fn an_active_filter_builds_on_every_tracked_edit_of_a_tree_with_an_order_revision() { + let mut tree = FakeLayers::fifty(); + tree.order = Some(1); + let mut rig = rig(tree); + { + let mut filter = rig.filter.borrow_mut(); + filter.by = FilterBy::Name; + filter.name = String::from("mark"); + } + rig.frame(); + rig.frame(); + assert_eq!(rig.builds(), 0, "the filtered order is kept while nothing changes"); + let title = rig.tree.borrow().layers.iter().find(|layer| layer.info.name == "Title").map(|layer| layer.id).expect("Title is in the tree"); + rig.tree.borrow_mut().selection = LayerSelection::new(vec![title], Some(title), LayerPart::Pixels); + rig.frame(); + assert_eq!(rig.builds(), 1, "an edit under the filter builds the display order once"); +} + /// A filter change builds the display order once. #[trial] fn a_filter_change_builds_once() {