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