1use gpui::{
2 elements::{Empty, MouseEventHandler, Svg},
3 CursorStyle, Element, ElementBox, Entity, MouseButton, RenderContext, View, ViewContext,
4 ViewHandle, WeakViewHandle,
5};
6use settings::Settings;
7
8use crate::{dock::FocusDock, item::ItemHandle, StatusItemView, Workspace};
9
10pub struct TerminalButton {
11 workspace: WeakViewHandle<Workspace>,
12}
13
14impl TerminalButton {
15 pub fn new(workspace: ViewHandle<Workspace>, cx: &mut ViewContext<Self>) -> Self {
16 // When dock moves, redraw so that the icon and toggle status matches.
17 cx.subscribe(&workspace, |_, _, _, cx| cx.notify()).detach();
18
19 Self {
20 workspace: workspace.downgrade(),
21 }
22 }
23}
24
25impl Entity for TerminalButton {
26 type Event = ();
27}
28
29impl View for TerminalButton {
30 fn ui_name() -> &'static str {
31 "TerminalButton"
32 }
33
34 fn render(&mut self, cx: &mut RenderContext<'_, Self>) -> ElementBox {
35 let workspace = self.workspace.upgrade(cx);
36
37 if workspace.is_none() {
38 return Empty::new().boxed();
39 }
40
41 // let workspace = workspace.unwrap();
42 let theme = cx.global::<Settings>().theme.clone();
43
44 MouseEventHandler::<Self>::new(0, cx, {
45 let theme = theme.clone();
46 move |state, _| {
47 let style = theme
48 .workspace
49 .status_bar
50 .sidebar_buttons
51 .item
52 .style_for(state, true);
53
54 Svg::new("icons/terminal_12.svg")
55 .with_color(style.icon_color)
56 .constrained()
57 .with_width(style.icon_size)
58 .with_height(style.icon_size)
59 .contained()
60 .with_style(style.container)
61 .boxed()
62 }
63 })
64 .with_cursor_style(CursorStyle::PointingHand)
65 .on_up(MouseButton::Left, move |_, _| {
66 // let dock_pane = workspace.read(cx.app).dock_pane();
67 // let drop_index = dock_pane.read(cx.app).items_len() + 1;
68 // handle_dropped_item(event, &dock_pane.downgrade(), drop_index, false, None, cx);
69 })
70 .on_click(MouseButton::Left, |_, cx| {
71 cx.dispatch_action(FocusDock);
72 })
73 .with_tooltip::<Self, _>(
74 0,
75 "Show Terminal".into(),
76 Some(Box::new(FocusDock)),
77 theme.tooltip.clone(),
78 cx,
79 )
80 .boxed()
81 }
82}
83
84impl StatusItemView for TerminalButton {
85 fn set_active_pane_item(&mut self, _: Option<&dyn ItemHandle>, _: &mut ViewContext<Self>) {}
86}