PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Filter dialogs: OK keeps the preview until the filter lands (#1825)
kayrockscreenprinting21649f60309e
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rsindex 44643311a..56cd9ed58 100644--- a/crates/ui-egui/src/canvas.rs+++ b/crates/ui-egui/src/canvas.rs@@ -1101,6 +1101,9 @@ fn gpu_budget(app: &mut PhotocraftApp, gpu: &crate::gpu_canvas::GpuCanvas, idx:/// the deterministic inline path (#1676), which also lets headless/CPU tests inspect previews/// without a GPU texture.fn ensure_filter_preview(app: &mut PhotocraftApp, idx: usize, ctx: &egui::Context) -> Option<(u32, u64, [u32; 2])> {+ if let Some(held) = committed_filter_preview(app, idx) {+ return held;+ }// Command dialogs always edit the active document. Never show their preview in another tab.if app.session.active_index() != Some(idx) {return None;@@ -1134,7 +1137,7 @@ fn ensure_filter_preview(app: &mut PhotocraftApp, idx: usize, ctx: &egui::Contexmatch computed {Ok(computed) => publish_filter_preview(app, idx, finished, computed.result, computed.ms),Err(error) => {- app.filter_preview = Some(crate::filter_dialog::FilterPreview { key: finished, result: None });+ app.filter_preview = Some(crate::filter_dialog::FilterPreview { key: finished, result: None, committing: false });crate::notices::error(app, error);}}@@ -1151,7 +1154,7 @@ fn ensure_filter_preview(app: &mut PhotocraftApp, idx: usize, ctx: &egui::Contex});crate::filter_preview_worker::Computed { result, ms: crate::gpu_canvas::now_ms() - t0 }}) {- app.filter_preview = Some(crate::filter_dialog::FilterPreview { key: request.clone(), result: None });+ app.filter_preview = Some(crate::filter_dialog::FilterPreview { key: request.clone(), result: None, committing: false });crate::notices::error(app, error);}}@@ -1215,7 +1218,28 @@ fn publish_filter_preview(app.perf.record("filter-preview", result.size.area(), ms, crate::gpu_canvas::now_ms() - t0);Some(result)});- app.filter_preview = Some(crate::filter_dialog::FilterPreview { key: request, result });+ app.filter_preview = Some(crate::filter_dialog::FilterPreview { key: request, result, committing: false });+}++/// After a filter dialog's OK: `Some(shown)` while its preview stands in for the background job+/// that commits it (the document unchanged and the job still running, for this document's views),+/// `None` otherwise (other documents' views included). A job that lands, fails or is cancelled+/// drops it.+pub(crate) fn committed_filter_preview(app: &mut PhotocraftApp, idx: usize) -> Option<Option<(u32, u64, [u32; 2])>> {+ let p = app.filter_preview.as_ref().filter(|p| p.committing)?;+ let st = app.session.documents().get(idx)?;+ if st.doc.id != p.key.doc {+ return None;+ }+ let size = p.result.as_ref().map(|r| [r.size.width, r.size.height]);+ if let Some(size) = size+ && st.revision == p.key.revision+ && app.session.job_on(p.key.doc).is_some()+ {+ return Some(Some((p.key.k, p.key.doc.0 ^ (1u64 << 61), size)));+ }+ app.filter_preview = None;+ Some(None)}/// The document pixels a view shows, with a margin for filtering. Uses all four canvas cornersdiff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rsindex 500b166ae..27487e13f 100644--- a/crates/ui-egui/src/dialogs.rs+++ b/crates/ui-egui/src/dialogs.rs@@ -373,10 +373,22 @@ pub fn confirm(app: &mut PhotocraftApp, id: u64) -> Result<Value, String> {DialogKind::Command if crate::prefs_ui::owns(&d.fields) => crate::prefs_ui::confirm(app, &d.fields),DialogKind::Command if d.fields.contains_key("__export") => crate::export_dialog::confirm(app, &d.fields),DialogKind::Command => {- app.filter_preview = None;- let cmd = d.fields.get("__command").and_then(|v| v.as_str().map(str::to_string)).ok_or("dialog has no command")?;+ let Some(cmd) = d.fields.get("__command").and_then(Value::as_str).map(str::to_string) else {+ app.filter_preview = None;+ return Err("dialog has no command".into());+ };let (cmd, params) = crate::smart_ui::confirm_command(&d.fields, cmd, crate::filter_dialog::params_of(&d.fields));let result = app.run(&cmd, params.clone());+ // A filter that runs as a background job keeps its preview on screen until the job+ // lands (`canvas::committed_filter_preview`) instead of flashing the unfiltered image.+ let running = app+ .session+ .active()+ .is_some_and(|st| app.filter_preview.as_ref().is_some_and(|p| p.key.doc == st.doc.id) && app.session.job_on(st.doc.id).is_some());+ match app.filter_preview.as_mut() {+ Some(p) if result.is_ok() && running => p.committing = true,+ _ => app.filter_preview = None,+ }if result.is_ok() && cmd == "view.newGuideLayout" {app.ui.view.guide_layout = params;}@@ -411,6 +423,61 @@ pub fn open_command_dialog(app: &mut PhotocraftApp, command: &str, label: &str)#[cfg(test)]mod tests {use super::*;++ /// A filter dialog over a 256×256 layer, with the preview the canvas computed for it on screen.+ fn filter_dialog_with_preview(background: bool) -> (PhotocraftApp, u64) {+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());+ app.background_jobs = background;+ app.run("file.new", serde_json::json!({"width": 256, "height": 256, "background": "#3366cc"})).unwrap();+ let id = open_command_dialog(&mut app, "filter.blur.gaussianBlur", "Gaussian Blur");+ let st = app.session.active().unwrap();+ let result = crate::filter_dialog::preview_document(&st.doc, st.active_layer, "filter.blur.gaussianBlur", &serde_json::json!({"radius": 4.0}), 1);+ let key = crate::filter_dialog::FilterPreviewKey {+ doc: st.doc.id,+ revision: st.revision,+ dialog: id,+ active: st.active_layer,+ command: "filter.blur.gaussianBlur".into(),+ params: serde_json::json!({"radius": 4.0}),+ k: 1,+ };+ app.filter_preview = Some(crate::filter_dialog::FilterPreview { key, result: result.map(std::sync::Arc::new), committing: false });+ (app, id)+ }++ #[test]+ fn a_filters_preview_stays_on_screen_until_its_job_lands() {+ // OK dropped the preview and the filter ran as a background job, so the canvas flashed+ // the unfiltered image until the job finished.+ let (mut app, id) = filter_dialog_with_preview(true);+ let revision = app.session.active().unwrap().revision;+ confirm(&mut app, id).unwrap();+ let doc = app.session.active().unwrap().doc.id;+ let job = app.session.job_on(doc).expect("the blur runs as a background job").id;+ assert!(matches!(crate::canvas::committed_filter_preview(&mut app, 0), Some(Some((1, _, [256, 256])))), "the preview is shown");+ // The job lands: the document changes and the preview goes.+ app.session.wait_job(job).unwrap();+ assert_ne!(app.session.active().unwrap().revision, revision);+ assert_eq!(crate::canvas::committed_filter_preview(&mut app, 0), Some(None));+ assert!(app.filter_preview.is_none());++ // A cancelled job leaves the document as it was, so its preview goes too.+ let (mut app, id) = filter_dialog_with_preview(true);+ confirm(&mut app, id).unwrap();+ let doc = app.session.active().unwrap().doc.id;+ let job = app.session.job_on(doc).unwrap().id;+ assert!(matches!(crate::canvas::committed_filter_preview(&mut app, 0), Some(Some(_))));+ assert!(app.session.cancel_job(job));+ assert_eq!(crate::canvas::committed_filter_preview(&mut app, 0), Some(None));+ assert!(app.filter_preview.is_none());++ // Inline (no background jobs): the filter has already landed, nothing is held.+ let (mut app, id) = filter_dialog_with_preview(false);+ confirm(&mut app, id).unwrap();+ assert!(app.filter_preview.is_none());+ assert_eq!(crate::canvas::committed_filter_preview(&mut app, 0), None);+ }+#[test]fn color_picker_has_inset_content_and_actions_at_the_right_edge() {use egui_kittest::{Harness, kittest::Queryable};diff --git a/crates/ui-egui/src/filter_dialog.rs b/crates/ui-egui/src/filter_dialog.rsindex 8ea3a6774..97318e75e 100644--- a/crates/ui-egui/src/filter_dialog.rs+++ b/crates/ui-egui/src/filter_dialog.rs@@ -470,6 +470,9 @@ pub fn preview_document(doc: &Document, active: Option<photocraft_doc::LayerId>,pub struct FilterPreview {pub key: FilterPreviewKey,pub result: Option<Arc<Document>>,+ /// OK was pressed and the filter runs as a background job: the preview stays on screen until+ /// the job lands, so the canvas doesn't flash the unfiltered image in between.+ pub committing: bool,}/// Match the full request before accepting a worker result, including a reopened dialog.