Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Smart filters: an edited filter's dialog previews again (#6324)

itsF4LCON0504331c51de
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs
index 391ad464f..22568ba3f 100644
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@ -1704,8 +1704,14 @@ pub(crate) fn ensure_filter_preview(app: &mut PhotocraftApp, idx: usize, ctx: &e
return None;
}
let cmd = d.fields.get("__command")?.as_str()?.to_string();
- // Previews edit what the command will: a targeted layer mask included (#780).
- let mut params = app.with_mask_target(&cmd, crate::filter_dialog::params_of(&d.fields));
+ // Previews edit what the command will: a targeted layer mask included (#780), and a smart
+ // filter being edited is replaced in its stack rather than stacked again (#6324).
+ let (cmd, mut params) = if d.fields.contains_key("__smartFilter") {
+ crate::smart_ui::confirm_command(&d.fields, cmd, crate::filter_dialog::params_of(&d.fields))
+ } else {
+ let params = app.with_mask_target(&cmd, crate::filter_dialog::params_of(&d.fields));
+ (cmd, params)
+ };
// Select and Mask previews in its view mode; holding X shows the image without one.
if crate::select_mask_view::owns(&d.fields) {
if crate::select_mask_view::hides_view(&d.fields) {
diff --git a/crates/ui-egui/src/filter_dialog.rs b/crates/ui-egui/src/filter_dialog.rs
index fa7cb9524..80bcd57d2 100644
--- a/crates/ui-egui/src/filter_dialog.rs
+++ b/crates/ui-egui/src/filter_dialog.rs
@@ -607,6 +607,8 @@ fn uses_angle_dial(command: &str, key: &str) -> bool {
/// eyedropper like Photoshop's. While armed, the next click on the image writes the merged
/// composite colour into the `color` field (the same sampler the Color Picker uses).
const REPLACE_COLOR: &str = "image.adjustments.replaceColor";
+/// What OK on an edited smart filter runs (`smart_ui::confirm_command`); its preview too.
+const SMART_FILTER_SET_PARAMS: &str = "layer.smartFilter.setParams";
const COLOR_EYEDROPPER: &str = "__colorEyedropper";
fn color_eyedropper_owns(f: &Map<String, Value>) -> bool {
@@ -896,8 +898,9 @@ pub(crate) fn preview_document_for_target(
return crate::select_mask_view::preview_document(doc, active, params, k, cancel);
}
// Warp Text renders the type again from the layer model (font size, position), which a
- // reduced proxy doesn't scale: it runs at full size and the result is reduced (#218).
- let (run_k, reduce_k) = if command == "type.warpText" { (1, k) } else { (k, 1) };
+ // reduced proxy doesn't scale: it runs at full size and the result is reduced (#218). So does
+ // a smart filter being edited: the smart object renders again from its source (#6324).
+ let (run_k, reduce_k) = if command == "type.warpText" || command == SMART_FILTER_SET_PARAMS { (1, k) } else { (k, 1) };
let proxy = crate::proxy::proxy_document(doc, run_k);
let mut s = photocraft_engine::Session::new();
s.set_inline_job_ctx(cancel.cloned());
diff --git a/crates/ui-egui/src/smart_ui.rs b/crates/ui-egui/src/smart_ui.rs
index 7f49843c5..722e8fe91 100644
--- a/crates/ui-egui/src/smart_ui.rs
+++ b/crates/ui-egui/src/smart_ui.rs
@@ -314,8 +314,12 @@ pub fn open_editor(app: &mut PhotocraftApp, ctx: &egui::Context, layer: u64, sm:
/// filter (`layer.smartFilter.setParams`, see [`confirm_command`]) instead of adding another.
fn edit_filter(app: &mut PhotocraftApp, id: u64, layer: u64, index: usize, params: &Value) {
if let Some(d) = app.ui.dialog_mut(id) {
- // The live preview would stack a second copy of the filter on the smart object.
- d.fields.remove("__preview");
+ // A filter's live preview runs `layer.smartFilter.setParams` on the proxy, as OK does
+ // (`canvas::ensure_filter_preview`, #6324). An adjustment's previews on the layer and
+ // would stack a second copy of it on the smart object.
+ if d.fields.contains_key("__adjust") {
+ d.fields.remove("__preview");
+ }
d.fields.insert("__smartFilter".into(), json!({"layer": layer, "index": index}));
if let Value::Object(p) = params {
for (k, v) in p {
@@ -452,4 +456,44 @@ mod tests {
assert_eq!(sm.smart_filters[0].params.get("brightness").and_then(Value::as_f64), Some(40.0));
assert_eq!(sm.smart_filters[0].params.get("contrast").and_then(Value::as_f64), Some(-10.0));
}
+
+ /// #6324: a smart filter opened for editing previews, and its preview replaces the filter in
+ /// the stack (as OK does) instead of stacking a second one.
+ #[test]
+ fn an_edited_smart_filter_previews_what_ok_gives() {
+ let mut s = photocraft_engine::Session::new();
+ s.execute("file.new", json!({"width": 64, "height": 48, "background": "#ffffff"})).unwrap();
+ s.execute("layer.new.layer", json!({})).unwrap();
+ s.execute("select.all", json!({})).unwrap();
+ s.execute("edit.stroke", json!({"width": 4, "color": "#000000", "location": "inside"})).unwrap();
+ s.execute("select.deselect", json!({})).unwrap();
+ let id = s.execute("layer.smartObjects.convertToSmartObject", json!({})).unwrap()["layer"].as_u64().unwrap();
+ s.execute("filter.blur.gaussianBlur", json!({"radius": 1.0})).unwrap();
+ let mut app = PhotocraftApp::new(s, crate::Services::default());
+ app.background_jobs = false;
+ let smart = |app: &PhotocraftApp, doc: &Document| match &doc.layer(LayerId(id)).unwrap().content {
+ LayerContent::Smart(sm) => sm.clone(),
+ other => panic!("expected a smart object, got {other:?} ({})", app.ui.status),
+ };
+ let sm = smart(&app, &app.session.active().unwrap().doc);
+ open_editor(&mut app, &egui::Context::default(), id, &sm, 0);
+ let d = app.ui.dialogs.last_mut().expect("the filter's dialog opened");
+ assert_eq!(d.fields.get("__preview"), Some(&json!(true)), "the dialog previews");
+ d.fields.insert("radius".into(), json!(6.0));
+ let dialog = d.id;
+
+ crate::canvas::ensure_filter_preview(&mut app, 0, &egui::Context::default()).expect("a preview");
+ let preview = app.filter_preview.as_ref().unwrap();
+ assert_eq!(preview.key.command, "layer.smartFilter.setParams");
+ let shown = preview.result.clone().expect("the preview's document");
+ let filters = smart(&app, &shown).smart_filters;
+ assert_eq!(filters.len(), 1, "the edited filter is replaced, not stacked");
+ assert_eq!(filters[0].params.get("radius").and_then(Value::as_f64), Some(6.0));
+ assert_eq!(smart(&app, &app.session.active().unwrap().doc).smart_filters[0].params["radius"], json!(1.0), "the document is untouched");
+
+ crate::dialogs::confirm(&mut app, dialog).unwrap();
+ let doc = &app.session.active().unwrap().doc;
+ assert_eq!(smart(&app, doc).smart_filters.len(), 1);
+ assert!(photocraft_compose::flatten(&shown).px == photocraft_compose::flatten(doc).px, "OK gives what the preview showed");
+ }
}