1use crate::{BufferDiagnosticsEditor, ProjectDiagnosticsEditor, ToggleDiagnosticsRefresh};
2use gpui::{Context, EventEmitter, ParentElement, Render, Window};
3use language::DiagnosticEntry;
4use text::{Anchor, BufferId};
5use ui::prelude::*;
6use ui::{IconButton, IconName, Tooltip};
7use workspace::{ToolbarItemEvent, ToolbarItemLocation, ToolbarItemView, item::ItemHandle};
8
9pub struct ToolbarControls {
10 editor: Option<Box<dyn DiagnosticsToolbarEditor>>,
11}
12
13pub(crate) trait DiagnosticsToolbarEditor: Send + Sync {
14 /// Informs the toolbar whether warnings are included in the diagnostics.
15 fn include_warnings(&self, cx: &App) -> bool;
16 /// Toggles whether warning diagnostics should be displayed by the
17 /// diagnostics editor.
18 fn toggle_warnings(&self, window: &mut Window, cx: &mut App);
19 /// Indicates whether any of the excerpts displayed by the diagnostics
20 /// editor are stale.
21 fn has_stale_excerpts(&self, cx: &App) -> bool;
22 /// Indicates whether the diagnostics editor is currently updating the
23 /// diagnostics.
24 fn is_updating(&self, cx: &App) -> bool;
25 /// Requests that the diagnostics editor stop updating the diagnostics.
26 fn stop_updating(&self, cx: &mut App);
27 /// Requests that the diagnostics editor updates the displayed diagnostics
28 /// with the latest information.
29 fn refresh_diagnostics(&self, window: &mut Window, cx: &mut App);
30 /// Returns a list of diagnostics for the provided buffer id.
31 fn get_diagnostics_for_buffer(
32 &self,
33 buffer_id: BufferId,
34 cx: &App,
35 ) -> Vec<DiagnosticEntry<Anchor>>;
36}
37
38impl Render for ToolbarControls {
39 fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
40 let mut has_stale_excerpts = false;
41 let mut include_warnings = false;
42 let mut is_updating = false;
43
44 match &self.editor {
45 Some(editor) => {
46 include_warnings = editor.include_warnings(cx);
47 has_stale_excerpts = editor.has_stale_excerpts(cx);
48 is_updating = editor.is_updating(cx);
49 }
50 None => {}
51 }
52
53 let warning_tooltip = if include_warnings {
54 "Exclude Warnings"
55 } else {
56 "Include Warnings"
57 };
58
59 let warning_color = if include_warnings {
60 Color::Warning
61 } else {
62 Color::Muted
63 };
64
65 h_flex()
66 .gap_1()
67 .map(|div| {
68 if is_updating {
69 div.child(
70 IconButton::new("stop-updating", IconName::Stop)
71 .icon_color(Color::Info)
72 .tooltip(Tooltip::for_action_title(
73 "Stop diagnostics update",
74 &ToggleDiagnosticsRefresh,
75 ))
76 .on_click(cx.listener(move |toolbar_controls, _, _, cx| {
77 if let Some(editor) = toolbar_controls.editor() {
78 editor.stop_updating(cx);
79 cx.notify();
80 }
81 })),
82 )
83 } else {
84 div.child(
85 IconButton::new("refresh-diagnostics", IconName::ArrowCircle)
86 .icon_color(Color::Info)
87 .disabled(!has_stale_excerpts)
88 .tooltip(Tooltip::for_action_title(
89 "Refresh diagnostics",
90 &ToggleDiagnosticsRefresh,
91 ))
92 .on_click(cx.listener({
93 move |toolbar_controls, _, window, cx| {
94 if let Some(editor) = toolbar_controls.editor() {
95 editor.refresh_diagnostics(window, cx)
96 }
97 }
98 })),
99 )
100 }
101 })
102 .child(
103 IconButton::new("toggle-warnings", IconName::Warning)
104 .icon_color(warning_color)
105 .tooltip(Tooltip::text(warning_tooltip))
106 .on_click(cx.listener(|this, _, window, cx| {
107 if let Some(editor) = &this.editor {
108 editor.toggle_warnings(window, cx)
109 }
110 })),
111 )
112 }
113}
114
115impl EventEmitter<ToolbarItemEvent> for ToolbarControls {}
116
117impl ToolbarItemView for ToolbarControls {
118 fn set_active_pane_item(
119 &mut self,
120 active_pane_item: Option<&dyn ItemHandle>,
121 _window: &mut Window,
122 _: &mut Context<Self>,
123 ) -> ToolbarItemLocation {
124 if let Some(pane_item) = active_pane_item.as_ref() {
125 if let Some(editor) = pane_item.downcast::<ProjectDiagnosticsEditor>() {
126 self.editor = Some(Box::new(editor.downgrade()));
127 ToolbarItemLocation::PrimaryRight
128 } else if let Some(editor) = pane_item.downcast::<BufferDiagnosticsEditor>() {
129 self.editor = Some(Box::new(editor.downgrade()));
130 ToolbarItemLocation::PrimaryRight
131 } else {
132 ToolbarItemLocation::Hidden
133 }
134 } else {
135 ToolbarItemLocation::Hidden
136 }
137 }
138}
139
140impl Default for ToolbarControls {
141 fn default() -> Self {
142 Self::new()
143 }
144}
145
146impl ToolbarControls {
147 pub fn new() -> Self {
148 ToolbarControls { editor: None }
149 }
150
151 fn editor(&self) -> Option<&dyn DiagnosticsToolbarEditor> {
152 self.editor.as_deref()
153 }
154}