1use gpui::{
2 elements::{Flex, Label, MouseEventHandler, ParentElement, Stack},
3 Axis, Element, Entity, View, ViewContext,
4};
5use settings::Settings;
6
7use crate::{Copilot, PromptingUser};
8
9pub enum Event {
10 Dismiss,
11}
12
13pub struct AuthModal {}
14
15impl Entity for AuthModal {
16 type Event = Event;
17}
18
19impl View for AuthModal {
20 fn ui_name() -> &'static str {
21 "AuthModal"
22 }
23
24 fn render(&mut self, cx: &mut gpui::RenderContext<'_, Self>) -> gpui::ElementBox {
25 let style = cx.global::<Settings>().theme.copilot.clone();
26
27 let user_code_and_url = Copilot::global(cx).read(cx).prompting_user().cloned();
28 let auth_text = style.auth_text.clone();
29 MouseEventHandler::<AuthModal>::new(0, cx, move |_state, cx| {
30 Stack::new()
31 .with_child(match user_code_and_url {
32 Some(PromptingUser {
33 user_code,
34 verification_uri,
35 }) => Flex::new(Axis::Vertical)
36 .with_children([
37 Label::new(user_code, auth_text.clone())
38 .constrained()
39 .with_width(540.)
40 .boxed(),
41 MouseEventHandler::<AuthModal>::new(1, cx, move |_state, _cx| {
42 Label::new("Click here to open github!", auth_text.clone())
43 .constrained()
44 .with_width(540.)
45 .boxed()
46 })
47 .on_click(gpui::MouseButton::Left, move |_click, cx| {
48 cx.platform().open_url(&verification_uri)
49 })
50 .with_cursor_style(gpui::CursorStyle::PointingHand)
51 .boxed(),
52 ])
53 .boxed(),
54 None => Label::new("Not signing in", style.auth_text.clone())
55 .constrained()
56 .with_width(540.)
57 .boxed(),
58 })
59 .contained()
60 .with_style(style.auth_modal)
61 .constrained()
62 .with_max_width(540.)
63 .with_max_height(420.)
64 .named("Copilot Authentication status modal")
65 })
66 .on_hover(|_, _| {})
67 .on_click(gpui::MouseButton::Left, |_, _| {})
68 .on_click(gpui::MouseButton::Left, |_, _| {})
69 .boxed()
70 }
71
72 fn focus_out(&mut self, _: gpui::AnyViewHandle, cx: &mut ViewContext<Self>) {
73 cx.emit(Event::Dismiss)
74 }
75}
76
77impl AuthModal {
78 pub fn new(cx: &mut ViewContext<Self>) -> Self {
79 cx.observe(&Copilot::global(cx), |_, _, cx| cx.notify())
80 .detach();
81
82 AuthModal {}
83 }
84}