A dialog answer that closes the question has its own role, so Windows orders Yes, No, Cancel

This commit is contained in:
aiongg
2026-11-21 08:02:56 +00:00
parent 7fb78caf54
commit 0afbb57410
7 changed files with 83 additions and 20 deletions
+7 -2
View File
@@ -103,10 +103,15 @@ impl Outcome for PromptOutput {
}
/// The index Escape and the close action answer with: the answer of `chosen` when there is one, else Cancel, else
/// the first Other answer, else the default answer.
/// the first Alternate answer, else the first Other answer, else the default answer.
fn escape_answer(answers: &[FooterButton], chosen: Option<DialogRole>) -> Option<usize> {
let find = |wanted: DialogRole| answers.iter().position(|answer| answer.role() == wanted);
chosen.and_then(find).or_else(|| find(DialogRole::Cancel)).or_else(|| find(DialogRole::Other)).or_else(|| default_answer(answers))
chosen
.and_then(find)
.or_else(|| find(DialogRole::Cancel))
.or_else(|| find(DialogRole::Alternate))
.or_else(|| find(DialogRole::Other))
.or_else(|| default_answer(answers))
}
/// The first Accept answer, else the first Destructive one, whether it is enabled or not.
@@ -26,7 +26,7 @@ const AGES: [(&str, Duration); 4] = [
fn answers() -> [FooterButton; 3] {
[
FooterButton::new("Save", DialogRole::Accept).letter('S'),
FooterButton::new("Don't Save", DialogRole::Other).letter('D'),
FooterButton::new("Don't Save", DialogRole::Alternate).letter('D'),
FooterButton::new("Cancel", DialogRole::Cancel).letter('C'),
]
}
+2 -2
View File
@@ -79,7 +79,7 @@ impl PromptChoice {
fn role(self) -> DialogRole {
match self {
PromptChoice::Proceed => DialogRole::Accept,
PromptChoice::Alternate => DialogRole::Other,
PromptChoice::Alternate => DialogRole::Alternate,
PromptChoice::Cancel => DialogRole::Cancel,
}
}
@@ -347,7 +347,7 @@ impl StandardPrompt {
let own = self.buttons.iter().find(|(button, _)| *button == choice).map(|(_, label)| Reply::new(label.clone(), choice.role()));
let reply = own.unwrap_or_else(|| match choice {
PromptChoice::Proceed => Reply::new(proceed.clone(), role),
PromptChoice::Alternate => Reply::new(words.discard.clone(), DialogRole::Other),
PromptChoice::Alternate => Reply::new(words.discard.clone(), DialogRole::Alternate),
PromptChoice::Cancel => Reply::new(common.cancel.clone(), DialogRole::Cancel),
});
(choice, reply.enabled(enabled, None))
+1 -1
View File
@@ -105,7 +105,7 @@ impl RecoveryDialog {
let message = words.recovery_message.fill(&[("name", self.document.name.as_str()), ("age", age.as_str())]);
let reason = self.document.blocked.clone().map(|reason| Label::from_cow(reason.into()));
let replies = vec![
Reply::plain(words.recovery_discard.clone(), DialogRole::Other),
Reply::plain(words.recovery_discard.clone(), DialogRole::Alternate),
Reply::plain(words.recovery_recover.clone(), DialogRole::Accept).enabled(reason.is_none(), reason),
];
let mut prompt = Prompt::new(words.recovery_title.clone(), message).answers(buttons(&env, replies));
+2 -2
View File
@@ -127,9 +127,9 @@ impl UnsavedChangesDialog {
CloseAnswer::Save => Reply::new(if mac { &words.unsaved_save } else { &words.unsaved_yes }.clone(), DialogRole::Accept),
CloseAnswer::DontSave if self.scope == CloseScope::Revert => Reply::new(words.unsaved_revert.clone(), DialogRole::Accept),
CloseAnswer::DontSave if mac => {
Reply::new(words.unsaved_dont_save.clone(), DialogRole::Other).chord(Chord::new(Mods::CMD, Key::D))
Reply::new(words.unsaved_dont_save.clone(), DialogRole::Alternate).chord(Chord::new(Mods::CMD, Key::D))
}
CloseAnswer::DontSave => Reply::new(words.unsaved_no.clone(), DialogRole::Other),
CloseAnswer::DontSave => Reply::new(words.unsaved_no.clone(), DialogRole::Alternate),
CloseAnswer::Cancel => Reply::new(cancel.clone(), DialogRole::Cancel),
};
(answer, reply)
+20 -12
View File
@@ -380,21 +380,28 @@ pub enum DialogRole {
Destructive,
/// Leaves without an outcome.
Cancel,
/// Another answer that closes the question: No, Don't Save, Discard. It stands with the other outcomes on macOS
/// and Linux and next to the accepting button on Windows.
Alternate,
/// Any other outcome.
Other,
}
impl DialogRole {
/// The position in the row: later roles stand nearer the end. Windows puts the accepting button first and the
/// others after it; macOS and Linux put it last.
/// answers that close the question after it, before Cancel; macOS and Linux put the accepting button last.
fn rank(self, windows: bool) -> u8 {
let rank = match self {
DialogRole::Other => 0,
DialogRole::Destructive => 1,
DialogRole::Cancel => 2,
DialogRole::Accept => 3,
};
if windows { 3 - rank } else { rank }
match (windows, self) {
(false, DialogRole::Other | DialogRole::Alternate) => 0,
(false, DialogRole::Destructive) => 1,
(false, DialogRole::Cancel) => 2,
(false, DialogRole::Accept) => 3,
(true, DialogRole::Accept) => 0,
(true, DialogRole::Alternate) => 1,
(true, DialogRole::Cancel) => 2,
(true, DialogRole::Destructive) => 3,
(true, DialogRole::Other) => 4,
}
}
}
@@ -515,14 +522,15 @@ enum Content {
}
/// 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.
/// `Accept` on top, `Alternate` and `Cancel` under it on every platform, then the others.
pub(crate) fn display_order(roles: &[DialogRole], windows: bool, column: bool) -> Vec<usize> {
let mut order: Vec<usize> = (0..roles.len()).collect();
if column {
order.sort_by_key(|&at| match roles[at] {
DialogRole::Accept => 0,
DialogRole::Cancel => 1,
DialogRole::Destructive | DialogRole::Other => 2,
DialogRole::Alternate => 1,
DialogRole::Cancel => 2,
DialogRole::Destructive | DialogRole::Other => 3,
});
} else {
order.sort_by_key(|&at| roles[at].rank(windows));
@@ -633,7 +641,7 @@ impl FooterBar {
let variant = match button.role {
DialogRole::Accept => Variant::PRIMARY,
DialogRole::Destructive => Variant::DANGER,
DialogRole::Cancel | DialogRole::Other => Variant::SECONDARY,
DialogRole::Cancel | DialogRole::Alternate | DialogRole::Other => Variant::SECONDARY,
};
let mut widget = Button::new(button.label.clone()).variant(variant).enabled(button.enabled).least_width(least);
if column {
+50
View File
@@ -365,6 +365,56 @@ fn cmd_d_answers_dont_save_on_macos() {
}
}
/// The unsaved-changes buttons run Yes, No, Cancel on Windows and Don't Save, Cancel, Save on macOS; on every OS Escape
/// answers Cancel, and the Yes and No letters answer Save and Don't Save.
#[trial]
fn the_unsaved_question_orders_its_buttons_by_platform_and_answers_by_role() {
let order = |os: Os, names: [&str; 3]| {
let host = Rc::new(RefCell::new(DialogHost::new()));
let shown = Rc::clone(&host);
let mut fixture = Fixture::new(move |ui| shown.borrow_mut().run(ui));
fixture.os(os);
host.borrow_mut().open(UnsavedChangesDialog::new(CloseScope::Close, "poster.psd"));
fixture.idle(6);
let lefts = names.map(|name| fixture.by_label(name).unwrap_or_else(|| panic!("{os:?}: {name} shows")).rect.left());
assert!(lefts.windows(2).all(|pair| pair[0] < pair[1]), "{os:?}: {names:?} run left to right, at {lefts:?}");
};
order(Os::Windows, ["Yes", "No", "Cancel"]);
order(Os::Mac, ["Don't Save", "Cancel", "Save"]);
for (key, expected) in [(egui::Key::Escape, CloseAnswer::Cancel), (egui::Key::Y, CloseAnswer::Save), (egui::Key::N, CloseAnswer::DontSave)] {
let host = Rc::new(RefCell::new(DialogHost::new()));
let shown = Rc::clone(&host);
let mut fixture = Fixture::new(move |ui| shown.borrow_mut().run(ui));
fixture.os(Os::Windows);
host.borrow_mut().open(UnsavedChangesDialog::new(CloseScope::Close, "poster.psd"));
fixture.idle(6);
fixture.key(key, Modifiers::NONE).idle(4);
let answered = host.borrow_mut().take::<UnsavedChangesDialog>().and_then(DialogResult::ok).map(|answer| answer.answer);
assert_eq!(answered, Some(expected), "{key:?} answers {expected:?}");
}
}
/// Apply, an auxiliary action, stands after Cancel on Windows; the Alternate role does not move it.
#[trial]
fn windows_puts_an_auxiliary_apply_after_cancel() {
let mut fixture = Fixture::new(|ui| {
let mut open = true;
Dialog::new("Options")
.opened(Bind::bound(&mut open))
.buttons([
FooterButton::new("OK", DialogRole::Accept),
FooterButton::new("Apply", DialogRole::Other).stays_open(true),
FooterButton::new("Cancel", DialogRole::Cancel),
])
.show(ui, |_, _| {});
});
fixture.os(Os::Windows);
fixture.idle(30);
let left = |name: &str| fixture.by_label(name).expect("the button shows").rect.left();
assert!(left("OK") < left("Cancel") && left("Cancel") < left("Apply"), "OK, Cancel, Apply from the start edge");
}
/// Each open of a dialog has its own serial, kept while it stays open, though its id is the same at every open: a
/// reopened dialog is told apart from the one cancelled before it.
#[trial]