mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 02:09:00 +00:00
Dialog: Close in place of OK and Cancel while the fields are unchanged (L-307)
This commit is contained in:
@@ -7,6 +7,8 @@ craft_ui::words! {
|
||||
pub struct CommonWords {
|
||||
cancel: "common.cancel" = "Cancel";
|
||||
ok: "common.ok" = "OK";
|
||||
/// The button of a dialog whose fields are unchanged, in place of OK and Cancel.
|
||||
close: "common.close" = "Close";
|
||||
dont_show_again: "common.dontShowAgain" = "Don't show again";
|
||||
/// The tooltip of a label's reset dot.
|
||||
reset_to_default: "common.resetToDefault" = "Reset to default";
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
//! focus trap, placement and size remembered under a `SaveKey`.
|
||||
|
||||
use super::frame::{self, DialogOutput, FOOTER, FooterCx, FooterFn, FooterHooks, HEADER, SIDE_BUTTONS};
|
||||
use crate::contract::Remember as _;
|
||||
use crate::contract::overlay::{FloatPlace, OverlayFit, Place};
|
||||
use crate::contract::{
|
||||
Bind, BindMut, BodyCx, Common, Component, Container, ContainerProps, Cx, DragBand, FooterPlace, FooterRegion, HeaderRegion, Mount, Overlay, Pad,
|
||||
@@ -9,7 +10,8 @@ use crate::contract::{
|
||||
};
|
||||
use crate::edit::outer::{self, OuterEnd};
|
||||
use crate::runtime::access::Access;
|
||||
use crate::runtime::env::Label;
|
||||
use crate::runtime::env::{Env, Label};
|
||||
use crate::runtime::words::CommonWords;
|
||||
use crate::runtime::focus::Initial;
|
||||
use crate::runtime::memory::sub;
|
||||
use crate::runtime::overlay::{self, Backdrop, CloseReason, Rank};
|
||||
@@ -82,6 +84,10 @@ pub struct Dialog<'a> {
|
||||
title_note: Option<Label>,
|
||||
/// The footer stands in a column at the end edge, beside the whole body.
|
||||
side: bool,
|
||||
/// While the body reports its fields unchanged ([`Dialog::report_changed`]), a Close button stands in place of the
|
||||
/// OK and Cancel pair.
|
||||
#[prop]
|
||||
close_unchanged: bool,
|
||||
closed: Option<CloseReason>,
|
||||
picked: Option<usize>,
|
||||
/// The body ran in this pass: the header region marks it.
|
||||
@@ -120,6 +126,7 @@ impl<'a> Dialog<'a> {
|
||||
closable: true,
|
||||
title_note: None,
|
||||
side: false,
|
||||
close_unchanged: false,
|
||||
closed: None,
|
||||
picked: None,
|
||||
body_shown: false,
|
||||
@@ -275,6 +282,12 @@ impl<'a> Dialog<'a> {
|
||||
self
|
||||
}
|
||||
|
||||
/// Reports from the body whether the dialog's fields differ from the ones it opened with; read by a dialog that
|
||||
/// [`close_unchanged`](Dialog::close_unchanged).
|
||||
pub fn report_changed(cx: &mut BodyCx<'_>, changed: bool) {
|
||||
cx.memory::<Reported>().unchanged = !changed;
|
||||
}
|
||||
|
||||
/// 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 }
|
||||
@@ -475,15 +488,30 @@ impl Container for Dialog<'_> {
|
||||
}
|
||||
|
||||
fn footer(&mut self, ui: &mut Ui, cx: &mut BodyCx<'_>) {
|
||||
// An unchanged dialog has nothing to accept: one Close button answers as the Cancel button would.
|
||||
let cancel_at = self.buttons.iter().position(|button| button.role() == DialogRole::Cancel);
|
||||
let has_accept = self.buttons.iter().any(|button| button.role() == DialogRole::Accept);
|
||||
let close = cancel_at.filter(|_| self.close_unchanged && has_accept && cx.memory::<Reported>().unchanged);
|
||||
let closing;
|
||||
let buttons = if close.is_some() {
|
||||
let label = Env::of(ui).words::<CommonWords>().close.clone();
|
||||
closing = [FooterButton::new(label, DialogRole::Cancel)];
|
||||
&closing[..]
|
||||
} else {
|
||||
&self.buttons[..]
|
||||
};
|
||||
frame::footer_in(
|
||||
ui,
|
||||
cx,
|
||||
&mut self.footer,
|
||||
&self.buttons,
|
||||
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,
|
||||
);
|
||||
if let (Some(at), Some(0)) = (close, self.picked) {
|
||||
self.picked = Some(at);
|
||||
}
|
||||
}
|
||||
|
||||
fn closed(&mut self, why: CloseReason) {
|
||||
@@ -508,3 +536,9 @@ impl<'a> FooterHooks<'a> for Dialog<'a> {
|
||||
Dialog::close_guard(self, allows)
|
||||
}
|
||||
}
|
||||
|
||||
/// What the body reported about its fields in this pass.
|
||||
#[derive(Clone, Copy, Default)]
|
||||
struct Reported {
|
||||
unchanged: bool,
|
||||
}
|
||||
|
||||
@@ -68,6 +68,35 @@ fn a_footer_button_closes_the_dialog_by_its_role() {
|
||||
assert_eq!(run("Delete"), (Some(CloseReason::Activated), Some(0)), "any other button closes as an outcome");
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn a_dialog_with_unchanged_fields_shows_close_in_place_of_ok_and_cancel() {
|
||||
let run = |changed: bool| {
|
||||
let seen = Rc::new(Cell::new((None, None)));
|
||||
let sink = seen.clone();
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let mut open = true;
|
||||
let shown = Dialog::new("Properties")
|
||||
.opened(Bind::bound(&mut open))
|
||||
.close_unchanged(true)
|
||||
.buttons([FooterButton::new("OK", DialogRole::Accept), FooterButton::new("Cancel", DialogRole::Cancel)])
|
||||
.show(ui, |_, cx| Dialog::report_changed(cx, changed));
|
||||
if shown.output.closed.is_some() {
|
||||
sink.set((shown.output.closed, shown.output.picked));
|
||||
}
|
||||
});
|
||||
fixture.idle(30);
|
||||
// The title row's close action carries the same name; the footer's button is the last of them.
|
||||
let closes: Vec<_> = fixture.by_role(Role::BUTTON).into_iter().filter(|target| fixture.tree().name_of(target.node) == Some("Close")).collect();
|
||||
let labels = (fixture.by_label("OK").is_some(), fixture.by_label("Cancel").is_some(), closes.len() > 1);
|
||||
if let Some(close) = closes.last().filter(|_| !changed) {
|
||||
fixture.click(close);
|
||||
}
|
||||
(labels, seen.get())
|
||||
};
|
||||
assert_eq!(run(false), ((false, false, true), (Some(CloseReason::Requested), Some(1))), "unchanged: Close, which answers as the Cancel button");
|
||||
assert_eq!(run(true).0, (true, true, false), "changed: OK and Cancel");
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn a_blocking_overlays_buttons_keep_their_place_after_it_settled() {
|
||||
let mut fixture = Fixture::new(|ui| {
|
||||
|
||||
@@ -354,6 +354,9 @@ pub struct ParamDialog<'a> {
|
||||
/// Whether the title row has a close action.
|
||||
#[prop]
|
||||
closable: bool,
|
||||
/// While no value differs from the one the dialog opened with, a Close button stands in place of OK and Cancel.
|
||||
#[prop]
|
||||
close_unchanged: bool,
|
||||
/// Whether the dialog has the Preview check box: the host previews its values while the box is on.
|
||||
#[prop]
|
||||
previews: bool,
|
||||
@@ -467,6 +470,7 @@ impl<'a> ParamDialog<'a> {
|
||||
reset_to: None,
|
||||
layout: None,
|
||||
closable: true,
|
||||
close_unchanged: false,
|
||||
previews: false,
|
||||
preview_on: None,
|
||||
preview: None,
|
||||
@@ -581,6 +585,7 @@ impl<'a> ParamDialog<'a> {
|
||||
reset_to,
|
||||
layout,
|
||||
closable,
|
||||
close_unchanged,
|
||||
previews,
|
||||
preview_on,
|
||||
preview,
|
||||
@@ -627,6 +632,7 @@ impl<'a> ParamDialog<'a> {
|
||||
// The tab strip is a collection in the body, as a browse list is.
|
||||
.collection_body(collection_body || tabbed)
|
||||
.closable(closable)
|
||||
.close_unchanged(close_unchanged)
|
||||
.accept_stays_open(hold)
|
||||
.answers(answers);
|
||||
if let Some(reset_to) = reset_to {
|
||||
@@ -949,6 +955,7 @@ impl ResetFooter {
|
||||
/// differ from the opening ones (Reset is enabled then) and whether OK can be pressed.
|
||||
pub fn report(cx: &mut BodyCx<'_>, changed: bool, ok: bool) {
|
||||
*cx.memory::<Footing>() = Footing { changed, ok: Some(ok) };
|
||||
Dialog::report_changed(cx, changed);
|
||||
}
|
||||
|
||||
/// Which button `DialogOutput::picked` names, for the buttons of this footer.
|
||||
|
||||
@@ -178,6 +178,7 @@ pub struct FormDialog<'a, V: Value + Default> {
|
||||
history: bool,
|
||||
collection_body: bool,
|
||||
closable: bool,
|
||||
close_unchanged: bool,
|
||||
accept_stays_open: bool,
|
||||
enter_confirms: bool,
|
||||
side_buttons: bool,
|
||||
@@ -210,6 +211,7 @@ impl<'a, V: Value + Default> FormDialog<'a, V> {
|
||||
history: false,
|
||||
collection_body: false,
|
||||
closable: true,
|
||||
close_unchanged: false,
|
||||
accept_stays_open: false,
|
||||
enter_confirms: true,
|
||||
side_buttons: false,
|
||||
@@ -291,6 +293,14 @@ impl<'a, V: Value + Default> FormDialog<'a, V> {
|
||||
self
|
||||
}
|
||||
|
||||
/// While no value differs from the one the dialog opened with, a Close button stands in place of OK and Cancel (as
|
||||
/// a Properties dialog does); the first edit brings them back.
|
||||
#[must_use]
|
||||
pub fn close_unchanged(mut self, on: bool) -> Self {
|
||||
self.close_unchanged = on;
|
||||
self
|
||||
}
|
||||
|
||||
/// Whether OK keeps the dialog open when it is picked, until the host answers with a close (an export asks for a
|
||||
/// path first, say). The body closes it with `FormCx::request_close_as`; the Reset footer is unchanged.
|
||||
pub fn accept_stays_open(mut self, on: bool) -> Self {
|
||||
@@ -382,6 +392,7 @@ impl<'a, V: Value + Default> FormDialog<'a, V> {
|
||||
history,
|
||||
collection_body,
|
||||
closable,
|
||||
close_unchanged,
|
||||
accept_stays_open,
|
||||
enter_confirms,
|
||||
side_buttons,
|
||||
@@ -425,6 +436,7 @@ impl<'a, V: Value + Default> FormDialog<'a, V> {
|
||||
};
|
||||
let mut dialog = Dialog::new(title)
|
||||
.closable(closable)
|
||||
.close_unchanged(close_unchanged)
|
||||
.close_on_enter(ok_enabled && enter_confirms)
|
||||
.outer_gesture(outer_gesture)
|
||||
.side_buttons(side_buttons);
|
||||
|
||||
@@ -92,3 +92,21 @@ fn a_document_dialog_keeps_the_decimals_of_its_number_rows() {
|
||||
assert!(shown(ParamLayout::Document).iter().any(|text| text == "0.50"), "the document layout writes both places");
|
||||
assert!(!shown(ParamLayout::Beside).iter().any(|text| text == "0.50"), "the plain layout trims the zero");
|
||||
}
|
||||
|
||||
/// A dialog that closes when unchanged shows Close in place of OK and Cancel until a value differs from the opening one.
|
||||
#[trial]
|
||||
fn an_unchanged_dialog_shows_close_until_a_field_changes() {
|
||||
let fields = std::rc::Rc::new(std::cell::RefCell::new(DialogFields::new()));
|
||||
let shared = fields.clone();
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let rows = vec![Param::number("size", "Size", 1.0, 1.0, 0)];
|
||||
let mut fields = shared.borrow_mut();
|
||||
ParamDialog::new("close", "Properties", rows).close_unchanged(true).values(Bind::bound(&mut *fields)).show(ui);
|
||||
});
|
||||
fixture.room(vec2(900.0, 700.0));
|
||||
fixture.settle();
|
||||
assert!(fixture.by_label("OK").is_none() && fixture.by_label("Cancel").is_none(), "unchanged: no OK or Cancel");
|
||||
fields.borrow_mut().set("size", 5.0_f64);
|
||||
fixture.settle();
|
||||
assert!(fixture.by_label("OK").is_some() && fixture.by_label("Cancel").is_some(), "after an edit: OK and Cancel");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user