toolbar_controls.rs

 1use crate::ProjectDiagnosticsEditor;
 2use gpui::{div, Div, EventEmitter, ParentElement, Render, ViewContext, WeakView};
 3use ui::{Icon, IconButton, Tooltip};
 4use workspace::{item::ItemHandle, ToolbarItemEvent, ToolbarItemLocation, ToolbarItemView};
 5
 6pub struct ToolbarControls {
 7    editor: Option<WeakView<ProjectDiagnosticsEditor>>,
 8}
 9
10impl Render for ToolbarControls {
11    type Element = Div;
12
13    fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
14        let include_warnings = self
15            .editor
16            .as_ref()
17            .and_then(|editor| editor.upgrade())
18            .map(|editor| editor.read(cx).include_warnings)
19            .unwrap_or(false);
20
21        let tooltip = if include_warnings {
22            "Exclude Warnings"
23        } else {
24            "Include Warnings"
25        };
26
27        div().child(
28            IconButton::new("toggle-warnings", Icon::ExclamationTriangle)
29                .tooltip(move |cx| Tooltip::text(tooltip, cx))
30                .on_click(cx.listener(|this, _, cx| {
31                    if let Some(editor) = this.editor.as_ref().and_then(|editor| editor.upgrade()) {
32                        editor.update(cx, |editor, cx| {
33                            editor.toggle_warnings(&Default::default(), cx);
34                        });
35                    }
36                })),
37        )
38    }
39}
40
41impl EventEmitter<ToolbarItemEvent> for ToolbarControls {}
42
43impl ToolbarItemView for ToolbarControls {
44    fn set_active_pane_item(
45        &mut self,
46        active_pane_item: Option<&dyn ItemHandle>,
47        _: &mut ViewContext<Self>,
48    ) -> ToolbarItemLocation {
49        if let Some(pane_item) = active_pane_item.as_ref() {
50            if let Some(editor) = pane_item.downcast::<ProjectDiagnosticsEditor>() {
51                self.editor = Some(editor.downgrade());
52                ToolbarItemLocation::PrimaryRight
53            } else {
54                ToolbarItemLocation::Hidden
55            }
56        } else {
57            ToolbarItemLocation::Hidden
58        }
59    }
60}
61
62impl ToolbarControls {
63    pub fn new() -> Self {
64        ToolbarControls { editor: None }
65    }
66}