1// use context_menu::{ContextMenu, ContextMenuItem};
2// use gpui::{
3// elements::*, impl_internal_actions, CursorStyle, Element, ElementBox, Entity, MouseButton,
4// MutableAppContext, RenderContext, View, ViewContext, ViewHandle, WeakModelHandle,
5// WeakViewHandle,
6// };
7// use settings::Settings;
8// use std::any::TypeId;
9// use workspace::{dock::FocusDock, item::ItemHandle, NewTerminal, StatusItemView, Workspace};
10
11// #[derive(Clone, PartialEq)]
12// pub struct DeployTerminalMenu;
13
14// impl_internal_actions!(terminal, [DeployTerminalMenu]);
15
16// pub fn init(cx: &mut MutableAppContext) {
17// cx.add_action(CopilotButton::deploy_terminal_menu);
18// }
19
20// pub struct CopilotButton {
21// workspace: WeakViewHandle<Workspace>,
22// popup_menu: ViewHandle<ContextMenu>,
23// }
24
25// impl Entity for CopilotButton {
26// type Event = ();
27// }
28
29// impl View for CopilotButton {
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// let project = match workspace {
37// Some(workspace) => workspace.read(cx).project().read(cx),
38// None => return Empty::new().boxed(),
39// };
40
41// let focused_view = cx.focused_view_id(cx.window_id());
42// let active = focused_view
43// .map(|view_id| {
44// cx.view_type_id(cx.window_id(), view_id) == Some(TypeId::of::<TerminalView>())
45// })
46// .unwrap_or(false);
47
48// let has_terminals = !project.local_terminal_handles().is_empty();
49// let terminal_count = project.local_terminal_handles().len() as i32;
50// let theme = cx.global::<Settings>().theme.clone();
51
52// Stack::new()
53// .with_child(
54// MouseEventHandler::<Self>::new(0, cx, {
55// let theme = theme.clone();
56// move |state, _cx| {
57// let style = theme
58// .workspace
59// .status_bar
60// .sidebar_buttons
61// .item
62// .style_for(state, active);
63
64// Flex::row()
65// .with_child(
66// Svg::new("icons/terminal_12.svg")
67// .with_color(style.icon_color)
68// .constrained()
69// .with_width(style.icon_size)
70// .aligned()
71// .named("terminals-icon"),
72// )
73// .with_children(has_terminals.then(|| {
74// Label::new(terminal_count.to_string(), style.label.text.clone())
75// .contained()
76// .with_style(style.label.container)
77// .aligned()
78// .boxed()
79// }))
80// .constrained()
81// .with_height(style.icon_size)
82// .contained()
83// .with_style(style.container)
84// .boxed()
85// }
86// })
87// .with_cursor_style(CursorStyle::PointingHand)
88// .on_click(MouseButton::Left, move |_, cx| {
89// if has_terminals {
90// cx.dispatch_action(DeployTerminalMenu);
91// } else {
92// if !active {
93// cx.dispatch_action(FocusDock);
94// }
95// };
96// })
97// .with_tooltip::<Self, _>(
98// 0,
99// "Show Terminal".into(),
100// Some(Box::new(FocusDock)),
101// theme.tooltip.clone(),
102// cx,
103// )
104// .boxed(),
105// )
106// .with_child(
107// ChildView::new(&self.popup_menu, cx)
108// .aligned()
109// .top()
110// .right()
111// .boxed(),
112// )
113// .boxed()
114// }
115// }
116
117// impl CopilotButton {
118// pub fn new(workspace: ViewHandle<Workspace>, cx: &mut ViewContext<Self>) -> Self {
119// cx.observe(&workspace, |_, _, cx| cx.notify()).detach();
120// Self {
121// workspace: workspace.downgrade(),
122// popup_menu: cx.add_view(|cx| {
123// let mut menu = ContextMenu::new(cx);
124// menu.set_position_mode(OverlayPositionMode::Local);
125// menu
126// }),
127// }
128// }
129
130// pub fn deploy_terminal_menu(
131// &mut self,
132// _action: &DeployTerminalMenu,
133// cx: &mut ViewContext<Self>,
134// ) {
135// let mut menu_options = vec![ContextMenuItem::item("New Terminal", NewTerminal)];
136
137// if let Some(workspace) = self.workspace.upgrade(cx) {
138// let project = workspace.read(cx).project().read(cx);
139// let local_terminal_handles = project.local_terminal_handles();
140
141// if !local_terminal_handles.is_empty() {
142// menu_options.push(ContextMenuItem::Separator)
143// }
144
145// for local_terminal_handle in local_terminal_handles {
146// if let Some(terminal) = local_terminal_handle.upgrade(cx) {
147// menu_options.push(ContextMenuItem::item(
148// terminal.read(cx).title(),
149// // FocusTerminal {
150// // terminal_handle: local_terminal_handle.clone(),
151// // },
152// ))
153// }
154// }
155// }
156
157// self.popup_menu.update(cx, |menu, cx| {
158// menu.show(
159// Default::default(),
160// AnchorCorner::BottomRight,
161// menu_options,
162// cx,
163// );
164// });
165// }
166// }
167
168// impl StatusItemView for CopilotButton {
169// fn set_active_pane_item(&mut self, _: Option<&dyn ItemHandle>, cx: &mut ViewContext<Self>) {
170// cx.notify();
171// }
172// }