diff --git a/crates/draw/src/ui/slice/dialogs.rs b/crates/draw/src/ui/slice/dialogs.rs index e96fd6826d..06dacf192d 100644 --- a/crates/draw/src/ui/slice/dialogs.rs +++ b/crates/draw/src/ui/slice/dialogs.rs @@ -331,7 +331,8 @@ impl StackDialog for SliceOptionsDialog { .map(|(value, label)| Entry::new(value, label.clone())) .collect(); let background_labels = [&words.background_none, &words.background_matte, &words.background_white, &words.background_black]; - let answers = self.requests.take_answers(); + let measuring = craft_ui::runtime::measure::active(ui.ctx()); + let answers = if measuring { Vec::new() } else { self.requests.take_answers() }; let shown = FormDialog::new(words.options_title.clone(), Bind::bound(&mut self.options)).answers(answers).show(ui, |ui, form, options| { for answer in form.take_answers() { if let HostAnswer::Color { key, color, .. } = answer diff --git a/crates/ui-patterns/src/patterns/form_dialogs/export/as_dialog.rs b/crates/ui-patterns/src/patterns/form_dialogs/export/as_dialog.rs index 39c003b1bb..c180dfc9bc 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/export/as_dialog.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/export/as_dialog.rs @@ -569,13 +569,15 @@ impl StackDialog for HostedExportAs { } fn show(&mut self, ui: &mut Ui, _: &DialogStack) -> Option> { + let measuring = crate::runtime::measure::active(ui.ctx()); + let answers = if measuring { Vec::new() } else { self.requests.take_answers() }; let dialog = ExportAsDialog { formats: self.formats.clone(), methods: self.methods.clone(), title: self.title.clone(), defaults: self.defaults, preview: self.preview.clone(), - answers: self.requests.take_answers(), + answers, saving: self.saving, open: Some(Bind::bound(&mut self.open)), presets: self.with_presets.then(|| Bind::bound(&mut self.presets)), diff --git a/crates/ui-patterns/src/patterns/form_dialogs/guides.rs b/crates/ui-patterns/src/patterns/form_dialogs/guides.rs index 75712a5fa1..51a3b8b90c 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/guides.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/guides.rs @@ -713,8 +713,10 @@ impl StackDialog for HostedGuideLayoutDialog { } fn show(&mut self, ui: &mut Ui, _: &DialogStack) -> Option> { + let measuring = crate::runtime::measure::active(ui.ctx()); + let answers = if measuring { Vec::new() } else { self.requests.take_answers() }; let shown = (self.build)(Bind::bound(&mut self.values)) - .answers(self.requests.take_answers()) + .answers(answers) .opened(Bind::bound(&mut self.open)) .preview(Bind::bound(&mut self.preview_on)) .show(ui); diff --git a/crates/ui-patterns/src/patterns/form_dialogs/guides/new_guide.rs b/crates/ui-patterns/src/patterns/form_dialogs/guides/new_guide.rs index f6513923b6..f62efc4f96 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/guides/new_guide.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/guides/new_guide.rs @@ -423,7 +423,9 @@ impl StackDialog for HostedNewGuideDialog { fn show(&mut self, ui: &mut Ui, _: &DialogStack) -> Option> { self.recall(ui.ctx()); - let mut dialog = (self.build)(Bind::bound(&mut self.values)).answers(self.requests.take_answers()).opened(Bind::bound(&mut self.open)); + let measuring = crate::runtime::measure::active(ui.ctx()); + let answers = if measuring { Vec::new() } else { self.requests.take_answers() }; + let mut dialog = (self.build)(Bind::bound(&mut self.values)).answers(answers).opened(Bind::bound(&mut self.open)); dialog.recalled = true; if self.error.is_some() { dialog.error = self.error.clone(); diff --git a/crates/ui-patterns/src/patterns/form_dialogs/new_document.rs b/crates/ui-patterns/src/patterns/form_dialogs/new_document.rs index dfe26c80ea..ad1fd59986 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/new_document.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/new_document.rs @@ -950,17 +950,19 @@ impl StackDialog for HostedNewDocument { } fn show(&mut self, ui: &mut Ui, _: &DialogStack) -> Option>> { - // The host accepted the held answer: this pass closes the dialog with it. - if self.held.is_accepted() { + let measuring = crate::runtime::measure::active(ui.ctx()); + // The host accepted the held answer: this pass's real run closes the dialog with it. + if !measuring && self.held.is_accepted() { return Some(self.held.finish().map_or(DialogResult::Cancel, DialogResult::Ok)); } + let answers = if measuring { Vec::new() } else { self.requests.take_answers() }; let mut dialog = (self.build)(&mut self.options) .opened(Bind::bound(&mut self.open)) .saved(Bind::bound(&mut self.saved)) .recent(Bind::bound(&mut self.recent)) .tab(Bind::bound(&mut self.tab)) .memory(&mut self.memory) - .answers(self.requests.take_answers()) + .answers(answers) .opening(self.opening.clone()); if let Some(setup) = &self.setup { dialog = dialog.setup(setup.clone()); diff --git a/crates/ui-patterns/src/patterns/form_dialogs/save_for_web/hosted.rs b/crates/ui-patterns/src/patterns/form_dialogs/save_for_web/hosted.rs index 8708ae4ce0..7b220c3631 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/save_for_web/hosted.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/save_for_web/hosted.rs @@ -514,10 +514,13 @@ impl StackDialog for HostedSaveForWeb { } fn show(&mut self, ui: &mut Ui, _: &DialogStack) -> Option> { + // A measuring run keeps the host's answers and the inputs: the real run of the pass takes them. + let measuring = crate::runtime::measure::active(ui.ctx()); + let answers = if measuring { Vec::new() } else { self.requests.take_answers() }; let dialog = SaveForWebDialog::new(self.name.clone(), self.size, self.pictures.clone(), Bind::bound(&mut self.options)) .presets(self.presets.clone()) .saving(self.saving) - .answers(self.requests.take_answers()); + .answers(answers); let dialog = SaveForWebDialog { animation: self.animation, remember: self.remember.clone(), @@ -532,11 +535,11 @@ impl StackDialog for HostedSaveForWeb { marks: Some(&self.marks), optimize: self.optimize, browser_preview: self.browser.clone(), - optimize_start: self.optimize_start.take(), - optimize_answer: self.optimize_answer.take(), - optimize_forget: std::mem::take(&mut self.optimize_forget), + optimize_start: if measuring { None } else { self.optimize_start.take() }, + optimize_answer: if measuring { None } else { self.optimize_answer.take() }, + optimize_forget: !measuring && std::mem::take(&mut self.optimize_forget), ok_label: self.ok_label.clone(), - inputs: std::mem::take(&mut self.inputs), + inputs: if measuring { Vec::new() } else { std::mem::take(&mut self.inputs) }, ..dialog }; let mut shown = dialog.show(ui); diff --git a/crates/ui-patterns/tests/it/form_dialogs/hosted.rs b/crates/ui-patterns/tests/it/form_dialogs/hosted.rs index 608b355f1f..d44f6dbbe4 100644 --- a/crates/ui-patterns/tests/it/form_dialogs/hosted.rs +++ b/crates/ui-patterns/tests/it/form_dialogs/hosted.rs @@ -11,6 +11,7 @@ use craft_ui::patterns::form_dialogs::{ NewGuideDialog, NewGuideValues, Param, ParamDialog, ParamKind, }; use craft_ui::runtime::env::UnitKind; +use craft_ui::runtime::measure; use craft_ui::testing::{Fixture, trial}; use egui::{Key, Modifiers, Vec2}; use serde_json::{Value, json}; @@ -82,6 +83,20 @@ fn fixture(host: &Rc>) -> Fixture { fixture } +/// A fixture that runs `host` in a measuring run, then in the real run, every pass, as a host inside a measured body +/// runs: the measuring run comes first, before the real run takes the pass's input. +pub(crate) fn measured_fixture(host: &Rc>) -> Fixture { + let shown = Rc::clone(host); + let mut fixture = Fixture::new(move |ui| { + let ctx = ui.ctx().clone(); + let builder = egui::UiBuilder::new().sizing_pass().invisible(); + measure::run(&ctx, || ui.scope_builder(builder, |ui| shown.borrow_mut().run(ui))); + shown.borrow_mut().run(ui); + }); + fixture.room(Vec2::new(1400.0, 1000.0)); + fixture +} + /// Opens `make()` on a host, then Escape; twice, so a dialog that closed once opens again. `cancel` is what the app /// takes after Escape. fn opens_and_cancels(make: fn(&egui::Context) -> D, title: &str, cancel: DialogResult) @@ -259,6 +274,26 @@ fn hosted_export_as_finishes_when_the_app_answers_its_save_request() { assert_eq!(answered, Some(path), "the answered path finishes the dialog with it"); } +/// A path the host answers in a frame whose first run measures Export As still finishes the dialog with it. Guards +/// against a hosted dialog that took its host's answers in the measuring run, whose form drops them. +#[trial] +fn export_as_takes_a_path_answer_in_the_real_run_after_a_measuring_run() { + let host = Rc::new(RefCell::new(DialogHost::new())); + let mut fixture = measured_fixture(&host); + host.borrow_mut().open(HostedExportAs::new("Poster", PICTURE, ExportOptions::default())); + fixture.idle(10); + let export = fixture.by_label("Export…").expect("the Export button"); + fixture.click(&export).idle(4); + let request = host.borrow_mut().get_mut::().and_then(HostedExportAs::pick_path).expect("Export asks for a path"); + let path = PathBuf::from("/tmp/poster.png"); + if let Some(dialog) = host.borrow_mut().get_mut::() { + dialog.answer_path(request.key, path.clone()); + } + fixture.idle(6); + let answered = host.borrow_mut().take::().and_then(DialogResult::ok).map(|result| result.path); + assert_eq!(answered, Some(path), "the answered path finishes the dialog with it"); +} + /// Export As: Enter alone does not export; Convert to sRGB starts on and its box gives the document's profile; + adds /// 0.5x "@0.5x", then 0.75x, and a row's trash button removes it while more than one is left; two rows ask for a folder /// and answer one output per row; without folder export, two rows refuse Export. diff --git a/crates/ui-patterns/tests/it/form_dialogs/save_for_web.rs b/crates/ui-patterns/tests/it/form_dialogs/save_for_web.rs index ad7f1d2008..e3171d24ba 100644 --- a/crates/ui-patterns/tests/it/form_dialogs/save_for_web.rs +++ b/crates/ui-patterns/tests/it/form_dialogs/save_for_web.rs @@ -421,3 +421,21 @@ fn a_palette_answer_survives_a_measuring_run_of_the_body() { let shift = (ColorTableEdit::Shift([255, 0, 255]), vec![[0, 255, 0]]); assert_eq!(table_events(&host), vec![shift], "the answer shifts the opened entry's colour"); } + +/// A palette colour the host answers in a frame whose first run measures the dialog still shifts the opened entry, once. +/// Guards against a hosted dialog that took its host's answers in the measuring run, whose form drops them. +#[trial] +fn a_palette_answer_in_a_measured_frame_shifts_the_entry_once() { + let host = Rc::new(RefCell::new(DialogHost::new())); + let mut fixture = super::hosted::measured_fixture(&host); + host.borrow_mut().open(HostedSaveForWeb::new("poster.psd", IMAGE, texture()).options(gif(8))); + fixture.idle(10); + show_palette(&host, &mut fixture); + let cell = fixture.by_label("1 #00ff00").expect("the Color Table lists the palette"); + fixture.click(&cell).key(Key::Enter, Modifiers::NONE).idle(4); + let (id, _) = host.borrow_mut().take_color_request().expect("opening a cell asks for its colour"); + host.borrow_mut().answer_color(id, PALETTE_COLOR_KEY, Srgba::rgb(1.0, 0.0, 1.0)); + fixture.idle(4); + let shift = (ColorTableEdit::Shift([255, 0, 255]), vec![[0, 255, 0]]); + assert_eq!(table_events(&host), vec![shift], "the answer shifts the opened entry's colour once"); +}