diff --git a/crates/ui-widgets/src/notices/notice.rs b/crates/ui-widgets/src/notices/notice.rs index 84b50dc53..4e5eb705f 100644 --- a/crates/ui-widgets/src/notices/notice.rs +++ b/crates/ui-widgets/src/notices/notice.rs @@ -5,8 +5,7 @@ use super::words::NoticeWords; use crate::buttons::{Button, IconButton, Link}; use crate::contract::{ - Access, Accessible as _, Common, Component, Cx, MeasureCx, Name, NoValue, Outcome, PaintUi, PartsCx, Politeness, Props as _, Remember as _, Role, - Styled as _, WordSetCx as _, + Access, Common, Component, Cx, MeasureCx, Name, NoValue, Outcome, PaintUi, PartsCx, Politeness, Props as _, Role, Styled as _, WordSetCx as _, }; use crate::primitives::{HAlign, IconSpec, TextRun, Wrap}; use crate::runtime::commands::CommandRef; @@ -104,10 +103,6 @@ const MESSAGE: PartKey = PartKey::new("message"); const TITLE_LEVEL: usize = 1; const DISMISS_ICON: &str = "close"; -/// Whether the first show of a notice has been announced. -#[derive(Clone, Default)] -struct Announced(bool); - /// The style of a notice. #[derive(Clone, Debug, PartialEq, ComponentStyle)] #[non_exhaustive] @@ -141,8 +136,8 @@ const READING_IN_BODY_SIZES: f32 = 38.5; /// A message in a tone. The tone picks the icon and the colours; the neutral tone reads muted and has no icon. The /// message is the label; a title above it is optional. Actions read as links (as buttons in the centred layout) and -/// are reported in the output; with `dismiss` a close button is the last part. With `announce` set, the first show is -/// announced once. The title and message wrap and can be selected and copied; Tab passes over them. +/// are reported in the output; with `dismiss` a close button is the last part. With `announce` set, the notice is a +/// live region: its title and message are announced when it appears and whenever they change. The title and message wrap and can be selected and copied; Tab passes over them. #[derive(crate::contract::Component, crate::contract::Props)] pub struct Notice<'a> { #[common] @@ -167,7 +162,8 @@ pub struct Notice<'a> { /// arrow keys are left to the surrounding scope. #[prop(flag)] footer_actions: bool, - /// Announces the notice once when it first shows. + /// Makes the notice a live region of this politeness: its title and message are announced when it appears and + /// whenever they change. #[prop] announce: Option, actions: Vec, @@ -501,20 +497,6 @@ impl Component for Notice<'_> { if cx.part_activated(DISMISS).is_some() || (self.dismiss && cx.core.intents.take(Intent::Cancel)) { self.dismissed = true; } - if let Some(politeness) = self.announce { - let first = { - let announced = cx.memory::(); - !std::mem::replace(&mut announced.0, true) - }; - if first { - let message = cx.resolved.label; - let text = match self.title.as_ref().map(|title| cx.env.resolve(title)) { - Some(title) => format!("{}, {message}", title.as_str()), - None => message.to_owned(), - }; - cx.announce(text, politeness); - } - } } fn access(&self, access: &mut Access, cx: &Cx<'_, Self>) { @@ -522,10 +504,12 @@ impl Component for Notice<'_> { (None, _) => { access.role(Role::LABEL); } - (Some(_), Tone::Bad) => { - access.role(Role::ALERT); + (Some(politeness), Tone::Bad) => { + access.role(Role::ALERT).live(politeness); + } + (Some(politeness), _) => { + access.live(politeness); } - (Some(_), _) => {} } if let Some(title) = self.title.as_ref().map(|title| cx.env.resolve(title).as_str().to_owned()) { if self.layout == NoticeLayout::Centred { diff --git a/crates/ui-widgets/tests/it/main.rs b/crates/ui-widgets/tests/it/main.rs index 92ee6fb48..9453fc2ca 100644 --- a/crates/ui-widgets/tests/it/main.rs +++ b/crates/ui-widgets/tests/it/main.rs @@ -17,6 +17,7 @@ mod layout; mod link; mod list; mod menu; +mod notices; mod overlay; mod pickers; diff --git a/crates/ui-widgets/tests/it/notices.rs b/crates/ui-widgets/tests/it/notices.rs new file mode 100644 index 000000000..c3bab1dbf --- /dev/null +++ b/crates/ui-widgets/tests/it/notices.rs @@ -0,0 +1,28 @@ +//! Notices: the live region of an announced notice. + +use craft_theme::variant::Tone; +use craft_ui::contract::{Politeness, Props as _, Show as _}; +use craft_ui::notices::Notice; +use craft_ui::testing::{Fixture, trial}; +use egui::Id; +use std::cell::Cell; +use std::rc::Rc; + +/// An announced notice is a live region whose name follows its message, so a message that changes while it shows is +/// announced again. +#[trial] +fn an_announced_notice_is_a_live_region_that_follows_its_message() { + let message = Rc::new(Cell::new("No matching commands")); + let id = Rc::new(Cell::new(Id::NULL)); + let (shown, seen) = (message.clone(), id.clone()); + let mut fixture = Fixture::new(move |ui| { + seen.set(Notice::new(Tone::Neutral, shown.get()).announce(Politeness::Polite).id_salt("status").show(ui).response.id); + }); + fixture.settle(); + assert_eq!(fixture.tree().live(id.get()), Some(Politeness::Polite), "the notice is a polite live region"); + assert!(fixture.by_label("No matching commands").is_some()); + message.set("No matching layers"); + fixture.settle(); + assert_eq!(fixture.tree().live(id.get()), Some(Politeness::Polite)); + assert!(fixture.by_label("No matching layers").is_some(), "the region's name is the new message"); +}