1use std::sync::Arc;
2
3use context_menu::{ContextMenu, ContextMenuItem};
4use editor::Editor;
5use gpui::{
6 elements::*,
7 impl_internal_actions,
8 platform::{CursorStyle, MouseButton},
9 AppContext, Drawable, Element, Entity, MouseState, Subscription, View, ViewContext,
10 ViewHandle,
11};
12use settings::{settings_file::SettingsFile, Settings};
13use workspace::{
14 item::ItemHandle, notifications::simple_message_notification::OsOpen, DismissToast,
15 StatusItemView,
16};
17
18use copilot::{Copilot, Reinstall, SignIn, SignOut, Status};
19
20const COPILOT_SETTINGS_URL: &str = "https://github.com/settings/copilot";
21const COPILOT_STARTING_TOAST_ID: usize = 1337;
22const COPILOT_ERROR_TOAST_ID: usize = 1338;
23
24#[derive(Clone, PartialEq)]
25pub struct DeployCopilotMenu;
26
27#[derive(Clone, PartialEq)]
28pub struct ToggleCopilotForLanguage {
29 language: Arc<str>,
30}
31
32#[derive(Clone, PartialEq)]
33pub struct ToggleCopilotGlobally;
34
35// TODO: Make the other code path use `get_or_insert` logic for this modal
36#[derive(Clone, PartialEq)]
37pub struct DeployCopilotModal;
38
39impl_internal_actions!(
40 copilot,
41 [
42 DeployCopilotMenu,
43 DeployCopilotModal,
44 ToggleCopilotForLanguage,
45 ToggleCopilotGlobally,
46 ]
47);
48
49pub fn init(cx: &mut AppContext) {
50 cx.add_action(CopilotButton::deploy_copilot_menu);
51 cx.add_action(
52 |_: &mut CopilotButton, action: &ToggleCopilotForLanguage, cx| {
53 let language = action.language.to_owned();
54
55 let current_langauge = cx.global::<Settings>().copilot_on(Some(&language));
56
57 SettingsFile::update(cx, move |file_contents| {
58 file_contents.languages.insert(
59 language.to_owned(),
60 settings::EditorSettings {
61 copilot: Some((!current_langauge).into()),
62 ..Default::default()
63 },
64 );
65 })
66 },
67 );
68
69 cx.add_action(|_: &mut CopilotButton, _: &ToggleCopilotGlobally, cx| {
70 let copilot_on = cx.global::<Settings>().copilot_on(None);
71
72 SettingsFile::update(cx, move |file_contents| {
73 file_contents.editor.copilot = Some((!copilot_on).into())
74 })
75 });
76}
77
78pub struct CopilotButton {
79 popup_menu: ViewHandle<ContextMenu>,
80 editor_subscription: Option<(Subscription, usize)>,
81 editor_enabled: Option<bool>,
82 language: Option<Arc<str>>,
83}
84
85impl Entity for CopilotButton {
86 type Event = ();
87}
88
89impl View for CopilotButton {
90 fn ui_name() -> &'static str {
91 "CopilotButton"
92 }
93
94 fn render(&mut self, cx: &mut ViewContext<Self>) -> Element<Self> {
95 let settings = cx.global::<Settings>();
96
97 if !settings.enable_copilot_integration {
98 return Empty::new().boxed();
99 }
100
101 let theme = settings.theme.clone();
102 let active = self.popup_menu.read(cx).visible();
103 let Some(copilot) = Copilot::global(cx) else {
104 return Empty::new().boxed();
105 };
106 let status = copilot.read(cx).status();
107
108 let enabled = self.editor_enabled.unwrap_or(settings.copilot_on(None));
109
110 let view_id = cx.view_id();
111
112 Stack::new()
113 .with_child(
114 MouseEventHandler::<Self, _>::new(0, cx, {
115 let theme = theme.clone();
116 let status = status.clone();
117 move |state, _cx| {
118 let style = theme
119 .workspace
120 .status_bar
121 .sidebar_buttons
122 .item
123 .style_for(state, active);
124
125 Flex::row()
126 .with_child(
127 Svg::new({
128 match status {
129 Status::Error(_) => "icons/copilot_error_16.svg",
130 Status::Authorized => {
131 if enabled {
132 "icons/copilot_16.svg"
133 } else {
134 "icons/copilot_disabled_16.svg"
135 }
136 }
137 _ => "icons/copilot_init_16.svg",
138 }
139 })
140 .with_color(style.icon_color)
141 .constrained()
142 .with_width(style.icon_size)
143 .aligned()
144 .named("copilot-icon"),
145 )
146 .constrained()
147 .with_height(style.icon_size)
148 .contained()
149 .with_style(style.container)
150 .boxed()
151 }
152 })
153 .with_cursor_style(CursorStyle::PointingHand)
154 .on_click(MouseButton::Left, {
155 let status = status.clone();
156 move |_, _, cx| match status {
157 Status::Authorized => cx.dispatch_action(DeployCopilotMenu),
158 Status::Starting { ref task } => {
159 cx.dispatch_action(workspace::Toast::new(
160 COPILOT_STARTING_TOAST_ID,
161 "Copilot is starting...",
162 ));
163 let window_id = cx.window_id();
164 let task = task.to_owned();
165 cx.spawn(|this, mut cx| async move {
166 task.await;
167 cx.update(|cx| {
168 if let Some(copilot) = Copilot::global(cx) {
169 let status = copilot.read(cx).status();
170 match status {
171 Status::Authorized => cx.dispatch_action_at(
172 window_id,
173 view_id,
174 workspace::Toast::new(
175 COPILOT_STARTING_TOAST_ID,
176 "Copilot has started!",
177 ),
178 ),
179 _ => {
180 cx.dispatch_action_at(
181 window_id,
182 view_id,
183 DismissToast::new(COPILOT_STARTING_TOAST_ID),
184 );
185 cx.dispatch_global_action(SignIn)
186 }
187 }
188 }
189 })
190 })
191 .detach();
192 }
193 Status::Error(ref e) => cx.dispatch_action(workspace::Toast::new_action(
194 COPILOT_ERROR_TOAST_ID,
195 format!("Copilot can't be started: {}", e),
196 "Reinstall Copilot",
197 Reinstall,
198 )),
199 _ => cx.dispatch_action(SignIn),
200 }
201 })
202 .with_tooltip::<Self>(0, "GitHub Copilot".into(), None, theme.tooltip.clone(), cx)
203 .boxed(),
204 )
205 .with_child(
206 ChildView::new(&self.popup_menu, cx)
207 .aligned()
208 .top()
209 .right()
210 .boxed(),
211 )
212 .boxed()
213 }
214}
215
216impl CopilotButton {
217 pub fn new(cx: &mut ViewContext<Self>) -> Self {
218 let menu = cx.add_view(|cx| {
219 let mut menu = ContextMenu::new(cx);
220 menu.set_position_mode(OverlayPositionMode::Local);
221 menu
222 });
223
224 cx.observe(&menu, |_, _, cx| cx.notify()).detach();
225
226 Copilot::global(cx).map(|copilot| cx.observe(&copilot, |_, _, cx| cx.notify()).detach());
227
228 cx.observe_global::<Settings, _>(move |_, cx| cx.notify())
229 .detach();
230
231 Self {
232 popup_menu: menu,
233 editor_subscription: None,
234 editor_enabled: None,
235 language: None,
236 }
237 }
238
239 pub fn deploy_copilot_menu(&mut self, _: &DeployCopilotMenu, cx: &mut ViewContext<Self>) {
240 let settings = cx.global::<Settings>();
241
242 let mut menu_options = Vec::with_capacity(6);
243
244 if let Some(language) = &self.language {
245 let language_enabled = settings.copilot_on(Some(language.as_ref()));
246
247 menu_options.push(ContextMenuItem::item(
248 format!(
249 "{} Copilot for {}",
250 if language_enabled {
251 "Disable"
252 } else {
253 "Enable"
254 },
255 language
256 ),
257 ToggleCopilotForLanguage {
258 language: language.to_owned(),
259 },
260 ));
261 }
262
263 let globally_enabled = cx.global::<Settings>().copilot_on(None);
264 menu_options.push(ContextMenuItem::item(
265 if globally_enabled {
266 "Disable Copilot Globally"
267 } else {
268 "Enable Copilot Globally"
269 },
270 ToggleCopilotGlobally,
271 ));
272
273 menu_options.push(ContextMenuItem::Separator);
274
275 let icon_style = settings.theme.copilot.out_link_icon.clone();
276 menu_options.push(ContextMenuItem::element_item(
277 Box::new(
278 move |state: &mut MouseState, style: &theme::ContextMenuItem| {
279 Flex::row()
280 .with_children([
281 Label::new("Copilot Settings", style.label.clone()).boxed(),
282 theme::ui::icon(icon_style.style_for(state, false)).boxed(),
283 ])
284 .align_children_center()
285 .boxed()
286 },
287 ),
288 OsOpen::new(COPILOT_SETTINGS_URL),
289 ));
290
291 menu_options.push(ContextMenuItem::item("Sign Out", SignOut));
292
293 self.popup_menu.update(cx, |menu, cx| {
294 menu.show(
295 Default::default(),
296 AnchorCorner::BottomRight,
297 menu_options,
298 cx,
299 );
300 });
301 }
302
303 pub fn update_enabled(&mut self, editor: ViewHandle<Editor>, cx: &mut ViewContext<Self>) {
304 let editor = editor.read(cx);
305
306 let snapshot = editor.buffer().read(cx).snapshot(cx);
307 let settings = cx.global::<Settings>();
308 let suggestion_anchor = editor.selections.newest_anchor().start;
309
310 let language_name = snapshot
311 .language_at(suggestion_anchor)
312 .map(|language| language.name());
313
314 self.language = language_name.clone();
315
316 self.editor_enabled = Some(settings.copilot_on(language_name.as_deref()));
317
318 cx.notify()
319 }
320}
321
322impl StatusItemView for CopilotButton {
323 fn set_active_pane_item(&mut self, item: Option<&dyn ItemHandle>, cx: &mut ViewContext<Self>) {
324 if let Some(editor) = item.map(|item| item.act_as::<Editor>(cx)).flatten() {
325 self.editor_subscription =
326 Some((cx.observe(&editor, Self::update_enabled), editor.id()));
327 self.update_enabled(editor, cx);
328 } else {
329 self.language = None;
330 self.editor_subscription = None;
331 self.editor_enabled = None;
332 }
333 cx.notify();
334 }
335}