notifications.rs

  1use crate::{Toast, Workspace};
  2use anyhow::Context;
  3use anyhow::{anyhow, Result};
  4use gpui::{
  5    svg, AnyView, AppContext, AsyncWindowContext, ClipboardItem, DismissEvent, EventEmitter,
  6    Global, PromptLevel, Render, ScrollHandle, Task, View, ViewContext, VisualContext,
  7    WindowContext,
  8};
  9use std::rc::Rc;
 10use std::{any::TypeId, time::Duration};
 11use ui::{prelude::*, Tooltip};
 12use util::ResultExt;
 13
 14pub fn init(cx: &mut AppContext) {
 15    cx.set_global(GlobalAppNotifications {
 16        app_notifications: Vec::new(),
 17    })
 18}
 19
 20#[derive(Debug, PartialEq, Clone)]
 21pub enum NotificationId {
 22    Unique(TypeId),
 23    Composite(TypeId, ElementId),
 24    Named(SharedString),
 25}
 26
 27impl NotificationId {
 28    /// Returns a unique [`NotificationId`] for the given type.
 29    pub fn unique<T: 'static>() -> Self {
 30        Self::Unique(TypeId::of::<T>())
 31    }
 32
 33    /// Returns a [`NotificationId`] for the given type that is also identified
 34    /// by the provided ID.
 35    pub fn composite<T: 'static>(id: impl Into<ElementId>) -> Self {
 36        Self::Composite(TypeId::of::<T>(), id.into())
 37    }
 38
 39    /// Builds a `NotificationId` out of the given string.
 40    pub fn named(id: SharedString) -> Self {
 41        Self::Named(id)
 42    }
 43}
 44
 45pub trait Notification: EventEmitter<DismissEvent> + Render {}
 46
 47impl<V: EventEmitter<DismissEvent> + Render> Notification for V {}
 48
 49impl Workspace {
 50    #[cfg(any(test, feature = "test-support"))]
 51    pub fn notification_ids(&self) -> Vec<NotificationId> {
 52        self.notifications
 53            .iter()
 54            .map(|(id, _)| id)
 55            .cloned()
 56            .collect()
 57    }
 58
 59    pub fn show_notification<V: Notification>(
 60        &mut self,
 61        id: NotificationId,
 62        cx: &mut ViewContext<Self>,
 63        build_notification: impl FnOnce(&mut ViewContext<Self>) -> View<V>,
 64    ) {
 65        self.show_notification_without_handling_dismiss_events(&id, cx, |cx| {
 66            let notification = build_notification(cx);
 67            cx.subscribe(&notification, {
 68                let id = id.clone();
 69                move |this, _, _: &DismissEvent, cx| {
 70                    this.dismiss_notification(&id, cx);
 71                }
 72            })
 73            .detach();
 74            notification.into()
 75        });
 76    }
 77
 78    /// Shows a notification in this workspace's window. Caller must handle dismiss.
 79    ///
 80    /// This exists so that the `build_notification` closures stored for app notifications can
 81    /// return `AnyView`. Subscribing to events from an `AnyView` is not supported, so instead that
 82    /// responsibility is pushed to the caller where the `V` type is known.
 83    pub(crate) fn show_notification_without_handling_dismiss_events(
 84        &mut self,
 85        id: &NotificationId,
 86        cx: &mut ViewContext<Self>,
 87        build_notification: impl FnOnce(&mut ViewContext<Self>) -> AnyView,
 88    ) {
 89        self.dismiss_notification(id, cx);
 90        self.notifications
 91            .push((id.clone(), build_notification(cx)));
 92        cx.notify();
 93    }
 94
 95    pub fn show_error<E>(&mut self, err: &E, cx: &mut ViewContext<Self>)
 96    where
 97        E: std::fmt::Debug + std::fmt::Display,
 98    {
 99        self.show_notification(workspace_error_notification_id(), cx, |cx| {
100            cx.new_view(|_cx| ErrorMessagePrompt::new(format!("Error: {err}")))
101        });
102    }
103
104    pub fn show_portal_error(&mut self, err: String, cx: &mut ViewContext<Self>) {
105        struct PortalError;
106
107        self.show_notification(NotificationId::unique::<PortalError>(), cx, |cx| {
108            cx.new_view(|_cx| {
109                ErrorMessagePrompt::new(err.to_string()).with_link_button(
110                    "See docs",
111                    "https://zed.dev/docs/linux#i-cant-open-any-files",
112                )
113            })
114        });
115    }
116
117    pub fn dismiss_notification(&mut self, id: &NotificationId, cx: &mut ViewContext<Self>) {
118        self.notifications.retain(|(existing_id, _)| {
119            if existing_id == id {
120                cx.notify();
121                false
122            } else {
123                true
124            }
125        });
126    }
127
128    pub fn show_toast(&mut self, toast: Toast, cx: &mut ViewContext<Self>) {
129        self.dismiss_notification(&toast.id, cx);
130        self.show_notification(toast.id.clone(), cx, |cx| {
131            cx.new_view(|_cx| match toast.on_click.as_ref() {
132                Some((click_msg, on_click)) => {
133                    let on_click = on_click.clone();
134                    simple_message_notification::MessageNotification::new(toast.msg.clone())
135                        .with_click_message(click_msg.clone())
136                        .on_click(move |cx| on_click(cx))
137                }
138                None => simple_message_notification::MessageNotification::new(toast.msg.clone()),
139            })
140        });
141        if toast.autohide {
142            cx.spawn(|workspace, mut cx| async move {
143                cx.background_executor()
144                    .timer(Duration::from_millis(5000))
145                    .await;
146                workspace
147                    .update(&mut cx, |workspace, cx| {
148                        workspace.dismiss_toast(&toast.id, cx)
149                    })
150                    .ok();
151            })
152            .detach();
153        }
154    }
155
156    pub fn dismiss_toast(&mut self, id: &NotificationId, cx: &mut ViewContext<Self>) {
157        self.dismiss_notification(id, cx);
158    }
159
160    pub fn clear_all_notifications(&mut self, cx: &mut ViewContext<Self>) {
161        self.notifications.clear();
162        cx.notify();
163    }
164
165    pub fn show_initial_notifications(&mut self, cx: &mut ViewContext<Self>) {
166        // Allow absence of the global so that tests don't need to initialize it.
167        let app_notifications = cx
168            .try_global::<GlobalAppNotifications>()
169            .iter()
170            .flat_map(|global| global.app_notifications.iter().cloned())
171            .collect::<Vec<_>>();
172        for (id, build_notification) in app_notifications {
173            self.show_notification_without_handling_dismiss_events(&id, cx, |cx| {
174                build_notification(cx)
175            });
176        }
177    }
178}
179
180pub struct LanguageServerPrompt {
181    request: Option<project::LanguageServerPromptRequest>,
182    scroll_handle: ScrollHandle,
183}
184
185impl LanguageServerPrompt {
186    pub fn new(request: project::LanguageServerPromptRequest) -> Self {
187        Self {
188            request: Some(request),
189            scroll_handle: ScrollHandle::new(),
190        }
191    }
192
193    async fn select_option(this: View<Self>, ix: usize, mut cx: AsyncWindowContext) {
194        util::maybe!(async move {
195            let potential_future = this.update(&mut cx, |this, _| {
196                this.request.take().map(|request| request.respond(ix))
197            });
198
199            potential_future? // App Closed
200                .ok_or_else(|| anyhow::anyhow!("Response already sent"))?
201                .await
202                .ok_or_else(|| anyhow::anyhow!("Stream already closed"))?;
203
204            this.update(&mut cx, |_, cx| cx.emit(DismissEvent))?;
205
206            anyhow::Ok(())
207        })
208        .await
209        .log_err();
210    }
211}
212
213impl Render for LanguageServerPrompt {
214    fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
215        let Some(request) = &self.request else {
216            return div().id("language_server_prompt_notification");
217        };
218
219        let (icon, color) = match request.level {
220            PromptLevel::Info => (IconName::Info, Color::Accent),
221            PromptLevel::Warning => (IconName::Warning, Color::Warning),
222            PromptLevel::Critical => (IconName::XCircle, Color::Error),
223        };
224
225        div()
226            .id("language_server_prompt_notification")
227            .group("language_server_prompt_notification")
228            .occlude()
229            .w_full()
230            .max_h(vh(0.8, cx))
231            .elevation_3(cx)
232            .overflow_y_scroll()
233            .track_scroll(&self.scroll_handle)
234            .child(
235                v_flex()
236                    .p_3()
237                    .overflow_hidden()
238                    .child(
239                        h_flex()
240                            .justify_between()
241                            .items_start()
242                            .child(
243                                h_flex()
244                                    .gap_2()
245                                    .child(Icon::new(icon).color(color))
246                                    .child(Label::new(request.lsp_name.clone())),
247                            )
248                            .child(
249                                h_flex()
250                                    .child(
251                                        IconButton::new("copy", IconName::Copy)
252                                            .on_click({
253                                                let message = request.message.clone();
254                                                move |_, cx| {
255                                                    cx.write_to_clipboard(
256                                                        ClipboardItem::new_string(message.clone()),
257                                                    )
258                                                }
259                                            })
260                                            .tooltip(|cx| Tooltip::text("Copy Description", cx)),
261                                    )
262                                    .child(IconButton::new("close", IconName::Close).on_click(
263                                        cx.listener(|_, _, cx| cx.emit(gpui::DismissEvent)),
264                                    )),
265                            ),
266                    )
267                    .child(Label::new(request.message.to_string()).size(LabelSize::Small))
268                    .children(request.actions.iter().enumerate().map(|(ix, action)| {
269                        let this_handle = cx.view().clone();
270                        Button::new(ix, action.title.clone())
271                            .size(ButtonSize::Large)
272                            .on_click(move |_, cx| {
273                                let this_handle = this_handle.clone();
274                                cx.spawn(|cx| async move {
275                                    LanguageServerPrompt::select_option(this_handle, ix, cx).await
276                                })
277                                .detach()
278                            })
279                    })),
280            )
281    }
282}
283
284impl EventEmitter<DismissEvent> for LanguageServerPrompt {}
285
286fn workspace_error_notification_id() -> NotificationId {
287    struct WorkspaceErrorNotification;
288    NotificationId::unique::<WorkspaceErrorNotification>()
289}
290
291#[derive(Debug, Clone)]
292pub struct ErrorMessagePrompt {
293    message: SharedString,
294    label_and_url_button: Option<(SharedString, SharedString)>,
295}
296
297impl ErrorMessagePrompt {
298    pub fn new<S>(message: S) -> Self
299    where
300        S: Into<SharedString>,
301    {
302        Self {
303            message: message.into(),
304            label_and_url_button: None,
305        }
306    }
307
308    pub fn with_link_button<S>(mut self, label: S, url: S) -> Self
309    where
310        S: Into<SharedString>,
311    {
312        self.label_and_url_button = Some((label.into(), url.into()));
313        self
314    }
315}
316
317impl Render for ErrorMessagePrompt {
318    fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
319        h_flex()
320            .id("error_message_prompt_notification")
321            .occlude()
322            .elevation_3(cx)
323            .items_start()
324            .justify_between()
325            .p_2()
326            .gap_2()
327            .w_full()
328            .child(
329                v_flex()
330                    .w_full()
331                    .child(
332                        h_flex()
333                            .w_full()
334                            .justify_between()
335                            .child(
336                                svg()
337                                    .size(cx.text_style().font_size)
338                                    .flex_none()
339                                    .mr_2()
340                                    .mt(px(-2.0))
341                                    .map(|icon| {
342                                        icon.path(IconName::Warning.path())
343                                            .text_color(Color::Error.color(cx))
344                                    }),
345                            )
346                            .child(
347                                ui::IconButton::new("close", ui::IconName::Close)
348                                    .on_click(cx.listener(|_, _, cx| cx.emit(gpui::DismissEvent))),
349                            ),
350                    )
351                    .child(
352                        div()
353                            .id("error_message")
354                            .max_w_96()
355                            .max_h_40()
356                            .overflow_y_scroll()
357                            .child(Label::new(self.message.clone()).size(LabelSize::Small)),
358                    )
359                    .when_some(self.label_and_url_button.clone(), |elm, (label, url)| {
360                        elm.child(
361                            div().mt_2().child(
362                                ui::Button::new("error_message_prompt_notification_button", label)
363                                    .on_click(move |_, cx| cx.open_url(&url)),
364                            ),
365                        )
366                    }),
367            )
368    }
369}
370
371impl EventEmitter<DismissEvent> for ErrorMessagePrompt {}
372
373pub mod simple_message_notification {
374    use std::sync::Arc;
375
376    use gpui::{
377        div, AnyElement, DismissEvent, EventEmitter, ParentElement, Render, SharedString, Styled,
378        ViewContext,
379    };
380    use ui::prelude::*;
381
382    pub struct MessageNotification {
383        content: Box<dyn Fn(&mut ViewContext<Self>) -> AnyElement>,
384        on_click: Option<Arc<dyn Fn(&mut ViewContext<Self>)>>,
385        click_message: Option<SharedString>,
386        secondary_click_message: Option<SharedString>,
387        secondary_on_click: Option<Arc<dyn Fn(&mut ViewContext<Self>)>>,
388    }
389
390    impl EventEmitter<DismissEvent> for MessageNotification {}
391
392    impl MessageNotification {
393        pub fn new<S>(message: S) -> MessageNotification
394        where
395            S: Into<SharedString>,
396        {
397            let message = message.into();
398            Self::new_from_builder(move |_| Label::new(message.clone()).into_any_element())
399        }
400
401        pub fn new_from_builder<F>(content: F) -> MessageNotification
402        where
403            F: 'static + Fn(&mut ViewContext<Self>) -> AnyElement,
404        {
405            Self {
406                content: Box::new(content),
407                on_click: None,
408                click_message: None,
409                secondary_on_click: None,
410                secondary_click_message: None,
411            }
412        }
413
414        pub fn with_click_message<S>(mut self, message: S) -> Self
415        where
416            S: Into<SharedString>,
417        {
418            self.click_message = Some(message.into());
419            self
420        }
421
422        pub fn on_click<F>(mut self, on_click: F) -> Self
423        where
424            F: 'static + Fn(&mut ViewContext<Self>),
425        {
426            self.on_click = Some(Arc::new(on_click));
427            self
428        }
429
430        pub fn with_secondary_click_message<S>(mut self, message: S) -> Self
431        where
432            S: Into<SharedString>,
433        {
434            self.secondary_click_message = Some(message.into());
435            self
436        }
437
438        pub fn on_secondary_click<F>(mut self, on_click: F) -> Self
439        where
440            F: 'static + Fn(&mut ViewContext<Self>),
441        {
442            self.secondary_on_click = Some(Arc::new(on_click));
443            self
444        }
445
446        pub fn dismiss(&mut self, cx: &mut ViewContext<Self>) {
447            cx.emit(DismissEvent);
448        }
449    }
450
451    impl Render for MessageNotification {
452        fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
453            v_flex()
454                .p_3()
455                .gap_2()
456                .elevation_3(cx)
457                .child(
458                    h_flex()
459                        .gap_4()
460                        .justify_between()
461                        .items_start()
462                        .child(div().max_w_96().child((self.content)(cx)))
463                        .child(
464                            IconButton::new("close", IconName::Close)
465                                .on_click(cx.listener(|this, _, cx| this.dismiss(cx))),
466                        ),
467                )
468                .child(
469                    h_flex()
470                        .gap_2()
471                        .children(self.click_message.iter().map(|message| {
472                            Button::new(message.clone(), message.clone())
473                                .label_size(LabelSize::Small)
474                                .icon(IconName::Check)
475                                .icon_position(IconPosition::Start)
476                                .icon_size(IconSize::Small)
477                                .icon_color(Color::Success)
478                                .on_click(cx.listener(|this, _, cx| {
479                                    if let Some(on_click) = this.on_click.as_ref() {
480                                        (on_click)(cx)
481                                    };
482                                    this.dismiss(cx)
483                                }))
484                        }))
485                        .children(self.secondary_click_message.iter().map(|message| {
486                            Button::new(message.clone(), message.clone())
487                                .label_size(LabelSize::Small)
488                                .icon(IconName::Close)
489                                .icon_position(IconPosition::Start)
490                                .icon_size(IconSize::Small)
491                                .icon_color(Color::Error)
492                                .on_click(cx.listener(|this, _, cx| {
493                                    if let Some(on_click) = this.secondary_on_click.as_ref() {
494                                        (on_click)(cx)
495                                    };
496                                    this.dismiss(cx)
497                                }))
498                        })),
499                )
500        }
501    }
502}
503
504/// Stores app notifications so that they can be shown in new workspaces.
505struct GlobalAppNotifications {
506    app_notifications: Vec<(
507        NotificationId,
508        Rc<dyn Fn(&mut ViewContext<Workspace>) -> AnyView>,
509    )>,
510}
511
512impl Global for GlobalAppNotifications {}
513
514impl GlobalAppNotifications {
515    pub fn insert(
516        &mut self,
517        id: NotificationId,
518        build_notification: Rc<dyn Fn(&mut ViewContext<Workspace>) -> AnyView>,
519    ) {
520        self.remove(&id);
521        self.app_notifications.push((id, build_notification))
522    }
523
524    pub fn remove(&mut self, id: &NotificationId) {
525        self.app_notifications
526            .retain(|(existing_id, _)| existing_id != id);
527    }
528}
529
530/// Shows a notification in all workspaces. New workspaces will also receive the notification - this
531/// is particularly to handle notifications that occur on initialization before any workspaces
532/// exist. If the notification is dismissed within any workspace, it will be removed from all.
533pub fn show_app_notification<V: Notification + 'static>(
534    id: NotificationId,
535    cx: &mut AppContext,
536    build_notification: impl Fn(&mut ViewContext<Workspace>) -> View<V> + 'static,
537) -> Result<()> {
538    // Handle dismiss events by removing the notification from all workspaces.
539    let build_notification: Rc<dyn Fn(&mut ViewContext<Workspace>) -> AnyView> = Rc::new({
540        let id = id.clone();
541        move |cx| {
542            let notification = build_notification(cx);
543            cx.subscribe(&notification, {
544                let id = id.clone();
545                move |_, _, _: &DismissEvent, cx| {
546                    dismiss_app_notification(&id, cx);
547                }
548            })
549            .detach();
550            notification.into()
551        }
552    });
553
554    // Store the notification so that new workspaces also receive it.
555    cx.global_mut::<GlobalAppNotifications>()
556        .insert(id.clone(), build_notification.clone());
557
558    let mut notify_errors = Vec::new();
559
560    for window in cx.windows() {
561        if let Some(workspace_window) = window.downcast::<Workspace>() {
562            let notify_result = workspace_window.update(cx, |workspace, cx| {
563                workspace.show_notification_without_handling_dismiss_events(&id, cx, |cx| {
564                    build_notification(cx)
565                });
566            });
567            match notify_result {
568                Ok(()) => {}
569                Err(notify_err) => notify_errors.push(notify_err),
570            }
571        }
572    }
573
574    if notify_errors.is_empty() {
575        Ok(())
576    } else {
577        Err(anyhow!(
578            "No workspaces were able to show notification. Errors:\n\n{}",
579            notify_errors
580                .iter()
581                .map(|e| e.to_string())
582                .collect::<Vec<_>>()
583                .join("\n\n")
584        ))
585    }
586}
587
588pub fn dismiss_app_notification(id: &NotificationId, cx: &mut AppContext) {
589    cx.global_mut::<GlobalAppNotifications>().remove(id);
590    for window in cx.windows() {
591        if let Some(workspace_window) = window.downcast::<Workspace>() {
592            let id = id.clone();
593            // This spawn is necessary in order to dismiss the notification on which the click
594            // occurred, because in that case we're already in the middle of an update.
595            cx.spawn(move |mut cx| async move {
596                workspace_window.update(&mut cx, |workspace, cx| {
597                    workspace.dismiss_notification(&id, cx)
598                })
599            })
600            .detach_and_log_err(cx);
601        }
602    }
603}
604
605pub trait NotifyResultExt {
606    type Ok;
607
608    fn notify_err(
609        self,
610        workspace: &mut Workspace,
611        cx: &mut ViewContext<Workspace>,
612    ) -> Option<Self::Ok>;
613
614    fn notify_async_err(self, cx: &mut AsyncWindowContext) -> Option<Self::Ok>;
615
616    /// Notifies the active workspace if there is one, otherwise notifies all workspaces.
617    fn notify_app_err(self, cx: &mut AppContext) -> Option<Self::Ok>;
618}
619
620impl<T, E> NotifyResultExt for std::result::Result<T, E>
621where
622    E: std::fmt::Debug + std::fmt::Display,
623{
624    type Ok = T;
625
626    fn notify_err(self, workspace: &mut Workspace, cx: &mut ViewContext<Workspace>) -> Option<T> {
627        match self {
628            Ok(value) => Some(value),
629            Err(err) => {
630                log::error!("Showing error notification in workspace: {err:?}");
631                workspace.show_error(&err, cx);
632                None
633            }
634        }
635    }
636
637    fn notify_async_err(self, cx: &mut AsyncWindowContext) -> Option<T> {
638        match self {
639            Ok(value) => Some(value),
640            Err(err) => {
641                log::error!("{err:?}");
642                cx.update_root(|view, cx| {
643                    if let Ok(workspace) = view.downcast::<Workspace>() {
644                        workspace.update(cx, |workspace, cx| workspace.show_error(&err, cx))
645                    }
646                })
647                .ok();
648                None
649            }
650        }
651    }
652
653    fn notify_app_err(self, cx: &mut AppContext) -> Option<T> {
654        match self {
655            Ok(value) => Some(value),
656            Err(err) => {
657                let message: SharedString = format!("Error: {err}").into();
658                log::error!("Showing error notification in app: {message}");
659                show_app_notification(workspace_error_notification_id(), cx, {
660                    let message = message.clone();
661                    move |cx| {
662                        cx.new_view({
663                            let message = message.clone();
664                            move |_cx| ErrorMessagePrompt::new(message)
665                        })
666                    }
667                })
668                .with_context(|| format!("Error while showing error notification: {message}"))
669                .log_err();
670                None
671            }
672        }
673    }
674}
675
676pub trait NotifyTaskExt {
677    fn detach_and_notify_err(self, cx: &mut WindowContext);
678}
679
680impl<R, E> NotifyTaskExt for Task<std::result::Result<R, E>>
681where
682    E: std::fmt::Debug + std::fmt::Display + Sized + 'static,
683    R: 'static,
684{
685    fn detach_and_notify_err(self, cx: &mut WindowContext) {
686        cx.spawn(|mut cx| async move { self.await.notify_async_err(&mut cx) })
687            .detach();
688    }
689}
690
691pub trait DetachAndPromptErr<R> {
692    fn prompt_err(
693        self,
694        msg: &str,
695        cx: &mut WindowContext,
696        f: impl FnOnce(&anyhow::Error, &mut WindowContext) -> Option<String> + 'static,
697    ) -> Task<Option<R>>;
698
699    fn detach_and_prompt_err(
700        self,
701        msg: &str,
702        cx: &mut WindowContext,
703        f: impl FnOnce(&anyhow::Error, &mut WindowContext) -> Option<String> + 'static,
704    );
705}
706
707impl<R> DetachAndPromptErr<R> for Task<anyhow::Result<R>>
708where
709    R: 'static,
710{
711    fn prompt_err(
712        self,
713        msg: &str,
714        cx: &mut WindowContext,
715        f: impl FnOnce(&anyhow::Error, &mut WindowContext) -> Option<String> + 'static,
716    ) -> Task<Option<R>> {
717        let msg = msg.to_owned();
718        cx.spawn(|mut cx| async move {
719            let result = self.await;
720            if let Err(err) = result.as_ref() {
721                log::error!("{err:?}");
722                if let Ok(prompt) = cx.update(|cx| {
723                    let detail = f(err, cx).unwrap_or_else(|| format!("{err}. Please try again."));
724                    cx.prompt(PromptLevel::Critical, &msg, Some(&detail), &["Ok"])
725                }) {
726                    prompt.await.ok();
727                }
728                return None;
729            }
730            Some(result.unwrap())
731        })
732    }
733
734    fn detach_and_prompt_err(
735        self,
736        msg: &str,
737        cx: &mut WindowContext,
738        f: impl FnOnce(&anyhow::Error, &mut WindowContext) -> Option<String> + 'static,
739    ) {
740        self.prompt_err(msg, cx, f).detach();
741    }
742}