1use crate::{MultiWorkspace, SuppressNotification, Toast, Workspace};
2use anyhow::Context as _;
3use gpui::{
4 AnyEntity, AnyView, App, AppContext as _, AsyncApp, AsyncWindowContext, ClickEvent, Context,
5 DismissEvent, Entity, EventEmitter, FocusHandle, Focusable, PromptLevel, Render, ScrollHandle,
6 Task, TextStyleRefinement, UnderlineStyle, WeakEntity, svg,
7};
8use markdown::{Markdown, MarkdownElement, MarkdownStyle};
9use parking_lot::Mutex;
10use project::project_settings::ProjectSettings;
11use settings::Settings;
12use theme::ThemeSettings;
13
14use std::ops::Deref;
15use std::sync::{Arc, LazyLock};
16use std::{any::TypeId, time::Duration};
17use ui::{CopyButton, Tooltip, prelude::*};
18use util::ResultExt;
19
20#[derive(Default)]
21pub struct Notifications {
22 notifications: Vec<(NotificationId, AnyView)>,
23}
24
25impl Deref for Notifications {
26 type Target = Vec<(NotificationId, AnyView)>;
27
28 fn deref(&self) -> &Self::Target {
29 &self.notifications
30 }
31}
32
33impl std::ops::DerefMut for Notifications {
34 fn deref_mut(&mut self) -> &mut Self::Target {
35 &mut self.notifications
36 }
37}
38
39#[derive(Debug, Eq, PartialEq, Clone, Hash)]
40pub enum NotificationId {
41 Unique(TypeId),
42 Composite(TypeId, ElementId),
43 Named(SharedString),
44}
45
46impl NotificationId {
47 /// Returns a unique [`NotificationId`] for the given type.
48 pub const fn unique<T: 'static>() -> Self {
49 Self::Unique(TypeId::of::<T>())
50 }
51
52 /// Returns a [`NotificationId`] for the given type that is also identified
53 /// by the provided ID.
54 pub fn composite<T: 'static>(id: impl Into<ElementId>) -> Self {
55 Self::Composite(TypeId::of::<T>(), id.into())
56 }
57
58 /// Builds a `NotificationId` out of the given string.
59 pub fn named(id: SharedString) -> Self {
60 Self::Named(id)
61 }
62}
63
64pub trait Notification:
65 EventEmitter<DismissEvent> + EventEmitter<SuppressEvent> + Focusable + Render
66{
67}
68
69pub struct SuppressEvent;
70
71impl Workspace {
72 #[cfg(any(test, feature = "test-support"))]
73 pub fn notification_ids(&self) -> Vec<NotificationId> {
74 self.notifications
75 .iter()
76 .map(|(id, _)| id)
77 .cloned()
78 .collect()
79 }
80
81 pub fn show_notification<V: Notification>(
82 &mut self,
83 id: NotificationId,
84 cx: &mut Context<Self>,
85 build_notification: impl FnOnce(&mut Context<Self>) -> Entity<V>,
86 ) {
87 self.show_notification_without_handling_dismiss_events(&id, cx, |cx| {
88 let notification = build_notification(cx);
89 cx.subscribe(¬ification, {
90 let id = id.clone();
91 move |this, _, _: &DismissEvent, cx| {
92 this.dismiss_notification(&id, cx);
93 }
94 })
95 .detach();
96 cx.subscribe(¬ification, {
97 let id = id.clone();
98 move |workspace: &mut Workspace, _, _: &SuppressEvent, cx| {
99 workspace.suppress_notification(&id, cx);
100 }
101 })
102 .detach();
103
104 if let Ok(prompt) =
105 AnyEntity::from(notification.clone()).downcast::<LanguageServerPrompt>()
106 {
107 let is_prompt_without_actions = prompt
108 .read(cx)
109 .request
110 .as_ref()
111 .is_some_and(|request| request.actions.is_empty());
112
113 let dismiss_timeout_ms = ProjectSettings::get_global(cx)
114 .global_lsp_settings
115 .notifications
116 .dismiss_timeout_ms;
117
118 if is_prompt_without_actions {
119 if let Some(dismiss_duration_ms) = dismiss_timeout_ms.filter(|&ms| ms > 0) {
120 let task = cx.spawn({
121 let id = id.clone();
122 async move |this, cx| {
123 cx.background_executor()
124 .timer(Duration::from_millis(dismiss_duration_ms))
125 .await;
126 let _ = this.update(cx, |workspace, cx| {
127 workspace.dismiss_notification(&id, cx);
128 });
129 }
130 });
131 prompt.update(cx, |prompt, _| {
132 prompt.dismiss_task = Some(task);
133 });
134 }
135 }
136 }
137 notification.into()
138 });
139 }
140
141 /// Shows a notification in this workspace's window. Caller must handle dismiss.
142 ///
143 /// This exists so that the `build_notification` closures stored for app notifications can
144 /// return `AnyView`. Subscribing to events from an `AnyView` is not supported, so instead that
145 /// responsibility is pushed to the caller where the `V` type is known.
146 pub(crate) fn show_notification_without_handling_dismiss_events(
147 &mut self,
148 id: &NotificationId,
149 cx: &mut Context<Self>,
150 build_notification: impl FnOnce(&mut Context<Self>) -> AnyView,
151 ) {
152 if self.suppressed_notifications.contains(id) {
153 return;
154 }
155 self.dismiss_notification(id, cx);
156 self.notifications
157 .push((id.clone(), build_notification(cx)));
158 cx.notify();
159 }
160
161 pub fn show_error<E>(&mut self, err: &E, cx: &mut Context<Self>)
162 where
163 E: std::fmt::Debug + std::fmt::Display,
164 {
165 self.show_notification(workspace_error_notification_id(), cx, |cx| {
166 cx.new(|cx| ErrorMessagePrompt::new(format!("Error: {err}"), cx))
167 });
168 }
169
170 pub fn show_portal_error(&mut self, err: String, cx: &mut Context<Self>) {
171 struct PortalError;
172
173 self.show_notification(NotificationId::unique::<PortalError>(), cx, |cx| {
174 cx.new(|cx| {
175 ErrorMessagePrompt::new(err.to_string(), cx).with_link_button(
176 "See docs",
177 "https://zed.dev/docs/linux#i-cant-open-any-files",
178 )
179 })
180 });
181 }
182
183 pub fn dismiss_notification(&mut self, id: &NotificationId, cx: &mut Context<Self>) {
184 self.notifications.retain(|(existing_id, _)| {
185 if existing_id == id {
186 cx.notify();
187 false
188 } else {
189 true
190 }
191 });
192 }
193
194 pub fn show_toast(&mut self, toast: Toast, cx: &mut Context<Self>) {
195 self.dismiss_notification(&toast.id, cx);
196 self.show_notification(toast.id.clone(), cx, |cx| {
197 cx.new(|cx| match toast.on_click.as_ref() {
198 Some((click_msg, on_click)) => {
199 let on_click = on_click.clone();
200 simple_message_notification::MessageNotification::new(toast.msg.clone(), cx)
201 .primary_message(click_msg.clone())
202 .primary_on_click(move |window, cx| on_click(window, cx))
203 }
204 None => {
205 simple_message_notification::MessageNotification::new(toast.msg.clone(), cx)
206 }
207 })
208 });
209 if toast.autohide {
210 cx.spawn(async move |workspace, cx| {
211 cx.background_executor()
212 .timer(Duration::from_millis(5000))
213 .await;
214 workspace
215 .update(cx, |workspace, cx| workspace.dismiss_toast(&toast.id, cx))
216 .ok();
217 })
218 .detach();
219 }
220 }
221
222 pub fn dismiss_toast(&mut self, id: &NotificationId, cx: &mut Context<Self>) {
223 self.dismiss_notification(id, cx);
224 }
225
226 pub fn clear_all_notifications(&mut self, cx: &mut Context<Self>) {
227 self.notifications.clear();
228 cx.notify();
229 }
230
231 /// Hide all notifications matching the given ID
232 pub fn suppress_notification(&mut self, id: &NotificationId, cx: &mut Context<Self>) {
233 self.dismiss_notification(id, cx);
234 self.suppressed_notifications.insert(id.clone());
235 }
236
237 pub fn show_initial_notifications(&mut self, cx: &mut Context<Self>) {
238 // Allow absence of the global so that tests don't need to initialize it.
239 let app_notifications = GLOBAL_APP_NOTIFICATIONS
240 .lock()
241 .app_notifications
242 .iter()
243 .cloned()
244 .collect::<Vec<_>>();
245 for (id, build_notification) in app_notifications {
246 self.show_notification_without_handling_dismiss_events(&id, cx, |cx| {
247 build_notification(cx)
248 });
249 }
250 }
251}
252
253pub struct LanguageServerPrompt {
254 focus_handle: FocusHandle,
255 request: Option<project::LanguageServerPromptRequest>,
256 scroll_handle: ScrollHandle,
257 markdown: Entity<Markdown>,
258 dismiss_task: Option<Task<()>>,
259}
260
261impl Focusable for LanguageServerPrompt {
262 fn focus_handle(&self, _cx: &App) -> gpui::FocusHandle {
263 self.focus_handle.clone()
264 }
265}
266
267impl Notification for LanguageServerPrompt {}
268
269impl LanguageServerPrompt {
270 pub fn new(request: project::LanguageServerPromptRequest, cx: &mut App) -> Self {
271 let markdown = cx.new(|cx| Markdown::new(request.message.clone().into(), None, None, cx));
272
273 Self {
274 focus_handle: cx.focus_handle(),
275 request: Some(request),
276 scroll_handle: ScrollHandle::new(),
277 markdown,
278 dismiss_task: None,
279 }
280 }
281
282 async fn select_option(this: Entity<Self>, ix: usize, cx: &mut AsyncWindowContext) {
283 util::maybe!(async move {
284 let potential_future = this.update(cx, |this, _| {
285 this.request.take().map(|request| request.respond(ix))
286 });
287
288 potential_future
289 .context("Response already sent")?
290 .await
291 .context("Stream already closed")?;
292
293 this.update(cx, |this, cx| {
294 this.dismiss_notification(cx);
295 });
296
297 anyhow::Ok(())
298 })
299 .await
300 .log_err();
301 }
302
303 fn dismiss_notification(&mut self, cx: &mut Context<Self>) {
304 self.dismiss_task = None;
305 cx.emit(DismissEvent);
306 }
307}
308
309impl Render for LanguageServerPrompt {
310 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
311 let Some(request) = &self.request else {
312 return div().id("language_server_prompt_notification");
313 };
314
315 let (icon, color) = match request.level {
316 PromptLevel::Info => (IconName::Info, Color::Muted),
317 PromptLevel::Warning => (IconName::Warning, Color::Warning),
318 PromptLevel::Critical => (IconName::XCircle, Color::Error),
319 };
320
321 let suppress = window.modifiers().shift;
322 let (close_id, close_icon) = if suppress {
323 ("suppress", IconName::Minimize)
324 } else {
325 ("close", IconName::Close)
326 };
327
328 div()
329 .id("language_server_prompt_notification")
330 .group("language_server_prompt_notification")
331 .occlude()
332 .w_full()
333 .max_h(vh(0.8, window))
334 .elevation_3(cx)
335 .overflow_y_scroll()
336 .track_scroll(&self.scroll_handle)
337 .on_modifiers_changed(cx.listener(|_, _, _, cx| cx.notify()))
338 .child(
339 v_flex()
340 .p_3()
341 .overflow_hidden()
342 .child(
343 h_flex()
344 .justify_between()
345 .child(
346 h_flex()
347 .gap_2()
348 .child(Icon::new(icon).color(color).size(IconSize::Small))
349 .child(Label::new(request.lsp_name.clone())),
350 )
351 .child(
352 h_flex()
353 .gap_1()
354 .child(
355 CopyButton::new(
356 "copy-description",
357 request.message.clone(),
358 )
359 .tooltip_label("Copy Description"),
360 )
361 .child(
362 IconButton::new(close_id, close_icon)
363 .tooltip(move |_window, cx| {
364 if suppress {
365 Tooltip::with_meta(
366 "Suppress",
367 Some(&SuppressNotification),
368 "Click to close",
369 cx,
370 )
371 } else {
372 Tooltip::with_meta(
373 "Close",
374 Some(&menu::Cancel),
375 "Suppress with shift-click",
376 cx,
377 )
378 }
379 })
380 .on_click(cx.listener(
381 move |this, _: &ClickEvent, _, cx| {
382 if suppress {
383 cx.emit(SuppressEvent);
384 } else {
385 this.dismiss_notification(cx);
386 }
387 },
388 )),
389 ),
390 ),
391 )
392 .child(
393 MarkdownElement::new(self.markdown.clone(), markdown_style(window, cx))
394 .text_size(TextSize::Small.rems(cx))
395 .code_block_renderer(markdown::CodeBlockRenderer::Default {
396 copy_button: false,
397 copy_button_on_hover: false,
398 border: false,
399 })
400 .on_url_click(|link, _, cx| cx.open_url(&link)),
401 )
402 .children(request.actions.iter().enumerate().map(|(ix, action)| {
403 let this_handle = cx.entity();
404 Button::new(ix, action.title.clone())
405 .size(ButtonSize::Large)
406 .on_click(move |_, window, cx| {
407 let this_handle = this_handle.clone();
408 window
409 .spawn(cx, async move |cx| {
410 LanguageServerPrompt::select_option(this_handle, ix, cx)
411 .await
412 })
413 .detach()
414 })
415 })),
416 )
417 }
418}
419
420impl EventEmitter<DismissEvent> for LanguageServerPrompt {}
421impl EventEmitter<SuppressEvent> for LanguageServerPrompt {}
422
423fn workspace_error_notification_id() -> NotificationId {
424 struct WorkspaceErrorNotification;
425 NotificationId::unique::<WorkspaceErrorNotification>()
426}
427
428fn markdown_style(window: &Window, cx: &App) -> MarkdownStyle {
429 let settings = ThemeSettings::get_global(cx);
430 let ui_font_family = settings.ui_font.family.clone();
431 let ui_font_fallbacks = settings.ui_font.fallbacks.clone();
432 let buffer_font_family = settings.buffer_font.family.clone();
433 let buffer_font_fallbacks = settings.buffer_font.fallbacks.clone();
434
435 let mut base_text_style = window.text_style();
436 base_text_style.refine(&TextStyleRefinement {
437 font_family: Some(ui_font_family),
438 font_fallbacks: ui_font_fallbacks,
439 color: Some(cx.theme().colors().text),
440 ..Default::default()
441 });
442
443 MarkdownStyle {
444 base_text_style,
445 selection_background_color: cx.theme().colors().element_selection_background,
446 inline_code: TextStyleRefinement {
447 background_color: Some(cx.theme().colors().editor_background.opacity(0.5)),
448 font_family: Some(buffer_font_family),
449 font_fallbacks: buffer_font_fallbacks,
450 ..Default::default()
451 },
452 link: TextStyleRefinement {
453 underline: Some(UnderlineStyle {
454 thickness: px(1.),
455 color: Some(cx.theme().colors().text_accent),
456 wavy: false,
457 }),
458 ..Default::default()
459 },
460 ..Default::default()
461 }
462}
463
464#[derive(Debug, Clone)]
465pub struct ErrorMessagePrompt {
466 message: SharedString,
467 focus_handle: gpui::FocusHandle,
468 label_and_url_button: Option<(SharedString, SharedString)>,
469}
470
471impl ErrorMessagePrompt {
472 pub fn new<S>(message: S, cx: &mut App) -> Self
473 where
474 S: Into<SharedString>,
475 {
476 Self {
477 message: message.into(),
478 focus_handle: cx.focus_handle(),
479 label_and_url_button: None,
480 }
481 }
482
483 pub fn with_link_button<S>(mut self, label: S, url: S) -> Self
484 where
485 S: Into<SharedString>,
486 {
487 self.label_and_url_button = Some((label.into(), url.into()));
488 self
489 }
490}
491
492impl Render for ErrorMessagePrompt {
493 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
494 h_flex()
495 .id("error_message_prompt_notification")
496 .occlude()
497 .elevation_3(cx)
498 .items_start()
499 .justify_between()
500 .p_2()
501 .gap_2()
502 .w_full()
503 .child(
504 v_flex()
505 .w_full()
506 .child(
507 h_flex()
508 .w_full()
509 .justify_between()
510 .child(
511 svg()
512 .size(window.text_style().font_size)
513 .flex_none()
514 .mr_2()
515 .mt(px(-2.0))
516 .map(|icon| {
517 icon.path(IconName::Warning.path())
518 .text_color(Color::Error.color(cx))
519 }),
520 )
521 .child(
522 h_flex()
523 .gap_1()
524 .child(
525 CopyButton::new("copy-error-message", self.message.clone())
526 .tooltip_label("Copy Error Message"),
527 )
528 .child(
529 ui::IconButton::new("close", ui::IconName::Close).on_click(
530 cx.listener(|_, _, _, cx| cx.emit(DismissEvent)),
531 ),
532 ),
533 ),
534 )
535 .child(
536 div()
537 .id("error_message")
538 .max_w_96()
539 .max_h_40()
540 .overflow_y_scroll()
541 .child(Label::new(self.message.clone()).size(LabelSize::Small)),
542 )
543 .when_some(self.label_and_url_button.clone(), |elm, (label, url)| {
544 elm.child(
545 div().mt_2().child(
546 ui::Button::new("error_message_prompt_notification_button", label)
547 .on_click(move |_, _, cx| cx.open_url(&url)),
548 ),
549 )
550 }),
551 )
552 }
553}
554
555impl Focusable for ErrorMessagePrompt {
556 fn focus_handle(&self, _cx: &App) -> gpui::FocusHandle {
557 self.focus_handle.clone()
558 }
559}
560
561impl EventEmitter<DismissEvent> for ErrorMessagePrompt {}
562impl EventEmitter<SuppressEvent> for ErrorMessagePrompt {}
563
564impl Notification for ErrorMessagePrompt {}
565
566#[derive(IntoElement, RegisterComponent)]
567pub struct NotificationFrame {
568 title: Option<SharedString>,
569 show_suppress_button: bool,
570 show_close_button: bool,
571 close: Option<Box<dyn Fn(&bool, &mut Window, &mut App) + 'static>>,
572 contents: Option<AnyElement>,
573 suffix: Option<AnyElement>,
574}
575
576impl NotificationFrame {
577 pub fn new() -> Self {
578 Self {
579 title: None,
580 contents: None,
581 suffix: None,
582 show_suppress_button: true,
583 show_close_button: true,
584 close: None,
585 }
586 }
587
588 pub fn with_title(mut self, title: Option<impl Into<SharedString>>) -> Self {
589 self.title = title.map(Into::into);
590 self
591 }
592
593 pub fn with_content(self, content: impl IntoElement) -> Self {
594 Self {
595 contents: Some(content.into_any_element()),
596 ..self
597 }
598 }
599
600 /// Determines whether the given notification ID should be suppressible
601 /// Suppressed notifications will not be shown anymore
602 pub fn show_suppress_button(mut self, show: bool) -> Self {
603 self.show_suppress_button = show;
604 self
605 }
606
607 pub fn show_close_button(mut self, show: bool) -> Self {
608 self.show_close_button = show;
609 self
610 }
611
612 pub fn on_close(self, on_close: impl Fn(&bool, &mut Window, &mut App) + 'static) -> Self {
613 Self {
614 close: Some(Box::new(on_close)),
615 ..self
616 }
617 }
618
619 pub fn with_suffix(mut self, suffix: impl IntoElement) -> Self {
620 self.suffix = Some(suffix.into_any_element());
621 self
622 }
623}
624
625impl RenderOnce for NotificationFrame {
626 fn render(mut self, window: &mut Window, cx: &mut App) -> impl IntoElement {
627 let entity = window.current_view();
628 let show_suppress_button = self.show_suppress_button;
629 let suppress = show_suppress_button && window.modifiers().shift;
630 let (close_id, close_icon) = if suppress {
631 ("suppress", IconName::Minimize)
632 } else {
633 ("close", IconName::Close)
634 };
635
636 v_flex()
637 .occlude()
638 .p_3()
639 .gap_2()
640 .elevation_3(cx)
641 .child(
642 h_flex()
643 .gap_4()
644 .justify_between()
645 .items_start()
646 .child(
647 v_flex()
648 .gap_0p5()
649 .when_some(self.title.clone(), |div, title| {
650 div.child(Label::new(title))
651 })
652 .child(div().max_w_96().children(self.contents)),
653 )
654 .when(self.show_close_button, |this| {
655 this.on_modifiers_changed(move |_, _, cx| cx.notify(entity))
656 .child(
657 IconButton::new(close_id, close_icon)
658 .tooltip(move |_window, cx| {
659 if suppress {
660 Tooltip::with_meta(
661 "Suppress",
662 Some(&SuppressNotification),
663 "Click to Close",
664 cx,
665 )
666 } else if show_suppress_button {
667 Tooltip::with_meta(
668 "Close",
669 Some(&menu::Cancel),
670 "Shift-click to Suppress",
671 cx,
672 )
673 } else {
674 Tooltip::for_action("Close", &menu::Cancel, cx)
675 }
676 })
677 .on_click({
678 let close = self.close.take();
679 move |_, window, cx| {
680 if let Some(close) = &close {
681 close(&suppress, window, cx)
682 }
683 }
684 }),
685 )
686 }),
687 )
688 .children(self.suffix)
689 }
690}
691
692impl Component for NotificationFrame {}
693
694pub mod simple_message_notification {
695 use std::sync::Arc;
696
697 use gpui::{
698 AnyElement, DismissEvent, EventEmitter, FocusHandle, Focusable, ParentElement, Render,
699 ScrollHandle, SharedString, Styled,
700 };
701 use ui::{WithScrollbar, prelude::*};
702
703 use crate::notifications::NotificationFrame;
704
705 use super::{Notification, SuppressEvent};
706
707 pub struct MessageNotification {
708 focus_handle: FocusHandle,
709 build_content: Box<dyn Fn(&mut Window, &mut Context<Self>) -> AnyElement>,
710 primary_message: Option<SharedString>,
711 primary_icon: Option<IconName>,
712 primary_icon_color: Option<Color>,
713 primary_on_click: Option<Arc<dyn Fn(&mut Window, &mut Context<Self>)>>,
714 secondary_message: Option<SharedString>,
715 secondary_icon: Option<IconName>,
716 secondary_icon_color: Option<Color>,
717 secondary_on_click: Option<Arc<dyn Fn(&mut Window, &mut Context<Self>)>>,
718 more_info_message: Option<SharedString>,
719 more_info_url: Option<Arc<str>>,
720 show_close_button: bool,
721 show_suppress_button: bool,
722 title: Option<SharedString>,
723 scroll_handle: ScrollHandle,
724 }
725
726 impl Focusable for MessageNotification {
727 fn focus_handle(&self, _: &App) -> FocusHandle {
728 self.focus_handle.clone()
729 }
730 }
731
732 impl EventEmitter<DismissEvent> for MessageNotification {}
733 impl EventEmitter<SuppressEvent> for MessageNotification {}
734
735 impl Notification for MessageNotification {}
736
737 impl MessageNotification {
738 pub fn new<S>(message: S, cx: &mut App) -> MessageNotification
739 where
740 S: Into<SharedString>,
741 {
742 let message = message.into();
743 Self::new_from_builder(cx, move |_, _| {
744 Label::new(message.clone()).into_any_element()
745 })
746 }
747
748 pub fn new_from_builder<F>(cx: &mut App, content: F) -> MessageNotification
749 where
750 F: 'static + Fn(&mut Window, &mut Context<Self>) -> AnyElement,
751 {
752 Self {
753 build_content: Box::new(content),
754 primary_message: None,
755 primary_icon: None,
756 primary_icon_color: None,
757 primary_on_click: None,
758 secondary_message: None,
759 secondary_icon: None,
760 secondary_icon_color: None,
761 secondary_on_click: None,
762 more_info_message: None,
763 more_info_url: None,
764 show_close_button: true,
765 show_suppress_button: true,
766 title: None,
767 focus_handle: cx.focus_handle(),
768 scroll_handle: ScrollHandle::new(),
769 }
770 }
771
772 pub fn primary_message<S>(mut self, message: S) -> Self
773 where
774 S: Into<SharedString>,
775 {
776 self.primary_message = Some(message.into());
777 self
778 }
779
780 pub fn primary_icon(mut self, icon: IconName) -> Self {
781 self.primary_icon = Some(icon);
782 self
783 }
784
785 pub fn primary_icon_color(mut self, color: Color) -> Self {
786 self.primary_icon_color = Some(color);
787 self
788 }
789
790 pub fn primary_on_click<F>(mut self, on_click: F) -> Self
791 where
792 F: 'static + Fn(&mut Window, &mut Context<Self>),
793 {
794 self.primary_on_click = Some(Arc::new(on_click));
795 self
796 }
797
798 pub fn primary_on_click_arc<F>(mut self, on_click: Arc<F>) -> Self
799 where
800 F: 'static + Fn(&mut Window, &mut Context<Self>),
801 {
802 self.primary_on_click = Some(on_click);
803 self
804 }
805
806 pub fn secondary_message<S>(mut self, message: S) -> Self
807 where
808 S: Into<SharedString>,
809 {
810 self.secondary_message = Some(message.into());
811 self
812 }
813
814 pub fn secondary_icon(mut self, icon: IconName) -> Self {
815 self.secondary_icon = Some(icon);
816 self
817 }
818
819 pub fn secondary_icon_color(mut self, color: Color) -> Self {
820 self.secondary_icon_color = Some(color);
821 self
822 }
823
824 pub fn secondary_on_click<F>(mut self, on_click: F) -> Self
825 where
826 F: 'static + Fn(&mut Window, &mut Context<Self>),
827 {
828 self.secondary_on_click = Some(Arc::new(on_click));
829 self
830 }
831
832 pub fn secondary_on_click_arc<F>(mut self, on_click: Arc<F>) -> Self
833 where
834 F: 'static + Fn(&mut Window, &mut Context<Self>),
835 {
836 self.secondary_on_click = Some(on_click);
837 self
838 }
839
840 pub fn more_info_message<S>(mut self, message: S) -> Self
841 where
842 S: Into<SharedString>,
843 {
844 self.more_info_message = Some(message.into());
845 self
846 }
847
848 pub fn more_info_url<S>(mut self, url: S) -> Self
849 where
850 S: Into<Arc<str>>,
851 {
852 self.more_info_url = Some(url.into());
853 self
854 }
855
856 pub fn dismiss(&mut self, cx: &mut Context<Self>) {
857 cx.emit(DismissEvent);
858 }
859
860 pub fn show_close_button(mut self, show: bool) -> Self {
861 self.show_close_button = show;
862 self
863 }
864
865 /// Determines whether the given notification ID should be suppressible
866 /// Suppressed notifications will not be shown anymor
867 pub fn show_suppress_button(mut self, show: bool) -> Self {
868 self.show_suppress_button = show;
869 self
870 }
871
872 pub fn with_title<S>(mut self, title: S) -> Self
873 where
874 S: Into<SharedString>,
875 {
876 self.title = Some(title.into());
877 self
878 }
879 }
880
881 impl Render for MessageNotification {
882 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
883 NotificationFrame::new()
884 .with_title(self.title.clone())
885 .with_content(
886 div()
887 .child(
888 div()
889 .id("message-notification-content")
890 .max_h(vh(0.6, window))
891 .overflow_y_scroll()
892 .track_scroll(&self.scroll_handle.clone())
893 .child((self.build_content)(window, cx)),
894 )
895 .vertical_scrollbar_for(&self.scroll_handle, window, cx),
896 )
897 .show_close_button(self.show_close_button)
898 .show_suppress_button(self.show_suppress_button)
899 .on_close(cx.listener(|_, suppress, _, cx| {
900 if *suppress {
901 cx.emit(SuppressEvent);
902 } else {
903 cx.emit(DismissEvent);
904 }
905 }))
906 .with_suffix(
907 h_flex()
908 .gap_1()
909 .children(self.primary_message.iter().map(|message| {
910 let mut button = Button::new(message.clone(), message.clone())
911 .label_size(LabelSize::Small)
912 .on_click(cx.listener(|this, _, window, cx| {
913 if let Some(on_click) = this.primary_on_click.as_ref() {
914 (on_click)(window, cx)
915 };
916 this.dismiss(cx)
917 }));
918
919 if let Some(icon) = self.primary_icon {
920 button = button
921 .icon(icon)
922 .icon_color(self.primary_icon_color.unwrap_or(Color::Muted))
923 .icon_position(IconPosition::Start)
924 .icon_size(IconSize::Small);
925 }
926
927 button
928 }))
929 .children(self.secondary_message.iter().map(|message| {
930 let mut button = Button::new(message.clone(), message.clone())
931 .label_size(LabelSize::Small)
932 .on_click(cx.listener(|this, _, window, cx| {
933 if let Some(on_click) = this.secondary_on_click.as_ref() {
934 (on_click)(window, cx)
935 };
936 this.dismiss(cx)
937 }));
938
939 if let Some(icon) = self.secondary_icon {
940 button = button
941 .icon(icon)
942 .icon_position(IconPosition::Start)
943 .icon_size(IconSize::Small)
944 .icon_color(self.secondary_icon_color.unwrap_or(Color::Muted));
945 }
946
947 button
948 }))
949 .child(
950 h_flex().w_full().justify_end().children(
951 self.more_info_message
952 .iter()
953 .zip(self.more_info_url.iter())
954 .map(|(message, url)| {
955 let url = url.clone();
956 Button::new(message.clone(), message.clone())
957 .label_size(LabelSize::Small)
958 .icon(IconName::ArrowUpRight)
959 .icon_size(IconSize::Indicator)
960 .icon_color(Color::Muted)
961 .on_click(cx.listener(move |_, _, _, cx| {
962 cx.open_url(&url);
963 }))
964 }),
965 ),
966 ),
967 )
968 }
969 }
970}
971
972static GLOBAL_APP_NOTIFICATIONS: LazyLock<Mutex<AppNotifications>> = LazyLock::new(|| {
973 Mutex::new(AppNotifications {
974 app_notifications: Vec::new(),
975 })
976});
977
978/// Stores app notifications so that they can be shown in new workspaces.
979struct AppNotifications {
980 app_notifications: Vec<(
981 NotificationId,
982 Arc<dyn Fn(&mut Context<Workspace>) -> AnyView + Send + Sync>,
983 )>,
984}
985
986impl AppNotifications {
987 pub fn insert(
988 &mut self,
989 id: NotificationId,
990 build_notification: Arc<dyn Fn(&mut Context<Workspace>) -> AnyView + Send + Sync>,
991 ) {
992 self.remove(&id);
993 self.app_notifications.push((id, build_notification))
994 }
995
996 pub fn remove(&mut self, id: &NotificationId) {
997 self.app_notifications
998 .retain(|(existing_id, _)| existing_id != id);
999 }
1000}
1001
1002/// Shows a notification in all workspaces. New workspaces will also receive the notification - this
1003/// is particularly to handle notifications that occur on initialization before any workspaces
1004/// exist. If the notification is dismissed within any workspace, it will be removed from all.
1005pub fn show_app_notification<V: Notification + 'static>(
1006 id: NotificationId,
1007 cx: &mut App,
1008 build_notification: impl Fn(&mut Context<Workspace>) -> Entity<V> + 'static + Send + Sync,
1009) {
1010 // Defer notification creation so that windows on the stack can be returned to GPUI
1011 cx.defer(move |cx| {
1012 // Handle dismiss events by removing the notification from all workspaces.
1013 let build_notification: Arc<dyn Fn(&mut Context<Workspace>) -> AnyView + Send + Sync> =
1014 Arc::new({
1015 let id = id.clone();
1016 move |cx| {
1017 let notification = build_notification(cx);
1018 cx.subscribe(¬ification, {
1019 let id = id.clone();
1020 move |_, _, _: &DismissEvent, cx| {
1021 dismiss_app_notification(&id, cx);
1022 }
1023 })
1024 .detach();
1025 cx.subscribe(¬ification, {
1026 let id = id.clone();
1027 move |workspace: &mut Workspace, _, _: &SuppressEvent, cx| {
1028 workspace.suppress_notification(&id, cx);
1029 }
1030 })
1031 .detach();
1032 notification.into()
1033 }
1034 });
1035
1036 // Store the notification so that new workspaces also receive it.
1037 GLOBAL_APP_NOTIFICATIONS
1038 .lock()
1039 .insert(id.clone(), build_notification.clone());
1040
1041 for window in cx.windows() {
1042 if let Some(multi_workspace) = window.downcast::<MultiWorkspace>() {
1043 multi_workspace
1044 .update(cx, |multi_workspace, _window, cx| {
1045 for workspace in multi_workspace.workspaces() {
1046 workspace.update(cx, |workspace, cx| {
1047 workspace.show_notification_without_handling_dismiss_events(
1048 &id,
1049 cx,
1050 |cx| build_notification(cx),
1051 );
1052 });
1053 }
1054 })
1055 .ok(); // Doesn't matter if the windows are dropped
1056 }
1057 }
1058 });
1059}
1060
1061pub fn dismiss_app_notification(id: &NotificationId, cx: &mut App) {
1062 let id = id.clone();
1063 // Defer notification dismissal so that windows on the stack can be returned to GPUI
1064 cx.defer(move |cx| {
1065 GLOBAL_APP_NOTIFICATIONS.lock().remove(&id);
1066 for window in cx.windows() {
1067 if let Some(multi_workspace) = window.downcast::<MultiWorkspace>() {
1068 let id = id.clone();
1069 multi_workspace
1070 .update(cx, |multi_workspace, _window, cx| {
1071 for workspace in multi_workspace.workspaces() {
1072 workspace.update(cx, |workspace, cx| {
1073 workspace.dismiss_notification(&id, cx)
1074 });
1075 }
1076 })
1077 .ok();
1078 }
1079 }
1080 });
1081}
1082
1083pub trait NotifyResultExt {
1084 type Ok;
1085
1086 fn notify_err(self, workspace: &mut Workspace, cx: &mut Context<Workspace>)
1087 -> Option<Self::Ok>;
1088
1089 fn notify_workspace_async_err(
1090 self,
1091 workspace: WeakEntity<Workspace>,
1092 cx: &mut AsyncApp,
1093 ) -> Option<Self::Ok>;
1094
1095 /// Notifies the active workspace if there is one, otherwise notifies all workspaces.
1096 fn notify_app_err(self, cx: &mut App) -> Option<Self::Ok>;
1097}
1098
1099impl<T, E> NotifyResultExt for std::result::Result<T, E>
1100where
1101 E: std::fmt::Debug + std::fmt::Display,
1102{
1103 type Ok = T;
1104
1105 fn notify_err(self, workspace: &mut Workspace, cx: &mut Context<Workspace>) -> Option<T> {
1106 match self {
1107 Ok(value) => Some(value),
1108 Err(err) => {
1109 log::error!("Showing error notification in workspace: {err:?}");
1110 workspace.show_error(&err, cx);
1111 None
1112 }
1113 }
1114 }
1115
1116 fn notify_workspace_async_err(
1117 self,
1118 workspace: WeakEntity<Workspace>,
1119 cx: &mut AsyncApp,
1120 ) -> Option<T> {
1121 match self {
1122 Ok(value) => Some(value),
1123 Err(err) => {
1124 log::error!("{err:?}");
1125 workspace
1126 .update(cx, |workspace, cx| workspace.show_error(&err, cx))
1127 .ok();
1128 None
1129 }
1130 }
1131 }
1132
1133 fn notify_app_err(self, cx: &mut App) -> Option<T> {
1134 match self {
1135 Ok(value) => Some(value),
1136 Err(err) => {
1137 let message: SharedString = format!("Error: {err}").into();
1138 log::error!("Showing error notification in app: {message}");
1139 show_app_notification(workspace_error_notification_id(), cx, {
1140 move |cx| {
1141 cx.new({
1142 let message = message.clone();
1143 move |cx| ErrorMessagePrompt::new(message, cx)
1144 })
1145 }
1146 });
1147
1148 None
1149 }
1150 }
1151 }
1152}
1153
1154pub trait NotifyTaskExt {
1155 fn detach_and_notify_err(
1156 self,
1157 workspace: WeakEntity<Workspace>,
1158 window: &mut Window,
1159 cx: &mut App,
1160 );
1161}
1162
1163impl<R, E> NotifyTaskExt for Task<std::result::Result<R, E>>
1164where
1165 E: std::fmt::Debug + std::fmt::Display + Sized + 'static,
1166 R: 'static,
1167{
1168 fn detach_and_notify_err(
1169 self,
1170 workspace: WeakEntity<Workspace>,
1171 window: &mut Window,
1172 cx: &mut App,
1173 ) {
1174 window
1175 .spawn(cx, async move |mut cx| {
1176 self.await.notify_workspace_async_err(workspace, &mut cx)
1177 })
1178 .detach();
1179 }
1180}
1181
1182pub trait DetachAndPromptErr<R> {
1183 fn prompt_err(
1184 self,
1185 msg: &str,
1186 window: &Window,
1187 cx: &App,
1188 f: impl FnOnce(&anyhow::Error, &mut Window, &mut App) -> Option<String> + 'static,
1189 ) -> Task<Option<R>>;
1190
1191 fn detach_and_prompt_err(
1192 self,
1193 msg: &str,
1194 window: &Window,
1195 cx: &App,
1196 f: impl FnOnce(&anyhow::Error, &mut Window, &mut App) -> Option<String> + 'static,
1197 );
1198}
1199
1200impl<R> DetachAndPromptErr<R> for Task<anyhow::Result<R>>
1201where
1202 R: 'static,
1203{
1204 fn prompt_err(
1205 self,
1206 msg: &str,
1207 window: &Window,
1208 cx: &App,
1209 f: impl FnOnce(&anyhow::Error, &mut Window, &mut App) -> Option<String> + 'static,
1210 ) -> Task<Option<R>> {
1211 let msg = msg.to_owned();
1212 window.spawn(cx, async move |cx| {
1213 let result = self.await;
1214 if let Err(err) = result.as_ref() {
1215 log::error!("{err:#}");
1216 if let Ok(prompt) = cx.update(|window, cx| {
1217 let mut display = format!("{err:#}");
1218 if !display.ends_with('\n') {
1219 display.push('.');
1220 display.push(' ')
1221 }
1222 let detail =
1223 f(err, window, cx).unwrap_or_else(|| format!("{display}Please try again."));
1224 window.prompt(PromptLevel::Critical, &msg, Some(&detail), &["Ok"], cx)
1225 }) {
1226 prompt.await.ok();
1227 }
1228 return None;
1229 }
1230 Some(result.unwrap())
1231 })
1232 }
1233
1234 fn detach_and_prompt_err(
1235 self,
1236 msg: &str,
1237 window: &Window,
1238 cx: &App,
1239 f: impl FnOnce(&anyhow::Error, &mut Window, &mut App) -> Option<String> + 'static,
1240 ) {
1241 self.prompt_err(msg, window, cx, f).detach();
1242 }
1243}
1244
1245#[cfg(test)]
1246mod tests {
1247 use fs::FakeFs;
1248 use gpui::TestAppContext;
1249 use project::{LanguageServerPromptRequest, Project};
1250
1251 use crate::tests::init_test;
1252
1253 use super::*;
1254
1255 #[gpui::test]
1256 async fn test_notification_auto_dismiss_with_notifications_from_multiple_language_servers(
1257 cx: &mut TestAppContext,
1258 ) {
1259 init_test(cx);
1260
1261 let fs = FakeFs::new(cx.executor());
1262 let project = Project::test(fs, [], cx).await;
1263
1264 let (workspace, cx) =
1265 cx.add_window_view(|window, cx| Workspace::test_new(project.clone(), window, cx));
1266
1267 let count_notifications = |workspace: &Entity<Workspace>, cx: &mut TestAppContext| {
1268 workspace.read_with(cx, |workspace, _| workspace.notification_ids().len())
1269 };
1270
1271 let show_notification = |workspace: &Entity<Workspace>,
1272 cx: &mut TestAppContext,
1273 lsp_name: &str| {
1274 workspace.update(cx, |workspace, cx| {
1275 let request = LanguageServerPromptRequest::test(
1276 gpui::PromptLevel::Warning,
1277 "Test notification".to_string(),
1278 vec![], // Empty actions triggers auto-dismiss
1279 lsp_name.to_string(),
1280 );
1281 let notification_id = NotificationId::composite::<LanguageServerPrompt>(request.id);
1282 workspace.show_notification(notification_id, cx, |cx| {
1283 cx.new(|cx| LanguageServerPrompt::new(request, cx))
1284 });
1285 })
1286 };
1287
1288 show_notification(&workspace, cx, "Lsp1");
1289 assert_eq!(count_notifications(&workspace, cx), 1);
1290
1291 cx.executor().advance_clock(Duration::from_millis(1000));
1292
1293 show_notification(&workspace, cx, "Lsp2");
1294 assert_eq!(count_notifications(&workspace, cx), 2);
1295
1296 cx.executor().advance_clock(Duration::from_millis(1000));
1297
1298 show_notification(&workspace, cx, "Lsp3");
1299 assert_eq!(count_notifications(&workspace, cx), 3);
1300
1301 cx.executor().advance_clock(Duration::from_millis(3000));
1302 assert_eq!(count_notifications(&workspace, cx), 2);
1303
1304 cx.executor().advance_clock(Duration::from_millis(1000));
1305 assert_eq!(count_notifications(&workspace, cx), 1);
1306
1307 cx.executor().advance_clock(Duration::from_millis(1000));
1308 assert_eq!(count_notifications(&workspace, cx), 0);
1309 }
1310
1311 #[gpui::test]
1312 async fn test_notification_auto_dismiss_with_multiple_notifications_from_single_language_server(
1313 cx: &mut TestAppContext,
1314 ) {
1315 init_test(cx);
1316
1317 let lsp_name = "server1";
1318
1319 let fs = FakeFs::new(cx.executor());
1320 let project = Project::test(fs, [], cx).await;
1321 let (workspace, cx) =
1322 cx.add_window_view(|window, cx| Workspace::test_new(project.clone(), window, cx));
1323
1324 let count_notifications = |workspace: &Entity<Workspace>, cx: &mut TestAppContext| {
1325 workspace.read_with(cx, |workspace, _| workspace.notification_ids().len())
1326 };
1327
1328 let show_notification = |lsp_name: &str,
1329 workspace: &Entity<Workspace>,
1330 cx: &mut TestAppContext| {
1331 workspace.update(cx, |workspace, cx| {
1332 let lsp_name = lsp_name.to_string();
1333 let request = LanguageServerPromptRequest::test(
1334 gpui::PromptLevel::Warning,
1335 "Test notification".to_string(),
1336 vec![], // Empty actions triggers auto-dismiss
1337 lsp_name,
1338 );
1339 let notification_id = NotificationId::composite::<LanguageServerPrompt>(request.id);
1340
1341 workspace.show_notification(notification_id, cx, |cx| {
1342 cx.new(|cx| LanguageServerPrompt::new(request, cx))
1343 });
1344 })
1345 };
1346
1347 show_notification(lsp_name, &workspace, cx);
1348 assert_eq!(count_notifications(&workspace, cx), 1);
1349
1350 cx.executor().advance_clock(Duration::from_millis(1000));
1351
1352 show_notification(lsp_name, &workspace, cx);
1353 assert_eq!(count_notifications(&workspace, cx), 2);
1354
1355 cx.executor().advance_clock(Duration::from_millis(4000));
1356 assert_eq!(count_notifications(&workspace, cx), 1);
1357
1358 cx.executor().advance_clock(Duration::from_millis(1000));
1359 assert_eq!(count_notifications(&workspace, cx), 0);
1360 }
1361
1362 #[gpui::test]
1363 async fn test_notification_auto_dismiss_turned_off(cx: &mut TestAppContext) {
1364 init_test(cx);
1365
1366 cx.update(|cx| {
1367 let mut settings = ProjectSettings::get_global(cx).clone();
1368 settings
1369 .global_lsp_settings
1370 .notifications
1371 .dismiss_timeout_ms = Some(0);
1372 ProjectSettings::override_global(settings, cx);
1373 });
1374
1375 let fs = FakeFs::new(cx.executor());
1376 let project = Project::test(fs, [], cx).await;
1377 let (workspace, cx) =
1378 cx.add_window_view(|window, cx| Workspace::test_new(project.clone(), window, cx));
1379
1380 let count_notifications = |workspace: &Entity<Workspace>, cx: &mut TestAppContext| {
1381 workspace.read_with(cx, |workspace, _| workspace.notification_ids().len())
1382 };
1383
1384 workspace.update(cx, |workspace, cx| {
1385 let request = LanguageServerPromptRequest::test(
1386 gpui::PromptLevel::Warning,
1387 "Test notification".to_string(),
1388 vec![], // Empty actions would trigger auto-dismiss if enabled
1389 "test_server".to_string(),
1390 );
1391 let notification_id = NotificationId::composite::<LanguageServerPrompt>(request.id);
1392 workspace.show_notification(notification_id, cx, |cx| {
1393 cx.new(|cx| LanguageServerPrompt::new(request, cx))
1394 });
1395 });
1396
1397 assert_eq!(count_notifications(&workspace, cx), 1);
1398
1399 // Advance time beyond the default auto-dismiss duration
1400 cx.executor().advance_clock(Duration::from_millis(10000));
1401 assert_eq!(count_notifications(&workspace, cx), 1);
1402 }
1403
1404 #[gpui::test]
1405 async fn test_notification_auto_dismiss_with_custom_duration(cx: &mut TestAppContext) {
1406 init_test(cx);
1407
1408 let custom_duration_ms: u64 = 2000;
1409 cx.update(|cx| {
1410 let mut settings = ProjectSettings::get_global(cx).clone();
1411 settings
1412 .global_lsp_settings
1413 .notifications
1414 .dismiss_timeout_ms = Some(custom_duration_ms);
1415 ProjectSettings::override_global(settings, cx);
1416 });
1417
1418 let fs = FakeFs::new(cx.executor());
1419 let project = Project::test(fs, [], cx).await;
1420 let (workspace, cx) =
1421 cx.add_window_view(|window, cx| Workspace::test_new(project.clone(), window, cx));
1422
1423 let count_notifications = |workspace: &Entity<Workspace>, cx: &mut TestAppContext| {
1424 workspace.read_with(cx, |workspace, _| workspace.notification_ids().len())
1425 };
1426
1427 workspace.update(cx, |workspace, cx| {
1428 let request = LanguageServerPromptRequest::test(
1429 gpui::PromptLevel::Warning,
1430 "Test notification".to_string(),
1431 vec![], // Empty actions triggers auto-dismiss
1432 "test_server".to_string(),
1433 );
1434 let notification_id = NotificationId::composite::<LanguageServerPrompt>(request.id);
1435 workspace.show_notification(notification_id, cx, |cx| {
1436 cx.new(|cx| LanguageServerPrompt::new(request, cx))
1437 });
1438 });
1439
1440 assert_eq!(count_notifications(&workspace, cx), 1);
1441
1442 // Advance time less than custom duration
1443 cx.executor()
1444 .advance_clock(Duration::from_millis(custom_duration_ms - 500));
1445 assert_eq!(count_notifications(&workspace, cx), 1);
1446
1447 // Advance time past the custom duration
1448 cx.executor().advance_clock(Duration::from_millis(1000));
1449 assert_eq!(count_notifications(&workspace, cx), 0);
1450 }
1451}