toast.rs

 1use gpui2::AnyElement;
 2use smallvec::SmallVec;
 3
 4use crate::prelude::*;
 5
 6#[derive(Default, Debug, PartialEq, Eq, Clone, Copy)]
 7pub enum ToastOrigin {
 8    #[default]
 9    Bottom,
10    BottomRight,
11}
12
13/// Don't use toast directly:
14///
15/// - For messages with a required action, use a `NotificationToast`.
16/// - For messages that convey information, use a `StatusToast`.
17///
18/// A toast is a small, temporary window that appears to show a message to the user
19/// or indicate a required action.
20///
21/// Toasts should not persist on the screen for more than a few seconds unless
22/// they are actively showing the a process in progress.
23///
24/// Only one toast may be visible at a time.
25#[derive(Component)]
26pub struct Toast<V: 'static> {
27    origin: ToastOrigin,
28    children: SmallVec<[AnyElement<V>; 2]>,
29}
30
31impl<V: 'static> Toast<V> {
32    pub fn new(origin: ToastOrigin) -> Self {
33        Self {
34            origin,
35            children: SmallVec::new(),
36        }
37    }
38
39    fn render(self, _view: &mut V, cx: &mut ViewContext<V>) -> impl Component<V> {
40        let theme = theme(cx);
41
42        let mut div = div();
43
44        if self.origin == ToastOrigin::Bottom {
45            div = div.right_1_2();
46        } else {
47            div = div.right_2();
48        }
49
50        div.z_index(5)
51            .absolute()
52            .bottom_9()
53            .flex()
54            .py_1()
55            .px_1p5()
56            .rounded_lg()
57            .shadow_md()
58            .overflow_hidden()
59            .bg(theme.elevated_surface)
60            .children(self.children)
61    }
62}
63
64impl<V: 'static> ParentElement<V> for Toast<V> {
65    fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<V>; 2]> {
66        &mut self.children
67    }
68}
69
70#[cfg(feature = "stories")]
71pub use stories::*;
72
73#[cfg(feature = "stories")]
74mod stories {
75    use crate::{Label, Story};
76
77    use super::*;
78
79    #[derive(Component)]
80    pub struct ToastStory;
81
82    impl ToastStory {
83        fn render<V: 'static>(self, _view: &mut V, cx: &mut ViewContext<V>) -> impl Component<V> {
84            Story::container(cx)
85                .child(Story::title_for::<_, Toast<V>>(cx))
86                .child(Story::label(cx, "Default"))
87                .child(Toast::new(ToastOrigin::Bottom).child(Label::new("label")))
88        }
89    }
90}