notifications.rs

  1use crate::{Toast, Workspace};
  2use collections::HashMap;
  3use gpui::{
  4    svg, AnyView, AppContext, AsyncWindowContext, ClipboardItem, DismissEvent, Entity, EntityId,
  5    EventEmitter, Global, PromptLevel, Render, ScrollHandle, Task, View, ViewContext,
  6    VisualContext, WindowContext,
  7};
  8use language::DiagnosticSeverity;
  9
 10use std::{any::TypeId, ops::DerefMut, time::Duration};
 11use ui::{prelude::*, Tooltip};
 12use util::ResultExt;
 13
 14pub fn init(cx: &mut AppContext) {
 15    cx.set_global(NotificationTracker::new());
 16}
 17
 18#[derive(Debug, PartialEq, Clone)]
 19pub struct NotificationId {
 20    /// A [`TypeId`] used to uniquely identify this notification.
 21    type_id: TypeId,
 22    /// A supplementary ID used to distinguish between multiple
 23    /// notifications that have the same [`type_id`](Self::type_id);
 24    id: Option<ElementId>,
 25}
 26
 27impl NotificationId {
 28    /// Returns a unique [`NotificationId`] for the given type.
 29    pub fn unique<T: 'static>() -> Self {
 30        Self {
 31            type_id: TypeId::of::<T>(),
 32            id: None,
 33        }
 34    }
 35
 36    /// Returns a [`NotificationId`] for the given type that is also identified
 37    /// by the provided ID.
 38    pub fn identified<T: 'static>(id: impl Into<ElementId>) -> Self {
 39        Self {
 40            type_id: TypeId::of::<T>(),
 41            id: Some(id.into()),
 42        }
 43    }
 44}
 45
 46pub trait Notification: EventEmitter<DismissEvent> + Render {}
 47
 48impl<V: EventEmitter<DismissEvent> + Render> Notification for V {}
 49
 50pub trait NotificationHandle: Send {
 51    fn id(&self) -> EntityId;
 52    fn to_any(&self) -> AnyView;
 53}
 54
 55impl<T: Notification> NotificationHandle for View<T> {
 56    fn id(&self) -> EntityId {
 57        self.entity_id()
 58    }
 59
 60    fn to_any(&self) -> AnyView {
 61        self.clone().into()
 62    }
 63}
 64
 65impl From<&dyn NotificationHandle> for AnyView {
 66    fn from(val: &dyn NotificationHandle) -> Self {
 67        val.to_any()
 68    }
 69}
 70
 71pub(crate) struct NotificationTracker {
 72    notifications_sent: HashMap<TypeId, Vec<NotificationId>>,
 73}
 74
 75impl Global for NotificationTracker {}
 76
 77impl std::ops::Deref for NotificationTracker {
 78    type Target = HashMap<TypeId, Vec<NotificationId>>;
 79
 80    fn deref(&self) -> &Self::Target {
 81        &self.notifications_sent
 82    }
 83}
 84
 85impl DerefMut for NotificationTracker {
 86    fn deref_mut(&mut self) -> &mut Self::Target {
 87        &mut self.notifications_sent
 88    }
 89}
 90
 91impl NotificationTracker {
 92    fn new() -> Self {
 93        Self {
 94            notifications_sent: Default::default(),
 95        }
 96    }
 97}
 98
 99impl Workspace {
100    pub fn has_shown_notification_once<V: Notification>(
101        &self,
102        id: &NotificationId,
103        cx: &ViewContext<Self>,
104    ) -> bool {
105        cx.global::<NotificationTracker>()
106            .get(&TypeId::of::<V>())
107            .map(|ids| ids.contains(id))
108            .unwrap_or(false)
109    }
110
111    pub fn show_notification_once<V: Notification>(
112        &mut self,
113        id: NotificationId,
114        cx: &mut ViewContext<Self>,
115        build_notification: impl FnOnce(&mut ViewContext<Self>) -> View<V>,
116    ) {
117        if !self.has_shown_notification_once::<V>(&id, cx) {
118            let tracker = cx.global_mut::<NotificationTracker>();
119            let entry = tracker.entry(TypeId::of::<V>()).or_default();
120            entry.push(id.clone());
121            self.show_notification::<V>(id, cx, build_notification)
122        }
123    }
124
125    #[cfg(any(test, feature = "test-support"))]
126    pub fn notification_ids(&self) -> Vec<NotificationId> {
127        self.notifications
128            .iter()
129            .map(|(id, _)| id)
130            .cloned()
131            .collect()
132    }
133
134    pub fn show_notification<V: Notification>(
135        &mut self,
136        id: NotificationId,
137        cx: &mut ViewContext<Self>,
138        build_notification: impl FnOnce(&mut ViewContext<Self>) -> View<V>,
139    ) {
140        self.dismiss_notification_internal(&id, cx);
141
142        let notification = build_notification(cx);
143        cx.subscribe(&notification, {
144            let id = id.clone();
145            move |this, _, _: &DismissEvent, cx| {
146                this.dismiss_notification_internal(&id, cx);
147            }
148        })
149        .detach();
150        self.notifications.push((id, Box::new(notification)));
151        cx.notify();
152    }
153
154    pub fn show_error<E>(&mut self, err: &E, cx: &mut ViewContext<Self>)
155    where
156        E: std::fmt::Debug + std::fmt::Display,
157    {
158        struct WorkspaceErrorNotification;
159
160        self.show_notification(
161            NotificationId::unique::<WorkspaceErrorNotification>(),
162            cx,
163            |cx| cx.new_view(|_cx| ErrorMessagePrompt::new(format!("Error: {err:#}"))),
164        );
165    }
166
167    pub fn show_portal_error(&mut self, err: String, cx: &mut ViewContext<Self>) {
168        struct PortalError;
169
170        self.show_notification(NotificationId::unique::<PortalError>(), cx, |cx| {
171            cx.new_view(|_cx| {
172                ErrorMessagePrompt::new(err.to_string()).with_link_button(
173                    "See docs",
174                    "https://zed.dev/docs/linux#i-cant-open-any-files",
175                )
176            })
177        });
178    }
179
180    pub fn dismiss_notification(&mut self, id: &NotificationId, cx: &mut ViewContext<Self>) {
181        self.dismiss_notification_internal(id, cx)
182    }
183
184    pub fn show_toast(&mut self, toast: Toast, cx: &mut ViewContext<Self>) {
185        self.dismiss_notification(&toast.id, cx);
186        self.show_notification(toast.id.clone(), cx, |cx| {
187            cx.new_view(|_cx| match toast.on_click.as_ref() {
188                Some((click_msg, on_click)) => {
189                    let on_click = on_click.clone();
190                    simple_message_notification::MessageNotification::new(toast.msg.clone())
191                        .with_click_message(click_msg.clone())
192                        .on_click(move |cx| on_click(cx))
193                }
194                None => simple_message_notification::MessageNotification::new(toast.msg.clone()),
195            })
196        });
197        if toast.autohide {
198            cx.spawn(|workspace, mut cx| async move {
199                cx.background_executor()
200                    .timer(Duration::from_millis(5000))
201                    .await;
202                workspace
203                    .update(&mut cx, |workspace, cx| {
204                        workspace.dismiss_toast(&toast.id, cx)
205                    })
206                    .ok();
207            })
208            .detach();
209        }
210    }
211
212    pub fn dismiss_toast(&mut self, id: &NotificationId, cx: &mut ViewContext<Self>) {
213        self.dismiss_notification(id, cx);
214    }
215
216    pub fn clear_all_notifications(&mut self, cx: &mut ViewContext<Self>) {
217        self.notifications.clear();
218        cx.notify();
219    }
220
221    fn dismiss_notification_internal(&mut self, id: &NotificationId, cx: &mut ViewContext<Self>) {
222        self.notifications.retain(|(existing_id, _)| {
223            if existing_id == id {
224                cx.notify();
225                false
226            } else {
227                true
228            }
229        });
230    }
231}
232
233pub struct LanguageServerPrompt {
234    request: Option<project::LanguageServerPromptRequest>,
235    scroll_handle: ScrollHandle,
236}
237
238impl LanguageServerPrompt {
239    pub fn new(request: project::LanguageServerPromptRequest) -> Self {
240        Self {
241            request: Some(request),
242            scroll_handle: ScrollHandle::new(),
243        }
244    }
245
246    async fn select_option(this: View<Self>, ix: usize, mut cx: AsyncWindowContext) {
247        util::maybe!(async move {
248            let potential_future = this.update(&mut cx, |this, _| {
249                this.request.take().map(|request| request.respond(ix))
250            });
251
252            potential_future? // App Closed
253                .ok_or_else(|| anyhow::anyhow!("Response already sent"))?
254                .await
255                .ok_or_else(|| anyhow::anyhow!("Stream already closed"))?;
256
257            this.update(&mut cx, |_, cx| cx.emit(DismissEvent))?;
258
259            anyhow::Ok(())
260        })
261        .await
262        .log_err();
263    }
264}
265
266impl Render for LanguageServerPrompt {
267    fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
268        let Some(request) = &self.request else {
269            return div().id("language_server_prompt_notification");
270        };
271
272        h_flex()
273            .id("language_server_prompt_notification")
274            .occlude()
275            .elevation_3(cx)
276            .items_start()
277            .justify_between()
278            .p_2()
279            .gap_2()
280            .w_full()
281            .max_h(vh(0.8, cx))
282            .overflow_y_scroll()
283            .track_scroll(&self.scroll_handle)
284            .group("")
285            .child(
286                v_flex()
287                    .w_full()
288                    .overflow_hidden()
289                    .child(
290                        h_flex()
291                            .w_full()
292                            .justify_between()
293                            .child(
294                                h_flex()
295                                    .flex_grow()
296                                    .children(
297                                        match request.level {
298                                            PromptLevel::Info => None,
299                                            PromptLevel::Warning => {
300                                                Some(DiagnosticSeverity::WARNING)
301                                            }
302                                            PromptLevel::Critical => {
303                                                Some(DiagnosticSeverity::ERROR)
304                                            }
305                                        }
306                                        .map(|severity| {
307                                            svg()
308                                                .size(cx.text_style().font_size)
309                                                .flex_none()
310                                                .mr_1()
311                                                .mt(px(-2.0))
312                                                .map(|icon| {
313                                                    if severity == DiagnosticSeverity::ERROR {
314                                                        icon.path(
315                                                            IconName::ExclamationTriangle.path(),
316                                                        )
317                                                        .text_color(Color::Error.color(cx))
318                                                    } else {
319                                                        icon.path(
320                                                            IconName::ExclamationTriangle.path(),
321                                                        )
322                                                        .text_color(Color::Warning.color(cx))
323                                                    }
324                                                })
325                                        }),
326                                    )
327                                    .child(
328                                        Label::new(request.lsp_name.clone())
329                                            .size(LabelSize::Default),
330                                    ),
331                            )
332                            .child(
333                                ui::IconButton::new("close", ui::IconName::Close)
334                                    .on_click(cx.listener(|_, _, cx| cx.emit(gpui::DismissEvent))),
335                            ),
336                    )
337                    .child(
338                        v_flex()
339                            .child(
340                                h_flex().absolute().right_0().rounded_md().child(
341                                    ui::IconButton::new("copy", ui::IconName::Copy)
342                                        .on_click({
343                                            let message = request.message.clone();
344                                            move |_, cx| {
345                                                cx.write_to_clipboard(ClipboardItem::new(
346                                                    message.clone(),
347                                                ))
348                                            }
349                                        })
350                                        .tooltip(|cx| Tooltip::text("Copy", cx))
351                                        .visible_on_hover(""),
352                                ),
353                            )
354                            .child(Label::new(request.message.to_string()).size(LabelSize::Small)),
355                    )
356                    .children(request.actions.iter().enumerate().map(|(ix, action)| {
357                        let this_handle = cx.view().clone();
358                        ui::Button::new(ix, action.title.clone())
359                            .size(ButtonSize::Large)
360                            .on_click(move |_, cx| {
361                                let this_handle = this_handle.clone();
362                                cx.spawn(|cx| async move {
363                                    LanguageServerPrompt::select_option(this_handle, ix, cx).await
364                                })
365                                .detach()
366                            })
367                    })),
368            )
369    }
370}
371
372impl EventEmitter<DismissEvent> for LanguageServerPrompt {}
373
374pub struct ErrorMessagePrompt {
375    message: SharedString,
376    label_and_url_button: Option<(SharedString, SharedString)>,
377}
378
379impl ErrorMessagePrompt {
380    pub fn new<S>(message: S) -> Self
381    where
382        S: Into<SharedString>,
383    {
384        Self {
385            message: message.into(),
386            label_and_url_button: None,
387        }
388    }
389
390    pub fn with_link_button<S>(mut self, label: S, url: S) -> Self
391    where
392        S: Into<SharedString>,
393    {
394        self.label_and_url_button = Some((label.into(), url.into()));
395        self
396    }
397}
398
399impl Render for ErrorMessagePrompt {
400    fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
401        h_flex()
402            .id("error_message_prompt_notification")
403            .occlude()
404            .elevation_3(cx)
405            .items_start()
406            .justify_between()
407            .p_2()
408            .gap_2()
409            .w_full()
410            .child(
411                v_flex()
412                    .w_full()
413                    .child(
414                        h_flex()
415                            .w_full()
416                            .justify_between()
417                            .child(
418                                svg()
419                                    .size(cx.text_style().font_size)
420                                    .flex_none()
421                                    .mr_2()
422                                    .mt(px(-2.0))
423                                    .map(|icon| {
424                                        icon.path(IconName::ExclamationTriangle.path())
425                                            .text_color(Color::Error.color(cx))
426                                    }),
427                            )
428                            .child(
429                                ui::IconButton::new("close", ui::IconName::Close)
430                                    .on_click(cx.listener(|_, _, cx| cx.emit(gpui::DismissEvent))),
431                            ),
432                    )
433                    .child(
434                        div()
435                            .max_w_80()
436                            .child(Label::new(self.message.clone()).size(LabelSize::Small)),
437                    )
438                    .when_some(self.label_and_url_button.clone(), |elm, (label, url)| {
439                        elm.child(
440                            div().mt_2().child(
441                                ui::Button::new("error_message_prompt_notification_button", label)
442                                    .on_click(move |_, cx| cx.open_url(&url)),
443                            ),
444                        )
445                    }),
446            )
447    }
448}
449
450impl EventEmitter<DismissEvent> for ErrorMessagePrompt {}
451
452pub mod simple_message_notification {
453    use gpui::{
454        div, DismissEvent, EventEmitter, InteractiveElement, ParentElement, Render, SharedString,
455        StatefulInteractiveElement, Styled, ViewContext,
456    };
457    use std::sync::Arc;
458    use ui::prelude::*;
459    use ui::{h_flex, v_flex, Button, Icon, IconName, Label, StyledExt};
460
461    pub struct MessageNotification {
462        message: SharedString,
463        on_click: Option<Arc<dyn Fn(&mut ViewContext<Self>)>>,
464        click_message: Option<SharedString>,
465        secondary_click_message: Option<SharedString>,
466        secondary_on_click: Option<Arc<dyn Fn(&mut ViewContext<Self>)>>,
467    }
468
469    impl EventEmitter<DismissEvent> for MessageNotification {}
470
471    impl MessageNotification {
472        pub fn new<S>(message: S) -> MessageNotification
473        where
474            S: Into<SharedString>,
475        {
476            Self {
477                message: message.into(),
478                on_click: None,
479                click_message: None,
480                secondary_on_click: None,
481                secondary_click_message: None,
482            }
483        }
484
485        pub fn with_click_message<S>(mut self, message: S) -> Self
486        where
487            S: Into<SharedString>,
488        {
489            self.click_message = Some(message.into());
490            self
491        }
492
493        pub fn on_click<F>(mut self, on_click: F) -> Self
494        where
495            F: 'static + Fn(&mut ViewContext<Self>),
496        {
497            self.on_click = Some(Arc::new(on_click));
498            self
499        }
500
501        pub fn with_secondary_click_message<S>(mut self, message: S) -> Self
502        where
503            S: Into<SharedString>,
504        {
505            self.secondary_click_message = Some(message.into());
506            self
507        }
508
509        pub fn on_secondary_click<F>(mut self, on_click: F) -> Self
510        where
511            F: 'static + Fn(&mut ViewContext<Self>),
512        {
513            self.secondary_on_click = Some(Arc::new(on_click));
514            self
515        }
516
517        pub fn dismiss(&mut self, cx: &mut ViewContext<Self>) {
518            cx.emit(DismissEvent);
519        }
520    }
521
522    impl Render for MessageNotification {
523        fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
524            v_flex()
525                .elevation_3(cx)
526                .p_4()
527                .child(
528                    h_flex()
529                        .justify_between()
530                        .child(div().max_w_80().child(Label::new(self.message.clone())))
531                        .child(
532                            div()
533                                .id("cancel")
534                                .child(Icon::new(IconName::Close))
535                                .cursor_pointer()
536                                .on_click(cx.listener(|this, _, cx| this.dismiss(cx))),
537                        ),
538                )
539                .child(
540                    h_flex()
541                        .gap_3()
542                        .children(self.click_message.iter().map(|message| {
543                            Button::new(message.clone(), message.clone()).on_click(cx.listener(
544                                |this, _, cx| {
545                                    if let Some(on_click) = this.on_click.as_ref() {
546                                        (on_click)(cx)
547                                    };
548                                    this.dismiss(cx)
549                                },
550                            ))
551                        }))
552                        .children(self.secondary_click_message.iter().map(|message| {
553                            Button::new(message.clone(), message.clone())
554                                .style(ButtonStyle::Filled)
555                                .on_click(cx.listener(|this, _, cx| {
556                                    if let Some(on_click) = this.secondary_on_click.as_ref() {
557                                        (on_click)(cx)
558                                    };
559                                    this.dismiss(cx)
560                                }))
561                        })),
562                )
563        }
564    }
565}
566
567pub trait NotifyResultExt {
568    type Ok;
569
570    fn notify_err(
571        self,
572        workspace: &mut Workspace,
573        cx: &mut ViewContext<Workspace>,
574    ) -> Option<Self::Ok>;
575
576    fn notify_async_err(self, cx: &mut AsyncWindowContext) -> Option<Self::Ok>;
577}
578
579impl<T, E> NotifyResultExt for Result<T, E>
580where
581    E: std::fmt::Debug + std::fmt::Display,
582{
583    type Ok = T;
584
585    fn notify_err(self, workspace: &mut Workspace, cx: &mut ViewContext<Workspace>) -> Option<T> {
586        match self {
587            Ok(value) => Some(value),
588            Err(err) => {
589                log::error!("TODO {err:?}");
590                workspace.show_error(&err, cx);
591                None
592            }
593        }
594    }
595
596    fn notify_async_err(self, cx: &mut AsyncWindowContext) -> Option<T> {
597        match self {
598            Ok(value) => Some(value),
599            Err(err) => {
600                log::error!("{err:?}");
601                cx.update_root(|view, cx| {
602                    if let Ok(workspace) = view.downcast::<Workspace>() {
603                        workspace.update(cx, |workspace, cx| workspace.show_error(&err, cx))
604                    }
605                })
606                .ok();
607                None
608            }
609        }
610    }
611}
612
613pub trait NotifyTaskExt {
614    fn detach_and_notify_err(self, cx: &mut WindowContext);
615}
616
617impl<R, E> NotifyTaskExt for Task<Result<R, E>>
618where
619    E: std::fmt::Debug + std::fmt::Display + Sized + 'static,
620    R: 'static,
621{
622    fn detach_and_notify_err(self, cx: &mut WindowContext) {
623        cx.spawn(|mut cx| async move { self.await.notify_async_err(&mut cx) })
624            .detach();
625    }
626}
627
628pub trait DetachAndPromptErr<R> {
629    fn prompt_err(
630        self,
631        msg: &str,
632        cx: &mut WindowContext,
633        f: impl FnOnce(&anyhow::Error, &mut WindowContext) -> Option<String> + 'static,
634    ) -> Task<Option<R>>;
635
636    fn detach_and_prompt_err(
637        self,
638        msg: &str,
639        cx: &mut WindowContext,
640        f: impl FnOnce(&anyhow::Error, &mut WindowContext) -> Option<String> + 'static,
641    );
642}
643
644impl<R> DetachAndPromptErr<R> for Task<anyhow::Result<R>>
645where
646    R: 'static,
647{
648    fn prompt_err(
649        self,
650        msg: &str,
651        cx: &mut WindowContext,
652        f: impl FnOnce(&anyhow::Error, &mut WindowContext) -> Option<String> + 'static,
653    ) -> Task<Option<R>> {
654        let msg = msg.to_owned();
655        cx.spawn(|mut cx| async move {
656            let result = self.await;
657            if let Err(err) = result.as_ref() {
658                log::error!("{err:?}");
659                if let Ok(prompt) = cx.update(|cx| {
660                    let detail = f(&err, cx).unwrap_or_else(|| format!("{err}. Please try again."));
661                    cx.prompt(PromptLevel::Critical, &msg, Some(&detail), &["Ok"])
662                }) {
663                    prompt.await.ok();
664                }
665                return None;
666            }
667            return Some(result.unwrap());
668        })
669    }
670
671    fn detach_and_prompt_err(
672        self,
673        msg: &str,
674        cx: &mut WindowContext,
675        f: impl FnOnce(&anyhow::Error, &mut WindowContext) -> Option<String> + 'static,
676    ) {
677        self.prompt_err(msg, cx, f).detach();
678    }
679}