items.rs

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