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