From 06d8d76868e08ac8aed7c5eb60114d610a06d69e Mon Sep 17 00:00:00 2001 From: aiongg Date: Wed, 11 Nov 2026 21:38:47 +0000 Subject: [PATCH] Footer content reaches the dialog's body context; GuideLayout's Preview under the side buttons FooterCx::dialog gives footer content the dialog's body context, as settle_buttons and on_pick have it. FormDialog::footer_value edits the bound value from the footer, and New Guide Layout's Preview check box uses it under the side buttons. --- crates/ui-canvas/src/canvas/full_window.rs | 2 +- crates/ui-panels/src/dialog/frame.rs | 25 +++++++----- crates/ui-panels/src/dialog/modal.rs | 2 +- .../src/patterns/form_dialogs/form.rs | 38 +++++++++++++++++-- .../src/patterns/form_dialogs/guides.rs | 21 ++++++---- .../patterns/form_dialogs/guides/trials.rs | 32 ++++++++++++++++ 6 files changed, 97 insertions(+), 23 deletions(-) create mode 100644 crates/ui-patterns/src/patterns/form_dialogs/guides/trials.rs diff --git a/crates/ui-canvas/src/canvas/full_window.rs b/crates/ui-canvas/src/canvas/full_window.rs index 49796d380..ba28ec7a2 100644 --- a/crates/ui-canvas/src/canvas/full_window.rs +++ b/crates/ui-canvas/src/canvas/full_window.rs @@ -168,7 +168,7 @@ impl<'a> FullWindowDialog<'a> { /// The footer's content: the closure fills a footer bar above the buttons, or alone. #[must_use] - pub fn footer(mut self, content: impl FnMut(&mut Ui, &mut FooterCx<'_, '_>) + 'a) -> Self { + pub fn footer(mut self, content: impl FnMut(&mut Ui, &mut FooterCx<'_, '_, '_>) + 'a) -> Self { let region = self.footer_region(); self.props = self.props.footer(region); self.footer = Some(Box::new(content)); diff --git a/crates/ui-panels/src/dialog/frame.rs b/crates/ui-panels/src/dialog/frame.rs index 24d841dd2..6b5199782 100644 --- a/crates/ui-panels/src/dialog/frame.rs +++ b/crates/ui-panels/src/dialog/frame.rs @@ -48,18 +48,25 @@ impl DialogOutput { /// The heading level AccessKit reports for a dialog's title. const TITLE_LEVEL: usize = 1; -/// What a footer closure reaches: the footer bar's body context, and a request to close the dialog. -pub struct FooterCx<'a, 'b> { +/// What a footer closure reaches: the footer bar's body context, the dialog's, and a request to close the dialog. +pub struct FooterCx<'a, 'b, 'c> { bar: &'a mut BodyCx<'b>, + dialog: &'a mut BodyCx<'c>, close: &'a mut Option, } -impl<'b> FooterCx<'_, 'b> { +impl<'b, 'c> FooterCx<'_, 'b, 'c> { /// The footer bar's body context. pub fn bar(&mut self) -> &mut BodyCx<'b> { self.bar } + /// The dialog's body context, the one its body closure gets: footer content that edits what the body shows + /// reads and writes its memory and its bindings through it. + pub fn dialog(&mut self) -> &mut BodyCx<'c> { + self.dialog + } + /// Asks the dialog to close, as `CloseReason::Requested`: step 15 closes it. pub fn request_close(&mut self) { self.request_close_as(CloseReason::Requested); @@ -74,7 +81,7 @@ impl<'b> FooterCx<'_, 'b> { /// The footer closure of a dialog shell. #[doc(hidden)] -pub type FooterFn<'a> = Box) + 'a>; +pub type FooterFn<'a> = Box) + 'a>; /// The host's answer to a picked button: given the button and the dialog's body context, whether the dialog may /// close. @@ -165,14 +172,14 @@ pub(crate) fn footer_in( None => buttons, }; let mut close = None; - let mut show_content = |ui: &mut Ui, close: &mut Option| { + let show_content = |ui: &mut Ui, cx: &mut BodyCx<'_>, content: &mut Option>, close: &mut Option| { if let Some(content) = content.as_mut() { let bar = if column { FooterBar::body().column() } else { FooterBar::body() }; - bar.show(ui, |ui, bar| content(ui, &mut FooterCx { bar, close })); + bar.show(ui, |ui, bar| content(ui, &mut FooterCx { bar, dialog: cx, close })); } }; if !column { - show_content(ui, &mut close); + show_content(ui, cx, content, &mut close); } if !buttons.is_empty() { let bar = FooterBar::buttons(buttons.iter().cloned()).enter_activates(enter); @@ -192,7 +199,7 @@ pub(crate) fn footer_in( } } if column { - show_content(ui, &mut close); + show_content(ui, cx, content, &mut close); } if let Some(why) = close { cx.request_close_as(why); @@ -253,7 +260,7 @@ impl<'a> DialogFrame<'a> { /// The footer's content: the closure fills a footer bar under the body. #[must_use] - pub fn footer(mut self, content: impl FnMut(&mut Ui, &mut FooterCx<'_, '_>) + 'a) -> Self { + pub fn footer(mut self, content: impl FnMut(&mut Ui, &mut FooterCx<'_, '_, '_>) + 'a) -> Self { self.props = self.props.footer(FOOTER); self.footer = Some(Box::new(content)); self diff --git a/crates/ui-panels/src/dialog/modal.rs b/crates/ui-panels/src/dialog/modal.rs index 741ccc1e3..187171813 100644 --- a/crates/ui-panels/src/dialog/modal.rs +++ b/crates/ui-panels/src/dialog/modal.rs @@ -244,7 +244,7 @@ impl<'a> Dialog<'a> { /// The footer's content: the closure fills a footer bar above the buttons (under them with /// [`side_buttons`](Dialog::side_buttons)), or alone. #[must_use] - pub fn footer(mut self, content: impl FnMut(&mut Ui, &mut FooterCx<'_, '_>) + 'a) -> Self { + pub fn footer(mut self, content: impl FnMut(&mut Ui, &mut FooterCx<'_, '_, '_>) + 'a) -> Self { let region = self.footer_region(); self.props = self.props.footer(region); self.footer = Some(Box::new(content)); diff --git a/crates/ui-patterns/src/patterns/form_dialogs/form.rs b/crates/ui-patterns/src/patterns/form_dialogs/form.rs index 57d729d14..407345774 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/form.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/form.rs @@ -102,6 +102,8 @@ pub struct FormOutput { type FooterExtraFn<'a> = Box; +type FooterValueFn<'a, V> = Box; + type OnResetFn<'a> = Box) + 'a>; type ResetWithFn<'a, V> = Box V + 'a>; @@ -131,6 +133,7 @@ pub struct FormDialog<'a, V: Value + Default> { closable: bool, accept_stays_open: bool, footer_extra: Option>, + footer_value: Option>, extra_buttons: Vec, shaped: Option) -> Dialog<'a> + 'a>>, answers: Vec, @@ -156,6 +159,7 @@ impl<'a, V: Value + Default> FormDialog<'a, V> { closable: true, accept_stays_open: false, footer_extra: None, + footer_value: None, extra_buttons: Vec::new(), shaped: None, answers: Vec::new(), @@ -236,6 +240,13 @@ impl<'a, V: Value + Default> FormDialog<'a, V> { self } + /// Controls in the footer that edit the value (a Preview check box under side buttons): they run after the body + /// and before the footer extra, and what they change is written to the bind as the body's edits are. + pub fn footer_value(mut self, edit: impl FnMut(&mut Ui, &mut V) + 'a) -> Self { + self.footer_value = Some(Box::new(edit)); + self + } + /// Footer buttons after OK and Cancel, which keep the dialog open: the caller acts on /// [`FormOutput::picked`]. pub fn extra_buttons(mut self, buttons: impl IntoIterator) -> Self { @@ -285,6 +296,7 @@ impl<'a, V: Value + Default> FormDialog<'a, V> { closable, accept_stays_open, footer_extra, + footer_value, extra_buttons, shaped, mut answers, @@ -328,15 +340,33 @@ impl<'a, V: Value + Default> FormDialog<'a, V> { if collection_body { dialog = dialog.collection_body(); } - if let Some(mut extra) = footer_extra { - dialog = dialog.footer(move |ui, _| extra(ui)); + let changed = Rc::new(RefCell::new(None::)); + if footer_extra.is_some() || footer_value.is_some() { + let (bind, changed, name) = (Rc::clone(&bind), Rc::clone(&changed), name.clone()); + let (mut extra, mut edit) = (footer_extra, footer_value); + dialog = dialog.footer(move |ui, footer| { + if let Some(edit) = edit.as_mut() { + let cx = footer.dialog(); + let mut held = bind.borrow_mut(); + if let Some(Mixed::Same(before)) = held.reborrow().read(cx) { + let mut values = before.clone(); + edit(ui, &mut values); + if values != before { + let _ = held.reborrow().write(cx, &EditEvent::set(Mixed::Same(before), values.clone(), name.clone(), None)); + *changed.borrow_mut() = Some(values); + } + } + } + if let Some(extra) = extra.as_mut() { + extra(ui); + } + }); } if let Some(shape) = shaped { dialog = shape(dialog); } let work = RefCell::new(None::); - let changed = RefCell::new(None::); let requests = RefCell::new(HostRequests::new()); let accepts = Cell::new(ok_enabled); let shown = dialog.show(ui, |ui, cx| { @@ -378,6 +408,6 @@ impl<'a, V: Value + Default> FormDialog<'a, V> { Some(CloseReason::Enter | CloseReason::Activated) if accepts.get() => work.into_inner(), _ => None, }; - FormOutput { response, closed, picked, accepted, value: changed.into_inner(), requests: requests.into_inner() } + FormOutput { response, closed, picked, accepted, value: changed.take(), requests: requests.into_inner() } } } diff --git a/crates/ui-patterns/src/patterns/form_dialogs/guides.rs b/crates/ui-patterns/src/patterns/form_dialogs/guides.rs index 0855ca5f6..a8e4d09ba 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/guides.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/guides.rs @@ -12,6 +12,8 @@ mod new_guide; #[cfg(feature = "testing")] mod stories; +#[cfg(feature = "testing")] +mod trials; pub use layout::{GuideGroup, GuideLayoutError, GuideLayoutOptions, GuideLayoutPreset, GuideLength, GuideRect, GuideTarget, GuideUnits, MAX_COUNT}; @@ -270,7 +272,7 @@ struct LastValid(Option>); /// New Guide Layout, Photoshop's dialog: Preset and Target lists, Color, the Columns and Rows groups (each a check /// box with Number, Width or Height, and Gutter), the Margin group (a check box and Top, Left, Bottom, Right), Center -/// Columns and Clear Existing Guides; OK, Cancel and Preview. +/// Columns and Clear Existing Guides; beside them a column of OK and Cancel with Preview under it. /// /// - Keys: Tab walks the fields, Enter is OK when the layout is valid, Escape cancels. Holding Alt turns Cancel /// into Reset, which restores the values from when the dialog opened and keeps the dialog open. @@ -318,8 +320,15 @@ impl<'a> GuideLayoutDialog<'a> { let words = env.words::(); let preview_word = env.words::().preview.clone(); let number_units = env.units(); - let mut dialog = - FormDialog::new(words.layout_title.clone(), values).outer_gesture(true).answers(answers).shaped(|dialog| dialog.side_buttons(true)); + let mut dialog = FormDialog::new(words.layout_title.clone(), values) + .outer_gesture(true) + .answers(answers) + .shaped(|dialog| dialog.side_buttons(true)) + .footer_value(move |ui, values| { + if let Some(on) = Checkbox::new(Bind::given(values.preview)).label(preview_word.clone()).show(ui).value().copied() { + values.preview = on; + } + }); if let Some(open) = open { dialog = dialog.opened(open); } @@ -327,7 +336,7 @@ impl<'a> GuideLayoutDialog<'a> { let last = RefCell::new(None::>); let previewing = Cell::new(false); let measure = Measure { words: &words, numbers: &number_units, units: &units, unparsed: Cell::new(false) }; - let rows = Form { document: &document, artboards: &artboards, preview_word: &preview_word }; + let rows = Form { document: &document, artboards: &artboards }; let shown = dialog.show(ui, |ui, form, values| { apply_color_answers(form.take_answers(), GUIDE_LAYOUT_COLOR_KEY, &mut values.color); measure.unparsed.set(false); @@ -394,9 +403,6 @@ fn layout_rows(ui: &mut Ui, measure: &Measure<'_>, form: &Form<'_>, values: &mut if let Some(on) = Checkbox::new(Bind::given(clear)).label(words.clear_existing.clone()).show(ui).value().copied() { values.options.clear_existing = on; } - if let Some(on) = Checkbox::new(Bind::given(values.preview)).label(form.preview_word.clone()).show(ui).value().copied() { - values.preview = on; - } }); if !values.options.same_fields(&before) && values.options.preset == before.preset { values.options.preset = GuideLayoutPreset::Custom; @@ -407,7 +413,6 @@ fn layout_rows(ui: &mut Ui, measure: &Measure<'_>, form: &Form<'_>, values: &mut struct Form<'a> { document: &'a GuideRect, artboards: &'a [GuideRect], - preview_word: &'a Label, } fn preset_row(ui: &mut Ui, words: &GuideDialogWords, options: &mut GuideLayoutOptions) { diff --git a/crates/ui-patterns/src/patterns/form_dialogs/guides/trials.rs b/crates/ui-patterns/src/patterns/form_dialogs/guides/trials.rs new file mode 100644 index 000000000..9cfc50cf4 --- /dev/null +++ b/crates/ui-patterns/src/patterns/form_dialogs/guides/trials.rs @@ -0,0 +1,32 @@ +//! Trials of the guide dialogs. + +use super::{GuideLayoutDialog, GuideLayoutValues, GuideRect, GuideUnits}; +use crate::contract::Bind; +use crate::runtime::env::UnitKind; +use crate::testing::{Fixture, trial}; +use std::cell::{Cell, RefCell}; +use std::rc::Rc; + +#[trial] +fn the_preview_check_box_under_the_side_buttons_edits_the_bound_values() { + let values = Rc::new(RefCell::new(GuideLayoutValues::default())); + let previewing = Rc::new(Cell::new(false)); + let (held, seen) = (Rc::clone(&values), Rc::clone(&previewing)); + let mut fixture = Fixture::new(move |ui| { + let units = GuideUnits::new(UnitKind::Pixels, 3000.0, 2000.0, 300.0); + let document = GuideRect::new(0.0, 0.0, 3000.0, 2000.0); + let mut values = held.borrow_mut(); + let shown = GuideLayoutDialog::new(units, document, Bind::bound(&mut *values)).opened(Bind::given(true)).show(ui); + seen.set(shown.preview.is_some()); + }); + fixture.settle(); + assert!(values.borrow().preview && previewing.get(), "Preview starts on and the layout previews"); + let (Some(preview), Some(ok)) = (fixture.by_label("Preview"), fixture.by_label("OK")) else { + panic!("the Preview check box and OK are shown"); + }; + assert!(preview.rect.min.y >= ok.rect.max.y && (preview.rect.min.x - ok.rect.min.x).abs() < 0.5, "Preview sits under the buttons"); + fixture.click(&preview); + fixture.settle(); + assert!(!values.borrow().preview, "the click reached the bound values"); + assert!(!previewing.get(), "with Preview off the dialog previews nothing"); +}