| 1 | use gpui_kit::component::button::{Button, ButtonVariants as _}; |
| 2 | use gpui_kit::component::dialog::{Dialog, DialogFooter}; |
| 3 | use gpui_kit::component::input::{Input, InputEvent, InputState}; |
| 4 | use gpui_kit::component::{WindowExt as _, v_flex}; |
| 5 | use gpui_kit::prelude::FluentBuilder as _; |
| 6 | use gpui_kit::*; |
| 7 | |
| 8 | use crate::app_state::AppState; |
| 9 | use crate::i18n::tr; |
| 10 | use crate::theme; |
| 11 | |
| 12 | const DIALOG_WIDTH: f32 = 380.; |
| 13 | const FIELD_GAP: f32 = 12.; |
| 14 | const LABEL_SIZE: f32 = 12.; |
| 15 | |
| 16 | struct JoinForm { |
| 17 | state: Entity<AppState>, |
| 18 | meeting_id: Entity<InputState>, |
| 19 | passcode: Entity<InputState>, |
| 20 | error: Option<String>, |
| 21 | _subscriptions: [Subscription; 2], |
| 22 | } |
| 23 | |
| 24 | impl 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(tr("calls.join_dialog.passcode_placeholder")) |
| 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 | |
| 67 | fn 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 | |
| 79 | impl 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( |
| 84 | &tr("calls.join_dialog.meeting_id"), |
| 85 | Input::new(&self.meeting_id), |
| 86 | )) |
| 87 | .child(labeled( |
| 88 | &tr("calls.join_dialog.passcode"), |
| 89 | Input::new(&self.passcode), |
| 90 | )) |
| 91 | .when_some(self.error.clone(), |form, error| { |
| 92 | form.child( |
| 93 | div() |
| 94 | .text_size(px(LABEL_SIZE)) |
| 95 | .text_color(theme::red_soft()) |
| 96 | .child(error), |
| 97 | ) |
| 98 | }) |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | pub fn open_join_with_id(state: Entity<AppState>, window: &mut Window, cx: &mut App) { |
| 103 | let form = cx.new(|cx| JoinForm::new(state, window, cx)); |
| 104 | let first_field = form.read(cx).meeting_id.clone(); |
| 105 | window.open_dialog(cx, move |dialog: Dialog, _, _| { |
| 106 | let submit_form = form.clone(); |
| 107 | let footer = DialogFooter::new() |
| 108 | .child( |
| 109 | Button::new("join-cancel") |
| 110 | .label(tr("common.cancel")) |
| 111 | .on_click(|_, window, cx| window.close_dialog(cx)), |
| 112 | ) |
| 113 | .child( |
| 114 | Button::new("join-confirm") |
| 115 | .primary() |
| 116 | .label(tr("common.join")) |
| 117 | .on_click(move |_, window, cx| { |
| 118 | if submit_form.update(cx, |form, cx| form.submit(cx)) { |
| 119 | window.close_dialog(cx); |
| 120 | } |
| 121 | }), |
| 122 | ); |
| 123 | dialog |
| 124 | .title(tr("calls.join_dialog.title")) |
| 125 | .w(px(DIALOG_WIDTH)) |
| 126 | .child(form.clone()) |
| 127 | .footer(footer) |
| 128 | }); |
| 129 | window.defer(cx, move |window, cx| { |
| 130 | first_field.update(cx, |input, cx| input.focus(window, cx)); |
| 131 | }); |
| 132 | } |