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