Barebones Copilot prompt

Filter out sign in/sign out when user is signed in/not signed in
This commit is contained in:
Piotr Osiewicz 2023-12-06 14:14:18 +01:00
parent 3f9fe58c48
commit 7998e8281c
2 changed files with 70 additions and 33 deletions

View file

@ -60,8 +60,8 @@ pub fn init(
TypeId::of::<PreviousSuggestion>(), TypeId::of::<PreviousSuggestion>(),
TypeId::of::<Reinstall>(), TypeId::of::<Reinstall>(),
]; ];
let copilot_auth_action_types = [TypeId::of::<SignIn>(), TypeId::of::<SignOut>()]; let copilot_auth_action_types = [TypeId::of::<SignOut>()];
let copilot_no_auth_action_types = [TypeId::of::<SignIn>()];
let status = handle.read(cx).status(); let status = handle.read(cx).status();
let filter = cx.default_global::<collections::CommandPaletteFilter>(); let filter = cx.default_global::<collections::CommandPaletteFilter>();
@ -69,8 +69,14 @@ pub fn init(
Status::Disabled => { Status::Disabled => {
filter.hidden_action_types.extend(copilot_action_types); filter.hidden_action_types.extend(copilot_action_types);
filter.hidden_action_types.extend(copilot_auth_action_types); filter.hidden_action_types.extend(copilot_auth_action_types);
filter
.hidden_action_types
.extend(copilot_no_auth_action_types);
} }
Status::Authorized => { Status::Authorized => {
filter
.hidden_action_types
.extend(copilot_no_auth_action_types);
for type_id in copilot_action_types for type_id in copilot_action_types
.iter() .iter()
.chain(&copilot_auth_action_types) .chain(&copilot_auth_action_types)
@ -80,7 +86,8 @@ pub fn init(
} }
_ => { _ => {
filter.hidden_action_types.extend(copilot_action_types); filter.hidden_action_types.extend(copilot_action_types);
for type_id in &copilot_auth_action_types { filter.hidden_action_types.extend(copilot_auth_action_types);
for type_id in &copilot_no_auth_action_types {
filter.hidden_action_types.remove(type_id); filter.hidden_action_types.remove(type_id);
} }
} }
@ -97,6 +104,7 @@ pub fn init(
} }
}); });
cx.on_action(|_: &SignOut, cx| { cx.on_action(|_: &SignOut, cx| {
dbg!("Signing out");
if let Some(copilot) = Copilot::global(cx) { if let Some(copilot) = Copilot::global(cx) {
copilot copilot
.update(cx, |copilot, cx| copilot.sign_out(cx)) .update(cx, |copilot, cx| copilot.sign_out(cx))

View file

@ -13,13 +13,12 @@ const COPILOT_SIGN_UP_URL: &'static str = "https://github.com/features/copilot";
use crate::{request::PromptUserDeviceFlow, Copilot, Status}; use crate::{request::PromptUserDeviceFlow, Copilot, Status};
use gpui::{ use gpui::{
div, px, red, size, AnyElement, AppContext, Bounds, ClipboardItem, Div, Element, GlobalPixels, div, size, AppContext, Bounds, ClipboardItem, Div, Element, GlobalPixels, InteractiveElement,
InteractiveElement, IntoElement, MouseButton, ParentElement, Point, Render, Stateful, IntoElement, ParentElement, Point, Render, Stateful, Styled, ViewContext, VisualContext,
StatefulInteractiveElement, Styled, ViewContext, VisualContext, WindowBounds, WindowHandle, WindowBounds, WindowHandle, WindowKind, WindowOptions,
WindowKind, WindowOptions,
}; };
use theme::ActiveTheme; use theme::ActiveTheme;
use ui::{h_stack, v_stack, Button, Clickable, Icon, IconElement, Label}; use ui::{h_stack, v_stack, Button, Clickable, Color, Icon, IconElement, Label};
pub fn init(cx: &mut AppContext) { pub fn init(cx: &mut AppContext) {
if let Some(copilot) = Copilot::global(cx) { if let Some(copilot) = Copilot::global(cx) {
@ -56,7 +55,9 @@ pub fn init(cx: &mut AppContext) {
} }
_ => { _ => {
if let Some(code_verification) = verification_window.take() { if let Some(code_verification) = verification_window.take() {
code_verification.update(cx, |_, cx| cx.remove_window()); code_verification
.update(cx, |_, cx| cx.remove_window())
.ok();
} }
} }
} }
@ -118,12 +119,12 @@ impl CopilotCodeVerification {
.on_mouse_down(gpui::MouseButton::Left, { .on_mouse_down(gpui::MouseButton::Left, {
let user_code = data.user_code.clone(); let user_code = data.user_code.clone();
move |_, cx| { move |_, cx| {
dbg!("Copied");
cx.write_to_clipboard(ClipboardItem::new(user_code.clone())); cx.write_to_clipboard(ClipboardItem::new(user_code.clone()));
cx.notify(); cx.notify();
} }
}) })
.child(Label::new(data.user_code.clone())) .child(Label::new(data.user_code.clone()))
.child(div())
.child(Label::new(if copied { "Copied!" } else { "Copy" })) .child(Label::new(if copied { "Copied!" } else { "Copy" }))
// MouseEventHandler::new::<Self, _>(0, cx, |state, _cx| { // MouseEventHandler::new::<Self, _>(0, cx, |state, _cx| {
@ -170,24 +171,18 @@ impl CopilotCodeVerification {
"Connect to Github" "Connect to Github"
}; };
v_stack() v_stack()
.child( .flex_1()
v_stack() .items_center()
.flex_1() .justify_between()
.w_full() .w_full()
.items_center() .child(Label::new(
.justify_between() "Enable Copilot by connecting your existing license",
.children([ ))
h_stack()
.items_center()
.child(Label::new("Enable Copilot by connecting")),
h_stack()
.items_center()
.child(Label::new("your existing license")),
]),
)
.child(Self::render_device_code(data, cx)) .child(Self::render_device_code(data, cx))
.child(Label::new("Paste this code into GitHub after").size(ui::LabelSize::Small)) .child(
.child(Label::new("clicking the button below.").size(ui::LabelSize::Small)) Label::new("Paste this code into GitHub after clicking the button below.")
.size(ui::LabelSize::Small),
)
.child( .child(
Button::new("connect-button", connect_button_label).on_click({ Button::new("connect-button", connect_button_label).on_click({
let verification_uri = data.verification_uri.clone(); let verification_uri = data.verification_uri.clone();
@ -236,6 +231,17 @@ impl CopilotCodeVerification {
// )) // ))
// .align_children_center() // .align_children_center()
} }
fn render_enabled_modal() -> impl Element {
v_stack()
.child(Label::new("Copilot Enabled!"))
.child(Label::new(
"You can update your settings or sign out from the Copilot menu in the status bar.",
))
.child(
Button::new("copilot-enabled-done-button", "Done")
.on_click(|_, cx| cx.remove_window()),
)
}
// fn render_enabled_modal( // fn render_enabled_modal(
// style: &theme::Copilot, // style: &theme::Copilot,
@ -280,7 +286,22 @@ impl CopilotCodeVerification {
// .align_children_center() // .align_children_center()
// .into_any() // .into_any()
// } // }
fn render_unauthorized_modal() -> impl Element {
v_stack()
.child(Label::new(
"Enable Copilot by connecting your existing license.",
))
.child(
Label::new("You must have an active Copilot license to use it in Zed.")
.color(Color::Warning),
)
.child(
Button::new("copilot-subscribe-button", "Subscibe on Github").on_click(|_, cx| {
cx.remove_window();
cx.open_url(COPILOT_SIGN_UP_URL)
}),
)
}
// fn render_unauthorized_modal( // fn render_unauthorized_modal(
// style: &theme::Copilot, // style: &theme::Copilot,
// cx: &mut ViewContext<Self>, // cx: &mut ViewContext<Self>,
@ -344,8 +365,18 @@ impl Render for CopilotCodeVerification {
fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element { fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
let prompt = match &self.status { let prompt = match &self.status {
Status::SigningIn { prompt } => prompt.as_ref(), Status::SigningIn {
_ => None, prompt: Some(prompt),
} => Self::render_prompting_modal(self.connect_clicked, &prompt, cx).into_any_element(),
Status::Unauthorized => {
self.connect_clicked = false;
Self::render_unauthorized_modal().into_any_element()
}
Status::Authorized => {
self.connect_clicked = false;
Self::render_enabled_modal().into_any_element()
}
_ => div().into_any_element(),
}; };
div() div()
.id("copilot code verification") .id("copilot code verification")
@ -357,9 +388,7 @@ impl Render for CopilotCodeVerification {
.bg(cx.theme().colors().element_background) .bg(cx.theme().colors().element_background)
.child(ui::Label::new("Connect Copilot to Zed")) .child(ui::Label::new("Connect Copilot to Zed"))
.child(IconElement::new(Icon::ZedXCopilot)) .child(IconElement::new(Icon::ZedXCopilot))
.children( .child(prompt)
prompt.map(|data| Self::render_prompting_modal(self.connect_clicked, data, cx)),
)
} }
} }