items.rs

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