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 `StatusToast`.
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 .min_w_64()
56 .max_w_96()
57 .gap_1()
58 .items_start()
59 .p_1()
60 .children(self.left_icon.map(|i| IconElement::new(i)))
61 .child(
62 v_stack()
63 .flex_1()
64 .w_full()
65 .gap_1()
66 .child(
67 h_stack()
68 .justify_between()
69 .child(Label::new(self.title.clone()))
70 .child(IconButton::new(Icon::Close).color(crate::IconColor::Muted)),
71 )
72 .child(
73 v_stack()
74 .overflow_hidden_x()
75 .gap_1()
76 .child(Label::new(self.message.clone()))
77 .child(
78 h_stack()
79 .gap_1()
80 .justify_end()
81 .children(self.secondary_action.take())
82 .children(self.primary_action.take()),
83 ),
84 ),
85 );
86
87 Toast::new(ToastOrigin::BottomRight).child(notification)
88 }
89}