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