mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-16 23:24:24 +00:00
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.
This commit is contained in:
@@ -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));
|
||||
|
||||
@@ -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<CloseReason>,
|
||||
}
|
||||
|
||||
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<dyn FnMut(&mut Ui, &mut FooterCx<'_, '_>) + 'a>;
|
||||
pub type FooterFn<'a> = Box<dyn FnMut(&mut Ui, &mut FooterCx<'_, '_, '_>) + '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<CloseReason>| {
|
||||
let show_content = |ui: &mut Ui, cx: &mut BodyCx<'_>, content: &mut Option<FooterFn<'_>>, close: &mut Option<CloseReason>| {
|
||||
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
|
||||
|
||||
@@ -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));
|
||||
|
||||
@@ -102,6 +102,8 @@ pub struct FormOutput<V> {
|
||||
|
||||
type FooterExtraFn<'a> = Box<dyn FnMut(&mut Ui) + 'a>;
|
||||
|
||||
type FooterValueFn<'a, V> = Box<dyn FnMut(&mut Ui, &mut V) + 'a>;
|
||||
|
||||
type OnResetFn<'a> = Box<dyn FnMut(&mut BodyCx<'_>) + 'a>;
|
||||
|
||||
type ResetWithFn<'a, V> = Box<dyn Fn(&V, &V) -> V + 'a>;
|
||||
@@ -131,6 +133,7 @@ pub struct FormDialog<'a, V: Value + Default> {
|
||||
closable: bool,
|
||||
accept_stays_open: bool,
|
||||
footer_extra: Option<FooterExtraFn<'a>>,
|
||||
footer_value: Option<FooterValueFn<'a, V>>,
|
||||
extra_buttons: Vec<FooterButton>,
|
||||
shaped: Option<Box<dyn FnOnce(Dialog<'a>) -> Dialog<'a> + 'a>>,
|
||||
answers: Vec<HostAnswer>,
|
||||
@@ -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<Item = FooterButton>) -> 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::<V>));
|
||||
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::<V>);
|
||||
let changed = RefCell::new(None::<V>);
|
||||
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() }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<Vec<Guide>>);
|
||||
|
||||
/// 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::<GuideDialogWords>();
|
||||
let preview_word = env.words::<ParamDialogWords>().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::<Vec<Guide>>);
|
||||
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) {
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
Reference in New Issue
Block a user