From 8f60b79b3932919b83f8879bea8800b4bc61d2dd Mon Sep 17 00:00:00 2001 From: bflatastic Date: Tue, 24 Nov 2026 07:16:18 +0700 Subject: [PATCH] ui-patterns: hosted Export As takes the host's answers only in a real run, so a path answered in a frame whose first run measures the dialog still finishes it --- .../patterns/form_dialogs/export/as_dialog.rs | 4 ++- .../tests/it/form_dialogs/hosted.rs | 35 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) 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 39c003b1b..c180dfc9b 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/tests/it/form_dialogs/hosted.rs b/crates/ui-patterns/tests/it/form_dialogs/hosted.rs index 608b355f1..d44f6dbbe 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.