items.rs

 1use crate::render_summary;
 2use gpui::{
 3    elements::*, platform::CursorStyle, Entity, ModelHandle, RenderContext, View, ViewContext,
 4};
 5use postage::watch;
 6use project::Project;
 7use workspace::{Settings, StatusItemView};
 8
 9pub struct DiagnosticSummary {
10    settings: watch::Receiver<Settings>,
11    summary: project::DiagnosticSummary,
12    in_progress: bool,
13}
14
15impl DiagnosticSummary {
16    pub fn new(
17        project: &ModelHandle<Project>,
18        settings: watch::Receiver<Settings>,
19        cx: &mut ViewContext<Self>,
20    ) -> Self {
21        cx.subscribe(project, |this, project, event, cx| match event {
22            project::Event::DiskBasedDiagnosticsUpdated => {
23                cx.notify();
24            }
25            project::Event::DiskBasedDiagnosticsStarted => {
26                this.in_progress = true;
27                cx.notify();
28            }
29            project::Event::DiskBasedDiagnosticsFinished => {
30                this.summary = project.read(cx).diagnostic_summary(cx);
31                this.in_progress = false;
32                cx.notify();
33            }
34            _ => {}
35        })
36        .detach();
37        Self {
38            settings,
39            summary: project.read(cx).diagnostic_summary(cx),
40            in_progress: project.read(cx).is_running_disk_based_diagnostics(),
41        }
42    }
43}
44
45impl Entity for DiagnosticSummary {
46    type Event = ();
47}
48
49impl View for DiagnosticSummary {
50    fn ui_name() -> &'static str {
51        "DiagnosticSummary"
52    }
53
54    fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox {
55        enum Tag {}
56
57        let theme = &self.settings.borrow().theme.project_diagnostics;
58
59        let in_progress = self.in_progress;
60        MouseEventHandler::new::<Tag, _, _>(0, cx, |_, _| {
61            if in_progress {
62                Label::new(
63                    "Checking... ".to_string(),
64                    theme.status_bar_item.text.clone(),
65                )
66                .contained()
67                .with_style(theme.status_bar_item.container)
68                .boxed()
69            } else {
70                render_summary(&self.summary, &theme.status_bar_item.text, &theme)
71            }
72        })
73        .with_cursor_style(CursorStyle::PointingHand)
74        .on_click(|cx| cx.dispatch_action(crate::Deploy))
75        .boxed()
76    }
77}
78
79impl StatusItemView for DiagnosticSummary {
80    fn set_active_pane_item(
81        &mut self,
82        _: Option<&dyn workspace::ItemViewHandle>,
83        _: &mut ViewContext<Self>,
84    ) {
85    }
86}