notifications.rs

  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(&notification, 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
125    use std::borrow::Cow;
126
127    use gpui::{
128        actions,
129        elements::{Flex, MouseEventHandler, Padding, ParentElement, Svg, Text},
130        impl_actions, Action, CursorStyle, Element, Entity, MouseButton, MutableAppContext, View,
131        ViewContext,
132    };
133    use menu::Cancel;
134    use serde::Deserialize;
135    use settings::Settings;
136
137    use crate::Workspace;
138
139    use super::Notification;
140
141    actions!(message_notifications, [CancelMessageNotification]);
142
143    #[derive(Clone, Default, Deserialize, PartialEq)]
144    pub struct OsOpen(pub Cow<'static, str>);
145
146    impl OsOpen {
147        pub fn new<I: Into<Cow<'static, str>>>(url: I) -> Self {
148            OsOpen(url.into())
149        }
150    }
151
152    impl_actions!(message_notifications, [OsOpen]);
153
154    pub fn init(cx: &mut MutableAppContext) {
155        cx.add_action(MessageNotification::dismiss);
156        cx.add_action(
157            |_workspace: &mut Workspace, open_action: &OsOpen, cx: &mut ViewContext<Workspace>| {
158                cx.platform().open_url(open_action.0.as_ref());
159            },
160        )
161    }
162
163    pub struct MessageNotification {
164        message: Cow<'static, str>,
165        click_action: Option<Box<dyn Action>>,
166        click_message: Option<Cow<'static, str>>,
167    }
168
169    pub enum MessageNotificationEvent {
170        Dismiss,
171    }
172
173    impl Entity for MessageNotification {
174        type Event = MessageNotificationEvent;
175    }
176
177    impl MessageNotification {
178        pub fn new_message<S: Into<Cow<'static, str>>>(message: S) -> MessageNotification {
179            Self {
180                message: message.into(),
181                click_action: None,
182                click_message: None,
183            }
184        }
185
186        pub fn new<S1: Into<Cow<'static, str>>, A: Action, S2: Into<Cow<'static, str>>>(
187            message: S1,
188            click_action: A,
189            click_message: S2,
190        ) -> Self {
191            Self {
192                message: message.into(),
193                click_action: Some(Box::new(click_action) as Box<dyn Action>),
194                click_message: Some(click_message.into()),
195            }
196        }
197
198        pub fn dismiss(&mut self, _: &CancelMessageNotification, cx: &mut ViewContext<Self>) {
199            cx.emit(MessageNotificationEvent::Dismiss);
200        }
201    }
202
203    impl View for MessageNotification {
204        fn ui_name() -> &'static str {
205            "MessageNotification"
206        }
207
208        fn render(&mut self, cx: &mut gpui::RenderContext<'_, Self>) -> gpui::ElementBox {
209            let theme = cx.global::<Settings>().theme.clone();
210            let theme = &theme.simple_message_notification;
211
212            enum MessageNotificationTag {}
213
214            let click_action = self
215                .click_action
216                .as_ref()
217                .map(|action| action.boxed_clone());
218            let click_message = self.click_message.as_ref().map(|message| message.clone());
219            let message = self.message.clone();
220
221            let has_click_action = click_action.is_some();
222
223            MouseEventHandler::<MessageNotificationTag>::new(0, cx, |state, cx| {
224                Flex::column()
225                    .with_child(
226                        Flex::row()
227                            .with_child(
228                                Text::new(message, theme.message.text.clone())
229                                    .contained()
230                                    .with_style(theme.message.container)
231                                    .aligned()
232                                    .top()
233                                    .left()
234                                    .flex(1., true)
235                                    .boxed(),
236                            )
237                            .with_child(
238                                MouseEventHandler::<Cancel>::new(0, cx, |state, _| {
239                                    let style = theme.dismiss_button.style_for(state, false);
240                                    Svg::new("icons/x_mark_8.svg")
241                                        .with_color(style.color)
242                                        .constrained()
243                                        .with_width(style.icon_width)
244                                        .aligned()
245                                        .contained()
246                                        .with_style(style.container)
247                                        .constrained()
248                                        .with_width(style.button_width)
249                                        .with_height(style.button_width)
250                                        .boxed()
251                                })
252                                .with_padding(Padding::uniform(5.))
253                                .on_click(MouseButton::Left, move |_, cx| {
254                                    cx.dispatch_action(CancelMessageNotification)
255                                })
256                                .with_cursor_style(CursorStyle::PointingHand)
257                                .aligned()
258                                .constrained()
259                                .with_height(
260                                    cx.font_cache().line_height(theme.message.text.font_size),
261                                )
262                                .aligned()
263                                .top()
264                                .flex_float()
265                                .boxed(),
266                            )
267                            .boxed(),
268                    )
269                    .with_children({
270                        let style = theme.action_message.style_for(state, false);
271                        if let Some(click_message) = click_message {
272                            Some(
273                                Text::new(click_message, style.text.clone())
274                                    .contained()
275                                    .with_style(style.container)
276                                    .boxed(),
277                            )
278                        } else {
279                            None
280                        }
281                        .into_iter()
282                    })
283                    .contained()
284                    .boxed()
285            })
286            // Since we're not using a proper overlay, we have to capture these extra events
287            .on_down(MouseButton::Left, |_, _| {})
288            .on_up(MouseButton::Left, |_, _| {})
289            .on_click(MouseButton::Left, move |_, cx| {
290                if let Some(click_action) = click_action.as_ref() {
291                    cx.dispatch_any_action(click_action.boxed_clone())
292                }
293            })
294            .with_cursor_style(if has_click_action {
295                CursorStyle::PointingHand
296            } else {
297                CursorStyle::Arrow
298            })
299            .boxed()
300        }
301    }
302
303    impl Notification for MessageNotification {
304        fn should_dismiss_notification_on_event(&self, event: &<Self as Entity>::Event) -> bool {
305            match event {
306                MessageNotificationEvent::Dismiss => true,
307            }
308        }
309    }
310}
311
312pub trait NotifyResultExt {
313    type Ok;
314
315    fn notify_err(
316        self,
317        workspace: &mut Workspace,
318        cx: &mut ViewContext<Workspace>,
319    ) -> Option<Self::Ok>;
320}
321
322impl<T, E> NotifyResultExt for Result<T, E>
323where
324    E: std::fmt::Debug,
325{
326    type Ok = T;
327
328    fn notify_err(self, workspace: &mut Workspace, cx: &mut ViewContext<Workspace>) -> Option<T> {
329        match self {
330            Ok(value) => Some(value),
331            Err(err) => {
332                workspace.show_notification(0, cx, |cx| {
333                    cx.add_view(|_cx| {
334                        simple_message_notification::MessageNotification::new_message(format!(
335                            "Error: {:?}",
336                            err,
337                        ))
338                    })
339                });
340
341                None
342            }
343        }
344    }
345}