diff --git a/crates/ui-egui/src/dialogs/contact_sheet_settings_file_tests.rs b/crates/ui-egui/src/dialogs/contact_sheet_settings_file_tests.rs index daab224f5f..ff0b2330a8 100644 --- a/crates/ui-egui/src/dialogs/contact_sheet_settings_file_tests.rs +++ b/crates/ui-egui/src/dialogs/contact_sheet_settings_file_tests.rs @@ -1,5 +1,6 @@ //! #13698: Contact Sheet II's Load…, Save… and Reset…, and the settings file they read and write. +use craft::host::{Picked, ScriptedFiles}; use craft::ui::dialog::{DialogId, StackDialog, StandardPrompt}; use serde_json::{Map, Value, json}; @@ -269,7 +270,11 @@ fn a_file_that_is_not_a_settings_file_is_an_error() { /// The dialog open from the menu, on a fresh app. fn opened() -> (H, DialogId) { - let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + opened_over(PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default())) +} + +/// The dialog open from the menu over `app`. +fn opened_over(mut app: Box) -> (H, DialogId) { let ctx = app.ctx().clone(); let reply = crate::menus::invoke(&mut app, &ctx, super::super::COMMAND, json!({})).unwrap(); let want = reply["dialog"].as_str().map(str::to_owned).or_else(|| reply["dialog"].as_u64().map(|v| v.to_string())).expect("the reply names a dialog"); @@ -380,3 +385,28 @@ fn reset_asks_first_and_yes_restores_the_defaults_with_no_folder() { } } } + +/// Load…'s pick answered after its dialog closed and opened again leaves the new open alone: every open has the same +/// id. +#[test] +fn a_load_answered_after_the_dialog_reopened_changes_nothing() { + let files = std::sync::Arc::new(ScriptedFiles::held()); + let services = crate::Services { files: Some(files.clone()), ..Default::default() }; + let (mut h, id) = opened_over(PhotocraftApp::new(photocraft_engine::Session::new(), services)); + let ctx = h.ctx.clone(); + super::super::footer(h.state_mut(), &ctx, id, "load"); + crate::dialogs::testing::cancel(h.state_mut(), id).expect("the dialog closes"); + h.run_steps(3); + crate::menus::invoke(h.state_mut(), &ctx, super::super::COMMAND, json!({})).unwrap(); + h.run_steps(4); + assert!(h.state().framework.dialogs.open_dialogs().iter().any(|d| d.id == id), "the new open has the same id"); + let shown = |h: &H| ["input", "units", "width", "mode", "columns"].map(|key| field(h, id, key)); + let before = shown(&h); + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("Contact Sheet II.xml"); + std::fs::write(&path, reference_load_file()).unwrap(); + files.answer(Picked::Paths(vec![path])).expect("Load…'s file dialog is open"); + h.state_mut().poll_file_dialog(); + h.run_steps(3); + assert_eq!(shown(&h), before); +} diff --git a/crates/ui-egui/src/dialogs/contact_sheet_ui.rs b/crates/ui-egui/src/dialogs/contact_sheet_ui.rs index 410e21cf96..980a23e696 100644 --- a/crates/ui-egui/src/dialogs/contact_sheet_ui.rs +++ b/crates/ui-egui/src/dialogs/contact_sheet_ui.rs @@ -167,6 +167,25 @@ pub(crate) fn footer_buttons() -> Vec<(String, Label)> { [("load", tl!("Load…")), ("save", tl!("Save…")), ("reset", tl!("Reset…"))].map(|(id, label)| (id.to_owned(), Label::from(label))).into() } +/// Every open of the dialog has the same id, so Load…'s pick and Reset…'s question, answered after the dialog closed, +/// would fill the next open. They answer only the open they were asked from, counted here. +fn opens_id() -> egui::Id { + egui::Id::unique("pc-contact-sheet-opens") +} + +/// Counts a new open of the dialog. +pub(crate) fn opened(ctx: &egui::Context) { + ctx.data_mut(|data| { + let opens = data.get_temp_mut_or_default::(opens_id()); + *opens = opens.wrapping_add(1); + }); +} + +/// The number of the dialog's latest open. +fn opens(ctx: &egui::Context) -> u64 { + ctx.data(|data| data.get_temp::(opens_id())).unwrap_or_default() +} + /// The largest settings file Load… reads: the reference's are under 1 KiB. const SETTINGS_FILE_MAX: usize = 1 << 20; @@ -175,9 +194,11 @@ const SETTINGS_FILE_MAX: usize = 1 << 20; pub(crate) fn footer(app: &mut PhotocraftApp, ctx: &egui::Context, id: DialogId, action: &str) { let outcome = match action { "load" => { - let ctx = ctx.clone(); + let (ctx, asked) = (ctx.clone(), opens(ctx)); app.pick_file_bytes_filtered(&["xml"], move |app, name, bytes| { - load(app, &ctx, id, &name, &bytes); + if opens(&ctx) == asked { + load(app, &ctx, id, &name, &bytes); + } Ok(Value::Null) }) } @@ -190,7 +211,7 @@ pub(crate) fn footer(app: &mut PhotocraftApp, ctx: &egui::Context, id: DialogId, }) } "reset" => { - ask_reset(app, id); + ask_reset(app, id, opens(ctx)); Ok(Value::Null) } _ => Ok(Value::Null), @@ -266,24 +287,25 @@ fn apply(app: &mut PhotocraftApp, ctx: &egui::Context, id: DialogId, mut read: M } } -/// The tag of Reset…'s question: the dialog it resets. -struct ResetQuestion(DialogId); +/// The tag of Reset…'s question: the dialog it resets and the open it was asked from ([`opens`]). +struct ResetQuestion(DialogId, u64); /// Reset… asks first, as the reference application's script alert does. -fn ask_reset(app: &mut PhotocraftApp, id: DialogId) { +fn ask_reset(app: &mut PhotocraftApp, id: DialogId, asked: u64) { let prompt = StandardPrompt::custom(tl!("Restore default settings?")) .title(tl!("Script Alert")) .button(PromptChoice::Proceed, tl!("Yes")) .button(PromptChoice::Cancel, tl!("No")) - .tag(ResetQuestion(id)); + .tag(ResetQuestion(id, asked)); let _ = crate::dialogs::alerts::open_prompt(app, prompt); } /// The `PROMPTS` entry of Reset…'s question: Yes shows the defaults, with no folder chosen; No changes nothing. `false` /// for another prompt's answer. -pub(crate) fn answer(app: &mut PhotocraftApp, _ctx: &egui::Context, answer: &mut PromptAnswer) -> bool { - let Some(ResetQuestion(id)) = answer.take_tag::() else { return false }; +pub(crate) fn answer(app: &mut PhotocraftApp, ctx: &egui::Context, answer: &mut PromptAnswer) -> bool { + let Some(ResetQuestion(id, asked)) = answer.take_tag::() else { return false }; if answer.choice == PromptChoice::Proceed + && opens(ctx) == asked && let Some(d) = app.framework.dialogs.get_mut_by_id::(id) { for param in build(&Value::Null) { diff --git a/crates/ui-egui/src/dialogs/file_forms.rs b/crates/ui-egui/src/dialogs/file_forms.rs index 00b0534294..88821f42cb 100644 --- a/crates/ui-egui/src/dialogs/file_forms.rs +++ b/crates/ui-egui/src/dialogs/file_forms.rs @@ -74,6 +74,7 @@ fn open_form(app: &mut PhotocraftApp, key: &'static str, title: &str, rows: Vec< /// File › Automate › Contact Sheet II. pub(crate) fn open_contact_sheet(app: &mut PhotocraftApp) -> DialogId { + crate::dialogs::contact_sheet_ui::opened(app.ctx()); let rows = crate::dialogs::contact_sheet_ui::rows(app); open_form(app, CONTACT_SHEET, tl!("Contact Sheet II"), rows) }