toolbar_controls.rs

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