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