1use std::{any::TypeId, ops::DerefMut};
2
3use collections::HashSet;
4use gpui::{AnyViewHandle, Entity, MutableAppContext, View, ViewContext, ViewHandle};
5
6use crate::Workspace;
7
8pub fn init(cx: &mut MutableAppContext) {
9 cx.set_global(NotificationTracker::new());
10 simple_message_notification::init(cx);
11}
12
13pub trait Notification: View {
14 fn should_dismiss_notification_on_event(&self, event: &<Self as Entity>::Event) -> bool;
15}
16
17pub trait NotificationHandle {
18 fn id(&self) -> usize;
19 fn to_any(&self) -> AnyViewHandle;
20}
21
22impl<T: Notification> NotificationHandle for ViewHandle<T> {
23 fn id(&self) -> usize {
24 self.id()
25 }
26
27 fn to_any(&self) -> AnyViewHandle {
28 self.into()
29 }
30}
31
32impl From<&dyn NotificationHandle> for AnyViewHandle {
33 fn from(val: &dyn NotificationHandle) -> Self {
34 val.to_any()
35 }
36}
37
38struct NotificationTracker {
39 notifications_sent: HashSet<TypeId>,
40}
41
42impl std::ops::Deref for NotificationTracker {
43 type Target = HashSet<TypeId>;
44
45 fn deref(&self) -> &Self::Target {
46 &self.notifications_sent
47 }
48}
49
50impl DerefMut for NotificationTracker {
51 fn deref_mut(&mut self) -> &mut Self::Target {
52 &mut self.notifications_sent
53 }
54}
55
56impl NotificationTracker {
57 fn new() -> Self {
58 Self {
59 notifications_sent: HashSet::default(),
60 }
61 }
62}
63
64impl Workspace {
65 pub fn show_notification_once<V: Notification>(
66 &mut self,
67 id: usize,
68 cx: &mut ViewContext<Self>,
69 build_notification: impl FnOnce(&mut ViewContext<Self>) -> ViewHandle<V>,
70 ) {
71 if !cx
72 .global::<NotificationTracker>()
73 .contains(&TypeId::of::<V>())
74 {
75 cx.update_global::<NotificationTracker, _, _>(|tracker, _| {
76 tracker.insert(TypeId::of::<V>())
77 });
78
79 self.show_notification::<V>(id, cx, build_notification)
80 }
81 }
82
83 pub fn show_notification<V: Notification>(
84 &mut self,
85 id: usize,
86 cx: &mut ViewContext<Self>,
87 build_notification: impl FnOnce(&mut ViewContext<Self>) -> ViewHandle<V>,
88 ) {
89 let type_id = TypeId::of::<V>();
90 if self
91 .notifications
92 .iter()
93 .all(|(existing_type_id, existing_id, _)| {
94 (*existing_type_id, *existing_id) != (type_id, id)
95 })
96 {
97 let notification = build_notification(cx);
98 cx.subscribe(¬ification, move |this, handle, event, cx| {
99 if handle.read(cx).should_dismiss_notification_on_event(event) {
100 this.dismiss_notification(type_id, id, cx);
101 }
102 })
103 .detach();
104 self.notifications
105 .push((type_id, id, Box::new(notification)));
106 cx.notify();
107 }
108 }
109
110 fn dismiss_notification(&mut self, type_id: TypeId, id: usize, cx: &mut ViewContext<Self>) {
111 self.notifications
112 .retain(|(existing_type_id, existing_id, _)| {
113 if (*existing_type_id, *existing_id) == (type_id, id) {
114 cx.notify();
115 false
116 } else {
117 true
118 }
119 });
120 }
121}
122
123pub mod simple_message_notification {
124 use std::process::Command;
125
126 use gpui::{
127 actions,
128 elements::{Flex, MouseEventHandler, Padding, ParentElement, Svg, Text},
129 impl_actions, Action, CursorStyle, Element, Entity, MouseButton, MutableAppContext, View,
130 ViewContext,
131 };
132 use menu::Cancel;
133 use serde::Deserialize;
134 use settings::Settings;
135
136 use crate::Workspace;
137
138 use super::Notification;
139
140 actions!(message_notifications, [CancelMessageNotification]);
141
142 #[derive(Clone, Default, Deserialize, PartialEq)]
143 pub struct OsOpen(pub String);
144
145 impl_actions!(message_notifications, [OsOpen]);
146
147 pub fn init(cx: &mut MutableAppContext) {
148 cx.add_action(MessageNotification::dismiss);
149 cx.add_action(
150 |_workspace: &mut Workspace, open_action: &OsOpen, _cx: &mut ViewContext<Workspace>| {
151 #[cfg(target_os = "macos")]
152 {
153 let mut command = Command::new("open");
154 command.arg(open_action.0.clone());
155
156 command.spawn().ok();
157 }
158 },
159 )
160 }
161
162 pub struct MessageNotification {
163 message: String,
164 click_action: Option<Box<dyn Action>>,
165 click_message: Option<String>,
166 }
167
168 pub enum MessageNotificationEvent {
169 Dismiss,
170 }
171
172 impl Entity for MessageNotification {
173 type Event = MessageNotificationEvent;
174 }
175
176 impl MessageNotification {
177 pub fn new_messsage<S: AsRef<str>>(message: S) -> MessageNotification {
178 Self {
179 message: message.as_ref().to_string(),
180 click_action: None,
181 click_message: None,
182 }
183 }
184
185 pub fn new<S1: AsRef<str>, A: Action, S2: AsRef<str>>(
186 message: S1,
187 click_action: A,
188 click_message: S2,
189 ) -> Self {
190 Self {
191 message: message.as_ref().to_string(),
192 click_action: Some(Box::new(click_action) as Box<dyn Action>),
193 click_message: Some(click_message.as_ref().to_string()),
194 }
195 }
196
197 pub fn dismiss(&mut self, _: &CancelMessageNotification, cx: &mut ViewContext<Self>) {
198 cx.emit(MessageNotificationEvent::Dismiss);
199 }
200 }
201
202 impl View for MessageNotification {
203 fn ui_name() -> &'static str {
204 "MessageNotification"
205 }
206
207 fn render(&mut self, cx: &mut gpui::RenderContext<'_, Self>) -> gpui::ElementBox {
208 let theme = cx.global::<Settings>().theme.clone();
209 let theme = &theme.simple_message_notification;
210
211 enum MessageNotificationTag {}
212
213 let click_action = self
214 .click_action
215 .as_ref()
216 .map(|action| action.boxed_clone());
217 let click_message = self.click_message.as_ref().map(|message| message.clone());
218 let message = self.message.clone();
219
220 MouseEventHandler::<MessageNotificationTag>::new(0, cx, |state, cx| {
221 Flex::column()
222 .with_child(
223 Flex::row()
224 .with_child(
225 Text::new(message, theme.message.text.clone())
226 .contained()
227 .with_style(theme.message.container)
228 .aligned()
229 .top()
230 .left()
231 .flex(1., true)
232 .boxed(),
233 )
234 .with_child(
235 MouseEventHandler::<Cancel>::new(0, cx, |state, _| {
236 let style = theme.dismiss_button.style_for(state, false);
237 Svg::new("icons/x_mark_8.svg")
238 .with_color(style.color)
239 .constrained()
240 .with_width(style.icon_width)
241 .aligned()
242 .contained()
243 .with_style(style.container)
244 .constrained()
245 .with_width(style.button_width)
246 .with_height(style.button_width)
247 .boxed()
248 })
249 .with_padding(Padding::uniform(5.))
250 .on_click(MouseButton::Left, move |_, cx| {
251 cx.dispatch_action(CancelMessageNotification)
252 })
253 .aligned()
254 .constrained()
255 .with_height(
256 cx.font_cache().line_height(theme.message.text.font_size),
257 )
258 .aligned()
259 .top()
260 .flex_float()
261 .boxed(),
262 )
263 .boxed(),
264 )
265 .with_children({
266 let style = theme.action_message.style_for(state, false);
267 if let Some(click_message) = click_message {
268 Some(
269 Text::new(click_message, style.text.clone())
270 .contained()
271 .with_style(style.container)
272 .boxed(),
273 )
274 } else {
275 None
276 }
277 .into_iter()
278 })
279 .contained()
280 .boxed()
281 })
282 .with_cursor_style(CursorStyle::PointingHand)
283 .on_click(MouseButton::Left, move |_, cx| {
284 if let Some(click_action) = click_action.as_ref() {
285 cx.dispatch_any_action(click_action.boxed_clone())
286 }
287 })
288 .boxed()
289 }
290 }
291
292 impl Notification for MessageNotification {
293 fn should_dismiss_notification_on_event(&self, event: &<Self as Entity>::Event) -> bool {
294 match event {
295 MessageNotificationEvent::Dismiss => true,
296 }
297 }
298 }
299}
300
301pub trait NotifyResultExt {
302 type Ok;
303
304 fn notify_err(
305 self,
306 workspace: &mut Workspace,
307 cx: &mut ViewContext<Workspace>,
308 ) -> Option<Self::Ok>;
309}
310
311impl<T, E> NotifyResultExt for Result<T, E>
312where
313 E: std::fmt::Debug,
314{
315 type Ok = T;
316
317 fn notify_err(self, workspace: &mut Workspace, cx: &mut ViewContext<Workspace>) -> Option<T> {
318 match self {
319 Ok(value) => Some(value),
320 Err(err) => {
321 workspace.show_notification(0, cx, |cx| {
322 cx.add_view(|_cx| {
323 simple_message_notification::MessageNotification::new_messsage(format!(
324 "Error: {:?}",
325 err,
326 ))
327 })
328 });
329
330 None
331 }
332 }
333 }
334}