From a5fac14ae0693fa09d3037772de29dcb6943d49e Mon Sep 17 00:00:00 2001 From: hnoerenberg Date: Wed, 25 Nov 2026 10:24:14 +0100 Subject: [PATCH] feat(dialogs): the Calculations dialog previews its result channel in grey The reference application shows what Calculations will produce while the dialog is open: a Preview check box, on by default and keyed to Alt+P, paints the result channel as a grey image over the whole canvas, and every field change redraws it. Until now the canvas kept showing the unchanged image until OK. The form is a hosted parameter dialog, so the check box and its state come from the framework's own Preview half (`previews(true)`): captioned, starting on, read and written as the `preview` field for automation. The command itself only adds a channel, so the post-OK document composites exactly as before and cannot be the picture; the canvas preview instead runs the same dialog values through the same engine as the New Document result, whose one-layer grayscale document is the grey rendering of the result channel, and the existing filter-preview pipeline draws it over the canvas at the document's proxy factor. Nothing is written: the run happens in a scratch session, so Cancel leaves the document, its channels and its history untouched, and OK stays the single "Calculations" step with exactly the values the preview showed. The pinned framework draws the box but keys no shortcut to it, so Alt+P is bound app-side, consumed in the canvas pass that feeds the preview. Fixes #14530 --- crates/ui-egui/src/canvas/filter_preview.rs | 7 + crates/ui-egui/src/dialogs.rs | 3 + .../src/dialogs/calculations_preview_tests.rs | 265 ++++++++++++++++++ crates/ui-egui/src/dialogs/calculations_ui.rs | 59 ++++ crates/ui-egui/src/menus/view_cmds/dialogs.rs | 9 +- 5 files changed, 342 insertions(+), 1 deletion(-) create mode 100644 crates/ui-egui/src/dialogs/calculations_preview_tests.rs create mode 100644 crates/ui-egui/src/dialogs/calculations_ui.rs diff --git a/crates/ui-egui/src/canvas/filter_preview.rs b/crates/ui-egui/src/canvas/filter_preview.rs index 9ac9589238..44eb6126b9 100644 --- a/crates/ui-egui/src/canvas/filter_preview.rs +++ b/crates/ui-egui/src/canvas/filter_preview.rs @@ -54,6 +54,9 @@ fn adjust_request(app: &PhotocraftApp) -> Option<(craft::ui::dialog::DialogId, S /// deterministic inline path (#1676), which also lets headless/CPU tests inspect previews /// without a GPU texture. pub(crate) fn ensure_filter_preview(app: &mut PhotocraftApp, idx: usize, ctx: &egui::Context) -> Option<(u32, u64, [u32; 2])> { + // Alt+P toggles the Calculations dialog's Preview box: the pinned framework draws the box but keys no + // shortcut to it, and the canvas pass is where a live preview is asked for (#14530). + crate::dialogs::calculations_ui::preview_shortcut(app, ctx); if let Some(held) = committed_filter_preview(app, idx) { return held; } @@ -86,6 +89,10 @@ pub(crate) fn ensure_filter_preview(app: &mut PhotocraftApp, idx: usize, ctx: &e crate::smart_filter_dialog::blend_command(app, &target, &dialog_fields).ok()? } else if crate::smart_filter_dialog::is_target(&target) { crate::smart_filter_dialog::params_command(app, &target, &key, crate::filter_dialog::params_of(&fields)).ok()? + } else if key == crate::dialogs::calculations_ui::COMMAND { + // Calculations previews the result channel as a grey image over the whole canvas, not the + // (unchanged) post-OK document: the values run as their New Document result (#14530). + crate::dialogs::calculations_ui::preview_request(app, &fields) } else { (key.clone(), app.with_mask_target(&key, crate::filter_dialog::params_of(&fields))) }; diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs index 7054431675..42426fe7d0 100644 --- a/crates/ui-egui/src/dialogs.rs +++ b/crates/ui-egui/src/dialogs.rs @@ -713,6 +713,9 @@ pub mod blend_options; pub mod blur_pins; pub mod brush_preset_dialog; pub mod caf_ui; +#[cfg(test)] +mod calculations_preview_tests; +pub(crate) mod calculations_ui; pub(super) mod camera_raw_detail_ui; pub(super) mod camera_raw_gpu; pub(super) mod camera_raw_scope_ui; diff --git a/crates/ui-egui/src/dialogs/calculations_preview_tests.rs b/crates/ui-egui/src/dialogs/calculations_preview_tests.rs new file mode 100644 index 0000000000..1c5f4ed233 --- /dev/null +++ b/crates/ui-egui/src/dialogs/calculations_preview_tests.rs @@ -0,0 +1,265 @@ +//! The Calculations dialog's live Preview (#14530): a check box on by default that shows the +//! result channel as a grey image over the whole canvas while the fields change, Alt+P and +//! automation toggling it, Cancel leaving the document untouched, and OK writing exactly what the +//! preview showed. The dialog form is opened as its menu item opens it; the canvas side is read +//! through the same entry point the canvas pass itself uses (`ensure_filter_preview`), with +//! previews computed inline (no worker thread) so each pass is deterministic. + +use crate::PhotocraftApp; +use crate::canvas::display_doc; +use craft::ui::dialog::{DialogId, StackDialog as _}; +use craft::ui::patterns::form_dialogs::HostedParamDialog; +use serde_json::{Value, json}; + +/// A 4x3 document whose only layer is solid red, so the Red channel reads 1 and the Green channel 0: +/// every blending of the two gives a value the test can name. +fn red_app() -> Box { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.background_jobs = false; + app.run("file.new", json!({"width": 4, "height": 3})).expect("a document"); + app.run("edit.fill", json!({"color": "#ff0000"})).expect("fill the layer solid red"); + app.sync_views(); + app +} + +/// Image › Calculations as its menu item runs it, then the first pass that loads the form's values. +fn open_calculations(app: &mut PhotocraftApp) -> DialogId { + let ctx = app.ctx().clone(); + let reply = crate::menus::invoke(app, &ctx, "image.calculations", json!({})).expect("the command opens its dialog"); + let want = reply["dialog"].as_str().expect("the reply names a dialog"); + let id = + app.framework.dialogs.open_dialogs().into_iter().map(|open| open.id).find(|open| open.value().to_string() == want).expect("the named dialog is open"); + crate::dialogs::testing::show(app); + id +} + +/// The open parameter dialog: the Calculations form. +fn hosted(app: &PhotocraftApp) -> &HostedParamDialog { + let dialog = app.framework.dialogs.get::().expect("a parameter dialog is open"); + assert_eq!(dialog.key(), Some("image.calculations"), "the open form is the Calculations dialog"); + dialog +} + +/// Sets a row of the open form, as a click on that row would. +fn set_field(app: &mut PhotocraftApp, id: DialogId, key: &str, value: Value) { + let ctx = app.ctx().clone(); + app.framework.dialogs.set_json_field(&ctx, id, key, value).unwrap_or_else(|error| panic!("the Calculations form takes {key}: {error}")); +} + +/// The document the canvas would show from the open dialog's preview, or None while it shows the +/// plain document. +fn previewed(app: &mut PhotocraftApp) -> Option { + let ctx = app.ctx().clone(); + crate::canvas::ensure_filter_preview(app, 0, &ctx)?; + app.filter_preview.as_ref().and_then(|preview| preview.result.clone()).map(|doc| (*doc).clone()) +} + +/// Every pixel of `doc` as a grey 8-bit value; a pixel with differing colour channels fails here, +/// so "in grey" is checked for every pixel of the whole document. +fn greys(doc: &photocraft_doc::Document) -> Vec { + photocraft_compose::flatten(doc) + .px + .iter() + .map(|p| { + assert!((p[0] - p[1]).abs() < 0.01 && (p[0] - p[2]).abs() < 0.01 && p[3] > 0.99, "grey over the whole document: {p:?}"); + (p[0] * 255.0 + 0.5) as u32 + }) + .collect() +} + +/// The Done-when row "the Preview check box, on by default": the framework reports the box only for +/// a dialog that previews, and it starts on, before its first pass answers anything. +#[test] +fn the_calculations_form_has_a_preview_box_on_by_default() { + let mut app = red_app(); + open_calculations(&mut app); + assert!(hosted(&app).is_previewing(), "the Calculations form has a Preview check box, on while it opens"); +} + +/// The measured reference: with Preview on, the canvas shows the result channel as a grey image +/// over the whole document. Both sources name channels of the solid red layer: Green is 0, Red is +/// 1, so Multiply gives black across every pixel. +#[test] +fn preview_shows_the_grey_result_channel_over_the_whole_document() { + let mut app = red_app(); + let id = open_calculations(&mut app); + set_field(&mut app, id, "source1Channel", json!("Red")); + set_field(&mut app, id, "source2Channel", json!("Green")); + set_field(&mut app, id, "blending", json!("multiply")); + let shown = previewed(&mut app).expect("with the Preview box on the canvas shows the preview"); + assert_eq!([shown.size.width, shown.size.height], [4, 3], "the grey image covers the whole document"); + assert_eq!(greys(&shown), vec![0; 12], "Multiply of Green 0 with Red 1 is black over every pixel"); +} + +/// The measured reference updates the preview as any field changes; here Blending, Opacity and +/// Invert Source 2 each redraw the grey channel in the same pass. +#[test] +fn every_field_change_redraws_the_grey_preview_live() { + let mut app = red_app(); + let id = open_calculations(&mut app); + set_field(&mut app, id, "source1Channel", json!("Red")); + set_field(&mut app, id, "source2Channel", json!("Green")); + set_field(&mut app, id, "blending", json!("multiply")); + let multiply = greys(&previewed(&mut app).expect("the opening preview")); + assert!(multiply.iter().all(|v| *v < 2), "Multiply of 0 with 1 is black: {multiply:?}"); + + set_field(&mut app, id, "blending", json!("screen")); + let screen = greys(&previewed(&mut app).expect("the preview follows the Blending row")); + assert!(screen.iter().all(|v| *v > 253), "Screen of 0 with 1 is white: {screen:?}"); + + set_field(&mut app, id, "opacity", json!(50.0)); + let half = greys(&previewed(&mut app).expect("the preview follows the Opacity row")); + assert!(half.iter().all(|v| (125..=131).contains(v)), "half opacity stands between the two: {half:?}"); + + set_field(&mut app, id, "source2Invert", json!(true)); + let inverted = greys(&previewed(&mut app).expect("the preview follows the Invert Source 2 row")); + assert!(inverted.iter().all(|v| *v > 253), "inverting Green gives Screen of 1 with 1: {inverted:?}"); +} + +/// Alt+P toggles the box (the reference application's mnemonic); with the box off the canvas shows +/// the normal image again, and Alt+P turns the preview back on. +#[test] +fn alt_p_toggles_the_preview_and_the_canvas_shows_the_document_without_it() { + let mut app = red_app(); + let id = open_calculations(&mut app); + set_field(&mut app, id, "source1Channel", json!("Red")); + set_field(&mut app, id, "source2Channel", json!("Green")); + assert!(previewed(&mut app).is_some(), "the preview shows while the box is on"); + + crate::canvas::testing::key(&mut app, egui::Key::P, egui::Modifiers::ALT); + assert!(!hosted(&app).is_previewing(), "Alt+P turns the Preview box off"); + assert!(previewed(&mut app).is_none(), "with the box off the preview is gone"); + let (shown, key) = display_doc(&mut app, 0); + assert_eq!(key, 0, "the canvas draws the document, not a preview"); + assert!(std::sync::Arc::ptr_eq(&shown, &app.session.documents()[0].doc), "the normal image is the document itself"); + + crate::canvas::testing::key(&mut app, egui::Key::P, egui::Modifiers::ALT); + assert!(hosted(&app).is_previewing(), "Alt+P turns the Preview box back on"); + assert!(previewed(&mut app).is_some(), "and the grey channel is on the canvas again"); + assert!(app.framework.dialogs.get_by_id::(id).is_some(), "the dialog stayed open across both toggles"); +} + +/// The automation channel reads and writes the box like any other row, so a script can turn the +/// preview on and off without a keyboard. +#[test] +fn automation_sets_the_preview_box() { + let mut app = red_app(); + let id = open_calculations(&mut app); + assert!(hosted(&app).is_previewing(), "on while it opens"); + set_field(&mut app, id, "preview", json!(false)); + assert!(!hosted(&app).is_previewing(), "the preview row reaches the box"); + assert!(previewed(&mut app).is_none(), "and the canvas shows the plain document"); + set_field(&mut app, id, "preview", json!(true)); + assert!(previewed(&mut app).is_some(), "a script can turn it back on"); +} + +/// Neighbour: the other channel-workflow forms the same opener builds keep today's behaviour - +/// Apply Image and Save Selection get no Preview box, and Alt+P does nothing to them. +#[test] +fn the_other_channel_forms_keep_no_preview_box() { + for command in ["image.applyImage", "select.saveSelection"] { + let mut app = red_app(); + let ctx = app.ctx().clone(); + let reply = crate::menus::invoke(&mut app, &ctx, command, json!({})).expect("the command opens its dialog"); + let want = reply["dialog"].as_str().expect("the reply names a dialog"); + let id = app + .framework + .dialogs + .open_dialogs() + .into_iter() + .map(|open| open.id) + .find(|open| open.value().to_string() == want) + .expect("the named dialog is open"); + crate::dialogs::testing::show(&mut app); + let dialog = app.framework.dialogs.get_by_id::(id).expect("the form is open"); + assert_eq!(dialog.key(), Some(command)); + assert!(!dialog.is_previewing(), "{command} has no Preview box"); + crate::canvas::testing::key(&mut app, egui::Key::P, egui::Modifiers::ALT); + assert!(!app.framework.dialogs.get_by_id::(id).expect("still open").is_previewing(), "Alt+P changes nothing in {command}"); + assert_eq!(app.session.active().expect("a document").doc.channels.len(), 0, "{command}: Alt+P wrote nothing"); + } +} + +/// Neighbour: another document's canvas never draws the Calculations preview - command dialogs +/// belong to the active document, as with every command preview. +#[test] +fn another_documents_canvas_shows_no_calculations_preview() { + let mut app = red_app(); + app.run("file.new", json!({"width": 6, "height": 6})).expect("a second document"); + app.sync_views(); + let id = open_calculations(&mut app); + assert!(hosted(&app).is_previewing(), "the new document's form previews"); + let ctx = app.ctx().clone(); + assert!(crate::canvas::ensure_filter_preview(&mut app, 0, &ctx).is_none(), "the inactive document's canvas shows nothing"); + assert!(crate::canvas::ensure_filter_preview(&mut app, 1, &ctx).is_some(), "the active document's canvas previews"); + assert!(app.framework.dialogs.get_by_id::(id).is_some(), "the dialog stayed open"); +} + +/// The measured reference shows the grey result channel; what the other Result choices preview +/// "beyond" it is not measured, and nothing more is shown: choosing New Document or Selection +/// keeps the same grey channel picture. +#[test] +fn every_result_choice_shows_the_same_grey_channel() { + let mut app = red_app(); + let id = open_calculations(&mut app); + set_field(&mut app, id, "source1Channel", json!("Red")); + set_field(&mut app, id, "source2Channel", json!("Green")); + set_field(&mut app, id, "blending", json!("multiply")); + for result in ["newDocument", "selection"] { + set_field(&mut app, id, "result", json!(result)); + let shown = previewed(&mut app).unwrap_or_else(|| panic!("{result}: the canvas still previews the grey channel")); + assert_eq!(greys(&shown), vec![0; 12], "{result}: the same grey channel picture"); + } +} + +/// The Done-when row "Cancel leaves the document, its channels and its history unchanged": even +/// with a preview computed and fields edited, nothing was written. +#[test] +fn cancel_leaves_the_document_its_channels_and_its_history_unchanged() { + let mut app = red_app(); + let id = open_calculations(&mut app); + set_field(&mut app, id, "source1Channel", json!("Red")); + set_field(&mut app, id, "source2Channel", json!("Green")); + set_field(&mut app, id, "blending", json!("screen")); + let shown = greys(&previewed(&mut app).expect("the preview shows while the dialog is open")); + assert!(shown.iter().all(|v| *v > 253), "a real preview was computed before Cancel"); + + let (channels, history, revision) = { + let st = app.session.active().expect("a document"); + (st.doc.channels.len(), st.history.entries(), st.revision) + }; + crate::dialogs::testing::cancel(&mut app, id).expect("Cancel answers the open dialog"); + let st = app.session.active().expect("the document is still open"); + assert_eq!(st.doc.channels.len(), channels, "no channel was added by the preview"); + assert_eq!(st.history.entries(), history, "the preview left no history step"); + assert_eq!(st.revision, revision, "the document itself did not change"); + assert!(previewed(&mut app).is_none(), "the closed dialog shows nothing on the canvas"); +} + +/// The Done-when row "OK gives the same values as the preview showed and adds exactly one +/// Calculations history step". +#[test] +fn ok_writes_one_calculations_step_with_the_values_the_preview_showed() { + let mut app = red_app(); + let id = open_calculations(&mut app); + set_field(&mut app, id, "source1Channel", json!("Red")); + set_field(&mut app, id, "source2Channel", json!("Green")); + set_field(&mut app, id, "blending", json!("screen")); + set_field(&mut app, id, "opacity", json!(50.0)); + let preview = greys(&previewed(&mut app).expect("the preview stands before OK")); + assert!(preview.iter().all(|v| (125..=131).contains(v)), "the preview holds half-Screen: {preview:?}"); + + let history = app.session.active().expect("a document").history.entries(); + crate::dialogs::testing::confirm(&mut app, id).expect("OK runs the command"); + let st = app.session.active().expect("the document is open"); + assert_eq!(st.history.entries().len(), history.len() + 1, "exactly one history step"); + assert_eq!(st.history.entries().last().map(String::as_str), Some("Calculations"), "the step is named Calculations"); + assert_eq!(st.doc.channels.len(), 1, "OK adds the result channel the preview showed"); + let got = st.doc.channels[0].surface.read_region(st.doc.bounds()); + for (i, grey) in preview.iter().enumerate() { + let want = *grey as f32 / 255.0; + assert!((got[i] - want).abs() <= 1.5 / 255.0, "OK gives the value the preview showed at {i}: {} vs {want}", got[i]); + } + assert_eq!(st.doc.channels[0].name, "Alpha 1", "the next free channel name"); + assert!(previewed(&mut app).is_none(), "the preview is gone once OK has run"); +} diff --git a/crates/ui-egui/src/dialogs/calculations_ui.rs b/crates/ui-egui/src/dialogs/calculations_ui.rs new file mode 100644 index 0000000000..e69391ca92 --- /dev/null +++ b/crates/ui-egui/src/dialogs/calculations_ui.rs @@ -0,0 +1,59 @@ +//! The Calculations dialog's live canvas preview (#14530). +//! +//! The dialog is the hosted parameter form `view_cmds::dialogs` opens for the command +//! [`COMMAND`]; its Preview check box and the value's automation half come from the framework's +//! form dialog (`ParamDialog::previews`): a captioned box, on from the first pass, reported and +//! written as the `preview` field like every other value. The command itself only adds a channel, +//! so the post-OK document composites exactly as before: what the measured reference shows on the +//! canvas is not the edited document but the *result channel itself*, as a grey image over the +//! whole document, redrawn with every field. [`preview_request`] gets that picture out of the same +//! engine: the form's values run on a scratch session as the New Document result, whose one-layer +//! grayscale document is the result channel's grey rendering, and the existing filter-preview +//! pipeline draws it over the canvas (proxy factor, GPU texture and CPU path included). While the +//! dialog stands, the document, its channels and its history stay untouched; OK runs the real +//! command with the chosen Result and the preview drops. +//! +//! Alt+P toggles the box while the dialog is open, as in the reference application (its mnemonic P). +//! The pinned framework draws the box but keys no shortcut to it, so the app binds the key here: +//! [`preview_shortcut`] consumes it in the canvas pass that feeds the preview, before the pass +//! reads the box, and writes the new value through the dialog host's own field channel. + +use craft::ui::dialog::StackDialog as _; +use craft::ui::patterns::form_dialogs::HostedParamDialog; +use serde_json::{Map, Value, json}; + +use crate::PhotocraftApp; + +/// The command whose dialog carries the Preview box and the grey channel preview. +pub(crate) const COMMAND: &str = "image.calculations"; + +/// The command and parameters the open Calculations form's canvas preview runs: the form's own +/// values (the same map OK sends), with the Result routed to New Document. Every Result option +/// computes the same result channel, and that run's one-layer grayscale document is exactly the +/// grey picture the reference shows - nothing else of the dialog's answer is previewed, which keeps +/// the New Document and Selection results at their measured behaviour. +pub(crate) fn preview_request(app: &PhotocraftApp, fields: &Map) -> (String, Value) { + let mut params = crate::filter_dialog::params_of(fields); + if let Some(object) = params.as_object_mut() { + object.insert("result".to_owned(), json!("newDocument")); + } + (COMMAND.to_owned(), app.with_mask_target(COMMAND, params)) +} + +/// Alt+P toggles the open Calculations form's Preview check box; whether it did. The pinned +/// framework has no key bound to the box, so this is the app's half; consuming the event keeps the +/// `P` away from any row. `canvas::ensure_filter_preview` calls it on every canvas pass, before it +/// reads the box, so it works from either state and the very pass that toggled shows the result. +pub(crate) fn preview_shortcut(app: &mut PhotocraftApp, ctx: &egui::Context) -> bool { + let Some((id, on)) = + app.framework.dialogs.get::().and_then(|dialog| (dialog.key() == Some(COMMAND)).then(|| (dialog.id(), !dialog.is_previewing()))) + else { + return false; + }; + if !ctx.input_mut(|i| i.consume_key(egui::Modifiers::ALT, egui::Key::P)) { + return false; + } + let _ = app.framework.dialogs.set_json_field(ctx, id, "preview", Value::Bool(on)); + ctx.request_repaint(); + true +} diff --git a/crates/ui-egui/src/menus/view_cmds/dialogs.rs b/crates/ui-egui/src/menus/view_cmds/dialogs.rs index 476be83615..52b99cad73 100644 --- a/crates/ui-egui/src/menus/view_cmds/dialogs.rs +++ b/crates/ui-egui/src/menus/view_cmds/dialogs.rs @@ -58,13 +58,20 @@ fn open_form(app: &mut PhotocraftApp, id: &str, title: &str, params: Vec, let title = crate::dialogs::filter_dialog::title_of(title); let key = id.to_owned(); let image_processor = id == image_processor_ui::COMMAND; + let calculations = id == crate::dialogs::calculations_ui::COMMAND; if image_processor { image_processor_ui::settings_opened(app.ctx()); } crate::dialogs::params::open_param(app, id, fields, fixed, move |fields: &mut DialogFields| { derive(&key, fields); let dialog = ParamDialog::command(key.clone(), title.clone(), params.clone()).values(Bind::bound(fields)); - if image_processor { image_processor_ui::settings_footer(dialog) } else { dialog } + if image_processor { + image_processor_ui::settings_footer(dialog) + } else if calculations { + dialog.previews(true) + } else { + dialog + } }) }