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