toolbar_controls.rs

  1use crate::{ProjectDiagnosticsEditor, ToggleWarnings};
  2use gpui::{
  3    div, Action, CursorStyle, Div, Entity, EventEmitter, MouseButton, ParentComponent, Render,
  4    View, ViewContext, WeakView,
  5};
  6use ui::{Icon, IconButton, StyledExt};
  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        div()
 18            .h_flex()
 19            .child(IconButton::new("toggle-warnings", Icon::Warning).on_click(|view, cx| todo!()))
 20    }
 21}
 22
 23impl EventEmitter<ToolbarItemEvent> for ToolbarControls {}
 24
 25// impl View for ToolbarControls {
 26//     fn ui_name() -> &'static str {
 27//         "ToolbarControls"
 28//     }
 29
 30//     fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
 31//         let include_warnings = self
 32//             .editor
 33//             .as_ref()
 34//             .and_then(|editor| editor.upgrade(cx))
 35//             .map(|editor| editor.read(cx).include_warnings)
 36//             .unwrap_or(false);
 37//         let tooltip = if include_warnings {
 38//             "Exclude Warnings".into()
 39//         } else {
 40//             "Include Warnings".into()
 41//         };
 42//         Flex::row()
 43//             .with_child(render_toggle_button(
 44//                 0,
 45//                 "icons/warning.svg",
 46//                 include_warnings,
 47//                 (tooltip, Some(Box::new(ToggleWarnings))),
 48//                 cx,
 49//                 move |this, cx| {
 50//                     if let Some(editor) = this.editor.and_then(|editor| editor.upgrade(cx)) {
 51//                         editor.update(cx, |editor, cx| {
 52//                             editor.toggle_warnings(&Default::default(), cx)
 53//                         });
 54//                     }
 55//                 },
 56//             ))
 57//             .into_any()
 58//     }
 59// }
 60
 61impl ToolbarItemView for ToolbarControls {
 62    fn set_active_pane_item(
 63        &mut self,
 64        active_pane_item: Option<&dyn ItemHandle>,
 65        _: &mut ViewContext<Self>,
 66    ) -> ToolbarItemLocation {
 67        if let Some(pane_item) = active_pane_item.as_ref() {
 68            if let Some(editor) = pane_item.downcast::<ProjectDiagnosticsEditor>() {
 69                self.editor = Some(editor.downgrade());
 70                ToolbarItemLocation::PrimaryRight { flex: None }
 71            } else {
 72                ToolbarItemLocation::Hidden
 73            }
 74        } else {
 75            ToolbarItemLocation::Hidden
 76        }
 77    }
 78}
 79
 80impl ToolbarControls {
 81    pub fn new() -> Self {
 82        ToolbarControls { editor: None }
 83    }
 84}
 85
 86// fn render_toggle_button<
 87//     F: 'static + Fn(&mut ToolbarControls, &mut EventContext<ToolbarControls>),
 88// >(
 89//     index: usize,
 90//     icon: &'static str,
 91//     toggled: bool,
 92//     tooltip: (String, Option<Box<dyn Action>>),
 93//     cx: &mut ViewContext<ToolbarControls>,
 94//     on_click: F,
 95// ) -> AnyElement<ToolbarControls> {
 96//     enum Button {}
 97
 98//     let theme = theme::current(cx);
 99//     let (tooltip_text, action) = tooltip;
100
101//     MouseEventHandler::new::<Button, _>(index, cx, |mouse_state, _| {
102//         let style = theme
103//             .workspace
104//             .toolbar
105//             .toggleable_tool
106//             .in_state(toggled)
107//             .style_for(mouse_state);
108//         Svg::new(icon)
109//             .with_color(style.color)
110//             .constrained()
111//             .with_width(style.icon_width)
112//             .aligned()
113//             .constrained()
114//             .with_width(style.button_width)
115//             .with_height(style.button_width)
116//             .contained()
117//             .with_style(style.container)
118//     })
119//     .with_cursor_style(CursorStyle::PointingHand)
120//     .on_click(MouseButton::Left, move |_, view, cx| on_click(view, cx))
121//     .with_tooltip::<Button>(index, tooltip_text, action, theme.tooltip.clone(), cx)
122//     .into_any_named("quick action bar button")
123// }