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