BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / views / join_dialog.rs

join_dialog.rs

join_dialog.rs 4.1 KiB Download Raw
1use gpui_kit::component::button::{Button, ButtonVariants as _};
2use gpui_kit::component::dialog::{Dialog, DialogFooter};
3use gpui_kit::component::input::{Input, InputEvent, InputState};
4use gpui_kit::component::{WindowExt as _, v_flex};
5use gpui_kit::prelude::FluentBuilder as _;
6use gpui_kit::*;
7
8use crate::app_state::AppState;
9use crate::theme;
10
11const DIALOG_WIDTH: f32 = 380.;
12const FIELD_GAP: f32 = 12.;
13const LABEL_SIZE: f32 = 12.;
14const TITLE: &str = "Join with a meeting ID";
15
16struct JoinForm {
17 state: Entity<AppState>,
18 meeting_id: Entity<InputState>,
19 passcode: Entity<InputState>,
20 error: Option<String>,
21 _subscriptions: [Subscription; 2],
22}
23
24impl JoinForm {
25 fn new(state: Entity<AppState>, window: &mut Window, cx: &mut Context<Self>) -> Self {
26 let meeting_id = cx.new(|cx| InputState::new(window, cx).placeholder("123 456 789 012"));
27 let passcode = cx.new(|cx| {
28 InputState::new(window, cx).placeholder("Passcode (if the invitation has one)")
29 });
30 let subscriptions = [
31 cx.subscribe_in(&meeting_id, window, Self::on_enter),
32 cx.subscribe_in(&passcode, window, Self::on_enter),
33 ];
34 JoinForm {
35 state,
36 meeting_id,
37 passcode,
38 error: None,
39 _subscriptions: subscriptions,
40 }
41 }
42
43 fn on_enter(
44 &mut self,
45 _: &Entity<InputState>,
46 event: &InputEvent,
47 window: &mut Window,
48 cx: &mut Context<Self>,
49 ) {
50 if matches!(event, InputEvent::PressEnter { .. }) && self.submit(cx) {
51 window.close_dialog(cx);
52 }
53 }
54
55 fn submit(&mut self, cx: &mut Context<Self>) -> bool {
56 let meeting_id = self.meeting_id.read(cx).value().to_string();
57 let passcode = self.passcode.read(cx).value().to_string();
58 let joined = self.state.update(cx, |state, cx| {
59 state.join_meeting_by_id(&meeting_id, &passcode, cx)
60 });
61 self.error = joined.as_ref().err().cloned();
62 cx.notify();
63 joined.is_ok()
64 }
65}
66
67fn labeled(label: &str, field: impl IntoElement) -> Div {
68 v_flex()
69 .gap(px(4.))
70 .child(
71 div()
72 .text_size(px(LABEL_SIZE))
73 .text_color(theme::text_muted())
74 .child(label.to_owned()),
75 )
76 .child(field)
77}
78
79impl Render for JoinForm {
80 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
81 v_flex()
82 .gap(px(FIELD_GAP))
83 .child(labeled("Meeting ID", Input::new(&self.meeting_id)))
84 .child(labeled("Passcode", Input::new(&self.passcode)))
85 .when_some(self.error.clone(), |form, error| {
86 form.child(
87 div()
88 .text_size(px(LABEL_SIZE))
89 .text_color(theme::red_soft())
90 .child(error),
91 )
92 })
93 }
94}
95
96pub fn open_join_with_id(state: Entity<AppState>, window: &mut Window, cx: &mut App) {
97 let form = cx.new(|cx| JoinForm::new(state, window, cx));
98 let first_field = form.read(cx).meeting_id.clone();
99 window.open_dialog(cx, move |dialog: Dialog, _, _| {
100 let submit_form = form.clone();
101 let footer = DialogFooter::new()
102 .child(
103 Button::new("join-cancel")
104 .label("Cancel")
105 .on_click(|_, window, cx| window.close_dialog(cx)),
106 )
107 .child(
108 Button::new("join-confirm")
109 .primary()
110 .label("Join")
111 .on_click(move |_, window, cx| {
112 if submit_form.update(cx, |form, cx| form.submit(cx)) {
113 window.close_dialog(cx);
114 }
115 }),
116 );
117 dialog
118 .title(TITLE)
119 .w(px(DIALOG_WIDTH))
120 .child(form.clone())
121 .footer(footer)
122 });
123 window.defer(cx, move |window, cx| {
124 first_field.update(cx, |input, cx| input.focus(window, cx));
125 });
126}