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| {
164                cx.new_view(|_cx| {
165                    simple_message_notification::MessageNotification::new(format!("Error: {err:#}"))
166                })
167            },
168        );
169    }
170
171    pub fn dismiss_notification(&mut self, id: &NotificationId, cx: &mut ViewContext<Self>) {
172        self.dismiss_notification_internal(id, cx)
173    }
174
175    pub fn show_toast(&mut self, toast: Toast, cx: &mut ViewContext<Self>) {
176        self.dismiss_notification(&toast.id, cx);
177        self.show_notification(toast.id.clone(), cx, |cx| {
178            cx.new_view(|_cx| match toast.on_click.as_ref() {
179                Some((click_msg, on_click)) => {
180                    let on_click = on_click.clone();
181                    simple_message_notification::MessageNotification::new(toast.msg.clone())
182                        .with_click_message(click_msg.clone())
183                        .on_click(move |cx| on_click(cx))
184                }
185                None => simple_message_notification::MessageNotification::new(toast.msg.clone()),
186            })
187        });
188        if toast.autohide {
189            cx.spawn(|workspace, mut cx| async move {
190                cx.background_executor()
191                    .timer(Duration::from_millis(5000))
192                    .await;
193                workspace
194                    .update(&mut cx, |workspace, cx| {
195                        workspace.dismiss_toast(&toast.id, cx)
196                    })
197                    .ok();
198            })
199            .detach();
200        }
201    }
202
203    pub fn dismiss_toast(&mut self, id: &NotificationId, cx: &mut ViewContext<Self>) {
204        self.dismiss_notification(id, cx);
205    }
206
207    pub fn clear_all_notifications(&mut self, cx: &mut ViewContext<Self>) {
208        self.notifications.clear();
209        cx.notify();
210    }
211
212    fn dismiss_notification_internal(&mut self, id: &NotificationId, cx: &mut ViewContext<Self>) {
213        self.notifications.retain(|(existing_id, _)| {
214            if existing_id == id {
215                cx.notify();
216                false
217            } else {
218                true
219            }
220        });
221    }
222}
223
224pub struct LanguageServerPrompt {
225    request: Option<project::LanguageServerPromptRequest>,
226    scroll_handle: ScrollHandle,
227}
228
229impl LanguageServerPrompt {
230    pub fn new(request: project::LanguageServerPromptRequest) -> Self {
231        Self {
232            request: Some(request),
233            scroll_handle: ScrollHandle::new(),
234        }
235    }
236
237    async fn select_option(this: View<Self>, ix: usize, mut cx: AsyncWindowContext) {
238        util::maybe!(async move {
239            let potential_future = this.update(&mut cx, |this, _| {
240                this.request.take().map(|request| request.respond(ix))
241            });
242
243            potential_future? // App Closed
244                .ok_or_else(|| anyhow::anyhow!("Response already sent"))?
245                .await
246                .ok_or_else(|| anyhow::anyhow!("Stream already closed"))?;
247
248            this.update(&mut cx, |_, cx| cx.emit(DismissEvent))?;
249
250            anyhow::Ok(())
251        })
252        .await
253        .log_err();
254    }
255}
256
257impl Render for LanguageServerPrompt {
258    fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
259        let Some(request) = &self.request else {
260            return div().id("language_server_prompt_notification");
261        };
262
263        h_flex()
264            .id("language_server_prompt_notification")
265            .occlude()
266            .elevation_3(cx)
267            .items_start()
268            .justify_between()
269            .p_2()
270            .gap_2()
271            .w_full()
272            .max_h(vh(0.8, cx))
273            .overflow_y_scroll()
274            .track_scroll(&self.scroll_handle)
275            .group("")
276            .child(
277                v_flex()
278                    .w_full()
279                    .overflow_hidden()
280                    .child(
281                        h_flex()
282                            .w_full()
283                            .justify_between()
284                            .child(
285                                h_flex()
286                                    .flex_grow()
287                                    .children(
288                                        match request.level {
289                                            PromptLevel::Info => None,
290                                            PromptLevel::Warning => {
291                                                Some(DiagnosticSeverity::WARNING)
292                                            }
293                                            PromptLevel::Critical => {
294                                                Some(DiagnosticSeverity::ERROR)
295                                            }
296                                        }
297                                        .map(|severity| {
298                                            svg()
299                                                .size(cx.text_style().font_size)
300                                                .flex_none()
301                                                .mr_1()
302                                                .mt(px(-2.0))
303                                                .map(|icon| {
304                                                    if severity == DiagnosticSeverity::ERROR {
305                                                        icon.path(
306                                                            IconName::ExclamationTriangle.path(),
307                                                        )
308                                                        .text_color(Color::Error.color(cx))
309                                                    } else {
310                                                        icon.path(
311                                                            IconName::ExclamationTriangle.path(),
312                                                        )
313                                                        .text_color(Color::Warning.color(cx))
314                                                    }
315                                                })
316                                        }),
317                                    )
318                                    .child(
319                                        Label::new(request.lsp_name.clone())
320                                            .size(LabelSize::Default),
321                                    ),
322                            )
323                            .child(
324                                ui::IconButton::new("close", ui::IconName::Close)
325                                    .on_click(cx.listener(|_, _, cx| cx.emit(gpui::DismissEvent))),
326                            ),
327                    )
328                    .child(
329                        v_flex()
330                            .child(
331                                h_flex().absolute().right_0().rounded_md().child(
332                                    ui::IconButton::new("copy", ui::IconName::Copy)
333                                        .on_click({
334                                            let message = request.message.clone();
335                                            move |_, cx| {
336                                                cx.write_to_clipboard(ClipboardItem::new(
337                                                    message.clone(),
338                                                ))
339                                            }
340                                        })
341                                        .tooltip(|cx| Tooltip::text("Copy", cx))
342                                        .visible_on_hover(""),
343                                ),
344                            )
345                            .child(Label::new(request.message.to_string()).size(LabelSize::Small)),
346                    )
347                    .children(request.actions.iter().enumerate().map(|(ix, action)| {
348                        let this_handle = cx.view().clone();
349                        ui::Button::new(ix, action.title.clone())
350                            .size(ButtonSize::Large)
351                            .on_click(move |_, cx| {
352                                let this_handle = this_handle.clone();
353                                cx.spawn(|cx| async move {
354                                    LanguageServerPrompt::select_option(this_handle, ix, cx).await
355                                })
356                                .detach()
357                            })
358                    })),
359            )
360    }
361}
362
363impl EventEmitter<DismissEvent> for LanguageServerPrompt {}
364
365pub mod simple_message_notification {
366    use gpui::{
367        div, DismissEvent, EventEmitter, InteractiveElement, ParentElement, Render, SharedString,
368        StatefulInteractiveElement, Styled, ViewContext,
369    };
370    use std::sync::Arc;
371    use ui::prelude::*;
372    use ui::{h_flex, v_flex, Button, Icon, IconName, Label, StyledExt};
373
374    pub struct MessageNotification {
375        message: SharedString,
376        on_click: Option<Arc<dyn Fn(&mut ViewContext<Self>)>>,
377        click_message: Option<SharedString>,
378        secondary_click_message: Option<SharedString>,
379        secondary_on_click: Option<Arc<dyn Fn(&mut ViewContext<Self>)>>,
380    }
381
382    impl EventEmitter<DismissEvent> for MessageNotification {}
383
384    impl MessageNotification {
385        pub fn new<S>(message: S) -> MessageNotification
386        where
387            S: Into<SharedString>,
388        {
389            Self {
390                message: message.into(),
391                on_click: None,
392                click_message: None,
393                secondary_on_click: None,
394                secondary_click_message: None,
395            }
396        }
397
398        pub fn with_click_message<S>(mut self, message: S) -> Self
399        where
400            S: Into<SharedString>,
401        {
402            self.click_message = Some(message.into());
403            self
404        }
405
406        pub fn on_click<F>(mut self, on_click: F) -> Self
407        where
408            F: 'static + Fn(&mut ViewContext<Self>),
409        {
410            self.on_click = Some(Arc::new(on_click));
411            self
412        }
413
414        pub fn with_secondary_click_message<S>(mut self, message: S) -> Self
415        where
416            S: Into<SharedString>,
417        {
418            self.secondary_click_message = Some(message.into());
419            self
420        }
421
422        pub fn on_secondary_click<F>(mut self, on_click: F) -> Self
423        where
424            F: 'static + Fn(&mut ViewContext<Self>),
425        {
426            self.secondary_on_click = Some(Arc::new(on_click));
427            self
428        }
429
430        pub fn dismiss(&mut self, cx: &mut ViewContext<Self>) {
431            cx.emit(DismissEvent);
432        }
433    }
434
435    impl Render for MessageNotification {
436        fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
437            v_flex()
438                .elevation_3(cx)
439                .p_4()
440                .child(
441                    h_flex()
442                        .justify_between()
443                        .child(div().max_w_80().child(Label::new(self.message.clone())))
444                        .child(
445                            div()
446                                .id("cancel")
447                                .child(Icon::new(IconName::Close))
448                                .cursor_pointer()
449                                .on_click(cx.listener(|this, _, cx| this.dismiss(cx))),
450                        ),
451                )
452                .child(
453                    h_flex()
454                        .gap_3()
455                        .children(self.click_message.iter().map(|message| {
456                            Button::new(message.clone(), message.clone()).on_click(cx.listener(
457                                |this, _, cx| {
458                                    if let Some(on_click) = this.on_click.as_ref() {
459                                        (on_click)(cx)
460                                    };
461                                    this.dismiss(cx)
462                                },
463                            ))
464                        }))
465                        .children(self.secondary_click_message.iter().map(|message| {
466                            Button::new(message.clone(), message.clone())
467                                .style(ButtonStyle::Filled)
468                                .on_click(cx.listener(|this, _, cx| {
469                                    if let Some(on_click) = this.secondary_on_click.as_ref() {
470                                        (on_click)(cx)
471                                    };
472                                    this.dismiss(cx)
473                                }))
474                        })),
475                )
476        }
477    }
478}
479
480pub trait NotifyResultExt {
481    type Ok;
482
483    fn notify_err(
484        self,
485        workspace: &mut Workspace,
486        cx: &mut ViewContext<Workspace>,
487    ) -> Option<Self::Ok>;
488
489    fn notify_async_err(self, cx: &mut AsyncWindowContext) -> Option<Self::Ok>;
490}
491
492impl<T, E> NotifyResultExt for Result<T, E>
493where
494    E: std::fmt::Debug + std::fmt::Display,
495{
496    type Ok = T;
497
498    fn notify_err(self, workspace: &mut Workspace, cx: &mut ViewContext<Workspace>) -> Option<T> {
499        match self {
500            Ok(value) => Some(value),
501            Err(err) => {
502                log::error!("TODO {err:?}");
503                workspace.show_error(&err, cx);
504                None
505            }
506        }
507    }
508
509    fn notify_async_err(self, cx: &mut AsyncWindowContext) -> Option<T> {
510        match self {
511            Ok(value) => Some(value),
512            Err(err) => {
513                log::error!("{err:?}");
514                cx.update_root(|view, cx| {
515                    if let Ok(workspace) = view.downcast::<Workspace>() {
516                        workspace.update(cx, |workspace, cx| workspace.show_error(&err, cx))
517                    }
518                })
519                .ok();
520                None
521            }
522        }
523    }
524}
525
526pub trait NotifyTaskExt {
527    fn detach_and_notify_err(self, cx: &mut WindowContext);
528}
529
530impl<R, E> NotifyTaskExt for Task<Result<R, E>>
531where
532    E: std::fmt::Debug + std::fmt::Display + Sized + 'static,
533    R: 'static,
534{
535    fn detach_and_notify_err(self, cx: &mut WindowContext) {
536        cx.spawn(|mut cx| async move { self.await.notify_async_err(&mut cx) })
537            .detach();
538    }
539}
540
541pub trait DetachAndPromptErr {
542    fn prompt_err(
543        self,
544        msg: &str,
545        cx: &mut WindowContext,
546        f: impl FnOnce(&anyhow::Error, &mut WindowContext) -> Option<String> + 'static,
547    ) -> Task<()>;
548
549    fn detach_and_prompt_err(
550        self,
551        msg: &str,
552        cx: &mut WindowContext,
553        f: impl FnOnce(&anyhow::Error, &mut WindowContext) -> Option<String> + 'static,
554    );
555}
556
557impl<R> DetachAndPromptErr for Task<anyhow::Result<R>>
558where
559    R: 'static,
560{
561    fn prompt_err(
562        self,
563        msg: &str,
564        cx: &mut WindowContext,
565        f: impl FnOnce(&anyhow::Error, &mut WindowContext) -> Option<String> + 'static,
566    ) -> Task<()> {
567        let msg = msg.to_owned();
568        cx.spawn(|mut cx| async move {
569            if let Err(err) = self.await {
570                log::error!("{err:?}");
571                if let Ok(prompt) = cx.update(|cx| {
572                    let detail = f(&err, cx).unwrap_or_else(|| format!("{err}. Please try again."));
573                    cx.prompt(PromptLevel::Critical, &msg, Some(&detail), &["Ok"])
574                }) {
575                    prompt.await.ok();
576                }
577            }
578        })
579    }
580
581    fn detach_and_prompt_err(
582        self,
583        msg: &str,
584        cx: &mut WindowContext,
585        f: impl FnOnce(&anyhow::Error, &mut WindowContext) -> Option<String> + 'static,
586    ) {
587        self.prompt_err(msg, cx, f).detach();
588    }
589}