From 8be8078c0398cbb86e6be62d232c3ed06d8c9776 Mon Sep 17 00:00:00 2001 From: bflatastic Date: Sat, 21 Nov 2026 21:27:39 +0700 Subject: [PATCH] document-panels-w3: net change of 1 commits - document-panels-w3: written from its sheet --- crates/layers/src/ui/filter/mod.rs | 150 +++++++++++++++++++++---- crates/layers/src/ui/filter/stories.rs | 21 +++- crates/layers/tests/it/panel_filter.rs | 76 ++++++++++++- 3 files changed, 221 insertions(+), 26 deletions(-) diff --git a/crates/layers/src/ui/filter/mod.rs b/crates/layers/src/ui/filter/mod.rs index 14bf9ed3c7..088655b68e 100644 --- a/crates/layers/src/ui/filter/mod.rs +++ b/crates/layers/src/ui/filter/mod.rs @@ -10,11 +10,15 @@ mod stories; 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}; +use craft_ui::buttons::{Button, IconButton, ToggleButton}; +use craft_ui::choice::Checkbox; +use craft_ui::contract::{Bind, Entry, NoValue, Outcome, PopupKind, Props as _, Show as _, ShowContainer as _, Sizing}; +use craft_ui::display::{Shade, Text}; use craft_ui::layout_components::Row; use craft_ui::list::{ListWords, label_color_label}; use craft_ui::pickers::Select; +use craft_ui::popover::Popover; +use craft_ui::primitives::IconSpec; use craft_ui::runtime::env::{Env, Label}; use craft_ui::runtime::focus::{self, FocusTarget}; use craft_ui::runtime::memory::Remember as _; @@ -183,6 +187,33 @@ impl FilterObject { FilterObject::Guides => "guides", } } + + /// Whether `info`'s row is of this kind. + fn matches(self, info: &LayerInfo) -> bool { + match self { + FilterObject::Paths => info.kind == LayerKind::Path, + FilterObject::Shapes => info.kind == LayerKind::Shape, + FilterObject::Text => info.kind == LayerKind::Text, + FilterObject::Images => info.kind == LayerKind::Image, + FilterObject::Effects => info.has_effects, + FilterObject::Groups => matches!(info.kind, LayerKind::Group | LayerKind::Layer), + FilterObject::LinkedFiles => info.smart == Some(SmartSource::Linked), + FilterObject::Guides => info.kind == LayerKind::Guide, + } + } + + fn label(self, words: &LayerFilterWords) -> &Label { + match self { + FilterObject::Paths => &words.object_paths, + FilterObject::Shapes => &words.object_shapes, + FilterObject::Text => &words.object_text, + FilterObject::Images => &words.object_images, + FilterObject::Effects => &words.object_effects, + FilterObject::Groups => &words.object_groups, + FilterObject::LinkedFiles => &words.object_linked_files, + FilterObject::Guides => &words.object_guides, + } + } } /// A row state the Objects filter ticks. @@ -207,6 +238,21 @@ impl FilterState { FilterState::Hidden => "hidden", } } + + /// Whether `info`'s row is in this state. + fn matches(self, info: &LayerInfo) -> bool { + match self { + FilterState::Locked => info.locks.any(), + FilterState::Hidden => !info.visible, + } + } + + fn label(self, words: &LayerFilterWords) -> &Label { + match self { + FilterState::Locked => &words.state_locked, + FilterState::Hidden => &words.state_hidden, + } + } } /// A layer effect kind, as the Effect filter picks it and [`LayerInfo::effect_kinds`] reports it, @@ -472,7 +518,8 @@ impl Default for LayerFilter { impl LayerFilter { /// Whether the panel is filtered. As Photoshop, Effect, Mode, Attribute, Color, Artboard and /// Group always filter (by Bevel & Emboss, Normal, Visible and no colour at first); Kind and - /// Smart Object only once a toggle is on, Name only once the trimmed text is not empty. + /// Smart Object only once a toggle is on, Name only once the trimmed text is not empty, Objects once + /// something is ticked or the name is not blank. /// Artboard lists every layer while no artboard holds the selection ([`LayerFilter::lists`]). pub fn active(&self) -> bool { // pc: crates/ui-egui/src/panels/layer_filter_ui.rs `Filter::active`. @@ -481,13 +528,13 @@ impl LayerFilter { FilterBy::Name => !self.name.trim().is_empty(), FilterBy::SmartObject => !self.smart.is_empty(), FilterBy::Effect | FilterBy::Mode | FilterBy::Attribute | FilterBy::Color | FilterBy::Artboard | FilterBy::Group => true, - FilterBy::Objects => todo!("dp.F3"), + FilterBy::Objects => !self.objects.is_empty() || !self.states.is_empty() || !self.name.trim().is_empty(), } } /// Whether a row that holds a listed row stays listed at its own depth: true for Objects. pub fn keeps_holders(&self) -> bool { - todo!("dp.F4") + matches!(self.by, FilterBy::Objects) } /// Whether the filtered panel lists the contents of closed groups, so a match inside one shows: @@ -504,7 +551,8 @@ impl LayerFilter { /// `info.blend_key == mode`. Attribute: [`LayerAttribute::shows`]. Color: its label is /// `color`. Smart Object: its [`smart`](LayerInfo::smart) source is toggled on (Missing lists /// none here). Artboard and Group read the tree, so this lists every layer for them; use - /// [`LayerFilter::lists`]. + /// [`LayerFilter::lists`]. Objects: one of the ticked `objects` and one of the ticked `states` (each + /// group takes every row while nothing is ticked in it) and the name, all AND-ed. pub fn shows(&self, info: &LayerInfo) -> bool { // pc: crates/ui-egui/src/panels/layer_filter_ui.rs `Filter::shows`. if !self.active() { @@ -523,7 +571,11 @@ impl LayerFilter { None => false, }, FilterBy::Artboard | FilterBy::Group => true, - FilterBy::Objects => todo!("dp.F3"), + FilterBy::Objects => { + (self.objects.is_empty() || self.objects.iter().any(|object| object.matches(info))) + && (self.states.is_empty() || self.states.iter().any(|state| state.matches(info))) + && info.name.to_lowercase().contains(&self.name.trim().to_lowercase()) + } } } @@ -561,9 +613,10 @@ impl LayerFilter { /// 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. + /// channel) calls it there. A layer selected later, while the filter hides it, stays selected. Objects never + /// changes the selection. pub fn deselect_hidden(&self, tree: &T) -> Option> { - if !self.active() { + if !self.active() || self.by == FilterBy::Objects { return None; } let selection = tree.selection(); @@ -634,6 +687,7 @@ craft_ui::words! { smart_objects: "layerFilterBar.smartObjects" = "Smart Object"; artboard: "layerFilterBar.artboard" = "Artboard"; group: "layerFilterBar.group" = "Group"; + objects: "layerFilterBar.objects" = "Objects"; /// The kind toggles' tooltips and names. pixel: "layerFilterBar.pixel" = "Filter for pixel layers"; adjustment: "layerFilterBar.adjustment" = "Filter for adjustment layers"; @@ -727,6 +781,8 @@ craft_ui::words! { /// to Pass Through is never listed by Mode). /// - **Smart Object**: three icon toggle buttons in [`SmartKind::ALL`] order, as Kind's. /// - **Artboard**, **Group**: no controls. +/// - **Objects**: a search field and a Filter button whose popover ticks object kinds and row states; +/// Clear All unticks both. The type select shows only when more than one type is offered. /// - **Keyboard**: each control is its own Tab stop; a select opens with Space or Alt+Down. #[must_use = "show the filter bar with `show`"] #[derive(craft_ui::contract::Pattern, craft_ui::contract::Props)] @@ -765,6 +821,13 @@ impl Outcome for LayerFilterOutput { } } +/// Whether the Objects filter's popover is open: the bar's memory, so it stays open across passes until a +/// click outside or Escape closes it. +#[derive(Clone, Default)] +struct FilterPopup { + open: bool, +} + /// Whether the Name field selects its text as it takes focus: set in the pass of a find, for the /// pass after it, where the field first has the focus the find asked for. #[derive(Clone, Default)] @@ -806,11 +869,13 @@ impl<'a> LayerFilterBar<'a> { if focus_name { next.by = FilterBy::Name.among(&offered); } - let types: Vec> = offered.iter().copied().map(|by| Entry::new(by, type_label(by, &words).clone())).collect(); - // The type select fits its widest type, so the controls after it keep their room. - let picked = Select::new(words.filter_type.clone(), Bind::given(next.by), &types).sizing(Sizing::Content).show(ui); - if let Some(by) = picked.value() { - next.by = *by; + if offered.len() > 1 { + let types: Vec> = offered.iter().copied().map(|by| Entry::new(by, type_label(by, &words).clone())).collect(); + // The type select fits its widest type, so the controls after it keep their room. + let picked = Select::new(words.filter_type.clone(), Bind::given(next.by), &types).sizing(Sizing::Content).show(ui); + if let Some(by) = picked.value() { + next.by = *by; + } } // Controls follow the type picked before this pass; a find shows the Name field at once. let shown = if focus_name { next.by } else { current.by }; @@ -882,7 +947,46 @@ impl<'a> LayerFilterBar<'a> { } // The selected layers pick the artboards; Group lists every group. FilterBy::Artboard | FilterBy::Group => {} - FilterBy::Objects => todo!("dp.F6"), + FilterBy::Objects => { + let typed = TextField::search(words.search_layers.clone(), Bind::given(current.name.clone())) + .placeholder(words.search_all.clone()) + .show(ui); + if let Some(text) = typed.value() { + next.name.clone_from(text); + } + let ticked = !current.objects.is_empty() || !current.states.is_empty(); + let button = IconButton::new(IconSpec::svg(IconId::new("funnel"))) + .label(words.filter.clone()) + .selected(ticked) + .popup(PopupKind::Menu) + .show(ui); + let mut open = cx.memory::().open; + if button.clicked() { + open = !open; + } + Popover::new(words.filter.clone(), Bind::bound(&mut open)).opened_by(&button).show(ui, |ui, _| { + if Button::new(words.clear_all.clone()).enabled(ticked).show(ui).clicked() { + next.objects.clear(); + next.states.clear(); + } + let _ = Text::new(words.by_object.clone()).shade(Shade::Muted).show(ui); + for object in FilterObject::ALL { + let checked = + Checkbox::new(Bind::given(current.objects.contains(&object))).label(object.label(&words).clone()).show(ui); + if let Some(on) = checked.value() { + tick(&mut next.objects, object, *on); + } + } + let _ = Text::new(words.by_layer.clone()).shade(Shade::Muted).show(ui); + for state in FilterState::ALL { + let checked = Checkbox::new(Bind::given(current.states.contains(&state))).label(state.label(&words).clone()).show(ui); + if let Some(on) = checked.value() { + tick(&mut next.states, state, *on); + } + } + }); + cx.memory::().open = open; + } } if next != stored { let name: std::sync::Arc = std::sync::Arc::from(words.filter_type.as_str()); @@ -906,7 +1010,7 @@ fn type_label(by: FilterBy, words: &LayerFilterWords) -> &Label { FilterBy::SmartObject => &words.smart_objects, FilterBy::Artboard => &words.artboard, FilterBy::Group => &words.group, - FilterBy::Objects => todo!("dp.F6"), + FilterBy::Objects => &words.objects, } } @@ -929,10 +1033,16 @@ fn toggles(ui: &mut Ui, buttons: &[(K, &'static str, &Label .size(ControlSize::Sm) .id_salt(salt(value)) .show(ui); - match toggled.value() { - Some(true) => on.push(value), - Some(false) => on.retain(|held| *held != value), - None => {} + if let Some(ticked) = toggled.value() { + tick(on, value, *ticked); } } } + +/// Puts `value` in `on` or takes it out, so each value is held at most once and a new one comes last. +fn tick(on: &mut Vec, value: K, ticked: bool) { + on.retain(|held| *held != value); + if ticked { + on.push(value); + } +} diff --git a/crates/layers/src/ui/filter/stories.rs b/crates/layers/src/ui/filter/stories.rs index dd3658e334..f30ff9e400 100644 --- a/crates/layers/src/ui/filter/stories.rs +++ b/crates/layers/src/ui/filter/stories.rs @@ -1,11 +1,11 @@ //! Stories of `LayerFilterBar`: each filter type in one card, with the count of a document's layers it lets through. -use super::{EffectKind, FilterBy, FilterKind, LayerAttribute, LayerFilter, LayerFilterBar, SmartKind}; +use super::{EffectKind, FilterBy, FilterKind, FilterObject, FilterState, LayerAttribute, LayerFilter, LayerFilterBar, SmartKind}; use crate::ui::mock::FakeLayers; use crate::ui::tree::LayerTree as _; use craft_theme::LabelColor; use craft_ui::contract::Bind; -use craft_ui::testing::{Drive, StoryData, StoryOutput, story}; +use craft_ui::testing::{Drive, Fixture, StoryData, StoryOutput, story}; use egui::Ui; /// The bar over `filter`, with the count of `tree`'s layers the filter lists. @@ -85,3 +85,20 @@ fn by_group(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { let filter = data.value("filter", LayerFilter { by: FilterBy::Group, ..LayerFilter::default() }); show(ui, filter) } + +/// The drive of the Objects story: a click on the Filter button opens the popover. +fn open_filter(fixture: &mut Fixture) { + if let Some(button) = fixture.by_label("Filter") { + fixture.click(&button); + } +} + +/// The Objects filter with Paths and Locked ticked; the popover starts closed. +#[story(LayerFilterBar<'static>, covers = [], drive = Drive::Custom(open_filter), group = "Types", title = "Filter by Objects")] +fn layer_filter_objects(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { + let filter = data.value( + "filter", + LayerFilter { by: FilterBy::Objects, objects: vec![FilterObject::Paths], states: vec![FilterState::Locked], ..LayerFilter::default() }, + ); + show(ui, filter) +} diff --git a/crates/layers/tests/it/panel_filter.rs b/crates/layers/tests/it/panel_filter.rs index 18a7177bb9..3f30f09f48 100644 --- a/crates/layers/tests/it/panel_filter.rs +++ b/crates/layers/tests/it/panel_filter.rs @@ -2,10 +2,10 @@ //! Find Layers puts the caret in the Name field. use craft_behavior::SelectionChange; -use craft_layers::ui::filter::{EffectKind, FilterBy, FilterKind, LayerAttribute, LayerFilter, SmartKind}; +use craft_layers::ui::filter::{EffectKind, FilterBy, FilterKind, FilterObject, FilterState, LayerAttribute, LayerFilter, SmartKind, SmartSource}; use craft_layers::ui::mock::FakeLayers; use craft_layers::ui::panel::{LayersEvent, LayersPanel}; -use craft_layers::ui::tree::{LayerInfo, LayerKind, LayerPart, LayerSelection}; +use craft_layers::ui::tree::{LayerInfo, LayerKind, LayerLocks, LayerPart, LayerSelection}; use craft_ui::contract::Bind; use craft_ui::testing::{Fixture, trial}; use egui::Vec2; @@ -114,7 +114,8 @@ fn the_app_decides_what_only_it_knows() { } /// The layers of the matching table, top first: a type layer, a group, two pixel layers (the lower one hidden and with -/// a stroke, Multiply) and a Normal group. +/// a stroke, Multiply), a Normal group, then a path with effects, a locked shape, a hidden image, a linked smart object +/// and a hidden guide (their blend key matches no Mode case). fn matching_layers() -> Vec { let mut notes = LayerInfo::new("ink notes", LayerKind::Text); notes.blend_key = String::from("normal"); @@ -128,7 +129,24 @@ fn matching_layers() -> Vec { paper.visible = false; let mut normal = LayerInfo::new("Normal group", LayerKind::Group); normal.blend_key = String::from("normal"); - vec![notes, sheets, ink, paper, normal] + let other = |name: &str, kind: LayerKind| { + let mut info = LayerInfo::new(name, kind); + info.blend_key = String::from("other"); + info + }; + let mut curve = other("Curve", LayerKind::Path); + curve.has_effects = true; + let mut boxed = other("Box", LayerKind::Shape); + let mut locks = LayerLocks::default(); + locks.all = true; + boxed.locks = locks; + let mut photo = other("Photo", LayerKind::Image); + photo.visible = false; + let mut placed = other("Placed Logo", LayerKind::SmartObject); + placed.smart = Some(SmartSource::Linked); + let mut ruler = other("Ruler", LayerKind::Guide); + ruler.visible = false; + vec![notes, sheets, ink, paper, normal, curve, boxed, photo, placed, ruler] } /// The names `filter` lists among `layers`. @@ -147,6 +165,7 @@ fn each_filter_type_lists_its_matches() { edit(&mut filter); filter }; + let every: Vec<&str> = layers.iter().map(|info| info.name.as_str()).collect(); let cases: Vec<(LayerFilter, Vec<&str>, &str)> = vec![ (by(&|f| (f.by, f.kinds) = (FilterBy::Kind, vec![FilterKind::Pixel])), vec!["Ink", "Paper"], "Kind Pixel: groups match no kind"), (by(&|f| (f.by, f.kinds) = (FilterBy::Kind, vec![FilterKind::Type])), vec!["ink notes"], "Kind Type"), @@ -165,6 +184,38 @@ fn each_filter_type_lists_its_matches() { vec![], "Attribute Not Visible lists what Visible does not", ), + (by(&|f| (f.by, f.objects) = (FilterBy::Objects, vec![FilterObject::Paths])), vec!["Curve"], "Objects: one kind"), + ( + by(&|f| (f.by, f.objects) = (FilterBy::Objects, vec![FilterObject::Paths, FilterObject::Shapes])), + vec!["Curve", "Box"], + "Objects: two kinds are OR-ed", + ), + (by(&|f| (f.by, f.states) = (FilterBy::Objects, vec![FilterState::Locked])), vec!["Box"], "Objects: a state alone"), + ( + by(&|f| (f.by, f.states) = (FilterBy::Objects, vec![FilterState::Hidden])), + vec!["Paper", "Photo", "Ruler"], + "Objects: Hidden lists the rows that are not visible", + ), + ( + by(&|f| (f.by, f.objects, f.states) = (FilterBy::Objects, vec![FilterObject::Images, FilterObject::Guides], vec![FilterState::Hidden])), + vec!["Photo", "Ruler"], + "Objects: kinds AND states", + ), + ( + by(&|f| (f.by, f.objects, f.states) = (FilterBy::Objects, vec![FilterObject::Paths], vec![FilterState::Locked])), + vec![], + "Objects: no row is both", + ), + ( + by(&|f| (f.by, f.objects, f.name) = (FilterBy::Objects, vec![FilterObject::Paths, FilterObject::Shapes], String::from(" BO "))), + vec!["Box"], + "Objects: the name AND-ed with the kinds", + ), + (by(&|f| f.by = FilterBy::Objects), every.clone(), "Objects: an empty selection lists all"), + (by(&|f| (f.by, f.objects) = (FilterBy::Objects, vec![FilterObject::Guides])), vec!["Ruler"], "Objects: Guides"), + (by(&|f| (f.by, f.objects) = (FilterBy::Objects, vec![FilterObject::LinkedFiles])), vec!["Placed Logo"], "Objects: Linked Files"), + (by(&|f| (f.by, f.objects) = (FilterBy::Objects, vec![FilterObject::Effects])), vec!["Curve"], "Objects: Effects"), + (by(&|f| (f.by, f.objects) = (FilterBy::Objects, vec![FilterObject::Groups])), vec!["Sheets", "Normal group"], "Objects: Groups"), ]; for (filter, want, case) in cases { if filter.attribute == LayerAttribute::NotVisible && filter.by == FilterBy::Attribute { @@ -179,6 +230,23 @@ fn each_filter_type_lists_its_matches() { } } +/// With Objects on Paths, a closed group holding a path is listed at its own depth above the path, and a group holding +/// none is not; a Kind filter lists no holder. +#[trial] +fn an_object_filter_keeps_the_rows_holding_a_match_at_their_depth() { + let mut rig = rig(|filter| (filter.by, filter.objects) = (FilterBy::Objects, vec![FilterObject::Paths])); + let (oak, trees) = (rig.id("Oak"), rig.id("Trees")); + rig.tree.borrow_mut().layer_mut(oak).info.kind = LayerKind::Path; + rig.tree.borrow_mut().layer_mut(trees).open = false; + rig.fixture.idle(3); + assert_eq!(rig.rows(), ["Cover", "Trees", "Oak"], "the holders and the path; Shapes holds none"); + let left = |rig: &Rig, label: &str| rig.fixture.by_label(label).expect("the row is shown").rect.left(); + assert!(left(&rig, "Cover") < left(&rig, "Trees") && left(&rig, "Trees") < left(&rig, "Oak"), "each holder sits at its own depth"); + + rig.filter_by(|filter| (filter.by, filter.kinds) = (FilterBy::Kind, vec![FilterKind::Pixel])); + assert_eq!(rig.rows(), ["Pine", "Grass", "Loose", "Background"], "a Kind filter lists no holder"); +} + /// Find Layers switches to Name and selects the field's text, so typing replaces it. #[trial] fn find_layers_puts_the_caret_in_the_name_field_with_its_text_selected() {