1use std::marker::PhantomData;
2
3use gpui3::{Element, ParentElement, StyleHelpers, ViewContext};
4
5use crate::{
6 h_stack, v_stack, Button, Icon, IconButton, IconElement, Label, ThemeColor, Toast, ToastOrigin,
7};
8
9/// Notification toasts are used to display a message
10/// that requires them to take action.
11///
12/// You must provide a primary action for the user to take.
13///
14/// To simply convey information, use a Status.
15#[derive(Element)]
16pub struct NotificationToast<S: 'static + Send + Sync + Clone> {
17 state_type: PhantomData<S>,
18 left_icon: Option<Icon>,
19 title: String,
20 message: String,
21 primary_action: Option<Button<S>>,
22 secondary_action: Option<Button<S>>,
23}
24
25impl<S: 'static + Send + Sync + Clone> NotificationToast<S> {
26 pub fn new(
27 title: impl Into<String>,
28 message: impl Into<String>,
29 primary_action: Button<S>,
30 ) -> Self {
31 Self {
32 state_type: PhantomData,
33 left_icon: None,
34 title: title.into(),
35 message: message.into(),
36 primary_action: Some(primary_action),
37 secondary_action: None,
38 }
39 }
40
41 pub fn left_icon(mut self, icon: Icon) -> Self {
42 self.left_icon = Some(icon);
43 self
44 }
45
46 pub fn secondary_action(mut self, action: Button<S>) -> Self {
47 self.secondary_action = Some(action);
48 self
49 }
50
51 fn render(&mut self, _view: &mut S, cx: &mut ViewContext<S>) -> impl Element<ViewState = S> {
52 let color = ThemeColor::new(cx);
53
54 let notification = h_stack()
55 .gap_1()
56 .items_start()
57 .children(self.left_icon.map(|i| IconElement::new(i)))
58 .child(
59 v_stack()
60 .child(
61 h_stack()
62 .justify_between()
63 .p_1()
64 .child(Label::new(self.title.clone()))
65 .child(IconButton::new(Icon::Close)),
66 )
67 .child(
68 v_stack()
69 .p_1()
70 .child(Label::new(self.message.clone()))
71 .child(
72 h_stack()
73 .gap_1()
74 .justify_end()
75 .children(self.secondary_action.take())
76 .children(self.primary_action.take()),
77 ),
78 ),
79 );
80
81 Toast::new(ToastOrigin::BottomRight).child(notification)
82 }
83}