relm4/abstractions/
toaster.rs

1#[derive(Debug, Default)]
2/// An abstraction over [`adw::ToastOverlay`] that
3/// makes it easy to store it in the model of components.
4///
5/// The only allowed action is to add toasts, effectively
6/// keeping the separation between UI and application state.
7pub struct Toaster {
8    overlay: adw::ToastOverlay,
9}
10
11impl Toaster {
12    /// Create a new [`Toaster`] with a given overlay.
13    #[must_use]
14    pub fn new(overlay: &adw::ToastOverlay) -> Self {
15        Self {
16            overlay: overlay.clone(),
17        }
18    }
19
20    /// The [`adw::ToastOverlay`] used internally.
21    #[must_use]
22    pub fn overlay_widget(&self) -> &adw::ToastOverlay {
23        &self.overlay
24    }
25
26    /// Create a simple [`adw::Toast`] that only contains
27    /// a text message.
28    pub fn toast(&self, title: &str) {
29        let toast = adw::Toast::new(title);
30        self.overlay.add_toast(toast);
31    }
32
33    /// Add a [`adw::Toast`] to the overlay.
34    pub fn add_toast(&self, toast: adw::Toast) {
35        self.overlay.add_toast(toast);
36    }
37}