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 .p_3()
506 .gap_3()
507 .elevation_3(cx)
508 .child(
509 h_flex()
510 .gap_4()
511 .justify_between()
512 .items_start()
513 .child(
514 v_flex()
515 .gap_0p5()
516 .when_some(self.title.clone(), |element, title| {
517 element.child(Label::new(title))
518 })
519 .child(div().max_w_96().child((self.build_content)(window, cx))),
520 )
521 .when(self.show_close_button, |this| {
522 this.child(
523 IconButton::new("close", IconName::Close)
524 .on_click(cx.listener(|this, _, _, cx| this.dismiss(cx))),
525 )
526 }),
527 )
528 .child(
529 h_flex()
530 .gap_1()
531 .children(self.click_message.iter().map(|message| {
532 Button::new(message.clone(), message.clone())
533 .label_size(LabelSize::Small)
534 .icon(IconName::Check)
535 .icon_position(IconPosition::Start)
536 .icon_size(IconSize::Small)
537 .icon_color(Color::Success)
538 .on_click(cx.listener(|this, _, window, cx| {
539 if let Some(on_click) = this.on_click.as_ref() {
540 (on_click)(window, cx)
541 };
542 this.dismiss(cx)
543 }))
544 }))
545 .children(self.secondary_click_message.iter().map(|message| {
546 Button::new(message.clone(), message.clone())
547 .label_size(LabelSize::Small)
548 .icon(IconName::Close)
549 .icon_position(IconPosition::Start)
550 .icon_size(IconSize::Small)
551 .icon_color(Color::Error)
552 .on_click(cx.listener(|this, _, window, cx| {
553 if let Some(on_click) = this.secondary_on_click.as_ref() {
554 (on_click)(window, cx)
555 };
556 this.dismiss(cx)
557 }))
558 }))
559 .child(
560 h_flex()
561 .w_full()
562 .gap_1()
563 .justify_end()
564 .children(self.tertiary_click_message.iter().map(|message| {
565 Button::new(message.clone(), message.clone())
566 .label_size(LabelSize::Small)
567 .on_click(cx.listener(|this, _, window, cx| {
568 if let Some(on_click) = this.tertiary_on_click.as_ref()
569 {
570 (on_click)(window, cx)
571 };
572 this.dismiss(cx)
573 }))
574 }))
575 .children(
576 self.more_info_message
577 .iter()
578 .zip(self.more_info_url.iter())
579 .map(|(message, url)| {
580 let url = url.clone();
581 Button::new(message.clone(), message.clone())
582 .label_size(LabelSize::Small)
583 .icon(IconName::ArrowUpRight)
584 .icon_size(IconSize::Indicator)
585 .icon_color(Color::Muted)
586 .on_click(cx.listener(move |_, _, _, cx| {
587 cx.open_url(&url);
588 }))
589 }),
590 ),
591 ),
592 )
593 }
594 }
595}
596
597static GLOBAL_APP_NOTIFICATIONS: LazyLock<Mutex<AppNotifications>> = LazyLock::new(|| {
598 Mutex::new(AppNotifications {
599 app_notifications: Vec::new(),
600 })
601});
602
603/// Stores app notifications so that they can be shown in new workspaces.
604struct AppNotifications {
605 app_notifications: Vec<(
606 NotificationId,
607 Arc<dyn Fn(&mut Context<Workspace>) -> AnyView + Send + Sync>,
608 )>,
609}
610
611impl Global for AppNotifications {}
612
613impl AppNotifications {
614 pub fn insert(
615 &mut self,
616 id: NotificationId,
617 build_notification: Arc<dyn Fn(&mut Context<Workspace>) -> AnyView + Send + Sync>,
618 ) {
619 self.remove(&id);
620 self.app_notifications.push((id, build_notification))
621 }
622
623 pub fn remove(&mut self, id: &NotificationId) {
624 self.app_notifications
625 .retain(|(existing_id, _)| existing_id != id);
626 }
627}
628
629/// Shows a notification in all workspaces. New workspaces will also receive the notification - this
630/// is particularly to handle notifications that occur on initialization before any workspaces
631/// exist. If the notification is dismissed within any workspace, it will be removed from all.
632pub fn show_app_notification<V: Notification + 'static>(
633 id: NotificationId,
634 cx: &mut App,
635 build_notification: impl Fn(&mut Context<Workspace>) -> Entity<V> + 'static + Send + Sync,
636) {
637 // Defer notification creation so that windows on the stack can be returned to GPUI
638 cx.defer(move |cx| {
639 // Handle dismiss events by removing the notification from all workspaces.
640 let build_notification: Arc<dyn Fn(&mut Context<Workspace>) -> AnyView + Send + Sync> =
641 Arc::new({
642 let id = id.clone();
643 move |cx| {
644 let notification = build_notification(cx);
645 cx.subscribe(¬ification, {
646 let id = id.clone();
647 move |_, _, _: &DismissEvent, cx| {
648 dismiss_app_notification(&id, cx);
649 }
650 })
651 .detach();
652 notification.into()
653 }
654 });
655
656 // Store the notification so that new workspaces also receive it.
657 GLOBAL_APP_NOTIFICATIONS
658 .lock()
659 .insert(id.clone(), build_notification.clone());
660
661 for window in cx.windows() {
662 if let Some(workspace_window) = window.downcast::<Workspace>() {
663 workspace_window
664 .update(cx, |workspace, _window, cx| {
665 workspace.show_notification_without_handling_dismiss_events(
666 &id,
667 cx,
668 |cx| build_notification(cx),
669 );
670 })
671 .ok(); // Doesn't matter if the windows are dropped
672 }
673 }
674 });
675}
676
677pub fn dismiss_app_notification(id: &NotificationId, cx: &mut App) {
678 let id = id.clone();
679 // Defer notification dismissal so that windows on the stack can be returned to GPUI
680 cx.defer(move |cx| {
681 GLOBAL_APP_NOTIFICATIONS.lock().remove(&id);
682 for window in cx.windows() {
683 if let Some(workspace_window) = window.downcast::<Workspace>() {
684 let id = id.clone();
685 workspace_window
686 .update(cx, |workspace, _window, cx| {
687 workspace.dismiss_notification(&id, cx)
688 })
689 .ok();
690 }
691 }
692 });
693}
694
695pub trait NotifyResultExt {
696 type Ok;
697
698 fn notify_err(self, workspace: &mut Workspace, cx: &mut Context<Workspace>)
699 -> Option<Self::Ok>;
700
701 fn notify_async_err(self, cx: &mut AsyncWindowContext) -> Option<Self::Ok>;
702
703 /// Notifies the active workspace if there is one, otherwise notifies all workspaces.
704 fn notify_app_err(self, cx: &mut App) -> Option<Self::Ok>;
705}
706
707impl<T, E> NotifyResultExt for std::result::Result<T, E>
708where
709 E: std::fmt::Debug + std::fmt::Display,
710{
711 type Ok = T;
712
713 fn notify_err(self, workspace: &mut Workspace, cx: &mut Context<Workspace>) -> Option<T> {
714 match self {
715 Ok(value) => Some(value),
716 Err(err) => {
717 log::error!("Showing error notification in workspace: {err:?}");
718 workspace.show_error(&err, cx);
719 None
720 }
721 }
722 }
723
724 fn notify_async_err(self, cx: &mut AsyncWindowContext) -> Option<T> {
725 match self {
726 Ok(value) => Some(value),
727 Err(err) => {
728 log::error!("{err:?}");
729 cx.update_root(|view, _, cx| {
730 if let Ok(workspace) = view.downcast::<Workspace>() {
731 workspace.update(cx, |workspace, cx| workspace.show_error(&err, cx))
732 }
733 })
734 .ok();
735 None
736 }
737 }
738 }
739
740 fn notify_app_err(self, cx: &mut App) -> Option<T> {
741 match self {
742 Ok(value) => Some(value),
743 Err(err) => {
744 let message: SharedString = format!("Error: {err}").into();
745 log::error!("Showing error notification in app: {message}");
746 show_app_notification(workspace_error_notification_id(), cx, {
747 let message = message.clone();
748 move |cx| {
749 cx.new({
750 let message = message.clone();
751 move |_cx| ErrorMessagePrompt::new(message)
752 })
753 }
754 });
755
756 None
757 }
758 }
759 }
760}
761
762pub trait NotifyTaskExt {
763 fn detach_and_notify_err(self, window: &mut Window, cx: &mut App);
764}
765
766impl<R, E> NotifyTaskExt for Task<std::result::Result<R, E>>
767where
768 E: std::fmt::Debug + std::fmt::Display + Sized + 'static,
769 R: 'static,
770{
771 fn detach_and_notify_err(self, window: &mut Window, cx: &mut App) {
772 window
773 .spawn(
774 cx,
775 |mut cx| async move { self.await.notify_async_err(&mut cx) },
776 )
777 .detach();
778 }
779}
780
781pub trait DetachAndPromptErr<R> {
782 fn prompt_err(
783 self,
784 msg: &str,
785 window: &Window,
786 cx: &App,
787 f: impl FnOnce(&anyhow::Error, &mut Window, &mut App) -> Option<String> + 'static,
788 ) -> Task<Option<R>>;
789
790 fn detach_and_prompt_err(
791 self,
792 msg: &str,
793 window: &Window,
794 cx: &App,
795 f: impl FnOnce(&anyhow::Error, &mut Window, &mut App) -> Option<String> + 'static,
796 );
797}
798
799impl<R> DetachAndPromptErr<R> for Task<anyhow::Result<R>>
800where
801 R: 'static,
802{
803 fn prompt_err(
804 self,
805 msg: &str,
806 window: &Window,
807 cx: &App,
808 f: impl FnOnce(&anyhow::Error, &mut Window, &mut App) -> Option<String> + 'static,
809 ) -> Task<Option<R>> {
810 let msg = msg.to_owned();
811 window.spawn(cx, |mut cx| async move {
812 let result = self.await;
813 if let Err(err) = result.as_ref() {
814 log::error!("{err:?}");
815 if let Ok(prompt) = cx.update(|window, cx| {
816 let detail =
817 f(err, window, cx).unwrap_or_else(|| format!("{err}. Please try again."));
818 window.prompt(PromptLevel::Critical, &msg, Some(&detail), &["Ok"], cx)
819 }) {
820 prompt.await.ok();
821 }
822 return None;
823 }
824 Some(result.unwrap())
825 })
826 }
827
828 fn detach_and_prompt_err(
829 self,
830 msg: &str,
831 window: &Window,
832 cx: &App,
833 f: impl FnOnce(&anyhow::Error, &mut Window, &mut App) -> Option<String> + 'static,
834 ) {
835 self.prompt_err(msg, window, cx, f).detach();
836 }
837}