From f3d0a4e2a1e119763539bf3df7ccda9a2c91729f Mon Sep 17 00:00:00 2001 From: aiongg Date: Thu, 12 Nov 2026 14:30:46 +0000 Subject: [PATCH] Lists keep the host's selection when the lead's row leaves; a new Layers filter deselects what it hides follow_lead_source moves the lead to the row now in its place and no longer selects that row: a row that was deleted or filtered out is not replaced by its neighbour. LayerFilter::deselect_hidden is the one rule for a newly applied filter (Photoshop #10879); the panel reports it as a Select on its filter bar's edit, and an app that sets the filter itself calls it there. --- crates/layers/src/ui/filter/mod.rs | 29 +++++++++- crates/layers/src/ui/panel/mod.rs | 11 +++- crates/layers/tests/it/panel_filter.rs | 53 ++++++++++++++++++- .../ui-contract/src/contract/items/hooks.rs | 2 +- crates/ui-contract/src/contract/items/lead.rs | 11 ++-- crates/ui-contract/src/contract/items/rows.rs | 6 --- .../ui-contract/tests/it/collection/rows.rs | 12 +++-- 7 files changed, 100 insertions(+), 24 deletions(-) diff --git a/crates/layers/src/ui/filter/mod.rs b/crates/layers/src/ui/filter/mod.rs index c17aaa730..5b080d3b2 100644 --- a/crates/layers/src/ui/filter/mod.rs +++ b/crates/layers/src/ui/filter/mod.rs @@ -8,7 +8,7 @@ #[cfg(feature = "screen-testing")] mod stories; -use craft_behavior::{EditEvent, IconId, Mixed}; +use craft_behavior::{EditEvent, IconId, Mixed, SelectionChange}; use craft_theme::{ControlSize, LabelColor}; use craft_ui::buttons::ToggleButton; use craft_ui::contract::{Bind, Entry, NoValue, Outcome, Props as _, Show as _, ShowContainer as _, Sizing}; @@ -21,6 +21,7 @@ use craft_ui::runtime::memory::Remember as _; use craft_ui::text::TextField; use egui::{Response, Ui}; +use super::panel::LayerSelect; use super::tree::{LayerInfo, LayerKind, LayerPart, LayerTree}; /// What the Layers panel filters by: the filter bar's type select, Photoshop's types in its order @@ -454,6 +455,32 @@ impl LayerFilter { _ => self.shows(&tree.info(id)), } } + + /// The selection change a newly applied filter makes, as Photoshop: the selected layers it no longer lists are + /// deselected, the listed ones stay selected and the active one stays active while listed (else the last listed + /// becomes active); `Clear` when none stays listed. `None` when the filter is off or lists every selected layer. + /// The panel calls it when its filter bar edits the filter; an app that sets the filter itself (a control + /// channel) calls it there. A layer selected later, while the filter hides it, stays selected. + pub fn deselect_hidden(&self, tree: &T) -> Option> { + if !self.active() { + return None; + } + let selection = tree.selection(); + let mut listed: Vec = selection.layers.iter().filter(|id| tree.listed(id, self)).cloned().collect(); + let active_hidden = selection.active.as_ref().is_some_and(|active| !listed.contains(active)); + if listed.len() == selection.layers.len() && !active_hidden { + return None; + } + let active = selection.active.filter(|active| listed.contains(active)); + if listed.is_empty() { + return Some(LayerSelect::new(SelectionChange::Clear, None)); + } + if let Some(active) = active { + listed.retain(|id| *id != active); + listed.push(active); + } + Some(LayerSelect::new(SelectionChange::Set(listed), None)) + } } /// The outermost artboard that holds `id`, or `id` itself when it is one. diff --git a/crates/layers/src/ui/panel/mod.rs b/crates/layers/src/ui/panel/mod.rs index 4e193fa32..3dacf9af7 100644 --- a/crates/layers/src/ui/panel/mod.rs +++ b/crates/layers/src/ui/panel/mod.rs @@ -369,6 +369,7 @@ impl<'a, T: LayerTree> LayersPanel<'a, T> { } }; let before = held(&mut filter, stack); + let mut edited = false; if filter_bar && settings.show_filters && !filter_types.is_empty() { let modes = tree.blend_modes(false, &before.mode); match filter.as_mut() { @@ -378,15 +379,17 @@ impl<'a, T: LayerTree> LayersPanel<'a, T> { .blend_modes(&modes) .focus_name(find_name) .show(ui); + edited = out.edit.is_some(); events.borrow_mut().extend(out.edit.map(LayersEvent::Filter)); } None => { let own = &mut stack.memory::>().filter; - let _ = LayerFilterBar::new(Bind::bound(own)) + edited = LayerFilterBar::new(Bind::bound(own)) .filter_types(filter_types.as_slice()) .blend_modes(&modes) .focus_name(find_name) - .show(ui); + .show(ui) + .changed; } } } @@ -394,6 +397,10 @@ impl<'a, T: LayerTree> LayersPanel<'a, T> { if !filter_types.is_empty() { listed.by = listed.by.among(&filter_types); } + // A filter the bar just applied deselects the layers it hides; the list picks no row in their place. + if edited && let Some(select) = listed.deselect_hidden(tree) { + events.borrow_mut().push(LayersEvent::Select(select)); + } *current.borrow_mut() = listed; if blend_rows { let properties = tree.properties(); diff --git a/crates/layers/tests/it/panel_filter.rs b/crates/layers/tests/it/panel_filter.rs index d1d2db918..09d4780d1 100644 --- a/crates/layers/tests/it/panel_filter.rs +++ b/crates/layers/tests/it/panel_filter.rs @@ -1,10 +1,11 @@ //! `LayersPanel` filters: Artboard and Group keep each group open or closed, the app answers what only it knows, and //! Find Layers puts the caret in the Name field. +use craft_behavior::SelectionChange; use craft_layers::ui::filter::{FilterBy, LayerAttribute, LayerFilter, SmartKind}; use craft_layers::ui::mock::FakeLayers; -use craft_layers::ui::panel::LayersPanel; -use craft_layers::ui::tree::LayerSelection; +use craft_layers::ui::panel::{LayersEvent, LayersPanel}; +use craft_layers::ui::tree::{LayerPart, LayerSelection}; use craft_ui::contract::Bind; use craft_ui::testing::{Fixture, trial}; use egui::Vec2; @@ -152,3 +153,51 @@ fn the_type_select_lists_only_the_offered_types() { let rows = BOARDS.iter().filter(|label| fixture.by_label(label).is_some()).count(); assert_eq!(rows, BOARDS.len(), "the fallback Kind filter is empty, so every layer is listed"); } + +/// #10879: a filter the bar applies deselects the selected layers it hides, keeps the listed ones (the active one last, +/// so it stays active) and clears the selection when none stays listed; the list picks no row in place of a hidden one. +#[trial] +fn a_filter_the_bar_applies_deselects_what_it_hides() { + let mut tree = FakeLayers::sample(); + tree.selection = LayerSelection::new(vec![3, 2, 4], Some(3), LayerPart::Pixels); + let tree = Rc::new(RefCell::new(tree)); + let filter = Rc::new(RefCell::new(LayerFilter::default())); + let find = Rc::new(Cell::new(false)); + let seen: Shared>> = Rc::default(); + let (shown, by, asked, sink) = (Rc::clone(&tree), Rc::clone(&filter), Rc::clone(&find), Rc::clone(&seen)); + let mut fixture = Fixture::new(move |ui| { + let tree = shown.borrow(); + let mut filter = by.borrow_mut(); + let mut panel = LayersPanel::new(&*tree).filter(Bind::bound(&mut *filter)); + if asked.replace(false) { + panel = panel.focus_filter_name(); + } + sink.borrow_mut().extend(panel.show(ui).events); + }); + fixture.room(ONE_PANEL); + fixture.settle(); + let selects = |seen: &Shared>>| -> Vec> { + seen.borrow_mut().drain(..).filter_map(|event| if let LayersEvent::Select(select) = event { Some(select.change) } else { None }).collect() + }; + selects(&seen); + + find.set(true); + fixture.idle(2); + fixture.type_text("ed"); + fixture.idle(2); + let changes = selects(&seen); + assert!(!changes.is_empty(), "the filter that hides Title deselects it"); + assert!(changes.iter().all(|change| *change == SelectionChange::Set(vec![2, 3])), "Clipped and Masked stay, Masked active: {changes:?}"); + + tree.borrow_mut().selection = LayerSelection::new(vec![2, 3], Some(3), LayerPart::Pixels); + find.set(true); + fixture.idle(2); + fixture.type_text("zz"); + fixture.idle(2); + let changes = selects(&seen); + assert!(!changes.is_empty() && changes.iter().all(|change| *change == SelectionChange::Clear), "none stays listed: {changes:?}"); + + tree.borrow_mut().selection = LayerSelection::new(vec![4], Some(4), LayerPart::Pixels); + fixture.idle(2); + assert!(selects(&seen).is_empty(), "a layer selected later, while the filter hides it, stays selected"); +} diff --git a/crates/ui-contract/src/contract/items/hooks.rs b/crates/ui-contract/src/contract/items/hooks.rs index af5119e44..75601c76d 100644 --- a/crates/ui-contract/src/contract/items/hooks.rs +++ b/crates/ui-contract/src/contract/items/hooks.rs @@ -350,7 +350,7 @@ impl ShapeHooks for ItemsHooks<'_, '_, C> { let header_held = self.header_step(cx); self.offset_x = self.offset_x.clamp(0.0, f64::from((self.content.x - rect.width()).max(0.0))); self.open_lead(comp); - self.follow_lead_source(comp); + self.follow_lead_source(); let lead_at = self.lead.key.as_ref().and_then(|key| self.index_of(key)); let props = comp.items().clone(); let Some(layout) = self.layout.take() else { return }; diff --git a/crates/ui-contract/src/contract/items/lead.rs b/crates/ui-contract/src/contract/items/lead.rs index 37df4e1cc..ced2fc9d7 100644 --- a/crates/ui-contract/src/contract/items/lead.rs +++ b/crates/ui-contract/src/contract/items/lead.rs @@ -106,19 +106,16 @@ impl ItemsHooks<'_, '_, C> { } } - /// A lead whose key left the source moves to the key now in its place, else the one before it. - pub(super) fn follow_lead_source(&mut self, comp: &C) { + /// A lead whose key left the source moves to the key now in its place, else the one before it. The selection is + /// the host's: a row that left (deleted, or hidden by a filter) is not replaced by its neighbour. + pub(super) fn follow_lead_source(&mut self) { let len = self.source.len(); if let Some(key) = self.lead.key.clone() && self.index_of(&key).is_none() { self.lead.key = None; if len > 0 { - let to = self.lead.at.min(len - 1); - self.move_lead(to, LeadSource::Keys); - if self.holds_key(&key, to) { - self.pick(comp, to, false, false); - } + self.move_lead(self.lead.at.min(len - 1), LeadSource::Keys); } } else if let Some(at) = self.lead.key.as_ref().and_then(|key| self.index_of(key)) { self.lead.at = at; diff --git a/crates/ui-contract/src/contract/items/rows.rs b/crates/ui-contract/src/contract/items/rows.rs index 4307f6213..875c0e6e6 100644 --- a/crates/ui-contract/src/contract/items/rows.rs +++ b/crates/ui-contract/src/contract/items/rows.rs @@ -90,12 +90,6 @@ impl ItemsHooks<'_, '_, C> { self.selection.contains(RowAt::new(&key, at, self.source)) } - /// Whether `key`, standing at position `at`, is selected. `key` need not be in the source: a selection of ranges - /// answers for the position. - pub(super) fn holds_key(&self, key: &C::Key, at: usize) -> bool { - self.selection.contains(RowAt::new(key, at, self.source)) - } - /// The selection placed once in the source's order; the reads of a pass go by position. pub(super) fn placed(&self) -> PlacedSelection { self.selection.placed(self.source) diff --git a/crates/ui-contract/tests/it/collection/rows.rs b/crates/ui-contract/tests/it/collection/rows.rs index 70730e140..3685818b3 100644 --- a/crates/ui-contract/tests/it/collection/rows.rs +++ b/crates/ui-contract/tests/it/collection/rows.rs @@ -359,10 +359,10 @@ fn rows_of_different_measured_heights_place_and_hit_correctly() { assert_eq!(log.leads().last(), Some(&2)); } -/// 17.3 step 9: when the lead's key leaves the source, the lead moves to the key now in its place and a selection -/// that held the old lead moves with it. +/// 17.3 step 9: when the lead's key leaves the source (deleted, or hidden by a filter), the lead moves to the key now +/// in its place and the selection stays the host's: the list selects no row in place of the one that left. #[trial] -fn a_selected_lead_that_leaves_the_source_moves_the_selection_to_the_new_lead() { +fn a_selected_lead_that_leaves_the_source_moves_the_lead_and_leaves_the_selection() { let rows = RefCell::new(vec![(10_usize, "a"), (11, "b"), (12, "c"), (13, "d")]); let selection = RefCell::new(Selection::::default()); let seen = Cell::new(None); @@ -385,12 +385,14 @@ fn a_selected_lead_that_leaves_the_source_moves_the_selection_to_the_new_lead() frames.idle(3, &mut show); pointer::click(&mut frames, ORIGIN + Vec2::new(5.0, ROW_H * 2.5), &mut show); assert_eq!(selection.borrow().items, vec![12]); + events.borrow_mut().clear(); rows.borrow_mut().remove(2); frames.idle(2, &mut show); - assert_eq!(selection.borrow().items, vec![13], "the selection follows the lead to the key now in its place"); + assert_eq!(selection.borrow().items, vec![12], "the selection is the host's: no neighbour is picked"); + assert!(!events.borrow().iter().any(|event| matches!(event, CollectionEvent::Selection(_))), "no selection change is reported"); let leads: Vec = events.borrow().iter().filter_map(|event| if let CollectionEvent::LeadMoved { key, .. } = event { Some(*key) } else { None }).collect(); - assert_eq!(leads.last(), Some(&13)); + assert_eq!(leads.last(), Some(&13), "the lead moves to the key now in its place"); } /// 17.3, I1: the lead row above the view grows (an auto row it measures taller): the rows in view stay where they