1use crate::ProjectDiagnosticsEditor;
2use gpui::{EventEmitter, ParentElement, Render, View, ViewContext, WeakView};
3use ui::prelude::*;
4use ui::{IconButton, 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.editor() {
18 let editor = editor.read(cx);
19 include_warnings = editor.include_warnings;
20 has_stale_excerpts = !editor.paths_to_update.is_empty();
21 is_updating = !editor.update_paths_tx.is_empty()
22 || editor
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 h_flex()
37 .when(has_stale_excerpts, |div| {
38 div.child(
39 IconButton::new("update-excerpts", IconName::Update)
40 .icon_color(Color::Info)
41 .disabled(is_updating)
42 .tooltip(move |cx| Tooltip::text("Update excerpts", cx))
43 .on_click(cx.listener(|this, _, cx| {
44 if let Some(editor) = this.editor() {
45 editor.update(cx, |editor, _| {
46 editor.enqueue_update_stale_excerpts(None);
47 });
48 }
49 })),
50 )
51 })
52 .child(
53 IconButton::new("toggle-warnings", IconName::Warning)
54 .tooltip(move |cx| Tooltip::text(tooltip, cx))
55 .on_click(cx.listener(|this, _, cx| {
56 if let Some(editor) = this.editor() {
57 editor.update(cx, |editor, cx| {
58 editor.toggle_warnings(&Default::default(), cx);
59 });
60 }
61 })),
62 )
63 }
64}
65
66impl EventEmitter<ToolbarItemEvent> for ToolbarControls {}
67
68impl ToolbarItemView for ToolbarControls {
69 fn set_active_pane_item(
70 &mut self,
71 active_pane_item: Option<&dyn ItemHandle>,
72 _: &mut ViewContext<Self>,
73 ) -> ToolbarItemLocation {
74 if let Some(pane_item) = active_pane_item.as_ref() {
75 if let Some(editor) = pane_item.downcast::<ProjectDiagnosticsEditor>() {
76 self.editor = Some(editor.downgrade());
77 ToolbarItemLocation::PrimaryRight
78 } else {
79 ToolbarItemLocation::Hidden
80 }
81 } else {
82 ToolbarItemLocation::Hidden
83 }
84 }
85}
86
87impl Default for ToolbarControls {
88 fn default() -> Self {
89 Self::new()
90 }
91}
92
93impl ToolbarControls {
94 pub fn new() -> Self {
95 ToolbarControls { editor: None }
96 }
97
98 fn editor(&self) -> Option<View<ProjectDiagnosticsEditor>> {
99 self.editor.as_ref()?.upgrade()
100 }
101}