diff --git a/crates/ui-panels/src/dialog/frame.rs b/crates/ui-panels/src/dialog/frame.rs index 54deddf3f..24d841dd2 100644 --- a/crates/ui-panels/src/dialog/frame.rs +++ b/crates/ui-panels/src/dialog/frame.rs @@ -140,6 +140,20 @@ pub fn header(ui: &mut Ui, cx: &mut BodyCx<'_>, actions: &[HeaderAction]) { /// Enter the focused part left untaken picks the default button, so it takes the way of a click on it, guard included. #[doc(hidden)] pub fn footer(ui: &mut Ui, cx: &mut BodyCx<'_>, content: &mut Option>, buttons: &[FooterButton], enter: bool, host: FooterHost<'_, '_>) { + footer_in(ui, cx, content, buttons, enter, host, false); +} + +/// [`footer`], with the buttons in a `column` beside the body when asked: the buttons on top, each as wide as the +/// widest, and the content closure under them. +pub(crate) fn footer_in( + ui: &mut Ui, + cx: &mut BodyCx<'_>, + content: &mut Option>, + buttons: &[FooterButton], + enter: bool, + host: FooterHost<'_, '_>, + column: bool, +) { let FooterHost { picked, guard, on_pick, settle } = host; let mut settled; let buttons = match settle.as_mut() { @@ -151,11 +165,18 @@ pub fn footer(ui: &mut Ui, cx: &mut BodyCx<'_>, content: &mut Option buttons, }; let mut close = None; - if let Some(content) = content.as_mut() { - FooterBar::body().show(ui, |ui, bar| content(ui, &mut FooterCx { bar, close: &mut close })); + let mut show_content = |ui: &mut Ui, 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 })); + } + }; + if !column { + show_content(ui, &mut close); } if !buttons.is_empty() { - *picked = FooterBar::buttons(buttons.iter().cloned()).enter_activates(enter).show_footer(ui).picked; + let bar = FooterBar::buttons(buttons.iter().cloned()).enter_activates(enter); + *picked = if column { bar.column() } else { bar }.show_footer(ui).picked; if let Some(at) = *picked { if let Some(act) = on_pick.as_mut() { act(&buttons[at], cx); @@ -170,6 +191,9 @@ pub fn footer(ui: &mut Ui, cx: &mut BodyCx<'_>, content: &mut Option { /// Whether the title row has a close action. #[prop] closable: bool, + /// The footer stands in a column at the end edge, beside the whole body. + side: bool, closed: Option, picked: Option, /// The body ran in this pass: the header region marks it. @@ -109,6 +111,7 @@ impl<'a> Dialog<'a> { buttons: Vec::new(), enter: false, closable: true, + side: false, closed: None, picked: None, body_shown: false, @@ -232,18 +235,40 @@ impl<'a> Dialog<'a> { /// `DialogOutput::picked` says which. #[must_use] pub fn buttons(mut self, buttons: impl IntoIterator) -> Self { - self.props = self.props.footer(FOOTER); + let region = self.footer_region(); + self.props = self.props.footer(region); self.buttons = buttons.into_iter().collect(); self.route_enter() } - /// The footer's content: the closure fills a footer bar above the buttons, or alone. + /// 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 { - self.props = self.props.footer(FOOTER); + let region = self.footer_region(); + self.props = self.props.footer(region); self.footer = Some(Box::new(content)); self } + + /// The footer stands in a column at the end edge, beside the whole body and at its top, instead of across the + /// dialog's foot: the buttons go `Accept`, `Cancel`, then the others, each as wide as the widest, and the footer + /// content sits under them. + #[must_use] + pub fn side_buttons(mut self, on: bool) -> Self { + self.side = on; + self.props.end = on.then(EndRegion::new); + if self.props.footer.is_some() { + let region = self.footer_region(); + self.props = self.props.footer(region); + } + self + } + + /// The footer region: in the end region's column, else across the foot. + fn footer_region(&self) -> FooterRegion { + if self.side { FooterRegion::new().place(FooterPlace::EndTop) } else { FOOTER } + } } impl Dialog<'_> { @@ -432,13 +457,14 @@ impl Container for Dialog<'_> { } fn footer(&mut self, ui: &mut Ui, cx: &mut BodyCx<'_>) { - frame::footer( + frame::footer_in( ui, cx, &mut self.footer, &self.buttons, self.enter, frame::FooterHost { picked: &mut self.picked, guard: &mut self.guard, on_pick: &mut self.on_pick, settle: &mut self.settle }, + self.side, ); } diff --git a/crates/ui-panels/src/dialog/stories.rs b/crates/ui-panels/src/dialog/stories.rs index e7aaab4eb..281ff7e58 100644 --- a/crates/ui-panels/src/dialog/stories.rs +++ b/crates/ui-panels/src/dialog/stories.rs @@ -50,6 +50,26 @@ fn dialog_with_footer(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { shown } +#[story(Dialog<'static>, covers = [], drive = Drive::Never)] +fn dialog_with_side_buttons(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { + let mut checks = *data.value("checks", [true, false]); + let shown: StoryOutput = Dialog::new("Grid options") + .opened(Bind::bound(data.value("open", true))) + .side_buttons(true) + .buttons([ + FooterButton::new("OK", DialogRole::Accept), + FooterButton::new("Cancel", DialogRole::Cancel), + FooterButton::new("Add to Swatches", DialogRole::Other).stays_open(true), + ]) + .footer(|ui, _| { + Checkbox::new(Bind::given(true)).label("Preview").show(ui); + }) + .show(ui, |ui, _| body(ui, &mut checks)) + .into(); + *data.value("checks", [true, false]) = checks; + shown +} + #[story(Dialog<'static>, covers = [], drive = Drive::Never)] fn dialog_resizable_with_initial_focus(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { let mut checks = *data.value("checks", [true, false]); diff --git a/crates/ui-panels/src/dialog/trials.rs b/crates/ui-panels/src/dialog/trials.rs index a1e58f2e4..2f4476a9d 100644 --- a/crates/ui-panels/src/dialog/trials.rs +++ b/crates/ui-panels/src/dialog/trials.rs @@ -99,3 +99,43 @@ fn a_dialog_widens_for_a_footer_wider_than_its_body() { assert!(short.iter().all(|width| *width > 0.0), "each button is shown: {short:?}"); assert_eq!(short, long, "a short body does not squeeze the buttons"); } + +#[trial] +fn side_buttons_stand_in_one_column_beside_the_body() { + let mut frames = Frames::new(); + let mut ui = |ui: &mut Ui| { + let (mut open, mut snap, mut preview) = (true, false, true); + Dialog::new("Options") + .opened(Bind::bound(&mut open)) + .side_buttons(true) + .buttons([ + FooterButton::new("Add to Swatches", DialogRole::Other), + FooterButton::new("Cancel", DialogRole::Cancel), + FooterButton::new("OK", DialogRole::Accept), + ]) + .footer(|ui, _| { + Checkbox::new(Bind::bound(&mut preview)).label("Preview").show(ui); + }) + .show(ui, |ui, _| { + Checkbox::new(Bind::bound(&mut snap)).label("Snap to the grid").show(ui); + }); + }; + frames.idle(30, &mut ui); + let tree = frames.tree(); + let rect = |name: &str| { + let key = tree.keys().into_iter().find(|key| tree.name_of(*key) == Some(name)); + key.and_then(|key| tree.bounds_of(key)) + }; + let [Some(ok), Some(cancel), Some(other), Some(preview), Some(body)] = + ["OK", "Cancel", "Add to Swatches", "Preview", "Snap to the grid"].map(rect) + else { + panic!("every part is shown"); + }; + assert!(ok.max.y <= cancel.min.y && cancel.max.y <= other.min.y, "OK, Cancel, then the other button, top to bottom"); + for button in [cancel, other] { + assert!((button.min.x - ok.min.x).abs() < 0.5 && (button.width() - ok.width()).abs() < 0.5, "one column, one width: {ok:?} {button:?}"); + } + assert!(ok.min.x >= body.max.x, "the column stands past the body's end edge"); + assert!((ok.min.y - body.min.y).abs() < 4.0, "the column starts at the body's top: {ok:?} {body:?}"); + assert!(preview.min.y >= other.max.y, "the footer content sits under the buttons"); +} diff --git a/crates/ui-panels/src/sections/footer.rs b/crates/ui-panels/src/sections/footer.rs index db04ab9b3..06ab3a6f8 100644 --- a/crates/ui-panels/src/sections/footer.rs +++ b/crates/ui-panels/src/sections/footer.rs @@ -48,6 +48,8 @@ pub struct FooterBar { /// default button (the first enabled `Accept` one) like a click on it. #[prop(name = "enter_activates")] enter: bool, + /// The members stand in a column, each as wide as the widest. + column: bool, } /// The payload a dropped-on action reports, type-erased so actions of different payload types share one footer. @@ -494,10 +496,19 @@ enum Content { Buttons(Vec), } -/// The indexes of `roles` in display order: by role rank, and in the given order within a rank. -pub(crate) fn display_order(roles: &[DialogRole], windows: bool) -> Vec { +/// The indexes of `roles` in display order: by role rank, and in the given order within a rank. A column puts +/// `Accept` on top and `Cancel` under it on every platform, then the others. +pub(crate) fn display_order(roles: &[DialogRole], windows: bool, column: bool) -> Vec { let mut order: Vec = (0..roles.len()).collect(); - order.sort_by_key(|&at| roles[at].rank(windows)); + if column { + order.sort_by_key(|&at| match roles[at] { + DialogRole::Accept => 0, + DialogRole::Cancel => 1, + DialogRole::Destructive | DialogRole::Other => 2, + }); + } else { + order.sort_by_key(|&at| roles[at].rank(windows)); + } order } @@ -529,9 +540,19 @@ impl FooterBar { props: ContainerProps::new().sizing(Sizing::FillWidth).roving(Roving::new(NavShape::Flat)).layout(BodyLayout::row().align(RowAlign::End)), content: Content::Body, enter: false, + column: false, } } + /// The members stand in a column at the top of the bar, each as wide as the widest: the buttons of a dialog whose + /// footer stands beside the body. Buttons go `Accept`, `Cancel`, then the others in the given order. + #[must_use] + pub fn column(mut self) -> Self { + self.props = self.props.layout(BodyLayout::stack()); + self.column = true; + self + } + /// A footer bar of icon actions, in the given order. Show it with `show_footer`. pub fn actions(actions: impl IntoIterator) -> Self { FooterBar { content: Content::Actions(actions.into_iter().collect()), ..FooterBar::body() } @@ -546,7 +567,7 @@ impl FooterBar { /// Shows the bar with the actions or buttons it was made with and reports what happened to them. pub fn show_footer(mut self, ui: &mut Ui) -> FooterOutput { let content = std::mem::take(&mut self.content); - let enter = self.enter; + let (enter, column) = (self.enter, self.column); let shown = self.show(ui, |ui, _| match &content { Content::Body => Shown::default(), Content::Actions(actions) => { @@ -585,14 +606,15 @@ impl FooterBar { let labels = accelerator_labels(&lettering, crate::runtime::windows::os_of(&ctx)); // Initial focus lands on the first enabled Accept button on every OS. let default_at = buttons.iter().position(|button| button.role == DialogRole::Accept && button.enabled); - for at in display_order(&roles, crate::runtime::windows::os_of(ui.ctx()) == Os::Windows) { + for at in display_order(&roles, crate::runtime::windows::os_of(ui.ctx()) == Os::Windows, column) { let button = &buttons[at]; let variant = match button.role { DialogRole::Accept => Variant::PRIMARY, DialogRole::Destructive => Variant::DANGER, DialogRole::Cancel | DialogRole::Other => Variant::SECONDARY, }; - let mut widget = Button::new(button.label.clone()).painted(labels[at].clone()).variant(variant).enabled(button.enabled); + let mut widget = + Button::new(button.label.clone()).painted(labels[at].clone()).variant(variant).enabled(button.enabled).fill_width(column); let tip = if button.enabled { button.tooltip.clone() } else { button.disabled_reason.clone().or_else(|| button.tooltip.clone()) }; if let Some(tip) = tip { widget = widget.tooltip(TooltipSpec::new(tip.clone()).disabled_text(tip)); diff --git a/crates/ui-panels/src/testing_internals.rs b/crates/ui-panels/src/testing_internals.rs index 9ad32811f..18de3a497 100644 --- a/crates/ui-panels/src/testing_internals.rs +++ b/crates/ui-panels/src/testing_internals.rs @@ -20,7 +20,7 @@ pub fn row_indent_steps(row: &ListRow<'_>) -> usize { /// The indexes of `roles` in display order. pub fn footer_order(roles: &[DialogRole], windows: bool) -> Vec { - crate::sections::footer::display_order(roles, windows) + crate::sections::footer::display_order(roles, windows, false) } /// The index an item ends at when it moves from `from` to the gap before item `insert`. diff --git a/crates/ui-widgets/src/buttons/button.rs b/crates/ui-widgets/src/buttons/button.rs index 1e9404d87..2e8b5669e 100644 --- a/crates/ui-widgets/src/buttons/button.rs +++ b/crates/ui-widgets/src/buttons/button.rs @@ -130,12 +130,16 @@ pub struct Button<'a> { #[style_key] #[prop] tint: Option, + /// Takes the width its column offers; in a column sized from its content, the width of the widest member, so a + /// column of buttons shares one width. + #[prop] + fill_width: bool, } impl<'a> Button<'a> { /// A button showing `label`, which is also its accessible name. pub fn new(label: impl Into