From 1a525c57d1416ad493e8dc9bd5f9a6cb5f4e55f5 Mon Sep 17 00:00:00 2001 From: ljkx <177671075+ljkx@users.noreply.github.com> Date: Mon, 16 Nov 2026 10:21:27 +0100 Subject: [PATCH 1/2] Batch: OK without a source or destination folder says which is missing (#13669) OK in File > Automate > Batch now runs the reference's checks before the dialog closes: Source Folder with no folder chosen ("You must choose a source folder."), then a folder destination with no folder chosen ("You must choose a destination folder."). The first that fails opens an alert titled "Batch" with OK; the dialog stays open with its values and nothing runs. A confirm through the control channel gets the same check. Opened Files without an output keeps running as Destination None. --- crates/ui-egui/src/dialogs.rs | 5 +- crates/ui-egui/src/menus/view_cmds.rs | 2 +- .../view_cmds/batch_ok_validation_tests.rs | 223 ++++++++++++++++++ crates/ui-egui/src/menus/view_cmds/dialogs.rs | 64 +++++ 4 files changed, 292 insertions(+), 2 deletions(-) create mode 100644 crates/ui-egui/src/menus/view_cmds/batch_ok_validation_tests.rs diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs index afa7a7d1ff..6cac43721a 100644 --- a/crates/ui-egui/src/dialogs.rs +++ b/crates/ui-egui/src/dialogs.rs @@ -929,7 +929,8 @@ pub fn title(d: &Dialog) -> String { DialogKind::About => "About PhotoCraft".into(), DialogKind::LayerStyle => "Layer Style".into(), DialogKind::Command => d.fields.get("__label").and_then(Value::as_str).unwrap_or("Command").trim_end_matches('…').to_string(), - DialogKind::Error => "Error".into(), + // An alert may carry its own title, such as Batch's for a missing folder (#13669). + DialogKind::Error => d.fields.get("__title").and_then(Value::as_str).unwrap_or("Error").into(), } } @@ -1121,6 +1122,8 @@ pub(crate) fn confirm_preflight(app: &mut PhotocraftApp, id: u64) -> Result<(), if crate::layer_style::color_picker::has_child(app, id) { return Err("confirm or cancel the Layer Style color picker first".into()); } + // Batch without a source or destination folder stays open under its alert (#13669). + crate::view_cmds::batch_preflight(app, id)?; // A control request installs the session gate; synthetic input sets `automation_input`. // Either way the handler's own file access is judged before it runs, as its command would be. if (app.automation_input || app.session.authorize.is_some()) diff --git a/crates/ui-egui/src/menus/view_cmds.rs b/crates/ui-egui/src/menus/view_cmds.rs index 60d2fadd77..5bb6117a9c 100644 --- a/crates/ui-egui/src/menus/view_cmds.rs +++ b/crates/ui-egui/src/menus/view_cmds.rs @@ -20,7 +20,7 @@ mod options; pub use arrange::{Border, borders, cells, drag_border}; pub(crate) use arrange::{document_is_floating, float_window}; -pub(crate) use dialogs::write_copy; +pub(crate) use dialogs::{batch_preflight, write_copy}; #[cfg(test)] use dialogs::{form, front, label_of}; pub use dialogs::{form_body, form_keys, reset_form}; diff --git a/crates/ui-egui/src/menus/view_cmds/batch_ok_validation_tests.rs b/crates/ui-egui/src/menus/view_cmds/batch_ok_validation_tests.rs new file mode 100644 index 0000000000..93864798c4 --- /dev/null +++ b/crates/ui-egui/src/menus/view_cmds/batch_ok_validation_tests.rs @@ -0,0 +1,223 @@ +//! File › Automate › Batch: OK checks for a missing source folder, then a missing destination +//! folder, as the reference does (#13669). The first that fails shows its alert titled "Batch" +//! with OK over the dialog, which stays open with every value, and nothing runs. The same holds +//! for a confirm through the control channel. + +use egui::accesskit::Role; +use egui_kittest::Harness; +use egui_kittest::kittest::Queryable; +use serde_json::{Map, Value, json}; + +use super::{BATCH_ALERT_TITLE, NO_DESTINATION_FOLDER, NO_SOURCE_FOLDER}; +use crate::PhotocraftApp; +use crate::control::{ControlRequest, Outcome, handle}; +use crate::state::DialogKind; + +type H = Harness<'static, PhotocraftApp>; + +/// The Batch dialog, opened from its menu command over an unsaved document with one recorded +/// action (Invert), with `input` and `output` set to the given paths. +fn batch(input: &str, output: &str) -> H { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 2, "height": 1})).unwrap(); + app.run("actions.record", json!({"name": "Invert"})).unwrap(); + app.run("image.adjustments.invert", json!({})).unwrap(); + app.run("actions.stop", json!({})).unwrap(); + let mut h = Harness::builder().with_size(egui::vec2(1100.0, 800.0)).build_ui_state(|ui, app| crate::dialogs::show(app, ui.ctx()), app); + PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::ALL[0]); + let ctx = h.ctx.clone(); + crate::menus::invoke(h.state_mut(), &ctx, "file.automate.batch", json!({})).expect("the Batch dialog opens"); + let fields = batch_fields_mut(&mut h); + fields.insert("input".into(), json!(input)); + fields.insert("output".into(), json!(output)); + h.run_steps(4); + h +} + +/// The open Batch dialog's id. +fn batch_id(h: &H) -> u64 { + h.state().ui.dialogs.iter().find(|d| d.fields.get("__command").and_then(Value::as_str) == Some("file.automate.batch")).expect("the Batch dialog is open").id +} + +fn batch_fields_mut(h: &mut H) -> &mut Map { + let id = batch_id(h); + &mut h.state_mut().ui.dialog_mut(id).unwrap().fields +} + +fn batch_fields(h: &H) -> Map { + let id = batch_id(h); + h.state().ui.dialogs.iter().find(|d| d.id == id).unwrap().fields.clone() +} + +/// Every open alert as (title, message). +fn alerts(h: &H) -> Vec<(String, String)> { + h.state() + .ui + .dialogs + .iter() + .filter(|d| d.kind == DialogKind::Error) + .map(|d| (crate::dialogs::title(d), d.fields.get("message").and_then(Value::as_str).unwrap_or_default().to_string())) + .collect() +} + +/// The alert the reference shows for `message`. +fn batch_alert(message: &str) -> Vec<(String, String)> { + vec![(BATCH_ALERT_TITLE.to_string(), message.to_string())] +} + +/// Empties a text field of the dialog the way a user does: select all, then Backspace. +fn clear_field(h: &mut H, label: &str) { + h.get_by_role_and_label(Role::TextInput, label).click(); + h.run_steps(1); + h.key_press_modifiers(egui::Modifiers::COMMAND, egui::Key::A); + h.run_steps(1); + h.key_press(egui::Key::Backspace); + h.run_steps(2); +} + +/// A folder holding one 2×1 PNG, `a.png`, and the path of an output folder beside it. +fn image_folder() -> (tempfile::TempDir, String, String) { + let dir = tempfile::tempdir().unwrap(); + std::fs::create_dir(dir.path().join("in")).unwrap(); + let mut session = photocraft_engine::Session::new(); + session.execute("file.new", json!({"width": 2, "height": 1})).unwrap(); + session.execute("file.saveACopy", json!({"path": dir.path().join("in/a.png").to_string_lossy()})).unwrap(); + let input = dir.path().join("in").to_string_lossy().into_owned(); + let output = dir.path().join("out").to_string_lossy().into_owned(); + (dir, input, output) +} + +/// Clicks the dialog's OK; with an alert up, the alert's OK is the other one. +fn click_batch_ok(h: &mut H) -> egui::Rect { + let ok = h.get_by_role_and_label(Role::Button, "OK").rect(); + h.get_by_role_and_label(Role::Button, "OK").click(); + h.run_steps(4); + ok +} + +#[test] +fn ok_without_a_source_folder_shows_the_batch_alert_and_keeps_the_dialog_open() { + crate::i18n::with_language(crate::i18n::Lang::EN, || { + let (dir, input, output) = image_folder(); + let mut h = batch(&input, &output); + clear_field(&mut h, "Input"); + let before = batch_fields(&h); + assert_eq!((&before["input"], &before["output"]), (&json!(""), &json!(output))); + + let ok = click_batch_ok(&mut h); + assert_eq!(alerts(&h), batch_alert(NO_SOURCE_FOLDER)); + assert_eq!(NO_SOURCE_FOLDER, "You must choose a source folder."); + assert!(h.query_by_label(NO_SOURCE_FOLDER).is_some(), "the alert is on screen"); + assert_eq!(batch_fields(&h), before, "the dialog stays open with every value"); + assert!(!dir.path().join("out").exists(), "nothing was written"); + assert_eq!(h.state().session.documents().len(), 1, "nothing was opened"); + + // The alert's OK closes the alert only; the dialog keeps its values. + h.get_all_by_role_and_label(Role::Button, "OK").find(|b| b.rect() != ok).expect("the alert has an OK button").click(); + h.run_steps(4); + assert!(alerts(&h).is_empty()); + assert_eq!(batch_fields(&h), before); + }); +} + +#[test] +fn ok_without_a_destination_folder_shows_the_batch_alert_and_writes_nothing() { + crate::i18n::with_language(crate::i18n::Lang::EN, || { + let (dir, input, output) = image_folder(); + let mut h = batch(&input, &output); + clear_field(&mut h, "Output"); + let before = batch_fields(&h); + assert_eq!((&before["input"], &before["output"]), (&json!(input), &json!(""))); + + click_batch_ok(&mut h); + assert_eq!(alerts(&h), batch_alert(NO_DESTINATION_FOLDER)); + assert_eq!(NO_DESTINATION_FOLDER, "You must choose a destination folder."); + assert_eq!(batch_fields(&h), before); + let files: Vec<_> = std::fs::read_dir(dir.path()).unwrap().map(|e| e.unwrap().file_name()).collect(); + assert_eq!(files, ["in"], "nothing was written"); + assert_eq!(h.state().session.documents().len(), 1, "nothing was opened"); + }); +} + +/// The checks run in the reference's order: with both folders missing, the source one shows. +/// A path of blanks names no folder either. +#[test] +fn the_source_folder_is_checked_before_the_destination() { + crate::i18n::with_language(crate::i18n::Lang::EN, || { + for (input, output, message) in [("", "", NO_SOURCE_FOLDER), (" ", "", NO_SOURCE_FOLDER), ("in", " ", NO_DESTINATION_FOLDER)] { + let mut h = batch(input, output); + click_batch_ok(&mut h); + assert_eq!(alerts(&h), batch_alert(message), "input {input:?}, output {output:?}"); + } + }); +} + +#[test] +fn a_confirm_over_the_control_channel_gets_the_same_check() { + crate::i18n::with_language(crate::i18n::Lang::EN, || { + let mut h = batch("", ""); + let ctx = h.ctx.clone(); + let id = batch_id(&h); + let before = batch_fields(&h); + // Repeated confirms answer the same error and keep one alert. + for _ in 0..2 { + let (confirm, _rx) = ControlRequest::new("ui.dialog.confirm", json!({"dialog": id})); + let Outcome::Done(reply) = handle(h.state_mut(), &ctx, &confirm) else { panic!("ui.dialog.confirm replies at once") }; + assert_eq!(reply["ok"], false, "{reply}"); + assert!(reply.to_string().contains(NO_SOURCE_FOLDER), "{reply}"); + } + assert_eq!(alerts(&h), batch_alert(NO_SOURCE_FOLDER)); + assert_eq!(batch_fields(&h), before); + + // With the source set over the channel, the destination is checked next. + let (set, _rx) = ControlRequest::new("ui.dialog.set", json!({"dialog": id, "field": "input", "value": "in"})); + assert!(matches!(handle(h.state_mut(), &ctx, &set), Outcome::Done(v) if v["ok"] == true)); + let alert = h.state().ui.dialogs.iter().find(|d| d.kind == DialogKind::Error).unwrap().id; + crate::dialogs::confirm(h.state_mut(), alert).unwrap(); + assert_eq!(crate::dialogs::confirm(h.state_mut(), id), Err(NO_DESTINATION_FOLDER.to_string())); + assert_eq!(alerts(&h), batch_alert(NO_DESTINATION_FOLDER)); + }); +} + +/// With both folders chosen OK runs the batch and closes the dialog, without an alert. +#[test] +fn ok_with_both_folders_runs_the_batch() { + crate::i18n::with_language(crate::i18n::Lang::EN, || { + let (dir, input, output) = image_folder(); + let mut h = batch(&input, &output); + click_batch_ok(&mut h); + assert!(alerts(&h).is_empty(), "{:?}", alerts(&h)); + assert!(h.state().ui.dialogs.is_empty(), "OK closes the dialog"); + assert!(dir.path().join("out/a.png").is_file(), "the batch wrote a.png"); + }); +} + +/// Opened Files reads no folder, and without an output folder it leaves the documents open and +/// changed (Destination None, as before): no check applies. +#[test] +fn opened_files_without_an_output_folder_runs_without_an_alert() { + crate::i18n::with_language(crate::i18n::Lang::EN, || { + let mut h = batch("", ""); + batch_fields_mut(&mut h).insert("source".into(), json!("open")); + h.run_steps(2); + let pixel = |h: &H| { + let st = h.state().session.active().unwrap(); + st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap().rgba(0, 0) + }; + let before = pixel(&h); + let id = batch_id(&h); + let result = crate::dialogs::confirm(h.state_mut(), id).expect("the batch runs"); + assert!(alerts(&h).is_empty(), "{:?}", alerts(&h)); + assert_eq!(result["documents"], 1, "{result}"); + assert_eq!(h.state().session.documents().len(), 1, "the document stays open"); + assert_eq!(pixel(&h)[0], 1.0 - before[0], "and the action inverted it"); + }); +} + +/// Other alerts keep the title "Error"; only one that names its own title shows another. +#[test] +fn an_alert_without_its_own_title_is_titled_error() { + let mut ui = crate::state::UiState::default(); + crate::panels::options_bar::range_alert(&mut ui, "A value between 1 and 300000 is required. Closest value inserted."); + assert_eq!(crate::dialogs::title(ui.dialogs.last().unwrap()), "Error"); +} diff --git a/crates/ui-egui/src/menus/view_cmds/dialogs.rs b/crates/ui-egui/src/menus/view_cmds/dialogs.rs index d4518748d9..d513998ab5 100644 --- a/crates/ui-egui/src/menus/view_cmds/dialogs.rs +++ b/crates/ui-egui/src/menus/view_cmds/dialogs.rs @@ -358,6 +358,70 @@ fn default_dir(app: &PhotocraftApp) -> String { app.session.active().and_then(|d| d.path.as_deref()).and_then(|p| p.rfind(['/', '\\']).map(|i| p[..i].to_string())).unwrap_or_else(|| ".".into()) } +/// The title of the alerts Batch's OK shows for a missing choice (#13669). +pub(crate) const BATCH_ALERT_TITLE: &str = "Batch"; +/// Batch's OK with Source: Folder and no source folder chosen (#13669). +pub(crate) const NO_SOURCE_FOLDER: &str = "You must choose a source folder."; +/// Batch's OK with a folder destination and no destination folder chosen (#13669). +pub(crate) const NO_DESTINATION_FOLDER: &str = "You must choose a destination folder."; + +/// Does a Batch path field name a folder (or files)? No value, an empty or blank path and an +/// empty file list count as nothing chosen. +fn batch_path_chosen(value: Option<&Value>) -> bool { + match value { + None | Some(Value::Null) => false, + Some(Value::String(path)) => !path.trim().is_empty(), + Some(Value::Array(files)) => !files.is_empty(), + Some(_) => true, + } +} + +/// The first of the reference's OK checks the Batch dialog's fields fail, in its order: the +/// source folder, then the destination folder. Only a folder source has either: Opened Files +/// reads no folder, and without an output it leaves the documents open (Destination None). A +/// Save and Close destination writes each source in place and has no folder to choose. +fn batch_missing_choice(f: &Map) -> Option<&'static str> { + let folder_source = matches!(f.get("source"), None | Some(Value::Null)) || f.get("source").and_then(Value::as_str) == Some("folder"); + if !folder_source { + return None; + } + // The source as the command reads it: `input`, else `files`. + if !batch_path_chosen(f.get("input").or_else(|| f.get("files"))) { + return Some(NO_SOURCE_FOLDER); + } + let folder_destination = matches!(f.get("destination"), None | Some(Value::Null)) || f.get("destination").and_then(Value::as_str) == Some("folder"); + (folder_destination && !batch_path_chosen(f.get("output"))).then_some(NO_DESTINATION_FOLDER) +} + +/// Checked before the Batch dialog closes (`dialogs::confirm_preflight`), for OK and for a +/// confirm through the control channel alike: a missing source or destination folder shows the +/// reference's alert titled "Batch" over the dialog, which stays open with its values, and +/// nothing runs (#13669). +pub(crate) fn batch_preflight(app: &mut PhotocraftApp, id: u64) -> Result<(), String> { + let Some(missing) = app + .ui + .dialogs + .iter() + .find(|d| d.id == id) + .map(|d| &d.fields) + .filter(|f| f.contains_key("__form") && f.get("__command").and_then(Value::as_str) == Some("file.automate.batch")) + .and_then(batch_missing_choice) + else { + return Ok(()); + }; + let message = tl!(missing).to_owned(); + // A confirm repeated from automation keeps one alert. + let shown = app.ui.dialogs.iter().any(|d| d.kind == DialogKind::Error && d.fields.get("message").and_then(Value::as_str) == Some(message.as_str())); + if !shown { + app.ui.open_dialog(DialogKind::Error, json!({"message": message, "__title": BATCH_ALERT_TITLE}).as_object().cloned().unwrap_or_default()); + } + Err(message) +} + +#[cfg(test)] +#[path = "batch_ok_validation_tests.rs"] +mod batch_ok_validation_tests; + /// Handle menu commands that need a shell action or a parameter dialog before execution. pub(super) fn front(app: &mut PhotocraftApp, ctx: &egui::Context, id: &str, params: &Value) -> Option> { if id == "file.closeAll" || id == "file.closeOthers" { From 02a6bee23d4ede8360b077b8d6dee188617c883a Mon Sep 17 00:00:00 2001 From: ljkx <177671075+ljkx@users.noreply.github.com> Date: Mon, 23 Nov 2026 09:43:53 +0100 Subject: [PATCH 2/2] Show the Batch OK refusal in a Batch alert As the reference does (#13669), a missing source or destination folder now opens a one-button alert titled Batch with the message. The dialog stays open with its values and nothing runs; the refusal still answers a control-channel confirm. --- .../view_cmds/batch_ok_validation_tests.rs | 12 +++++++++++- crates/ui-egui/src/menus/view_cmds/dialogs.rs | 19 +++++++++++-------- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/crates/ui-egui/src/menus/view_cmds/batch_ok_validation_tests.rs b/crates/ui-egui/src/menus/view_cmds/batch_ok_validation_tests.rs index 45f08e289f..27432203d9 100644 --- a/crates/ui-egui/src/menus/view_cmds/batch_ok_validation_tests.rs +++ b/crates/ui-egui/src/menus/view_cmds/batch_ok_validation_tests.rs @@ -1,6 +1,7 @@ //! File › Automate › Batch: OK checks for a missing source folder, then a missing destination //! folder, as the reference does (#13669). The first that fails refuses OK with the reference's -//! message; the dialog stays open with every value, and nothing runs. The same holds for a +//! message in a one-button alert titled "Batch"; the dialog stays open with every value, and +//! nothing runs. The same holds for a //! confirm through the control channel. use craft::automation::actions::{Action, ActionStep}; @@ -48,6 +49,12 @@ fn fields(app: &PhotocraftApp, id: DialogId) -> Option { app.framework.dialogs.open_dialogs().into_iter().find(|open| open.id == id).and_then(|open| open.fields) } +/// The messages of the alerts the host has open (`HostedAlert`'s key is "alert"). +fn alerts(app: &PhotocraftApp) -> Vec { + let open = app.framework.dialogs.open_dialogs().into_iter().filter(|d| d.key.as_deref() == Some("alert")); + open.filter_map(|d| Some(d.fields.as_ref()?["message"].as_str()?.to_string())).collect() +} + /// A folder holding one 2 × 1 PNG, `in/a.png`, and the path of an output folder `out` beside it. fn image_folder() -> (tempfile::TempDir, String, String) { let dir = tempfile::tempdir().unwrap(); @@ -68,6 +75,7 @@ fn ok_without_a_source_folder_is_refused_and_keeps_the_dialog_open() { let before = fields(&app, id).expect("the dialog is open"); assert_eq!(crate::dialogs::confirm(&mut app, &ctx, id), Err(NO_SOURCE_FOLDER.to_string())); assert_eq!(NO_SOURCE_FOLDER, "You must choose a source folder."); + assert_eq!(alerts(&app), [NO_SOURCE_FOLDER], "the refusal shows in an alert"); assert_eq!(fields(&app, id), Some(before), "the dialog stays open with every value"); assert!(!dir.path().join("out").exists(), "nothing was written"); assert_eq!(app.session.documents().len(), 1, "nothing was opened"); @@ -82,6 +90,7 @@ fn ok_without_a_destination_folder_is_refused_and_writes_nothing() { let before = fields(&app, id).expect("the dialog is open"); assert_eq!(crate::dialogs::confirm(&mut app, &ctx, id), Err(NO_DESTINATION_FOLDER.to_string())); assert_eq!(NO_DESTINATION_FOLDER, "You must choose a destination folder."); + assert_eq!(alerts(&app), [NO_DESTINATION_FOLDER], "the refusal shows in an alert"); assert_eq!(fields(&app, id), Some(before)); let files: Vec<_> = std::fs::read_dir(dir.path()).unwrap().map(|e| e.unwrap().file_name()).collect(); assert_eq!(files, ["in"], "nothing was written"); @@ -132,6 +141,7 @@ fn ok_with_both_folders_runs_the_batch() { let (mut app, ctx, id) = batch(&input, &output); crate::dialogs::confirm(&mut app, &ctx, id).expect("OK is accepted"); assert!(fields(&app, id).is_none(), "OK closes the dialog"); + assert!(alerts(&app).is_empty(), "an accepted OK shows no alert"); assert!(dir.path().join("out/a.png").is_file(), "the batch wrote a.png"); }); } diff --git a/crates/ui-egui/src/menus/view_cmds/dialogs.rs b/crates/ui-egui/src/menus/view_cmds/dialogs.rs index ebc50fd26c..3bd98567ef 100644 --- a/crates/ui-egui/src/menus/view_cmds/dialogs.rs +++ b/crates/ui-egui/src/menus/view_cmds/dialogs.rs @@ -686,14 +686,17 @@ fn batch_missing_choice(fields: &DialogFields) -> Option<&'static str> { } /// The `ParamFamily::check` of the Batch dialog (#13669): OK, from the button or a confirm through the control channel, -/// is refused while a folder source or its destination names no folder. The dialog stays open with its values, shows -/// the reference's message, and nothing runs. -pub(crate) fn batch_check(_app: &mut PhotocraftApp, result: &ParamResult) -> Result<(), Label> { - match batch_missing_choice(&result.fields) { - Some(NO_SOURCE_FOLDER) => Err(Label::from(tl!("You must choose a source folder."))), - Some(_) => Err(Label::from(tl!("You must choose a destination folder."))), - None => Ok(()), - } +/// is refused while a folder source or its destination names no folder. As in the reference, a one-button alert +/// titled "Batch" shows the message; the dialog stays open with its values (the refusal also shows under its rows and +/// answers a control-channel confirm), and nothing runs. +pub(crate) fn batch_check(app: &mut PhotocraftApp, result: &ParamResult) -> Result<(), Label> { + let message = match batch_missing_choice(&result.fields) { + Some(NO_SOURCE_FOLDER) => tl!("You must choose a source folder."), + Some(_) => tl!("You must choose a destination folder."), + None => return Ok(()), + }; + crate::dialogs::alerts::show(app, craft::ui::dialog::Alert::new(tl!("Batch"), message)); + Err(Label::from(message)) } #[cfg(test)]