Merge PR #11235: Layers panel: applying a filter deselects the layers it hides (#10879)

This commit is contained in:
David Mann committed 2026-11-04 14:50:45 +01:00
commit 6a989bacdc
4 files changed
+176

No files matched your search

@@ -313,11 +313,15 @@ pub fn bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
FilterBy::Artboard => {}
}
});
selection::follow(app, ui.ctx());
}
pub mod effect;
pub mod selection;
#[cfg(test)]
mod effect_tests;
#[cfg(test)]
mod selection_tests;
#[cfg(test)]
mod tests;
@@ -0,0 +1,49 @@
//! #10879: applying the Layers panel filter, or changing its criteria while it filters, deselects
//! the selected layers it no longer lists, as in Photoshop. The listed ones stay selected (one of
//! them becomes the active layer when the active one is hidden), and turning the filter off changes
//! nothing. Like any layer selection this is no history step. A layer selected later, while the
//! filter hides it, stays selected: the filter only acts when it changes.
use photocraft_doc::LayerId;
use serde_json::json;
use super::Filter;
use crate::PhotocraftApp;
/// Deselect the layers the filter hides when its criteria changed since the last call. The
/// criteria are kept in `ctx`'s memory, so a change from the filter row or from `ui.set
/// {layerFilter}` is seen the next time the row is drawn.
pub fn follow(app: &mut PhotocraftApp, ctx: &egui::Context) {
let id = egui::Id::unique("layer-filter-applied");
let now = Filter::of(&app.ui);
let before = ctx.data_mut(|d| {
let before = d.get_temp::<Filter>(id);
d.insert_temp(id, now.clone());
before
});
if before.is_some_and(|b| b != now) && now.active() {
deselect_hidden(app);
}
}
/// Deselect the active document's selected layers that [`Filter::of_app`] doesn't list: through
/// `select.deselectLayers` when none stays listed, else by keeping only the listed ones.
pub fn deselect_hidden(app: &mut PhotocraftApp) {
let f = Filter::of_app(app);
if !f.active() {
return;
}
let Some(st) = app.session.active() else { return };
let selected = st.selected_layers();
let listed: Vec<LayerId> = selected.iter().copied().filter(|id| st.doc.layer(*id).is_some_and(|l| f.shows(l))).collect();
if listed.len() == selected.len() && st.active_layer.is_none_or(|a| listed.contains(&a)) {
return;
}
if listed.is_empty() {
let _ = app.run("select.deselectLayers", json!({}));
return;
}
let active = st.active_layer.filter(|a| listed.contains(a)).or(listed.last().copied());
let anchor = st.layer_anchor.filter(|a| listed.contains(a)).or(active);
let _ = photocraft_engine::layer_multi_cmds::set_selection(&mut app.session, listed, active, anchor);
}
@@ -0,0 +1,118 @@
//! #10879: applying a Layers panel filter deselects the selected layers it hides, against the
//! cases measured in Photoshop.
use egui::vec2;
use egui_kittest::Harness;
use photocraft_doc::LayerId;
use serde_json::json;
use super::FilterBy;
use crate::PhotocraftApp;
/// "Red" and "Deep Pixel" (pixel layers) and "Hello" (type) over the Background, in the panel.
fn harness() -> Harness<'static, PhotocraftApp> {
let mut s = photocraft_engine::Session::new();
s.execute("file.new", json!({"width": 64, "height": 48})).unwrap();
s.execute("layer.new.layer", json!({"name": "Red"})).unwrap();
s.execute("layer.new.layer", json!({"name": "Deep Pixel"})).unwrap();
let text = s.execute("type.create", json!({"text": "Hello", "x": 4, "y": 20, "size": 12})).unwrap()["layer"].as_u64().unwrap();
s.execute("layer.setProps", json!({"layer": text, "name": "Hello"})).unwrap();
let mut h = Harness::builder().with_size(vec2(1440.0, 900.0)).with_max_steps(64).build_eframe(move |cc| {
PhotocraftApp::setup_context(&cc.egui_ctx, Default::default());
PhotocraftApp::new(s, crate::Services::default())
});
h.run_steps(8);
h
}
fn id(h: &Harness<'_, PhotocraftApp>, name: &str) -> LayerId {
h.state()
.session
.active()
.unwrap()
.doc
.walk()
.into_iter()
.find(|(_, _, l)| l.name == name)
.map(|(_, _, l)| l.id)
.unwrap_or_else(|| panic!("no layer {name}"))
}
fn select(h: &mut Harness<'_, PhotocraftApp>, names: &[&str]) {
for (i, name) in names.iter().enumerate() {
let layer = id(h, name).0;
h.state_mut().session.execute("layer.select", json!({"layer": layer, "mode": if i == 0 { "replace" } else { "add" }})).unwrap();
}
h.run_steps(4);
}
/// The selected layers' names, bottom to top, and the active layer's.
fn selection(h: &Harness<'_, PhotocraftApp>) -> (Vec<String>, Option<String>) {
let st = h.state().session.active().unwrap();
let name = |id: LayerId| st.doc.layer(id).unwrap().name.clone();
(st.selected_layers().into_iter().map(name).collect(), st.active_layer.map(name))
}
fn none() -> (Vec<String>, Option<String>) {
(Vec::new(), None)
}
#[test]
fn a_filter_that_hides_the_selected_layer_deselects_it_and_turning_it_off_keeps_that() {
let mut h = harness();
select(&mut h, &["Red"]);
h.state_mut().ui.layer_filter = vec!["type".into()];
h.run_steps(4);
assert_eq!(selection(&h), none(), "Kind › Type hides Red: no layer is selected");
h.state_mut().ui.layer_filter.clear();
h.run_steps(4);
assert_eq!(selection(&h), none(), "turning the filter off does not bring the selection back");
}
#[test]
fn a_filter_that_lists_none_of_the_selected_layers_deselects_them_all() {
let mut h = harness();
select(&mut h, &["Hello", "Deep Pixel"]);
h.state_mut().ui.layer_filter_by = FilterBy::SmartObject;
h.state_mut().ui.layer_filter_smart = vec!["embedded".into()];
h.run_steps(4);
assert_eq!(selection(&h), none(), "Smart Object › Embedded matches neither");
}
#[test]
fn the_listed_layers_stay_selected_and_one_of_them_becomes_active() {
let mut h = harness();
select(&mut h, &["Red", "Hello", "Deep Pixel"]);
assert_eq!(selection(&h).1.as_deref(), Some("Deep Pixel"));
h.state_mut().ui.layer_filter = vec!["type".into()];
h.run_steps(4);
assert_eq!(selection(&h), (vec!["Hello".to_string()], Some("Hello".to_string())));
let history = h.state().session.active().unwrap().history.entries().len();
// Changing the criteria while the filter is on acts again: Name "zzzz" lists nothing.
h.state_mut().ui.layer_filter_by = FilterBy::Name;
h.state_mut().ui.layer_filter_name = "zzzz".into();
h.run_steps(4);
assert_eq!(selection(&h), none());
assert_eq!(h.state().session.active().unwrap().history.entries().len(), history, "deselecting is no history step");
}
#[test]
fn a_layer_selected_while_the_filter_hides_it_stays_selected() {
let mut h = harness();
select(&mut h, &["Hello"]);
h.state_mut().ui.layer_filter = vec!["type".into()];
h.run_steps(4);
assert_eq!(selection(&h).0, ["Hello"]);
select(&mut h, &["Red"]);
assert_eq!(selection(&h).0, ["Red"], "the filter only acts when it is applied");
}
#[test]
fn a_filter_set_through_the_control_channel_deselects_too() {
let mut h = harness();
select(&mut h, &["Red"]);
let f = super::patched(&h.state().ui, &json!({"by": "name", "name": "hello"})).unwrap();
super::apply(&mut h.state_mut().ui, f);
h.run_steps(4);
assert_eq!(selection(&h), none());
}
@@ -297,6 +297,11 @@ fn a_hidden_filter_row_is_deselected() {
h.state_mut().ui.layer_filter_name.clear();
h.run_steps(4);
assert!(!selected(&h, "Gaussian Blur"));
// The Name filter hid Smart A too, so it deselected it (#10879): select it again.
assert!(h.state().session.active().unwrap().selected_layers().is_empty());
h.state_mut().session.execute("layer.select", json!({"layer": a})).unwrap();
h.run_steps(4);
assert!(!selected(&h, "Gaussian Blur"), "selecting the layer does not bring the filter row back");
h.key_press(egui::Key::Delete);
h.run_steps(4);
let st = h.state().session.active().unwrap();