craft-layers: the app chooses the filter types the Layers panel offers

This commit is contained in:
aiongg
2026-11-12 14:32:51 +00:00
parent ed285c40f5
commit 3ffaa00ea5
4 changed files with 97 additions and 12 deletions
+31 -7
View File
@@ -63,6 +63,12 @@ impl FilterBy {
FilterBy::Artboard,
FilterBy::Group,
];
/// The type the filter uses when `by` is not among `offered`: `by` itself when offered, else the
/// first offered type in [`ALL`](Self::ALL) order, or `by` when nothing is offered.
pub(crate) fn among(self, offered: &[FilterBy]) -> FilterBy {
if offered.contains(&self) { self } else { Self::ALL.into_iter().find(|by| offered.contains(by)).unwrap_or(self) }
}
}
/// A layer kind the Kind filter toggles.
@@ -584,6 +590,8 @@ pub struct LayerFilterBar<'a> {
/// text selected, so typing replaces it.
#[prop]
focus_name: bool,
/// The filter types the type select offers, in [`FilterBy::ALL`] order.
types: Vec<FilterBy>,
}
/// What [`LayerFilterBar::show`] returns.
@@ -617,29 +625,45 @@ struct FindName {
impl<'a> LayerFilterBar<'a> {
/// The bar editing `filter`.
pub fn new(filter: Bind<'a, LayerFilter>) -> Self {
Self { filter, blend_modes: &[], focus_name: false }
Self { filter, blend_modes: &[], focus_name: false, types: FilterBy::ALL.to_vec() }
}
/// The filter types the type select offers (default: all of [`FilterBy::ALL`]), listed in the
/// order `FilterBy` declares them whatever the order given. A filter whose type is not offered
/// shows and uses the first offered type, and reports that type as an edit. An empty list
/// draws no bar.
pub fn filter_types(mut self, types: impl Into<Vec<FilterBy>>) -> Self {
let given = types.into();
self.types = FilterBy::ALL.into_iter().filter(|by| given.contains(by)).collect();
self
}
/// Draws the bar on one row of the available width.
pub fn show(self, ui: &mut Ui) -> LayerFilterOutput {
let Self { mut filter, blend_modes, focus_name } = self;
let Self { mut filter, blend_modes, focus_name, types: offered } = self;
if offered.is_empty() {
return LayerFilterOutput { response: ui.allocate_response(egui::Vec2::ZERO, egui::Sense::hover()), changed: false, edit: None };
}
let words = Env::of(ui).words::<LayerFilterWords>();
let size = ControlSize::Sm;
let mut edit = None;
let row = Row::new().show(ui, |ui, cx| {
let select_name = std::mem::replace(&mut cx.memory::<FindName>().select, focus_name);
let Some(Mixed::Same(current)) = filter.reborrow().read(cx) else { return };
let stored = current;
let mut current = stored.clone();
current.by = current.by.among(&offered);
let mut next = current.clone();
if focus_name {
next.by = FilterBy::Name;
next.by = FilterBy::Name.among(&offered);
}
let types: Vec<Entry<FilterBy>> = FilterBy::ALL.into_iter().map(|by| Entry::new(by, type_label(by, &words).clone())).collect();
let types: Vec<Entry<FilterBy>> = offered.iter().copied().map(|by| Entry::new(by, type_label(by, &words).clone())).collect();
let picked = Select::new(words.filter_type.clone(), Bind::given(next.by), &types).size(size).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 { FilterBy::Name } else { current.by };
let shown = if focus_name { next.by } else { current.by };
match shown {
FilterBy::Kind => {
let kinds = [
@@ -710,9 +734,9 @@ impl<'a> LayerFilterBar<'a> {
// The selected layers pick the artboards; Group lists every group.
FilterBy::Artboard | FilterBy::Group => {}
}
if next != current {
if next != stored {
let name: std::sync::Arc<str> = std::sync::Arc::from(words.filter_type.as_str());
let event = EditEvent::set(Mixed::Same(current), next, name, None);
let event = EditEvent::set(Mixed::Same(stored), next, name, None);
filter.reborrow().write(cx, &event);
edit = Some(event);
}
+10
View File
@@ -36,6 +36,16 @@ fn by_name(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
show(ui, filter)
}
/// An app that offers three of the nine types.
#[story(LayerFilterBar<'static>, covers = [], drive = Drive::Never, group = "Types", title = "Offering Kind, Name and Mode")]
fn offered_types(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
let filter = data.value("filter", LayerFilter { by: FilterBy::Mode, mode: String::from("multiply"), ..LayerFilter::default() });
let tree = FakeLayers::fifty();
let modes = tree.blend_modes(false, "normal");
let bar = LayerFilterBar::new(Bind::bound(filter)).filter_types([FilterBy::Kind, FilterBy::Name, FilterBy::Mode]).blend_modes(&modes);
StoryOutput::from(bar.show(ui))
}
#[story(LayerFilterBar<'static>, covers = [], drive = Drive::Never, group = "Types", title = "Filter by Effect")]
fn by_effect(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
let filter = data.value("filter", LayerFilter { by: FilterBy::Effect, effect: EffectKind::Stroke, ..LayerFilter::default() });
+28 -5
View File
@@ -38,7 +38,7 @@ use footer::show_footer;
use menu::{OpenMenu, RowMenu, show_row_menus};
use std::marker::PhantomData;
use super::filter::{LayerFilter, LayerFilterBar};
use super::filter::{FilterBy, LayerFilter, LayerFilterBar};
use super::options::LayersSettings;
use super::rows::BlendRows;
use super::thumbs::LayerThumb;
@@ -169,6 +169,7 @@ pub struct LayersPanel<'a, T: LayerTree> {
visibility_key: Option<craft_keymap::Chord>,
blend_rows: bool,
filter_bar: bool,
filter_types: Vec<FilterBy>,
find_name: bool,
row_menu: Option<Box<RowMenu<'a, T::Id>>>,
trailing: Option<Box<dyn TrailingCell<T::Id> + 'a>>,
@@ -213,6 +214,7 @@ impl<'a, T: LayerTree> LayersPanel<'a, T> {
visibility_key: None,
blend_rows: true,
filter_bar: true,
filter_types: FilterBy::ALL.to_vec(),
find_name: false,
row_menu: None,
trailing: None,
@@ -269,6 +271,14 @@ impl<'a, T: LayerTree> LayersPanel<'a, T> {
self
}
/// The filter types the filter bar offers (default: all of [`FilterBy::ALL`]), listed in the order `FilterBy` declares
/// them. A filter whose type is not offered falls back to the first offered type: the bar shows it, the rows are
/// listed as if it were chosen, and the correction is a [`LayersEvent::Filter`]. An empty list draws no bar.
pub fn filter_types(mut self, types: impl Into<Vec<FilterBy>>) -> Self {
self.filter_types = types.into();
self
}
/// Find Layers (PhotoCraft's Select › Find Layers): this pass the filter bar switches to Name and its field takes
/// the focus with its text selected, so typing replaces it. A hidden filter bar stays hidden and takes no focus.
pub fn focus_filter_name(mut self) -> Self {
@@ -318,6 +328,7 @@ impl<'a, T: LayerTree> LayersPanel<'a, T> {
visibility_key,
blend_rows,
filter_bar,
filter_types,
find_name,
row_menu,
trailing,
@@ -358,20 +369,32 @@ impl<'a, T: LayerTree> LayersPanel<'a, T> {
}
};
let before = held(&mut filter, stack);
if filter_bar && settings.show_filters {
if filter_bar && settings.show_filters && !filter_types.is_empty() {
let modes = tree.blend_modes(false, &before.mode);
match filter.as_mut() {
Some(bind) => {
let out = LayerFilterBar::new(bind.reborrowed()).blend_modes(&modes).focus_name(find_name).show(ui);
let out = LayerFilterBar::new(bind.reborrowed())
.filter_types(filter_types.as_slice())
.blend_modes(&modes)
.focus_name(find_name)
.show(ui);
events.borrow_mut().extend(out.edit.map(LayersEvent::Filter));
}
None => {
let own = &mut stack.memory::<Session<T::Id>>().filter;
let _ = LayerFilterBar::new(Bind::bound(own)).blend_modes(&modes).focus_name(find_name).show(ui);
let _ = LayerFilterBar::new(Bind::bound(own))
.filter_types(filter_types.as_slice())
.blend_modes(&modes)
.focus_name(find_name)
.show(ui);
}
}
}
*current.borrow_mut() = held(&mut filter, stack);
let mut listed = held(&mut filter, stack);
if !filter_types.is_empty() {
listed.by = listed.by.among(&filter_types);
}
*current.borrow_mut() = listed;
if blend_rows {
let properties = tree.properties();
let modes =
+28
View File
@@ -124,3 +124,31 @@ fn find_layers_puts_the_caret_in_the_name_field_with_its_text_selected() {
assert_eq!(rig.filter.borrow().name, "pine", "the typed text replaces the old");
assert_eq!(rig.rows(), ["Pine"]);
}
/// A panel offering Kind and Name lists only those two in the type select, and a filter set to Artboard falls back to
/// Kind, reported as a filter event so the app stores it.
#[trial]
fn the_type_select_lists_only_the_offered_types() {
let tree = Rc::new(RefCell::new(FakeLayers::boards()));
let mut start = LayerFilter::default();
start.by = FilterBy::Artboard;
let filter = Rc::new(RefCell::new(start));
let (shown, by) = (Rc::clone(&tree), Rc::clone(&filter));
let mut fixture = Fixture::new(move |ui| {
let tree = shown.borrow();
let mut filter = by.borrow_mut();
let _ = LayersPanel::new(&*tree).filter(Bind::bound(&mut *filter)).filter_types([FilterBy::Name, FilterBy::Kind]).show(ui);
});
fixture.room(ONE_PANEL);
fixture.settle();
assert_eq!(filter.borrow().by, FilterBy::Kind, "the stored type falls back to the first offered");
let select = fixture.by_label("Filter type").expect("the type select is shown");
fixture.click(&select);
fixture.idle(3);
assert!(fixture.by_label("Name").is_some(), "Name is offered");
for hidden in ["Mode", "Effect", "Artboard", "Group"] {
assert!(fixture.by_label(hidden).is_none(), "{hidden} is not offered");
}
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");
}