PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Smart Filters: Delete removes the selected filter, not its smart object (part of #4751) (#4764)
Marco Arnulfo665cb581ad64
diff --git a/crates/ui-egui/src/panels.rs b/crates/ui-egui/src/panels.rsindex a7f034782..601f049ee 100644--- a/crates/ui-egui/src/panels.rs+++ b/crates/ui-egui/src/panels.rs@@ -2389,7 +2389,14 @@ fn layers(app: &mut PhotocraftApp, ui: &mut egui::Ui) {("layer.delete", "Delete layer")};let trash = icons::button(ui, "trash", 26.0, false, tl!(label));- if trash.clicked() {+ // A selected smart filter row is what Delete removes, as in Photoshop (#4751).+ let picked_filter = crate::smart_ui::selected(&ctx, &doc, active);+ if trash.clicked()+ && let Some(picked) = picked_filter+ {+ actions.push(crate::smart_ui::trash_action(picked));+ crate::smart_ui::deselect(&ctx, doc.id);+ } else if trash.clicked() {// Like Photoshop, ⌥/Alt-click deletes without asking.let confirm = delete == "layer.delete" && !ui.input(|i| i.modifiers.alt);let params = if confirm { json!({"__trash": true}) } else { json!({}) };@@ -2844,6 +2851,7 @@ fn layer_row(} else if resp.clicked() && !eye_resp.clicked() && !toggled && !fx_toggled && !masks.clicked {let mode = select_mode(ui.input(|i| i.modifiers));actions.push(("layer.select".into(), json!({"layer": l.id.0, "mode": mode})));+ crate::smart_ui::deselect(ctx, doc.id);// A thumbnail explicitly selects content or a mask. Selecting an adjustment/fill row// defaults to its mask, but clicking its content thumbnail must leave the mask target.let pos = resp.interact_pointer_pos();diff --git a/crates/ui-egui/src/smart_filter_rows_tests.rs b/crates/ui-egui/src/smart_filter_rows_tests.rsindex 519402672..da34be9c6 100644--- a/crates/ui-egui/src/smart_filter_rows_tests.rs+++ b/crates/ui-egui/src/smart_filter_rows_tests.rs@@ -162,3 +162,42 @@ fn a_camera_raw_filter_copies_and_its_copy_opens_on_the_target() {let dialog = h.state().camera_raw.as_ref().expect("Camera Raw opened");assert_eq!(dialog.layer, LayerId(b));}++fn click_at(h: &mut egui_kittest::Harness<'static, PhotocraftApp>, p: Pos2) {+ h.hover_at(p);+ h.run_steps(1);+ h.drag_at(p);+ h.run_steps(1);+ h.drop_at(p);+ h.run_steps(4);+}++/// #4751: with a filter's row selected, clicking Delete removes that filter, not the smart object.+#[test]+fn clicking_delete_removes_the_selected_filter() {+ let (mut h, a, _, _) = panel();+ let blur = at(&h, "Gaussian Blur");+ click_at(&mut h, blur);+ let trash = at(&h, "Delete layer");+ click_at(&mut h, trash);+ assert_eq!(filters(&h, a), ["filter.other.offset"]);+ assert_eq!(h.state().session.active().unwrap().doc.layers.len(), 4, "no layer was deleted");+ // The deleted row's selection doesn't carry over to Offset, now at its index.+ click_at(&mut h, trash);+ assert_eq!(filters(&h, a), ["filter.other.offset"]);+}++/// Clicking the smart object's own row leaves its filters: Delete then deletes the layer as usual.+#[test]+fn clicking_the_smart_object_row_deselects_its_filter() {+ let (mut h, a, _, _) = panel();+ let offset = at(&h, "Offset");+ click_at(&mut h, offset);+ let row = at(&h, "Smart A");+ click_at(&mut h, row);+ assert_eq!(h.state().session.active().unwrap().active_layer, Some(LayerId(a)));+ assert!(!selected(&h, "Offset"));+ let trash = at(&h, "Delete layer");+ click_at(&mut h, trash);+ assert_eq!(filters(&h, a).len(), 2, "the filters stay");+}diff --git a/crates/ui-egui/src/smart_ui.rs b/crates/ui-egui/src/smart_ui.rsindex fd1ed6f01..9932c8866 100644--- a/crates/ui-egui/src/smart_ui.rs+++ b/crates/ui-egui/src/smart_ui.rs@@ -151,6 +151,26 @@ fn selected_key(doc: photocraft_doc::DocId) -> egui::Id {egui::Id::new(("smart-filter-selected", doc))}+/// The selected smart filter row, while its smart object is active and the filter still exists.+pub(crate) fn selected(ctx: &egui::Context, doc: &Document, active: Option<LayerId>) -> Option<FilterDrag> {+ let (layer, filter) = ctx.data(|d| d.get_temp::<FilterDrag>(selected_key(doc.id)))?;+ if active != Some(LayerId(layer)) {+ return None;+ }+ let LayerContent::Smart(sm) = &doc.layer(LayerId(layer))?.content else { return None };+ let exists = match filter {+ Some(i) => i < sm.smart_filters.len(),+ None => !sm.smart_filters.is_empty(),+ };+ exists.then_some((layer, filter))+}++/// Forgets the selected smart filter row: its smart object's own row was clicked, or the filter+/// was deleted (its index would now name the next one).+pub(crate) fn deselect(ctx: &egui::Context, doc: photocraft_doc::DocId) {+ ctx.data_mut(|d| d.remove::<FilterDrag>(selected_key(doc)));+}+/// While smart filters are dragged: their name by the pointer, and the copy cursor while ⌥ is held.pub(crate) fn drag_feedback(ctx: &egui::Context, doc: &Document) {let Some((from, filter)) = ctx.data(|d| d.get_temp::<FilterDrag>(drag_key())) else { return };