Skip to main content

iced_cube/overlay/
toast.rs

1//! Transient notifications stacked in a corner of the window.
2//!
3//! Toasts usually come from outside the UI thread, so they arrive through a
4//! channel. [`subscription`] creates a bounded channel, emits
5//! [`Event::Ready`] with its [`Sender`] once, then delivers whatever
6//! producers send in batches as [`Event::Received`]. The app keeps the
7//! sender, hands clones to producers and feeds every [`Event`] into its
8//! [`State`]. [`timer`] ticks only while a visible toast is counting down.
9//!
10//! ```no_run
11//! use iced::widget::text;
12//! use iced::{Element, Subscription};
13//! use iced_cube::overlay::toast::{self, toasts};
14//!
15//! #[derive(Debug, Clone)]
16//! enum Message {
17//!     Toast(toast::Event),
18//! }
19//!
20//! #[derive(Default)]
21//! struct App {
22//!     toasts: toast::State,
23//!     sender: Option<toast::Sender>,
24//! }
25//!
26//! impl App {
27//!     fn update(&mut self, message: Message) {
28//!         let Message::Toast(event) = message;
29//!         if let Some(toast::Output::Ready(sender)) = self.toasts.update(event) {
30//!             self.sender = Some(sender);
31//!         }
32//!     }
33//!
34//!     fn view(&self) -> Element<'_, Message> {
35//!         toasts(&self.toasts, text("Content"))
36//!             .on_event(Message::Toast)
37//!             .into()
38//!     }
39//!
40//!     fn subscription(&self) -> Subscription<Message> {
41//!         Subscription::batch([toast::subscription(), toast::timer(&self.toasts)])
42//!             .map(Message::Toast)
43//!     }
44//! }
45//! ```
46
47use std::collections::VecDeque;
48
49use iced::futures::channel::mpsc;
50use iced::futures::{Stream, StreamExt, stream};
51use iced::keyboard::key::Named;
52use iced::time::{Duration, Instant};
53use iced::widget::{self, column, container, mouse_area, row, stack, text};
54use iced::{
55    Alignment, Background, Border, Color, Element, Length, Padding, Shadow, Subscription, Theme,
56    Vector,
57};
58
59use crate::after_touch::after_touch;
60use crate::hit_slop::hit_slop;
61use crate::icon::{Glyph, themed};
62use crate::keys::{self, Chord, Keymap};
63use crate::primitives::button::{self, Size};
64use crate::primitives::icon_button::icon_button;
65use crate::solid::solid;
66use crate::theme::{Tokens, fade, mix, radius, space, text_size};
67
68/// How many toasts a producer can send before it has to wait.
69pub const CHANNEL_CAPACITY: usize = 32;
70/// The most toasts delivered in one [`Event::Received`].
71pub const BATCH_SIZE: usize = 16;
72/// How long a toast stays visible unless it sets its own duration.
73pub const DEFAULT_DURATION: Duration = Duration::from_secs(5);
74/// The shortest time a toast with an action stays visible, so there is
75/// time to read it and press the button.
76pub const ACTION_DURATION: Duration = Duration::from_secs(10);
77/// How many toasts are shown at once. The rest wait their turn.
78pub const DEFAULT_LIMIT: usize = 3;
79/// How often [`timer`] checks for expired toasts.
80pub const TICK: Duration = Duration::from_millis(250);
81
82/// The widest toasts get by default. A narrower window fits them to its
83/// width.
84pub const MAX_WIDTH: f32 = 356.0;
85/// The default space between the toasts and the window edges.
86pub const INSET: f32 = space::LG;
87
88/// The sending half of the toast channel. Clone it for each producer.
89pub type Sender = mpsc::Sender<Toast>;
90
91/// Colour and icon of a toast.
92#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
93pub enum Variant {
94    #[default]
95    Default,
96    Success,
97    Destructive,
98}
99
100impl Variant {
101    pub const ALL: [Variant; 3] = [Variant::Default, Variant::Success, Variant::Destructive];
102
103    fn glyph(self) -> Option<Glyph> {
104        match self {
105            Variant::Default => None,
106            Variant::Success => Some(crate::lucide!(CircleCheck)),
107            Variant::Destructive => Some(crate::lucide!(CircleX)),
108        }
109    }
110}
111
112/// The corner or edge the toasts stack in.
113#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
114pub enum Position {
115    TopLeft,
116    TopCenter,
117    TopRight,
118    BottomLeft,
119    BottomCenter,
120    #[default]
121    BottomRight,
122}
123
124impl Position {
125    pub const ALL: [Position; 6] = [
126        Position::TopLeft,
127        Position::TopCenter,
128        Position::TopRight,
129        Position::BottomLeft,
130        Position::BottomCenter,
131        Position::BottomRight,
132    ];
133
134    fn alignment(self) -> (Alignment, Alignment) {
135        match self {
136            Position::TopLeft => (Alignment::Start, Alignment::Start),
137            Position::TopCenter => (Alignment::Center, Alignment::Start),
138            Position::TopRight => (Alignment::End, Alignment::Start),
139            Position::BottomLeft => (Alignment::Start, Alignment::End),
140            Position::BottomCenter => (Alignment::Center, Alignment::End),
141            Position::BottomRight => (Alignment::End, Alignment::End),
142        }
143    }
144}
145
146/// One notification.
147#[derive(Debug, Clone, PartialEq)]
148pub struct Toast {
149    pub title: String,
150    pub description: Option<String>,
151    pub variant: Variant,
152    /// Label of the action button.
153    pub action: Option<String>,
154    /// How long the toast stays once shown. `None` keeps it until closed.
155    pub duration: Option<Duration>,
156}
157
158/// Creates a toast with a title that closes after [`DEFAULT_DURATION`].
159pub fn toast(title: impl Into<String>) -> Toast {
160    Toast {
161        title: title.into(),
162        description: None,
163        variant: Variant::default(),
164        action: None,
165        duration: Some(DEFAULT_DURATION),
166    }
167}
168
169impl Toast {
170    pub fn description(mut self, description: impl Into<String>) -> Self {
171        self.description = Some(description.into());
172        self
173    }
174
175    pub fn variant(mut self, variant: Variant) -> Self {
176        self.variant = variant;
177        self
178    }
179
180    /// Adds an action button. Pressing it closes the toast and returns
181    /// [`Output::Action`]. A toast with an action stays at least
182    /// [`ACTION_DURATION`] and shows ahead of toasts waiting their turn.
183    pub fn action(mut self, label: impl Into<String>) -> Self {
184        self.action = Some(label.into());
185        self
186    }
187
188    /// How long the toast stays visible once it is shown.
189    pub fn duration(mut self, duration: Duration) -> Self {
190        self.duration = Some(duration);
191        self
192    }
193
194    /// Keeps the toast until it is closed.
195    pub fn persistent(mut self) -> Self {
196        self.duration = None;
197        self
198    }
199
200    /// How long the toast stays once shown: its duration, but at least
201    /// [`ACTION_DURATION`] when it has an action. `None` keeps it until
202    /// it is closed.
203    pub fn lifetime(&self) -> Option<Duration> {
204        let duration = self.duration?;
205        if self.action.is_some() {
206            return Some(duration.max(ACTION_DURATION));
207        }
208        Some(duration)
209    }
210}
211
212/// Identifies a toast in a [`State`].
213#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
214pub struct Id(u64);
215
216/// Everything that changes the toast queue.
217#[derive(Debug, Clone)]
218pub enum Event {
219    /// The channel is open. Keep the sender and hand clones to producers.
220    Ready(Sender),
221    /// A batch of toasts from the channel, oldest first.
222    Received(Vec<Toast>),
223    Dismiss(Id),
224    Action(Id),
225    Tick(Instant),
226    /// The pointer is over the toasts. Their countdowns stop until
227    /// [`Event::Resume`].
228    Pause,
229    /// The pointer left the toasts. Their countdowns carry on.
230    Resume,
231}
232
233/// What the app may need to act on after an [`Event`]. `A` is the payload
234/// type of the [`State`].
235#[derive(Debug, Clone)]
236pub enum Output<A = ()> {
237    Ready(Sender),
238    /// The action button of this toast was pressed.
239    Action(Id, Toast),
240    /// The action button of a toast queued with [`State::push_with`] was
241    /// pressed. Handle the payload as the app's own message.
242    Payload(A),
243}
244
245#[derive(Debug, Clone)]
246struct Entry<A> {
247    id: Id,
248    toast: Toast,
249    payload: Option<A>,
250    expires_at: Option<Instant>,
251}
252
253/// The toast queue. The first [`limit`](State::limit) toasts are visible and
254/// counting down; the rest wait until a visible one closes.
255///
256/// A toast with an action goes ahead of every toast without one that is
257/// waiting, and when the visible toasts are full it takes the place of the
258/// newest visible toast without an action, which waits again and restarts
259/// its countdown when it comes back. While the pointer is over the toasts
260/// ([`Event::Pause`]) no countdown runs.
261///
262/// `A` is what action buttons hand back, usually the app's message, so an
263/// Undo toast can carry the message that undoes. A queue without payloads
264/// is `State<()>`, made with [`State::new`]; one with payloads is made with
265/// [`State::default`].
266#[derive(Debug, Clone)]
267pub struct State<A = ()> {
268    entries: VecDeque<Entry<A>>,
269    next_id: u64,
270    limit: usize,
271    paused: bool,
272    last_tick: Option<Instant>,
273}
274
275impl<A> Default for State<A> {
276    fn default() -> Self {
277        Self {
278            entries: VecDeque::new(),
279            next_id: 0,
280            limit: DEFAULT_LIMIT,
281            paused: false,
282            last_tick: None,
283        }
284    }
285}
286
287impl State {
288    pub fn new() -> Self {
289        Self::default()
290    }
291}
292
293impl<A> State<A> {
294    /// Sets how many toasts are visible at once. At least one is.
295    pub fn with_limit(mut self, limit: usize) -> Self {
296        self.limit = limit.max(1);
297        self
298    }
299
300    pub fn limit(&self) -> usize {
301        self.limit
302    }
303
304    /// Queues a toast and returns its id.
305    pub fn push(&mut self, toast: Toast) -> Id {
306        self.enqueue(toast, None)
307    }
308
309    /// Queues a toast whose action button hands back `payload` as
310    /// [`Output::Payload`], and returns its id.
311    pub fn push_with(&mut self, toast: Toast, payload: A) -> Id {
312        self.enqueue(toast, Some(payload))
313    }
314
315    fn enqueue(&mut self, toast: Toast, payload: Option<A>) -> Id {
316        let id = Id(self.next_id);
317        self.next_id += 1;
318        let entry = Entry {
319            id,
320            toast,
321            payload,
322            expires_at: None,
323        };
324        if entry.toast.action.is_some() {
325            self.insert_ahead(entry);
326        } else {
327            self.entries.push_back(entry);
328        }
329        id
330    }
331
332    /// Puts a toast with an action in view, or at the front of the waiting
333    /// toasts when every visible toast has an action too.
334    fn insert_ahead(&mut self, entry: Entry<A>) {
335        let limit = self.limit;
336        if self.entries.len() < limit {
337            self.entries.push_back(entry);
338            return;
339        }
340        let newest_plain = (0..limit)
341            .rev()
342            .find(|&index| self.entries[index].toast.action.is_none());
343        let Some(index) = newest_plain else {
344            let after_actions = self
345                .entries
346                .iter()
347                .rposition(|entry| entry.toast.action.is_some())
348                .map_or(0, |index| index + 1);
349            self.entries.insert(after_actions, entry);
350            return;
351        };
352        let Some(mut displaced) = self.entries.remove(index) else {
353            return;
354        };
355        displaced.expires_at = None;
356        self.entries.insert(limit - 1, entry);
357        self.entries.insert(limit, displaced);
358    }
359
360    /// Removes a toast, visible or waiting, dropping any payload.
361    pub fn dismiss(&mut self, id: Id) -> Option<Toast> {
362        self.remove(id).map(|entry| entry.toast)
363    }
364
365    fn remove(&mut self, id: Id) -> Option<Entry<A>> {
366        let index = self.entries.iter().position(|entry| entry.id == id)?;
367        let entry = self.entries.remove(index);
368        self.settle();
369        entry
370    }
371
372    /// With no toasts left under the pointer, nothing stays paused.
373    fn settle(&mut self) {
374        if self.entries.is_empty() {
375            self.paused = false;
376        }
377    }
378
379    /// Closes expired toasts and starts the countdown of newly visible ones.
380    /// While paused, the time since the last tick is added back to every
381    /// countdown instead. Returns the ids that closed.
382    pub fn tick(&mut self, now: Instant) -> Vec<Id> {
383        let last = self.last_tick.replace(now);
384        if self.paused
385            && let Some(last) = last
386        {
387            let paused_for = now.saturating_duration_since(last);
388            for entry in &mut self.entries {
389                entry.expires_at = entry.expires_at.map(|at| at + paused_for);
390            }
391        }
392
393        let mut expired = Vec::new();
394        self.entries.retain(|entry| {
395            let alive = entry.expires_at.is_none_or(|at| at > now);
396            if !alive {
397                expired.push(entry.id);
398            }
399            alive
400        });
401        self.settle();
402
403        let limit = self.limit;
404        for entry in self.entries.iter_mut().take(limit) {
405            if entry.expires_at.is_none() {
406                entry.expires_at = entry.toast.lifetime().map(|duration| now + duration);
407            }
408        }
409        expired
410    }
411
412    /// Whether the countdowns are stopped because the pointer is over the
413    /// toasts.
414    pub fn is_paused(&self) -> bool {
415        self.paused
416    }
417
418    pub fn update(&mut self, event: Event) -> Option<Output<A>> {
419        match event {
420            Event::Ready(sender) => Some(Output::Ready(sender)),
421            Event::Received(batch) => {
422                for toast in batch {
423                    let _ = self.push(toast);
424                }
425                None
426            }
427            Event::Dismiss(id) => {
428                let _ = self.dismiss(id);
429                None
430            }
431            Event::Action(id) => {
432                let entry = self.remove(id)?;
433                Some(match entry.payload {
434                    Some(payload) => Output::Payload(payload),
435                    None => Output::Action(id, entry.toast),
436                })
437            }
438            Event::Tick(now) => {
439                let _ = self.tick(now);
440                None
441            }
442            Event::Pause => {
443                self.paused = true;
444                None
445            }
446            Event::Resume => {
447                self.paused = false;
448                None
449            }
450        }
451    }
452
453    /// The visible toasts, top to bottom in queue order: oldest first, with
454    /// toasts that have an action moved ahead of waiting ones.
455    pub fn visible(&self) -> impl Iterator<Item = (Id, &Toast)> {
456        self.entries
457            .iter()
458            .take(self.limit)
459            .map(|entry| (entry.id, &entry.toast))
460    }
461
462    /// How many toasts are waiting behind the visible ones.
463    pub fn waiting(&self) -> usize {
464        self.entries.len().saturating_sub(self.limit)
465    }
466
467    pub fn len(&self) -> usize {
468        self.entries.len()
469    }
470
471    pub fn is_empty(&self) -> bool {
472        self.entries.is_empty()
473    }
474
475    /// Whether a visible toast still has to count down.
476    pub fn needs_tick(&self) -> bool {
477        self.visible().any(|(_, toast)| toast.lifetime().is_some())
478    }
479
480    /// Every queued toast id, visible first, in queue order.
481    pub fn ids(&self) -> impl Iterator<Item = Id> {
482        self.entries.iter().map(|entry| entry.id)
483    }
484}
485
486/// What a toast keyboard shortcut does.
487#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
488pub enum Action {
489    DismissLatest,
490    DismissAll,
491    ActivateLatest,
492}
493
494impl Action {
495    /// The events this action sends to `state`. Empty when there is
496    /// nothing to act on.
497    pub fn events<A>(self, state: &State<A>) -> Vec<Event> {
498        match self {
499            Action::DismissLatest => state
500                .visible()
501                .map(|(id, _)| id)
502                .max()
503                .map(Event::Dismiss)
504                .into_iter()
505                .collect(),
506            Action::DismissAll => state.ids().map(Event::Dismiss).collect(),
507            Action::ActivateLatest => state
508                .visible()
509                .filter(|(_, toast)| toast.action.is_some())
510                .map(|(id, _)| id)
511                .max()
512                .map(Event::Action)
513                .into_iter()
514                .collect(),
515        }
516    }
517}
518
519impl keys::Action for Action {
520    const ALL: &'static [Self] = &[
521        Action::DismissLatest,
522        Action::DismissAll,
523        Action::ActivateLatest,
524    ];
525
526    fn defaults() -> Keymap<Self> {
527        default_keymap()
528    }
529
530    fn name(self) -> &'static str {
531        match self {
532            Action::DismissLatest => "DismissLatest",
533            Action::DismissAll => "DismissAll",
534            Action::ActivateLatest => "ActivateLatest",
535        }
536    }
537
538    fn description(self) -> &'static str {
539        match self {
540            Action::DismissLatest => "Closes the newest visible toast.",
541            Action::DismissAll => "Closes every toast, including those waiting their turn.",
542            Action::ActivateLatest => {
543                "Presses the action button, such as Undo, of the newest visible toast that has one."
544            }
545        }
546    }
547}
548
549/// The default toast shortcuts:
550///
551/// | Keys | Action |
552/// | --- | --- |
553/// | `Escape` | [`Action::DismissLatest`] |
554/// | `Shift+Escape` | [`Action::DismissAll`] |
555/// | `Alt+Z` | [`Action::ActivateLatest`] |
556///
557/// Escape often closes dialogs and menus too, so resolve the toast keymap
558/// after any overlay that should take Escape first.
559pub fn default_keymap() -> Keymap<Action> {
560    Keymap::new()
561        .bind(Chord::named(Named::Escape), Action::DismissLatest)
562        .bind(Chord::named(Named::Escape).shift(), Action::DismissAll)
563        .bind(Chord::character('z').alt(), Action::ActivateLatest)
564}
565
566/// Owns the toast channel. Emits [`Event::Ready`] first, then batches.
567pub fn subscription() -> Subscription<Event> {
568    Subscription::run(stream)
569}
570
571/// The stream behind [`subscription`], for driving it without a runtime.
572pub fn stream() -> impl Stream<Item = Event> {
573    let (sender, receiver) = mpsc::channel(CHANNEL_CAPACITY);
574    stream::once(async move { Event::Ready(sender) }).chain(batches(receiver).map(Event::Received))
575}
576
577/// Groups whatever is already waiting in the channel into one batch.
578pub fn batches<T>(receiver: impl Stream<Item = T>) -> impl Stream<Item = Vec<T>> {
579    receiver.ready_chunks(BATCH_SIZE)
580}
581
582/// Ticks every [`TICK`] while a visible toast is counting down.
583pub fn timer<A>(state: &State<A>) -> Subscription<Event> {
584    if !state.needs_tick() {
585        return Subscription::none();
586    }
587    crate::ticker::every(TICK).map(Event::Tick)
588}
589
590/// The widget id of a toast's close button, for tests and focus.
591pub fn close_button_id(id: Id) -> widget::Id {
592    widget::Id::from(format!("iced-cube-toast-close-{}", id.0))
593}
594
595/// Renders the visible toasts of a [`State`] over some content.
596pub struct Toasts<'a, Message, A = ()> {
597    state: &'a State<A>,
598    content: Element<'a, Message>,
599    position: Position,
600    inset: Padding,
601    max_width: f32,
602    on_event: Option<Box<dyn Fn(Event) -> Message + 'a>>,
603}
604
605/// Stacks the toasts of `state` over `content`. Without
606/// [`on_event`](Toasts::on_event) the buttons render disabled.
607pub fn toasts<'a, Message, A>(
608    state: &'a State<A>,
609    content: impl Into<Element<'a, Message>>,
610) -> Toasts<'a, Message, A> {
611    Toasts {
612        state,
613        content: content.into(),
614        position: Position::default(),
615        inset: Padding::from(INSET),
616        max_width: MAX_WIDTH,
617        on_event: None,
618    }
619}
620
621impl<Message, A: std::fmt::Debug> std::fmt::Debug for Toasts<'_, Message, A> {
622    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
623        f.debug_struct("Toasts")
624            .field("state", self.state)
625            .field("position", &self.position)
626            .field("inset", &self.inset)
627            .field("max_width", &self.max_width)
628            .finish_non_exhaustive()
629    }
630}
631
632impl<'a, Message, A> Toasts<'a, Message, A> {
633    pub fn position(mut self, position: Position) -> Self {
634        self.position = position;
635        self
636    }
637
638    /// The space between the toasts and each window edge. Defaults to
639    /// [`INSET`] all round. Raise the bottom to keep toasts above a bottom
640    /// bar, such as `Padding::from(16).bottom(96)`.
641    pub fn inset(mut self, inset: impl Into<Padding>) -> Self {
642        self.inset = inset.into();
643        self
644    }
645
646    /// The widest the toasts get. They fill a narrower window up to its
647    /// inset, so on a phone a bottom-centre toast spans the screen like a
648    /// snackbar. Defaults to [`MAX_WIDTH`].
649    pub fn max_width(mut self, max_width: f32) -> Self {
650        self.max_width = max_width.max(0.0);
651        self
652    }
653
654    pub fn on_event(mut self, on_event: impl Fn(Event) -> Message + 'a) -> Self {
655        self.on_event = Some(Box::new(on_event));
656        self
657    }
658}
659
660impl<'a, Message: Clone + 'a, A> From<Toasts<'a, Message, A>> for Element<'a, Message> {
661    fn from(toasts: Toasts<'a, Message, A>) -> Self {
662        let Toasts {
663            state,
664            content,
665            position,
666            inset,
667            max_width,
668            on_event,
669        } = toasts;
670        let emit = |event: Event| on_event.as_ref().map(|on_event| on_event(event));
671
672        let cards = state.visible().map(|(id, toast)| card(id, toast, &emit));
673
674        let cards = column(cards)
675            .spacing(space::SM)
676            .width(Length::Fill)
677            .max_width(max_width);
678        // Hovering the toasts pauses their countdowns. A finger pauses them
679        // only while it is down.
680        let cards: Element<'a, Message> = match (emit(Event::Pause), emit(Event::Resume)) {
681            (Some(pause), Some(resume)) => {
682                after_touch(mouse_area(cards).on_enter(pause).on_exit(resume))
683            }
684            _ => cards.into(),
685        };
686
687        let (align_x, align_y) = position.alignment();
688        let layer = container(cards)
689            .padding(inset)
690            .width(Length::Fill)
691            .height(Length::Fill)
692            .align_x(align_x)
693            .align_y(align_y);
694
695        // The layer is always present so the content keeps its widget state
696        // when the first toast appears.
697        stack![content, layer]
698            .width(Length::Fill)
699            .height(Length::Fill)
700            .into()
701    }
702}
703
704fn card<'a, Message: Clone + 'a>(
705    id: Id,
706    toast: &'a Toast,
707    emit: &impl Fn(Event) -> Option<Message>,
708) -> Element<'a, Message> {
709    let variant = toast.variant;
710
711    let mut body = column![
712        text(&toast.title)
713            .size(text_size::SM)
714            .font(crate::theme::semibold())
715    ]
716    .spacing(2)
717    .width(Length::Fill);
718    if let Some(description) = &toast.description {
719        body = body.push(
720            text(description)
721                .size(text_size::SM)
722                .style(move |theme: &Theme| text::Style {
723                    color: Some(Tokens::of(theme).muted_foreground),
724                }),
725        );
726    }
727
728    let mut content = row![].spacing(space::MD).align_y(Alignment::Center);
729    if let Some(glyph) = variant.glyph() {
730        content = content.push(themed(glyph, 18.0, 1.0, move |theme| {
731            accent(&Tokens::of(theme), variant)
732        }));
733    }
734    content = content.push(body);
735    if let Some(label) = &toast.action {
736        content = content.push(hit_slop(
737            button::button(label.as_str())
738                .variant(button::Variant::Outline)
739                .size(Size::Sm)
740                .on_press_maybe(emit(Event::Action(id))),
741            HIT_SLOP,
742        ));
743    }
744    content = content.push(
745        container(hit_slop(
746            icon_button(crate::lucide!(X))
747                .size(Size::Sm)
748                .on_press_maybe(emit(Event::Dismiss(id))),
749            HIT_SLOP,
750        ))
751        .id(close_button_id(id)),
752    );
753
754    let card = solid(
755        container(content)
756            .padding([space::MD, space::LG])
757            .width(Length::Fill)
758            .style(move |theme| style(&Tokens::of(theme), variant)),
759    );
760    #[cfg(feature = "icedwright")]
761    let card = {
762        use icedwright_probe::{Semantic, role, semantic};
763
764        fn finish<'a, Message: 'a, R: role::RoleKind>(
765            annotated: Semantic<'a, Message, Theme, iced::Renderer, R>,
766            toast: &'a Toast,
767        ) -> Element<'a, Message> {
768            let annotated = annotated
769                .name(toast.title.as_str())
770                .component("iced_cube::toast");
771            match &toast.description {
772                Some(description) => annotated.description(description.as_str()).into(),
773                None => annotated.into(),
774            }
775        }
776
777        match variant {
778            Variant::Destructive => finish(semantic(card).role(role::Alert), toast),
779            Variant::Default | Variant::Success => finish(semantic(card).role(role::Status), toast),
780        }
781    };
782    card
783}
784
785/// How far past its edges a toast button takes presses: a 32 pixel button becomes 48 tall and
786/// 44 wide. Sideways it reaches half the gap between the buttons, so their margins never meet.
787const HIT_SLOP: Padding = Padding {
788    top: 8.0,
789    bottom: 8.0,
790    left: space::MD / 2.0,
791    right: space::MD / 2.0,
792};
793
794/// The colour of a variant's icon and tint.
795pub fn accent(tokens: &Tokens, variant: Variant) -> Color {
796    match variant {
797        Variant::Default => tokens.foreground,
798        Variant::Success => tokens.success,
799        Variant::Destructive => tokens.destructive,
800    }
801}
802
803/// The card style of a toast.
804pub fn style(tokens: &Tokens, variant: Variant) -> container::Style {
805    let (background, border) = match variant {
806        Variant::Default => (tokens.background, tokens.border),
807        Variant::Success | Variant::Destructive => {
808            let accent = accent(tokens, variant);
809            (
810                mix(tokens.background, accent, 0.08),
811                mix(tokens.border, accent, 0.45),
812            )
813        }
814    };
815    let shadow_alpha = if tokens.is_dark { 0.5 } else { 0.12 };
816
817    container::Style {
818        background: Some(Background::Color(background)),
819        text_color: Some(tokens.foreground),
820        border: Border {
821            color: border,
822            width: 1.0,
823            radius: radius::LG.into(),
824        },
825        shadow: Shadow {
826            color: fade(Color::BLACK, shadow_alpha),
827            offset: Vector::new(0.0, 4.0),
828            blur_radius: 12.0,
829        },
830        snap: true,
831    }
832}
833
834#[cfg(test)]
835mod tests {
836    use super::*;
837    use crate::theme::{dark, light};
838    use futures::SinkExt;
839    use futures::executor::block_on;
840
841    fn titles(state: &State) -> Vec<&str> {
842        state
843            .visible()
844            .map(|(_, toast)| toast.title.as_str())
845            .collect()
846    }
847
848    #[test]
849    fn builder_defaults() {
850        let t = toast("Saved");
851        assert_eq!(t.title, "Saved");
852        assert_eq!(t.variant, Variant::Default);
853        assert_eq!(t.duration, Some(DEFAULT_DURATION));
854        assert!(t.description.is_none());
855        assert!(t.action.is_none());
856        assert!(toast("Sticky").persistent().duration.is_none());
857    }
858
859    #[test]
860    fn layer_defaults_keep_the_corner_and_options_override_them() {
861        let state: State = State::new();
862        let layer: Toasts<'_, (), ()> = toasts(&state, text("Content"));
863        assert_eq!(layer.position, Position::BottomRight);
864        assert_eq!(layer.inset, Padding::from(INSET));
865        assert_eq!(layer.max_width, MAX_WIDTH);
866        let layer: Toasts<'_, (), ()> = toasts(&state, text("Content"))
867            .inset(Padding::from(16).bottom(96))
868            .max_width(-1.0);
869        assert_eq!(layer.inset.bottom, 96.0);
870        assert_eq!(layer.max_width, 0.0);
871    }
872
873    #[test]
874    fn queue_keeps_arrival_order_and_unique_ids() {
875        let mut state = State::new();
876        let a = state.push(toast("a"));
877        let b = state.push(toast("b"));
878        let _ = state.update(Event::Received(vec![toast("c"), toast("d")]));
879
880        assert_ne!(a, b);
881        assert_eq!(state.len(), 4);
882        assert_eq!(titles(&state), ["a", "b", "c"]);
883        assert_eq!(state.waiting(), 1);
884    }
885
886    #[test]
887    fn limit_caps_visible_toasts_and_is_at_least_one() {
888        let mut state = State::new().with_limit(2);
889        for title in ["a", "b", "c"] {
890            let _ = state.push(toast(title));
891        }
892        assert_eq!(titles(&state), ["a", "b"]);
893        assert_eq!(State::new().with_limit(0).limit(), 1);
894    }
895
896    #[test]
897    fn toasts_expire_using_injected_time() {
898        let start = Instant::now();
899        let mut state = State::new();
900        let id = state.push(toast("a").duration(Duration::from_secs(2)));
901
902        assert!(state.tick(start).is_empty());
903        assert!(state.tick(start + Duration::from_millis(1999)).is_empty());
904        assert_eq!(state.tick(start + Duration::from_secs(2)), vec![id]);
905        assert!(state.is_empty());
906    }
907
908    #[test]
909    fn waiting_toasts_start_counting_when_shown() {
910        let start = Instant::now();
911        let mut state = State::new().with_limit(1);
912        let _ = state.push(toast("a").duration(Duration::from_secs(1)));
913        let _ = state.push(toast("b").duration(Duration::from_secs(1)));
914
915        let _ = state.tick(start);
916        let _ = state.tick(start + Duration::from_secs(1));
917        assert_eq!(titles(&state), ["b"]);
918
919        // "b" became visible at 1s, so it lives until 2s.
920        assert!(state.tick(start + Duration::from_millis(1500)).is_empty());
921        assert_eq!(state.tick(start + Duration::from_secs(2)).len(), 1);
922    }
923
924    #[test]
925    fn persistent_toasts_never_expire_and_need_no_timer() {
926        let start = Instant::now();
927        let mut state = State::new();
928        let _ = state.push(toast("a").persistent());
929        assert!(!state.needs_tick());
930
931        let _ = state.tick(start + Duration::from_secs(3600));
932        assert_eq!(state.len(), 1);
933    }
934
935    #[test]
936    fn empty_state_needs_no_timer() {
937        let mut state = State::new();
938        assert!(!state.needs_tick());
939        let _ = state.push(toast("a"));
940        assert!(state.needs_tick());
941    }
942
943    #[test]
944    fn dismiss_and_action_remove_the_toast() {
945        let mut state = State::new();
946        let a = state.push(toast("a"));
947        let b = state.push(toast("b").action("Undo"));
948
949        assert!(state.update(Event::Dismiss(a)).is_none());
950        let Some(Output::Action(id, toast)) = state.update(Event::Action(b)) else {
951            panic!("expected an action output");
952        };
953        assert_eq!(id, b);
954        assert_eq!(toast.action.as_deref(), Some("Undo"));
955        assert!(state.is_empty());
956        assert!(state.update(Event::Action(b)).is_none());
957    }
958
959    #[derive(Debug, Clone, PartialEq)]
960    enum Undo {
961        Restore(&'static str),
962    }
963
964    #[test]
965    fn an_action_hands_back_the_payload_it_was_queued_with() {
966        let mut state: State<Undo> = State::default();
967        let plain = state.push(toast("Saved").action("View"));
968        let undo = state.push_with(toast("Archived").action("Undo"), Undo::Restore("inbox"));
969        assert_eq!(state.len(), 2);
970
971        let Some(Output::Payload(payload)) = state.update(Event::Action(undo)) else {
972            panic!("expected the payload");
973        };
974        assert_eq!(payload, Undo::Restore("inbox"));
975        assert!(matches!(
976            state.update(Event::Action(plain)),
977            Some(Output::Action(id, _)) if id == plain
978        ));
979        assert!(state.is_empty());
980    }
981
982    #[test]
983    fn dismissing_drops_the_payload() {
984        let mut state: State<Undo> = State::default();
985        let undo = state.push_with(toast("Archived").action("Undo"), Undo::Restore("inbox"));
986        assert!(state.update(Event::Dismiss(undo)).is_none());
987        assert!(state.update(Event::Action(undo)).is_none());
988        let expired = state.push_with(
989            toast("Archived").duration(Duration::from_secs(1)),
990            Undo::Restore("inbox"),
991        );
992        let start = Instant::now();
993        let _ = state.tick(start);
994        assert_eq!(state.tick(start + Duration::from_secs(1)), vec![expired]);
995        assert!(state.update(Event::Action(expired)).is_none());
996    }
997
998    fn press(keymap: &Keymap<Action>, chord: &str) -> Option<Action> {
999        let chord: Chord = chord.parse().unwrap();
1000        keymap.resolve(chord.key(), chord.modifiers())
1001    }
1002
1003    #[test]
1004    fn default_keymap_and_overrides() {
1005        let keymap = default_keymap();
1006        assert_eq!(press(&keymap, "Escape"), Some(Action::DismissLatest));
1007        assert_eq!(press(&keymap, "Shift+Escape"), Some(Action::DismissAll));
1008        assert_eq!(press(&keymap, "Alt+Z"), Some(Action::ActivateLatest));
1009        assert_eq!(press(&keymap, "Ctrl+Escape"), None);
1010        assert_eq!(press(&keymap, "Z"), None);
1011        for action in <Action as keys::Action>::ALL {
1012            assert!(!keymap.chords(action).is_empty(), "{action:?}");
1013            assert!(keys::Action::description(*action).ends_with('.'));
1014        }
1015
1016        let custom = keymap
1017            .unbind(&"Shift+Escape".parse().unwrap())
1018            .bind("Ctrl+Shift+X".parse().unwrap(), Action::DismissAll);
1019        assert_eq!(press(&custom, "Shift+Escape"), None);
1020        assert_eq!(press(&custom, "ctrl+shift+x"), Some(Action::DismissAll));
1021    }
1022
1023    #[test]
1024    fn dismiss_latest_closes_the_newest_visible_toast() {
1025        let mut state = State::new().with_limit(2);
1026        let _ = state.push(toast("a"));
1027        let b = state.push(toast("b"));
1028        let _ = state.push(toast("c"));
1029
1030        let events = Action::DismissLatest.events(&state);
1031        assert!(matches!(events.as_slice(), [Event::Dismiss(id)] if *id == b));
1032        for event in events {
1033            let _ = state.update(event);
1034        }
1035        assert_eq!(titles(&state), ["a", "c"]);
1036        assert!(Action::DismissLatest.events(&State::new()).is_empty());
1037    }
1038
1039    #[test]
1040    fn action_toasts_last_at_least_the_action_duration() {
1041        assert_eq!(toast("a").action("Undo").lifetime(), Some(ACTION_DURATION));
1042        let long = Duration::from_secs(30);
1043        assert_eq!(
1044            toast("a").action("Undo").duration(long).lifetime(),
1045            Some(long)
1046        );
1047        assert_eq!(toast("a").action("Undo").persistent().lifetime(), None);
1048        assert_eq!(toast("a").lifetime(), Some(DEFAULT_DURATION));
1049
1050        let start = Instant::now();
1051        let mut state = State::new();
1052        let _ = state.push(toast("Deleted").action("Undo"));
1053        let _ = state.tick(start);
1054        assert!(state.tick(start + DEFAULT_DURATION).is_empty());
1055        assert_eq!(state.tick(start + ACTION_DURATION).len(), 1);
1056    }
1057
1058    #[test]
1059    fn an_action_toast_takes_the_place_of_the_newest_plain_one() {
1060        let start = Instant::now();
1061        let mut state = State::new();
1062        for title in ["a", "b", "c", "d"] {
1063            let _ = state.push(toast(title));
1064        }
1065        let _ = state.tick(start);
1066        let _ = state.push(toast("Undo me").action("Undo"));
1067        assert_eq!(titles(&state), ["a", "b", "Undo me"]);
1068        assert_eq!(state.waiting(), 2);
1069
1070        // "c" went back to waiting, so it counts down afresh once it shows.
1071        let _ = state.tick(start + Duration::from_secs(1));
1072        let _ = state.tick(start + DEFAULT_DURATION);
1073        assert_eq!(titles(&state), ["Undo me", "c", "d"]);
1074        assert!(state.tick(start + Duration::from_secs(9)).is_empty());
1075        assert_eq!(state.tick(start + DEFAULT_DURATION * 2).len(), 2);
1076    }
1077
1078    #[test]
1079    fn action_toasts_wait_ahead_of_plain_ones_when_every_visible_toast_has_an_action() {
1080        let mut state = State::new().with_limit(2);
1081        let _ = state.push(toast("plain"));
1082        let _ = state.push(toast("one").action("Undo"));
1083        let _ = state.push(toast("two").action("Undo"));
1084        assert_eq!(titles(&state), ["one", "two"]);
1085        let _ = state.push(toast("three").action("Undo"));
1086        let order: Vec<_> = state
1087            .entries
1088            .iter()
1089            .map(|entry| entry.toast.title.as_str())
1090            .collect();
1091        assert_eq!(order, ["one", "two", "three", "plain"]);
1092    }
1093
1094    #[test]
1095    fn a_plain_toast_with_room_just_joins_the_end() {
1096        let mut state = State::new();
1097        let _ = state.push(toast("one").action("Undo"));
1098        let _ = state.push(toast("plain"));
1099        let _ = state.push(toast("two").action("Undo"));
1100        assert_eq!(titles(&state), ["one", "plain", "two"]);
1101    }
1102
1103    #[test]
1104    fn hovering_pauses_every_countdown() {
1105        let start = Instant::now();
1106        let mut state = State::new();
1107        let id = state.push(toast("a").duration(Duration::from_secs(2)));
1108        let _ = state.tick(start);
1109
1110        let _ = state.update(Event::Pause);
1111        assert!(state.is_paused());
1112        assert!(state.needs_tick());
1113        assert!(state.tick(start + Duration::from_secs(1)).is_empty());
1114        assert!(state.tick(start + Duration::from_secs(10)).is_empty());
1115
1116        let _ = state.update(Event::Resume);
1117        assert!(!state.is_paused());
1118        // The pause began as the countdown started, so both seconds are left.
1119        let resumed = start + Duration::from_secs(10);
1120        assert!(state.tick(resumed + Duration::from_millis(1900)).is_empty());
1121        assert_eq!(state.tick(resumed + Duration::from_secs(2)), vec![id]);
1122    }
1123
1124    #[test]
1125    fn the_pause_ends_when_the_last_toast_closes() {
1126        let mut state = State::new();
1127        let id = state.push(toast("a"));
1128        let _ = state.update(Event::Pause);
1129        let _ = state.dismiss(id);
1130        assert!(!state.is_paused());
1131    }
1132
1133    #[test]
1134    fn activate_latest_presses_the_newest_visible_action() {
1135        let mut state: State<Undo> = State::default();
1136        assert!(Action::ActivateLatest.events(&state).is_empty());
1137        let _ = state.push(toast("plain"));
1138        assert!(Action::ActivateLatest.events(&state).is_empty());
1139
1140        let _ = state.push_with(toast("first").action("Undo"), Undo::Restore("a"));
1141        let second = state.push_with(toast("second").action("Undo"), Undo::Restore("b"));
1142        let _ = state.push(toast("newer plain"));
1143        let events = Action::ActivateLatest.events(&state);
1144        assert!(matches!(events.as_slice(), [Event::Action(id)] if *id == second));
1145
1146        let outputs: Vec<_> = events
1147            .into_iter()
1148            .filter_map(|event| state.update(event))
1149            .collect();
1150        assert!(matches!(
1151            outputs.as_slice(),
1152            [Output::Payload(Undo::Restore("b"))]
1153        ));
1154    }
1155
1156    #[test]
1157    fn dismiss_all_includes_waiting_toasts() {
1158        let mut state = State::new().with_limit(1);
1159        for title in ["a", "b", "c"] {
1160            let _ = state.push(toast(title));
1161        }
1162        let events = Action::DismissAll.events(&state);
1163        assert_eq!(events.len(), 3);
1164        for event in events {
1165            let _ = state.update(event);
1166        }
1167        assert!(state.is_empty());
1168        assert!(Action::DismissAll.events(&state).is_empty());
1169    }
1170
1171    #[test]
1172    fn ready_hands_the_sender_back() {
1173        let (sender, _receiver) = mpsc::channel(1);
1174        let output = State::new().update(Event::Ready(sender));
1175        assert!(matches!(output, Some(Output::Ready(_))));
1176    }
1177
1178    #[test]
1179    fn stream_emits_ready_first_then_batches() {
1180        let mut events = Box::pin(stream());
1181        let Some(Event::Ready(mut sender)) = block_on(events.next()) else {
1182            panic!("first event must be Ready");
1183        };
1184
1185        block_on(async {
1186            for title in ["a", "b", "c"] {
1187                sender.send(toast(title)).await.ok();
1188            }
1189        });
1190        let Some(Event::Received(batch)) = block_on(events.next()) else {
1191            panic!("expected a batch");
1192        };
1193        let titles: Vec<_> = batch.iter().map(|toast| toast.title.as_str()).collect();
1194        assert_eq!(titles, ["a", "b", "c"]);
1195    }
1196
1197    #[test]
1198    fn batches_split_bursts_at_batch_size() {
1199        let items = stream::iter(0..BATCH_SIZE + 3);
1200        let sizes: Vec<_> = block_on(batches(items).map(|batch| batch.len()).collect());
1201        assert_eq!(sizes, [BATCH_SIZE, 3]);
1202    }
1203
1204    #[test]
1205    fn default_style_uses_background_and_border_tokens() {
1206        for theme in [light(), dark()] {
1207            let tokens = Tokens::of(&theme);
1208            let style = style(&tokens, Variant::Default);
1209            assert_eq!(style.background, Some(Background::Color(tokens.background)));
1210            assert_eq!(style.border.color, tokens.border);
1211            assert_eq!(style.text_color, Some(tokens.foreground));
1212        }
1213    }
1214
1215    #[test]
1216    fn variants_are_tinted_differently_in_both_themes() {
1217        for theme in [light(), dark()] {
1218            let tokens = Tokens::of(&theme);
1219            let styles = Variant::ALL.map(|variant| style(&tokens, variant).border.color);
1220            assert_ne!(styles[0], styles[1]);
1221            assert_ne!(styles[1], styles[2]);
1222            assert_eq!(accent(&tokens, Variant::Destructive), tokens.destructive);
1223            assert_eq!(accent(&tokens, Variant::Success), tokens.success);
1224        }
1225    }
1226
1227    #[test]
1228    fn dark_shadow_is_stronger() {
1229        let light = style(&Tokens::of(&light()), Variant::Default)
1230            .shadow
1231            .color
1232            .a;
1233        let dark = style(&Tokens::of(&dark()), Variant::Default).shadow.color.a;
1234        assert!(dark > light);
1235    }
1236}