1use crate::ProjectDiagnosticsEditor;
2use gpui::{EventEmitter, ParentElement, Render, View, ViewContext, WeakView};
3use ui::prelude::*;
4use ui::{IconButton, IconButtonShape, IconName, Tooltip};
5use workspace::{item::ItemHandle, ToolbarItemEvent, ToolbarItemLocation, ToolbarItemView};
6
7pub struct ToolbarControls {
8 editor: Option<WeakView<ProjectDiagnosticsEditor>>,
9}
10
11impl Render for ToolbarControls {
12 fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
13 let mut include_warnings = false;
14 let mut has_stale_excerpts = false;
15 let mut is_updating = false;
16
17 if let Some(editor) = self.diagnostics() {
18 let diagnostics = editor.read(cx);
19 include_warnings = diagnostics.include_warnings;
20 has_stale_excerpts = !diagnostics.paths_to_update.is_empty();
21 is_updating = diagnostics.update_excerpts_task.is_some()
22 || diagnostics
23 .project
24 .read(cx)
25 .language_servers_running_disk_based_diagnostics(cx)
26 .next()
27 .is_some();
28 }
29
30 let tooltip = if include_warnings {
31 "Exclude Warnings"
32 } else {
33 "Include Warnings"
34 };
35
36 let warning_color = if include_warnings {
37 Color::Warning
38 } else {
39 Color::Muted
40 };
41
42 h_flex()
43 .gap_1()
44 .when(has_stale_excerpts, |div| {
45 div.child(
46 IconButton::new("update-excerpts", IconName::Update)
47 .icon_color(Color::Info)
48 .shape(IconButtonShape::Square)
49 .disabled(is_updating)
50 .tooltip(move |cx| Tooltip::text("Update excerpts", cx))
51 .on_click(cx.listener(|this, _, cx| {
52 if let Some(diagnostics) = this.diagnostics() {
53 diagnostics.update(cx, |diagnostics, cx| {
54 diagnostics.update_all_excerpts(cx);
55 });
56 }
57 })),
58 )
59 })
60 .child(
61 IconButton::new("toggle-warnings", IconName::Warning)
62 .icon_color(warning_color)
63 .shape(IconButtonShape::Square)
64 .tooltip(move |cx| Tooltip::text(tooltip, cx))
65 .on_click(cx.listener(|this, _, cx| {
66 if let Some(editor) = this.diagnostics() {
67 editor.update(cx, |editor, cx| {
68 editor.toggle_warnings(&Default::default(), cx);
69 });
70 }
71 })),
72 )
73 }
74}
75
76impl EventEmitter<ToolbarItemEvent> for ToolbarControls {}
77
78impl ToolbarItemView for ToolbarControls {
79 fn set_active_pane_item(
80 &mut self,
81 active_pane_item: Option<&dyn ItemHandle>,
82 _: &mut ViewContext<Self>,
83 ) -> ToolbarItemLocation {
84 if let Some(pane_item) = active_pane_item.as_ref() {
85 if let Some(editor) = pane_item.downcast::<ProjectDiagnosticsEditor>() {
86 self.editor = Some(editor.downgrade());
87 ToolbarItemLocation::PrimaryRight
88 } else {
89 ToolbarItemLocation::Hidden
90 }
91 } else {
92 ToolbarItemLocation::Hidden
93 }
94 }
95}
96
97impl Default for ToolbarControls {
98 fn default() -> Self {
99 Self::new()
100 }
101}
102
103impl ToolbarControls {
104 pub fn new() -> Self {
105 ToolbarControls { editor: None }
106 }
107
108 fn diagnostics(&self) -> Option<View<ProjectDiagnosticsEditor>> {
109 self.editor.as_ref()?.upgrade()
110 }
111}