1use std::rc::Rc;
2
3use gpui::{DismissEvent, Entity, EventEmitter, FocusHandle, Focusable, IntoElement};
4use ui::{Tooltip, prelude::*};
5use workspace::{ToastAction, ToastView};
6
7#[derive(Clone, Copy)]
8pub struct ToastIcon {
9 icon: IconName,
10 color: Color,
11}
12
13impl ToastIcon {
14 pub fn new(icon: IconName) -> Self {
15 Self {
16 icon,
17 color: Color::default(),
18 }
19 }
20
21 pub fn color(mut self, color: Color) -> Self {
22 self.color = color;
23 self
24 }
25}
26
27impl From<IconName> for ToastIcon {
28 fn from(icon: IconName) -> Self {
29 Self {
30 icon,
31 color: Color::default(),
32 }
33 }
34}
35
36#[derive(IntoComponent)]
37#[component(scope = "Notification")]
38pub struct StatusToast {
39 icon: Option<ToastIcon>,
40 text: SharedString,
41 action: Option<ToastAction>,
42 this_handle: Entity<Self>,
43 focus_handle: FocusHandle,
44}
45
46impl StatusToast {
47 pub fn new(
48 text: impl Into<SharedString>,
49 cx: &mut App,
50 f: impl FnOnce(Self, &mut Context<Self>) -> Self,
51 ) -> Entity<Self> {
52 cx.new(|cx| {
53 let focus_handle = cx.focus_handle();
54
55 f(
56 Self {
57 text: text.into(),
58 icon: None,
59 action: None,
60 this_handle: cx.entity(),
61 focus_handle,
62 },
63 cx,
64 )
65 })
66 }
67
68 pub fn icon(mut self, icon: ToastIcon) -> Self {
69 self.icon = Some(icon);
70 self
71 }
72
73 pub fn action(
74 mut self,
75 label: impl Into<SharedString>,
76 f: impl Fn(&mut Window, &mut App) + 'static,
77 ) -> Self {
78 let this_handle = self.this_handle.clone();
79 self.action = Some(ToastAction::new(
80 label.into(),
81 Some(Rc::new(move |window, cx| {
82 this_handle.update(cx, |_, cx| {
83 cx.emit(DismissEvent);
84 });
85 f(window, cx);
86 })),
87 ));
88 self
89 }
90}
91
92impl Render for StatusToast {
93 fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
94 h_flex()
95 .id("status-toast")
96 .elevation_3(cx)
97 .gap_2()
98 .py_1p5()
99 .px_2p5()
100 .flex_none()
101 .bg(cx.theme().colors().surface_background)
102 .shadow_lg()
103 .items_center()
104 .when_some(self.icon.as_ref(), |this, icon| {
105 this.child(Icon::new(icon.icon).color(icon.color))
106 })
107 .child(Label::new(self.text.clone()).color(Color::Default))
108 .when_some(self.action.as_ref(), |this, action| {
109 this.child(
110 Button::new(action.id.clone(), action.label.clone())
111 .tooltip(Tooltip::for_action_title(
112 action.label.clone(),
113 &workspace::RunAction,
114 ))
115 .color(Color::Muted)
116 .when_some(action.on_click.clone(), |el, handler| {
117 el.on_click(move |_click_event, window, cx| handler(window, cx))
118 }),
119 )
120 })
121 }
122}
123
124impl ToastView for StatusToast {
125 fn action(&self) -> Option<ToastAction> {
126 self.action.clone()
127 }
128}
129
130impl Focusable for StatusToast {
131 fn focus_handle(&self, _cx: &App) -> gpui::FocusHandle {
132 self.focus_handle.clone()
133 }
134}
135
136impl EventEmitter<DismissEvent> for StatusToast {}
137
138impl ComponentPreview for StatusToast {
139 fn preview(_window: &mut Window, cx: &mut App) -> AnyElement {
140 let text_example = StatusToast::new("Operation completed", cx, |this, _| this);
141
142 let action_example = StatusToast::new("Update ready to install", cx, |this, _cx| {
143 this.action("Restart", |_, _| {})
144 });
145
146 let icon_example = StatusToast::new(
147 "Nathan Sobo accepted your contact request",
148 cx,
149 |this, _| this.icon(ToastIcon::new(IconName::Check).color(Color::Muted)),
150 );
151
152 let success_example = StatusToast::new("Pushed 4 changes to `zed/main`", cx, |this, _| {
153 this.icon(ToastIcon::new(IconName::Check).color(Color::Success))
154 });
155
156 let error_example = StatusToast::new(
157 "git push: Couldn't find remote origin `iamnbutler/zed`",
158 cx,
159 |this, _cx| {
160 this.icon(ToastIcon::new(IconName::XCircle).color(Color::Error))
161 .action("More Info", |_, _| {})
162 },
163 );
164
165 let warning_example = StatusToast::new("You have outdated settings", cx, |this, _cx| {
166 this.icon(ToastIcon::new(IconName::Warning).color(Color::Warning))
167 .action("More Info", |_, _| {})
168 });
169
170 let pr_example =
171 StatusToast::new("`zed/new-notification-system` created!", cx, |this, _cx| {
172 this.icon(ToastIcon::new(IconName::GitBranchSmall).color(Color::Muted))
173 .action("Open Pull Request", |_, cx| {
174 cx.open_url("https://github.com/")
175 })
176 });
177
178 v_flex()
179 .gap_6()
180 .p_4()
181 .children(vec![
182 example_group_with_title(
183 "Basic Toast",
184 vec![
185 single_example("Text", div().child(text_example).into_any_element()),
186 single_example("Action", div().child(action_example).into_any_element()),
187 single_example("Icon", div().child(icon_example).into_any_element()),
188 ],
189 ),
190 example_group_with_title(
191 "Examples",
192 vec![
193 single_example("Success", div().child(success_example).into_any_element()),
194 single_example("Error", div().child(error_example).into_any_element()),
195 single_example("Warning", div().child(warning_example).into_any_element()),
196 single_example("Create PR", div().child(pr_example).into_any_element()),
197 ],
198 )
199 .vertical(),
200 ])
201 .into_any_element()
202 }
203}