toast.rs

 1use crate::prelude::*;
 2
 3#[derive(Default, Debug, PartialEq, Eq, Clone, Copy)]
 4pub enum ToastOrigin {
 5    #[default]
 6    Bottom,
 7    BottomRight,
 8}
 9
10#[derive(Default, Debug, PartialEq, Eq, Clone, Copy)]
11pub enum ToastVariant {
12    #[default]
13    Toast,
14    Status,
15}
16
17/// A toast is a small, temporary window that appears to show a message to the user
18/// or indicate a required action.
19///
20/// Toasts should not persist on the screen for more than a few seconds unless
21/// they are actively showing the a process in progress.
22///
23/// Only one toast may be visible at a time.
24#[derive(Element)]
25pub struct Toast<V: 'static> {
26    origin: ToastOrigin,
27    children: HackyChildren<V>,
28    payload: HackyChildrenPayload,
29}
30
31impl<V: 'static> Toast<V> {
32    pub fn new(
33        origin: ToastOrigin,
34        children: HackyChildren<V>,
35        payload: HackyChildrenPayload,
36    ) -> Self {
37        Self {
38            origin,
39            children,
40            payload,
41        }
42    }
43
44    fn render(&mut self, _: &mut V, cx: &mut ViewContext<V>) -> impl IntoElement<V> {
45        let color = ThemeColor::new(cx);
46
47        let mut div = div();
48
49        if self.origin == ToastOrigin::Bottom {
50            div = div.right_1_2();
51        } else {
52            div = div.right_4();
53        }
54
55        div.absolute()
56            .bottom_4()
57            .flex()
58            .py_2()
59            .px_1p5()
60            .min_w_40()
61            .rounded_md()
62            .fill(color.elevated_surface)
63            .max_w_64()
64            .children_any((self.children)(cx, self.payload.as_ref()))
65    }
66}