manage_profiles_modal.rs

 1use gpui::{prelude::*, DismissEvent, Entity, EventEmitter, FocusHandle, Focusable, WeakEntity};
 2use ui::prelude::*;
 3use workspace::{ModalView, Workspace};
 4
 5use crate::assistant_configuration::profile_picker::{ProfilePicker, ProfilePickerDelegate};
 6use crate::ManageProfiles;
 7
 8enum Mode {
 9    ChooseProfile(Entity<ProfilePicker>),
10}
11
12pub struct ManageProfilesModal {
13    #[allow(dead_code)]
14    workspace: WeakEntity<Workspace>,
15    mode: Mode,
16}
17
18impl ManageProfilesModal {
19    pub fn register(
20        workspace: &mut Workspace,
21        _window: Option<&mut Window>,
22        _cx: &mut Context<Workspace>,
23    ) {
24        workspace.register_action(|workspace, _: &ManageProfiles, window, cx| {
25            let workspace_handle = cx.entity().downgrade();
26            workspace.toggle_modal(window, cx, |window, cx| {
27                Self::new(workspace_handle, window, cx)
28            })
29        });
30    }
31
32    pub fn new(
33        workspace: WeakEntity<Workspace>,
34        window: &mut Window,
35        cx: &mut Context<Self>,
36    ) -> Self {
37        Self {
38            workspace,
39            mode: Mode::ChooseProfile(cx.new(|cx| {
40                let delegate = ProfilePickerDelegate::new(cx);
41                ProfilePicker::new(delegate, window, cx)
42            })),
43        }
44    }
45
46    fn confirm(&mut self, _window: &mut Window, _cx: &mut Context<Self>) {}
47
48    fn cancel(&mut self, _window: &mut Window, _cx: &mut Context<Self>) {}
49}
50
51impl ModalView for ManageProfilesModal {}
52
53impl Focusable for ManageProfilesModal {
54    fn focus_handle(&self, cx: &App) -> FocusHandle {
55        match &self.mode {
56            Mode::ChooseProfile(profile_picker) => profile_picker.read(cx).focus_handle(cx),
57        }
58    }
59}
60
61impl EventEmitter<DismissEvent> for ManageProfilesModal {}
62
63impl Render for ManageProfilesModal {
64    fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
65        div()
66            .elevation_3(cx)
67            .w(rems(34.))
68            .key_context("ManageProfilesModal")
69            .on_action(cx.listener(|this, _: &menu::Cancel, window, cx| this.cancel(window, cx)))
70            .on_action(cx.listener(|this, _: &menu::Confirm, window, cx| this.confirm(window, cx)))
71            .capture_any_mouse_down(cx.listener(|this, _, window, cx| {
72                this.focus_handle(cx).focus(window);
73            }))
74            .on_mouse_down_out(cx.listener(|_this, _, _, cx| cx.emit(DismissEvent)))
75            .child(match &self.mode {
76                Mode::ChooseProfile(profile_picker) => profile_picker.clone().into_any_element(),
77            })
78    }
79}