items.rs

 1use gpui::{
 2    elements::*, platform::CursorStyle, Entity, ModelHandle, RenderContext, View, ViewContext,
 3};
 4use postage::watch;
 5use project::Project;
 6use std::fmt::Write;
 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                this.summary = project.read(cx).diagnostic_summary(cx);
24                cx.notify();
25            }
26            project::Event::DiskBasedDiagnosticsStarted => {
27                this.in_progress = true;
28                cx.notify();
29            }
30            project::Event::DiskBasedDiagnosticsFinished => {
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        let mut message = String::new();
59        if self.in_progress {
60            message.push_str("Checking... ");
61        }
62        write!(
63            message,
64            "Errors: {}, Warnings: {}",
65            self.summary.error_count, self.summary.warning_count
66        )
67        .unwrap();
68        MouseEventHandler::new::<Tag, _, _, _>(0, cx, |_, _| {
69            Label::new(message, theme.status_bar_item.text.clone())
70                .contained()
71                .with_style(theme.status_bar_item.container)
72                .boxed()
73        })
74        .with_cursor_style(CursorStyle::PointingHand)
75        .on_click(|cx| cx.dispatch_action(crate::Deploy))
76        .boxed()
77    }
78}
79
80impl StatusItemView for DiagnosticSummary {
81    fn set_active_pane_item(
82        &mut self,
83        _: Option<&dyn workspace::ItemViewHandle>,
84        _: &mut ViewContext<Self>,
85    ) {
86    }
87}