diff --git a/crates/ui-panels/src/dialog/prompt.rs b/crates/ui-panels/src/dialog/prompt.rs index 42a3d23a0..bf885a90d 100644 --- a/crates/ui-panels/src/dialog/prompt.rs +++ b/crates/ui-panels/src/dialog/prompt.rs @@ -191,7 +191,7 @@ pub struct Prompt<'a> { /// The role whose answer Escape and the close action give, instead of the default choice. #[prop] escape: Option, - /// Whether the dialog reports the alert-dialog role, described by the message. + /// Whether the dialog reports the alert-dialog role, described by the message and any tone. alert: bool, } @@ -209,7 +209,8 @@ impl<'a> Prompt<'a> { } } - /// The prompt is an alert: its node has the alert-dialog role and the message as its description. + /// The prompt is an alert: its node has the alert-dialog role and the message (including any tone) as its + /// description. #[must_use] pub(super) fn alert(mut self) -> Self { self.alert = true; @@ -267,7 +268,27 @@ impl<'a> Prompt<'a> { let initial = if enter_at.is_none() && has_default(&answers) { Initial::Keep } else { Initial::Default }; let mut dialog = Dialog::prompt(title).buttons(answers).initial_focus(initial).close_on_enter(enter_at.is_some()); if alert { - dialog = dialog.alert().description(Env::of(ui).view().resolve(&message).as_str()); + let env = Env::of(ui); + let view = env.view(); + let message_str = view.resolve(&message); + let description = match &extras.tone { + Some((_, tone_label)) => { + let tone_str = view.resolve(tone_label); + let tone = tone_str.as_str().trim(); + let msg = message_str.as_str().trim(); + if tone.is_empty() { + msg.to_owned() + } else if msg.is_empty() { + tone.to_owned() + } else if msg.ends_with(['.', '!', '?']) { + format!("{msg} {tone}") + } else { + format!("{msg}. {tone}") + } + } + None => message_str.as_str().to_owned(), + }; + dialog = dialog.alert().description(description); } if let Some(open) = open { dialog = dialog.opened(open); diff --git a/crates/ui-panels/src/dialog/unsaved.rs b/crates/ui-panels/src/dialog/unsaved.rs index e69c9a488..5379f02f5 100644 --- a/crates/ui-panels/src/dialog/unsaved.rs +++ b/crates/ui-panels/src/dialog/unsaved.rs @@ -54,6 +54,9 @@ impl Outcome for UnsavedChangesOutput { /// guard has more documents, an "Apply to All" check box. Windows and Linux ask Yes, No and Cancel. Reverting asks /// Revert and Cancel; Revert answers `CloseAnswer::DontSave`. /// +/// The node has the alert-dialog role (`Role::ALERT_DIALOG`), described by the consequence message and any save +/// failure reason. +/// /// The result is `Ok(UnsavedAnswer)` for every answer, Cancel included (the guard needs it), and /// `DialogResult::Cancel` only when the host closed the dialog from outside. #[derive(crate::contract::Pattern)] @@ -164,7 +167,7 @@ impl UnsavedChangesDialog { let replies = self.replies(&words, common.cancel.clone(), env.view().shell().os); let answers: Vec = replies.iter().map(|(answer, _)| *answer).collect(); let replies: Vec = replies.into_iter().map(|(_, reply)| reply).collect(); - let mut prompt = Prompt::new(words.unsaved_title.clone(), message).answers(buttons(&env, replies)); + let mut prompt = Prompt::new(words.unsaved_title.clone(), message).alert().answers(buttons(&env, replies)); if let Some(reason) = &self.reason { prompt = prompt.tone(Tone::Bad, reason.clone()); } diff --git a/crates/ui-panels/tests/it/dialogs.rs b/crates/ui-panels/tests/it/dialogs.rs index ef4a79649..ff7ded22e 100644 --- a/crates/ui-panels/tests/it/dialogs.rs +++ b/crates/ui-panels/tests/it/dialogs.rs @@ -395,6 +395,43 @@ fn the_unsaved_question_orders_its_buttons_by_platform_and_answers_by_role() { } } +/// The unsaved-changes dialog exposes Role::ALERT_DIALOG, with its title as its accessible name and its consequence +/// message and save-failure reason in its AccessKit description. +#[trial] +fn the_unsaved_dialog_exposes_alert_dialog_role_and_folds_reason_into_description() { + 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)); + + // 1. Without reason: Role::ALERT_DIALOG, label is "Unsaved changes", description contains the closing question. + host.borrow_mut().open(UnsavedChangesDialog::new(CloseScope::Close, "poster.psd")); + fixture.idle(6); + let dialogs = fixture.by_role(Role::ALERT_DIALOG); + assert_eq!(dialogs.len(), 1, "the unsaved changes dialog exposes Role::ALERT_DIALOG"); + let dialog = dialogs[0]; + assert_eq!(fixture.tree().name_of(dialog.node), Some("Unsaved changes"), "accessible name is the dialog title"); + let desc_without_reason = fixture.tree().description_of(dialog.node).expect("dialog has an accessible description").to_owned(); + assert!(desc_without_reason.contains("poster.psd"), "description mentions the document name: {desc_without_reason}"); + assert!(desc_without_reason.contains("before closing"), "description conveys closing consequence: {desc_without_reason}"); + + // Cancel to close + assert!(host.borrow_mut().cancel::(fixture.ctx())); + fixture.idle(4); + + // 2. With reason: Role::ALERT_DIALOG, description contains both the closing question and the save-failure reason. + let reason_text = "Disk full or permission denied"; + host.borrow_mut().open(UnsavedChangesDialog::new(CloseScope::Close, "poster.psd").reason(reason_text)); + fixture.idle(6); + let dialogs = fixture.by_role(Role::ALERT_DIALOG); + assert_eq!(dialogs.len(), 1, "reopened unsaved dialog with reason exposes Role::ALERT_DIALOG"); + let dialog = dialogs[0]; + assert_eq!(fixture.tree().name_of(dialog.node), Some("Unsaved changes")); + let desc_with_reason = fixture.tree().description_of(dialog.node).expect("dialog has an accessible description"); + assert!(desc_with_reason.contains("poster.psd"), "description mentions document name: {desc_with_reason}"); + assert!(desc_with_reason.contains(reason_text), "description includes save failure reason: {desc_with_reason}"); + assert_eq!(desc_with_reason, format!("{desc_without_reason} {reason_text}")); +} + /// 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() {