From a4176ef04e39cd2dace50ed22dcabc1bff3257c6 Mon Sep 17 00:00:00 2001 From: aiongg Date: Sun, 15 Nov 2026 21:32:41 +0000 Subject: [PATCH] tfix4-f6: HostedNewDocument opens its settings at construction (ctx in new); a host-opened dialog's first pass keeps them --- .../src/patterns/form_dialogs/new_document.rs | 55 ++++++++++------- .../tests/it/form_dialogs/hosted.rs | 59 ++++++++++++++----- 2 files changed, 77 insertions(+), 37 deletions(-) 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 54fb1e4e0..5aebc7bd4 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/new_document.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/new_document.rs @@ -159,6 +159,8 @@ struct Shared<'a> { library_words: &'a LibraryWords, units: &'a Units, untitled: &'a str, + /// The host opened the settings ([`NewDocumentDialog::opening`]). + host_opened: bool, } /// What the dialog keeps while it is open besides the settings: the fields that show the size, the open tab and card, @@ -240,21 +242,13 @@ fn finish(mut settings: DocumentOptions, untitled: &str) -> DocumentOptions { settings } -/// Fills what the dialog adds on the pass it opens in: the clipboard's size (the dialog opens on its card) and the +/// Fills what the dialog adds to the settings it opens on: the clipboard's size at the screen resolution and the /// default name. -fn open_values(values: &mut DocumentOptions, session: &mut Session, setup: &Setup, untitled: &str) { - open_options(values, setup, untitled); - if setup.clipboard.is_some() { - session.card = Some(0); - } -} - -/// The settings part of [`open_values`]: what the opening settings are on the pass the dialog opens in. -fn open_options(values: &mut DocumentOptions, setup: &Setup, untitled: &str) { - if let Some(size) = setup.clipboard { +fn open_options(values: &mut DocumentOptions, clipboard: Option, screen_ppi: f64, untitled: &str) { + if let Some(size) = clipboard { values.size = size; values.unit = UnitKind::Pixels; - values.pixels_per_inch = setup.screen_ppi; + values.pixels_per_inch = screen_ppi; } if values.name.is_empty() { untitled.clone_into(&mut values.name); @@ -352,11 +346,17 @@ static COLUMNS: [ColumnWidth; 2] = [ColumnWidth::fill(1.0), ColumnWidth::Content /// The body of the dialog: the columns, then what they asked for. fn body(ui: &mut Ui, form: &mut FormCx<'_, '_>, values: &mut DocumentOptions, shared: &Shared<'_>) -> bool { - let Shared { bound, setup, words, preset_words, library_words, units, untitled } = *shared; + let Shared { bound, setup, words, preset_words, library_words, units, untitled, host_opened } = *shared; let points = units.points_per_inch(); let mut session = form.memory::().clone(); + // The dialog opens on the clipboard's card; settings the host opened already hold its size. if form.first() { - open_values(values, &mut session, setup, untitled); + if !host_opened { + open_options(values, setup.clipboard, setup.screen_ppi, untitled); + } + if setup.clipboard.is_some() { + session.card = Some(0); + } } for answer in form.take_answers() { if let HostAnswer::Color { key, color } = answer @@ -525,8 +525,9 @@ pub struct NewDocumentDialog<'a> { /// The host's answers to the requests of earlier passes. #[prop] answers: Vec, - /// The settings the host opened the dialog with: what Reset restores, in place of the bound settings of the first - /// pass (a field edited before the dialog was first shown is not the Reset target). + /// The settings the host opened the dialog with, the clipboard's size and the default name filled in (as + /// `HostedNewDocument` does when it is made): the first pass keeps the bound settings as they are, and Reset + /// restores these (a field edited before the dialog was first shown is not the Reset target). #[prop] opening: Option, } @@ -610,6 +611,7 @@ impl<'a> NewDocumentDialog<'a> { library_words: &library_words, units: &units, untitled: &untitled, + host_opened: opening.is_some(), }; let mut dialog = FormDialog::new(words.title.clone(), bound) @@ -622,8 +624,7 @@ impl<'a> NewDocumentDialog<'a> { if let Some(open) = open { dialog = dialog.opened(open); } - if let Some(mut opening) = opening { - open_options(&mut opening, &setup, &untitled); + if let Some(opening) = opening { dialog = dialog.opening(opening); } if let Some(key) = remember { @@ -665,7 +666,7 @@ type Build = dyn for<'b> Fn(&'b mut DocumentOptions) -> NewDocumentDialog<'b> + /// A [`NewDocumentDialog`] the [`DialogHost`](crate::dialog::DialogHost) owns: the host keeps the settings and the colour request, and `build` makes the dialog over the settings each pass. /// /// ```ignore -/// host.open(HostedNewDocument::new(last_created, |options| { +/// host.open(HostedNewDocument::new(ctx, last_created, |options| { /// NewDocumentDialog::new(Bind::bound(options)).clipboard(size).app_name("PhotoCraft") /// })); /// // each pass, for a colour request: @@ -688,14 +689,24 @@ pub struct HostedNewDocument { tab: String, /// The saved-preset edits the app has not taken yet. events: Vec>, - /// The settings the dialog was made with: what Reset restores, whatever automation sets before the first show. + /// The settings the dialog opened on, the clipboard's size and the default name filled in: what Reset restores, + /// whatever automation sets before the first show. opening: DocumentOptions, } impl HostedNewDocument { /// A hosted dialog opening on `options`; `build` makes the dialog over them (the clipboard, the profiles, the - /// app's name). - pub fn new(options: DocumentOptions, build: impl for<'b> Fn(&'b mut DocumentOptions) -> NewDocumentDialog<'b> + Send + 'static) -> Self { + /// app's name). The settings open at once (the clipboard's size, the default name from the root's words), so + /// automation lists and sets them before the first pass. + pub fn new( + ctx: &egui::Context, + mut options: DocumentOptions, + build: impl for<'b> Fn(&'b mut DocumentOptions) -> NewDocumentDialog<'b> + Send + 'static, + ) -> Self { + let built = build(&mut options); + let (clipboard, screen_ppi) = (built.clipboard, built.screen_resolution); + drop(built); + open_options(&mut options, clipboard, screen_ppi, Env::of_root(ctx).words::().untitled.as_str()); HostedNewDocument { id: dialog_id("new-document"), opening: options.clone(), diff --git a/crates/ui-patterns/tests/it/form_dialogs/hosted.rs b/crates/ui-patterns/tests/it/form_dialogs/hosted.rs index eb5dfa4c8..b5dfe7a0d 100644 --- a/crates/ui-patterns/tests/it/form_dialogs/hosted.rs +++ b/crates/ui-patterns/tests/it/form_dialogs/hosted.rs @@ -6,7 +6,8 @@ use craft_ui::dialog::{DialogHost, DialogResult, HeldOk, StackDialog}; use craft_ui::patterns::form_dialogs::{ AboutInfo, AboutLink, CanvasSize, CanvasSizeValues, Credits, ExportOptions, ExportOptionsKind, FormatOptions, GuideLayoutDialog, GuideLayoutValues, GuideRect, GuideUnits, HostedAbout, HostedCanvasSize, HostedExportAs, HostedFormatOptions, HostedGuideLayoutDialog, - HostedImageSize, HostedNewGuideDialog, ImageSize, ImageSizeValues, JpegOptions, NewGuideDialog, NewGuideValues, + HostedImageSize, HostedNewDocument, HostedNewGuideDialog, ImageSize, ImageSizeValues, JpegOptions, NewDocumentDialog, NewGuideDialog, + NewGuideValues, }; use craft_ui::runtime::env::UnitKind; use craft_ui::testing::{Fixture, trial}; @@ -78,14 +79,15 @@ fn fixture(host: &Rc>) -> 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() -> D, title: &str, cancel: DialogResult) +fn opens_and_cancels(make: fn(&egui::Context) -> D, title: &str, cancel: DialogResult) where D::Result: Clone + Debug + PartialEq, { let host = Rc::new(RefCell::new(DialogHost::new())); let mut fixture = fixture(&host); for round in ["first", "second"] { - host.borrow_mut().open(make()); + let made = make(fixture.ctx()); + host.borrow_mut().open(made); fixture.idle(10); assert!(fixture.by_label(title).is_some(), "{title} shows on the host ({round} open)"); fixture.key(Key::Escape, Modifiers::NONE).idle(4); @@ -96,11 +98,11 @@ where #[trial] fn each_hosted_form_opens_on_the_host_and_escape_reaches_the_app() { - opens_and_cancels(canvas_size, "Canvas Size", DialogResult::Cancel); - opens_and_cancels(|| HostedFormatOptions::new(ExportOptionsKind::Jpeg), "JPEG Options", DialogResult::Cancel); - opens_and_cancels(|| HostedExportAs::new("Poster", PICTURE, ExportOptions::default()), "Export As", DialogResult::Cancel); + opens_and_cancels(|_| canvas_size(), "Canvas Size", DialogResult::Cancel); + opens_and_cancels(|ctx| HostedFormatOptions::new(ctx, ExportOptionsKind::Jpeg), "JPEG Options", DialogResult::Cancel); + opens_and_cancels(|_| HostedExportAs::new("Poster", PICTURE, ExportOptions::default()), "Export As", DialogResult::Cancel); // Escape closes About like OK: a notice has no Cancel. - opens_and_cancels(about, "About PhotoCraft", DialogResult::Ok(())); + opens_and_cancels(|_| about(), "About PhotoCraft", DialogResult::Ok(())); } #[trial] @@ -113,7 +115,8 @@ fn hosted_forms_answer_ok_with_their_values() { let size = host.borrow_mut().take::().and_then(DialogResult::ok).map(|result| result.size); assert_eq!(size, Some(PICTURE), "Enter answers Canvas Size with its size"); - host.borrow_mut().open(HostedFormatOptions::new(ExportOptionsKind::Jpeg).initial(FormatOptions::Jpeg(JpegOptions::new(10)))); + let jpeg = HostedFormatOptions::new(fixture.ctx(), ExportOptionsKind::Jpeg).initial(FormatOptions::Jpeg(JpegOptions::new(10))); + host.borrow_mut().open(jpeg); fixture.idle(10).key(Key::Enter, Modifiers::NONE).idle(4); let quality = match host.borrow_mut().take::() { Some(DialogResult::Ok(FormatOptions::Jpeg(options))) => Some(options.quality), @@ -140,21 +143,21 @@ fn hosted_forms_answer_ok_with_their_values() { assert_eq!(guide, Some(250.0), "New Guide answers the guide at the set position"); } -// The JPEG quality row lists the quality the dialog opened with, and the quality automation sets, kept in the form -// automation gives it, is the one OK answers. +// The JPEG quality row lists the quality the dialog opened with from the moment it opens, a set made before the first +// pass stays, and the quality automation sets, kept in the form automation gives it, is the one OK answers. #[trial] fn the_jpeg_quality_lists_its_opening_value_and_ok_answers_a_set_one() { let host = Rc::new(RefCell::new(DialogHost::new())); let mut fixture = fixture(&host); - let id = host.borrow_mut().open(HostedFormatOptions::new(ExportOptionsKind::Jpeg).initial(FormatOptions::Jpeg(JpegOptions::new(10)))); - fixture.idle(2); + let ctx = fixture.ctx().clone(); + let id = host.borrow_mut().open(HostedFormatOptions::new(&ctx, ExportOptionsKind::Jpeg).initial(FormatOptions::Jpeg(JpegOptions::new(10)))); let quality = |host: &Rc>| { host.borrow().open_dialogs().into_iter().find(|open| open.id == id).and_then(|open| open.fields).and_then(|fields| fields["quality"].as_u64()) }; - assert_eq!(quality(&host), Some(10), "the row opens with the start quality"); - let ctx = fixture.ctx().clone(); + assert_eq!(quality(&host), Some(10), "the row lists the start quality before the first pass"); assert_eq!(host.borrow_mut().set_json_field(&ctx, id, "quality", json!(3)), Ok(())); - assert_eq!(quality(&host), Some(3)); + fixture.idle(2); + assert_eq!(quality(&host), Some(3), "the set made before the first pass stays"); assert!(host.borrow_mut().confirm::(&ctx), "OK is enabled"); let answered = match host.borrow_mut().take::() { Some(DialogResult::Ok(FormatOptions::Jpeg(options))) => Some(options.quality), @@ -163,6 +166,32 @@ fn the_jpeg_quality_lists_its_opening_value_and_ok_answers_a_set_one() { assert_eq!(answered, Some(3), "OK answers the set quality"); } +// New Document opened with an image on the clipboard lists the clipboard's size and the default name from the moment +// it opens, and a size automation sets before the first pass is the one the dialog shows and Create answers. +#[trial] +fn new_document_lists_its_clipboard_size_at_once_and_keeps_a_set_made_before_the_first_pass() { + use craft_ui::contract::Bind; + use craft_ui::patterns::form_dialogs::{DocumentOptions, NewDocumentResult}; + + let host = Rc::new(RefCell::new(DialogHost::new())); + let mut fixture = fixture(&host); + let ctx = fixture.ctx().clone(); + let clipboard = PixelSize::new(640, 480); + let dialog = HostedNewDocument::new(&ctx, DocumentOptions::default(), move |options| NewDocumentDialog::new(Bind::bound(options)).clipboard(clipboard)); + let id = host.borrow_mut().open(dialog); + let fields = |host: &Rc>| host.borrow().open_dialogs().into_iter().find(|open| open.id == id).and_then(|open| open.fields); + let opened = fields(&host).expect("the dialog lists its settings"); + assert_eq!((opened["size"]["width"].as_u64(), opened["size"]["height"].as_u64()), (Some(640), Some(480)), "the clipboard's size, before the first pass"); + assert!(opened["name"].as_str().is_some_and(|name| !name.is_empty()), "the default name, before the first pass"); + assert_eq!(host.borrow_mut().set_json_field(&ctx, id, "size", json!({"width": 800, "height": 600})), Ok(())); + fixture.idle(10); + let shown = fields(&host).expect("the dialog lists its settings"); + assert_eq!((shown["size"]["width"].as_u64(), shown["size"]["height"].as_u64()), (Some(800), Some(600)), "the first pass keeps the set size"); + assert!(host.borrow_mut().confirm::(&ctx), "Create is enabled"); + let created = host.borrow_mut().take::().and_then(DialogResult::ok).map(|result: NewDocumentResult| result.options.size); + assert_eq!(created, Some(PixelSize::new(800, 600)), "Create answers the set size"); +} + // New Guide's OK refuses while the Position field holds text that does not parse, rather than answering the last good // position; once a parsing text is committed, OK answers it. #[trial]