1use crate::{Toast, Workspace};
2use collections::HashMap;
3use gpui::{
4 svg, AnyView, AppContext, AsyncWindowContext, ClipboardItem, DismissEvent, Entity, EntityId,
5 EventEmitter, Global, PromptLevel, Render, ScrollHandle, Task, View, ViewContext,
6 VisualContext, WindowContext,
7};
8
9use std::{any::TypeId, ops::DerefMut, time::Duration};
10use ui::{prelude::*, Tooltip};
11use util::ResultExt;
12
13pub fn init(cx: &mut AppContext) {
14 cx.set_global(NotificationTracker::new());
15}
16
17#[derive(Debug, PartialEq, Clone)]
18pub enum NotificationId {
19 Unique(TypeId),
20 Composite(TypeId, ElementId),
21 Named(SharedString),
22}
23
24impl NotificationId {
25 /// Returns a unique [`NotificationId`] for the given type.
26 pub fn unique<T: 'static>() -> Self {
27 Self::Unique(TypeId::of::<T>())
28 }
29
30 /// Returns a [`NotificationId`] for the given type that is also identified
31 /// by the provided ID.
32 pub fn composite<T: 'static>(id: impl Into<ElementId>) -> Self {
33 Self::Composite(TypeId::of::<T>(), id.into())
34 }
35
36 /// Builds a `NotificationId` out of the given string.
37 pub fn named(id: SharedString) -> Self {
38 Self::Named(id)
39 }
40}
41
42pub trait Notification: EventEmitter<DismissEvent> + Render {}
43
44impl<V: EventEmitter<DismissEvent> + Render> Notification for V {}
45
46pub trait NotificationHandle: Send {
47 fn id(&self) -> EntityId;
48 fn to_any(&self) -> AnyView;
49}
50
51impl<T: Notification> NotificationHandle for View<T> {
52 fn id(&self) -> EntityId {
53 self.entity_id()
54 }
55
56 fn to_any(&self) -> AnyView {
57 self.clone().into()
58 }
59}
60
61impl From<&dyn NotificationHandle> for AnyView {
62 fn from(val: &dyn NotificationHandle) -> Self {
63 val.to_any()
64 }
65}
66
67pub(crate) struct NotificationTracker {
68 notifications_sent: HashMap<TypeId, Vec<NotificationId>>,
69}
70
71impl Global for NotificationTracker {}
72
73impl std::ops::Deref for NotificationTracker {
74 type Target = HashMap<TypeId, Vec<NotificationId>>;
75
76 fn deref(&self) -> &Self::Target {
77 &self.notifications_sent
78 }
79}
80
81impl DerefMut for NotificationTracker {
82 fn deref_mut(&mut self) -> &mut Self::Target {
83 &mut self.notifications_sent
84 }
85}
86
87impl NotificationTracker {
88 fn new() -> Self {
89 Self {
90 notifications_sent: Default::default(),
91 }
92 }
93}
94
95impl Workspace {
96 pub fn has_shown_notification_once<V: Notification>(
97 &self,
98 id: &NotificationId,
99 cx: &ViewContext<Self>,
100 ) -> bool {
101 cx.global::<NotificationTracker>()
102 .get(&TypeId::of::<V>())
103 .map(|ids| ids.contains(id))
104 .unwrap_or(false)
105 }
106
107 pub fn show_notification_once<V: Notification>(
108 &mut self,
109 id: NotificationId,
110 cx: &mut ViewContext<Self>,
111 build_notification: impl FnOnce(&mut ViewContext<Self>) -> View<V>,
112 ) {
113 if !self.has_shown_notification_once::<V>(&id, cx) {
114 let tracker = cx.global_mut::<NotificationTracker>();
115 let entry = tracker.entry(TypeId::of::<V>()).or_default();
116 entry.push(id.clone());
117 self.show_notification::<V>(id, cx, build_notification)
118 }
119 }
120
121 #[cfg(any(test, feature = "test-support"))]
122 pub fn notification_ids(&self) -> Vec<NotificationId> {
123 self.notifications
124 .iter()
125 .map(|(id, _)| id)
126 .cloned()
127 .collect()
128 }
129
130 pub fn show_notification<V: Notification>(
131 &mut self,
132 id: NotificationId,
133 cx: &mut ViewContext<Self>,
134 build_notification: impl FnOnce(&mut ViewContext<Self>) -> View<V>,
135 ) {
136 self.dismiss_notification_internal(&id, cx);
137
138 let notification = build_notification(cx);
139 cx.subscribe(¬ification, {
140 let id = id.clone();
141 move |this, _, _: &DismissEvent, cx| {
142 this.dismiss_notification_internal(&id, cx);
143 }
144 })
145 .detach();
146 self.notifications.push((id, Box::new(notification)));
147 cx.notify();
148 }
149
150 pub fn show_error<E>(&mut self, err: &E, cx: &mut ViewContext<Self>)
151 where
152 E: std::fmt::Debug + std::fmt::Display,
153 {
154 struct WorkspaceErrorNotification;
155
156 self.show_notification(
157 NotificationId::unique::<WorkspaceErrorNotification>(),
158 cx,
159 |cx| cx.new_view(|_cx| ErrorMessagePrompt::new(format!("Error: {err:#}"))),
160 );
161 }
162
163 pub fn show_portal_error(&mut self, err: String, cx: &mut ViewContext<Self>) {
164 struct PortalError;
165
166 self.show_notification(NotificationId::unique::<PortalError>(), cx, |cx| {
167 cx.new_view(|_cx| {
168 ErrorMessagePrompt::new(err.to_string()).with_link_button(
169 "See docs",
170 "https://zed.dev/docs/linux#i-cant-open-any-files",
171 )
172 })
173 });
174 }
175
176 pub fn dismiss_notification(&mut self, id: &NotificationId, cx: &mut ViewContext<Self>) {
177 self.dismiss_notification_internal(id, cx)
178 }
179
180 pub fn show_toast(&mut self, toast: Toast, cx: &mut ViewContext<Self>) {
181 self.dismiss_notification(&toast.id, cx);
182 self.show_notification(toast.id.clone(), cx, |cx| {
183 cx.new_view(|_cx| match toast.on_click.as_ref() {
184 Some((click_msg, on_click)) => {
185 let on_click = on_click.clone();
186 simple_message_notification::MessageNotification::new(toast.msg.clone())
187 .with_click_message(click_msg.clone())
188 .on_click(move |cx| on_click(cx))
189 }
190 None => simple_message_notification::MessageNotification::new(toast.msg.clone()),
191 })
192 });
193 if toast.autohide {
194 cx.spawn(|workspace, mut cx| async move {
195 cx.background_executor()
196 .timer(Duration::from_millis(5000))
197 .await;
198 workspace
199 .update(&mut cx, |workspace, cx| {
200 workspace.dismiss_toast(&toast.id, cx)
201 })
202 .ok();
203 })
204 .detach();
205 }
206 }
207
208 pub fn dismiss_toast(&mut self, id: &NotificationId, cx: &mut ViewContext<Self>) {
209 self.dismiss_notification(id, cx);
210 }
211
212 pub fn clear_all_notifications(&mut self, cx: &mut ViewContext<Self>) {
213 self.notifications.clear();
214 cx.notify();
215 }
216
217 fn dismiss_notification_internal(&mut self, id: &NotificationId, cx: &mut ViewContext<Self>) {
218 self.notifications.retain(|(existing_id, _)| {
219 if existing_id == id {
220 cx.notify();
221 false
222 } else {
223 true
224 }
225 });
226 }
227}
228
229pub struct LanguageServerPrompt {
230 request: Option<project::LanguageServerPromptRequest>,
231 scroll_handle: ScrollHandle,
232}
233
234impl LanguageServerPrompt {
235 pub fn new(request: project::LanguageServerPromptRequest) -> Self {
236 Self {
237 request: Some(request),
238 scroll_handle: ScrollHandle::new(),
239 }
240 }
241
242 async fn select_option(this: View<Self>, ix: usize, mut cx: AsyncWindowContext) {
243 util::maybe!(async move {
244 let potential_future = this.update(&mut cx, |this, _| {
245 this.request.take().map(|request| request.respond(ix))
246 });
247
248 potential_future? // App Closed
249 .ok_or_else(|| anyhow::anyhow!("Response already sent"))?
250 .await
251 .ok_or_else(|| anyhow::anyhow!("Stream already closed"))?;
252
253 this.update(&mut cx, |_, cx| cx.emit(DismissEvent))?;
254
255 anyhow::Ok(())
256 })
257 .await
258 .log_err();
259 }
260}
261
262impl Render for LanguageServerPrompt {
263 fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
264 let Some(request) = &self.request else {
265 return div().id("language_server_prompt_notification");
266 };
267
268 let (icon, color) = match request.level {
269 PromptLevel::Info => (IconName::Info, Color::Accent),
270 PromptLevel::Warning => (IconName::Warning, Color::Warning),
271 PromptLevel::Critical => (IconName::XCircle, Color::Error),
272 };
273
274 div()
275 .id("language_server_prompt_notification")
276 .group("language_server_prompt_notification")
277 .occlude()
278 .w_full()
279 .max_h(vh(0.8, cx))
280 .elevation_3(cx)
281 .overflow_y_scroll()
282 .track_scroll(&self.scroll_handle)
283 .child(
284 v_flex()
285 .p_3()
286 .overflow_hidden()
287 .child(
288 h_flex()
289 .justify_between()
290 .child(
291 h_flex()
292 .gap_2()
293 .child(Icon::new(icon).color(color))
294 .child(Label::new(request.lsp_name.clone())),
295 )
296 .child(
297 h_flex()
298 .child(
299 IconButton::new("copy", IconName::Copy)
300 .on_click({
301 let message = request.message.clone();
302 move |_, cx| {
303 cx.write_to_clipboard(
304 ClipboardItem::new_string(message.clone()),
305 )
306 }
307 })
308 .tooltip(|cx| Tooltip::text("Copy Description", cx)),
309 )
310 .child(IconButton::new("close", IconName::Close).on_click(
311 cx.listener(|_, _, cx| cx.emit(gpui::DismissEvent)),
312 )),
313 ),
314 )
315 .child(Label::new(request.message.to_string()).size(LabelSize::Small))
316 .children(request.actions.iter().enumerate().map(|(ix, action)| {
317 let this_handle = cx.view().clone();
318 Button::new(ix, action.title.clone())
319 .size(ButtonSize::Large)
320 .on_click(move |_, cx| {
321 let this_handle = this_handle.clone();
322 cx.spawn(|cx| async move {
323 LanguageServerPrompt::select_option(this_handle, ix, cx).await
324 })
325 .detach()
326 })
327 })),
328 )
329 }
330}
331
332impl EventEmitter<DismissEvent> for LanguageServerPrompt {}
333
334pub struct ErrorMessagePrompt {
335 message: SharedString,
336 label_and_url_button: Option<(SharedString, SharedString)>,
337}
338
339impl ErrorMessagePrompt {
340 pub fn new<S>(message: S) -> Self
341 where
342 S: Into<SharedString>,
343 {
344 Self {
345 message: message.into(),
346 label_and_url_button: None,
347 }
348 }
349
350 pub fn with_link_button<S>(mut self, label: S, url: S) -> Self
351 where
352 S: Into<SharedString>,
353 {
354 self.label_and_url_button = Some((label.into(), url.into()));
355 self
356 }
357}
358
359impl Render for ErrorMessagePrompt {
360 fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
361 h_flex()
362 .id("error_message_prompt_notification")
363 .occlude()
364 .elevation_3(cx)
365 .items_start()
366 .justify_between()
367 .p_2()
368 .gap_2()
369 .w_full()
370 .child(
371 v_flex()
372 .w_full()
373 .child(
374 h_flex()
375 .w_full()
376 .justify_between()
377 .child(
378 svg()
379 .size(cx.text_style().font_size)
380 .flex_none()
381 .mr_2()
382 .mt(px(-2.0))
383 .map(|icon| {
384 icon.path(IconName::Warning.path())
385 .text_color(Color::Error.color(cx))
386 }),
387 )
388 .child(
389 ui::IconButton::new("close", ui::IconName::Close)
390 .on_click(cx.listener(|_, _, cx| cx.emit(gpui::DismissEvent))),
391 ),
392 )
393 .child(
394 div()
395 .id("error_message")
396 .max_w_80()
397 .max_h_40()
398 .overflow_y_scroll()
399 .child(Label::new(self.message.clone()).size(LabelSize::Small)),
400 )
401 .when_some(self.label_and_url_button.clone(), |elm, (label, url)| {
402 elm.child(
403 div().mt_2().child(
404 ui::Button::new("error_message_prompt_notification_button", label)
405 .on_click(move |_, cx| cx.open_url(&url)),
406 ),
407 )
408 }),
409 )
410 }
411}
412
413impl EventEmitter<DismissEvent> for ErrorMessagePrompt {}
414
415pub mod simple_message_notification {
416 use gpui::{
417 div, DismissEvent, EventEmitter, ParentElement, Render, SharedString, Styled, ViewContext,
418 };
419 use std::sync::Arc;
420 use ui::prelude::*;
421
422 pub struct MessageNotification {
423 message: SharedString,
424 on_click: Option<Arc<dyn Fn(&mut ViewContext<Self>)>>,
425 click_message: Option<SharedString>,
426 secondary_click_message: Option<SharedString>,
427 secondary_on_click: Option<Arc<dyn Fn(&mut ViewContext<Self>)>>,
428 }
429
430 impl EventEmitter<DismissEvent> for MessageNotification {}
431
432 impl MessageNotification {
433 pub fn new<S>(message: S) -> MessageNotification
434 where
435 S: Into<SharedString>,
436 {
437 Self {
438 message: message.into(),
439 on_click: None,
440 click_message: None,
441 secondary_on_click: None,
442 secondary_click_message: None,
443 }
444 }
445
446 pub fn with_click_message<S>(mut self, message: S) -> Self
447 where
448 S: Into<SharedString>,
449 {
450 self.click_message = Some(message.into());
451 self
452 }
453
454 pub fn on_click<F>(mut self, on_click: F) -> Self
455 where
456 F: 'static + Fn(&mut ViewContext<Self>),
457 {
458 self.on_click = Some(Arc::new(on_click));
459 self
460 }
461
462 pub fn with_secondary_click_message<S>(mut self, message: S) -> Self
463 where
464 S: Into<SharedString>,
465 {
466 self.secondary_click_message = Some(message.into());
467 self
468 }
469
470 pub fn on_secondary_click<F>(mut self, on_click: F) -> Self
471 where
472 F: 'static + Fn(&mut ViewContext<Self>),
473 {
474 self.secondary_on_click = Some(Arc::new(on_click));
475 self
476 }
477
478 pub fn dismiss(&mut self, cx: &mut ViewContext<Self>) {
479 cx.emit(DismissEvent);
480 }
481 }
482
483 impl Render for MessageNotification {
484 fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
485 v_flex()
486 .p_3()
487 .gap_2()
488 .elevation_3(cx)
489 .child(
490 h_flex()
491 .gap_4()
492 .justify_between()
493 .child(div().max_w_80().child(Label::new(self.message.clone())))
494 .child(
495 IconButton::new("close", IconName::Close)
496 .on_click(cx.listener(|this, _, cx| this.dismiss(cx))),
497 ),
498 )
499 .child(
500 h_flex()
501 .gap_2()
502 .children(self.click_message.iter().map(|message| {
503 Button::new(message.clone(), message.clone())
504 .label_size(LabelSize::Small)
505 .icon(IconName::Check)
506 .icon_position(IconPosition::Start)
507 .icon_size(IconSize::Small)
508 .icon_color(Color::Success)
509 .on_click(cx.listener(|this, _, cx| {
510 if let Some(on_click) = this.on_click.as_ref() {
511 (on_click)(cx)
512 };
513 this.dismiss(cx)
514 }))
515 }))
516 .children(self.secondary_click_message.iter().map(|message| {
517 Button::new(message.clone(), message.clone())
518 .label_size(LabelSize::Small)
519 .icon(IconName::Close)
520 .icon_position(IconPosition::Start)
521 .icon_size(IconSize::Small)
522 .icon_color(Color::Error)
523 .on_click(cx.listener(|this, _, cx| {
524 if let Some(on_click) = this.secondary_on_click.as_ref() {
525 (on_click)(cx)
526 };
527 this.dismiss(cx)
528 }))
529 })),
530 )
531 }
532 }
533}
534
535pub trait NotifyResultExt {
536 type Ok;
537
538 fn notify_err(
539 self,
540 workspace: &mut Workspace,
541 cx: &mut ViewContext<Workspace>,
542 ) -> Option<Self::Ok>;
543
544 fn notify_async_err(self, cx: &mut AsyncWindowContext) -> Option<Self::Ok>;
545}
546
547impl<T, E> NotifyResultExt for Result<T, E>
548where
549 E: std::fmt::Debug + std::fmt::Display,
550{
551 type Ok = T;
552
553 fn notify_err(self, workspace: &mut Workspace, cx: &mut ViewContext<Workspace>) -> Option<T> {
554 match self {
555 Ok(value) => Some(value),
556 Err(err) => {
557 log::error!("TODO {err:?}");
558 workspace.show_error(&err, cx);
559 None
560 }
561 }
562 }
563
564 fn notify_async_err(self, cx: &mut AsyncWindowContext) -> Option<T> {
565 match self {
566 Ok(value) => Some(value),
567 Err(err) => {
568 log::error!("{err:?}");
569 cx.update_root(|view, cx| {
570 if let Ok(workspace) = view.downcast::<Workspace>() {
571 workspace.update(cx, |workspace, cx| workspace.show_error(&err, cx))
572 }
573 })
574 .ok();
575 None
576 }
577 }
578 }
579}
580
581pub trait NotifyTaskExt {
582 fn detach_and_notify_err(self, cx: &mut WindowContext);
583}
584
585impl<R, E> NotifyTaskExt for Task<Result<R, E>>
586where
587 E: std::fmt::Debug + std::fmt::Display + Sized + 'static,
588 R: 'static,
589{
590 fn detach_and_notify_err(self, cx: &mut WindowContext) {
591 cx.spawn(|mut cx| async move { self.await.notify_async_err(&mut cx) })
592 .detach();
593 }
594}
595
596pub trait DetachAndPromptErr<R> {
597 fn prompt_err(
598 self,
599 msg: &str,
600 cx: &mut WindowContext,
601 f: impl FnOnce(&anyhow::Error, &mut WindowContext) -> Option<String> + 'static,
602 ) -> Task<Option<R>>;
603
604 fn detach_and_prompt_err(
605 self,
606 msg: &str,
607 cx: &mut WindowContext,
608 f: impl FnOnce(&anyhow::Error, &mut WindowContext) -> Option<String> + 'static,
609 );
610}
611
612impl<R> DetachAndPromptErr<R> for Task<anyhow::Result<R>>
613where
614 R: 'static,
615{
616 fn prompt_err(
617 self,
618 msg: &str,
619 cx: &mut WindowContext,
620 f: impl FnOnce(&anyhow::Error, &mut WindowContext) -> Option<String> + 'static,
621 ) -> Task<Option<R>> {
622 let msg = msg.to_owned();
623 cx.spawn(|mut cx| async move {
624 let result = self.await;
625 if let Err(err) = result.as_ref() {
626 log::error!("{err:?}");
627 if let Ok(prompt) = cx.update(|cx| {
628 let detail = f(err, cx).unwrap_or_else(|| format!("{err}. Please try again."));
629 cx.prompt(PromptLevel::Critical, &msg, Some(&detail), &["Ok"])
630 }) {
631 prompt.await.ok();
632 }
633 return None;
634 }
635 Some(result.unwrap())
636 })
637 }
638
639 fn detach_and_prompt_err(
640 self,
641 msg: &str,
642 cx: &mut WindowContext,
643 f: impl FnOnce(&anyhow::Error, &mut WindowContext) -> Option<String> + 'static,
644 ) {
645 self.prompt_err(msg, cx, f).detach();
646 }
647}