| 1 | use std::cell::RefCell; |
| 2 | use std::rc::Rc; |
| 3 | |
| 4 | use chatsvc::{INVALID_TOPIC_CHARACTER, MAX_TOPIC_LENGTH, TopicProblem, topic_problem}; |
| 5 | use gpui_kit::assets::IconName; |
| 6 | use gpui_kit::base::PopoverState; |
| 7 | use gpui_kit::component::button::{Button, ButtonVariants as _}; |
| 8 | use gpui_kit::component::input::{Input, InputEvent, InputState}; |
| 9 | use gpui_kit::component::popover::Popover; |
| 10 | use gpui_kit::component::{Disableable as _, h_flex, v_flex}; |
| 11 | use gpui_kit::prelude::FluentBuilder as _; |
| 12 | use gpui_kit::*; |
| 13 | |
| 14 | use super::widgets::icon; |
| 15 | use crate::app_state::AppState; |
| 16 | use crate::i18n::tr; |
| 17 | use crate::theme; |
| 18 | |
| 19 | pub const GROUP_NAME_FORM: &str = "shell/islands/main/group-name-form"; |
| 20 | pub const GROUP_NAME_BUTTON: &str = "shell/islands/main/group-name-button"; |
| 21 | const BUTTON_TOOLTIP: &str = "Name group chat"; |
| 22 | const FIELD_LABEL: &str = "Group name"; |
| 23 | const PLACEHOLDER: &str = "Type a group name"; |
| 24 | const FORM_WIDTH: f32 = 300.; |
| 25 | const FORM_PADDING: f32 = 12.; |
| 26 | const FORM_GAP: f32 = 8.; |
| 27 | const LABEL_SIZE: f32 = 12.; |
| 28 | const ICON_SIZE: f32 = 16.; |
| 29 | |
| 30 | pub fn problem_text(problem: TopicProblem) -> String { |
| 31 | match problem { |
| 32 | TopicProblem::TooLong => { |
| 33 | format!("Your group name can't be longer than {MAX_TOPIC_LENGTH} characters.") |
| 34 | } |
| 35 | TopicProblem::InvalidCharacter => { |
| 36 | format!("'{INVALID_TOPIC_CHARACTER}' is not a valid character in group names.") |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | struct GroupNameForm { |
| 42 | app: Entity<AppState>, |
| 43 | chat_id: String, |
| 44 | input: Entity<InputState>, |
| 45 | popover: WeakEntity<PopoverState>, |
| 46 | _subscription: Subscription, |
| 47 | } |
| 48 | |
| 49 | impl GroupNameForm { |
| 50 | fn new( |
| 51 | app: Entity<AppState>, |
| 52 | chat_id: String, |
| 53 | title: String, |
| 54 | popover: WeakEntity<PopoverState>, |
| 55 | window: &mut Window, |
| 56 | cx: &mut Context<Self>, |
| 57 | ) -> Self { |
| 58 | let input = cx.new(|cx| { |
| 59 | let mut state = InputState::new(window, cx).placeholder(PLACEHOLDER); |
| 60 | state.set_value(title, window, cx); |
| 61 | state |
| 62 | }); |
| 63 | input.update(cx, |state, cx| state.focus(window, cx)); |
| 64 | let subscription = cx.subscribe_in(&input, window, Self::on_input_event); |
| 65 | GroupNameForm { |
| 66 | app, |
| 67 | chat_id, |
| 68 | input, |
| 69 | popover, |
| 70 | _subscription: subscription, |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | fn on_input_event( |
| 75 | &mut self, |
| 76 | _: &Entity<InputState>, |
| 77 | event: &InputEvent, |
| 78 | window: &mut Window, |
| 79 | cx: &mut Context<Self>, |
| 80 | ) { |
| 81 | match event { |
| 82 | InputEvent::Change => cx.notify(), |
| 83 | InputEvent::PressEnter { .. } => self.save(window, cx), |
| 84 | _ => {} |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | fn problem(&self, cx: &App) -> Option<TopicProblem> { |
| 89 | topic_problem(self.input.read(cx).value().trim()) |
| 90 | } |
| 91 | |
| 92 | fn close(&self, window: &mut Window, cx: &mut App) { |
| 93 | self.popover |
| 94 | .update(cx, |state, cx| state.dismiss(window, cx)) |
| 95 | .ok(); |
| 96 | } |
| 97 | |
| 98 | fn save(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 99 | if self.problem(cx).is_some() { |
| 100 | return; |
| 101 | } |
| 102 | let topic = self.input.read(cx).value().trim().to_owned(); |
| 103 | let chat_id = self.chat_id.clone(); |
| 104 | self.app |
| 105 | .update(cx, |state, cx| state.rename_chat(&chat_id, &topic, cx)); |
| 106 | self.close(window, cx); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | impl Render for GroupNameForm { |
| 111 | fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { |
| 112 | let problem = self.problem(cx); |
| 113 | v_flex() |
| 114 | .debug_selector(|| GROUP_NAME_FORM.into()) |
| 115 | .w(px(FORM_WIDTH)) |
| 116 | .p(px(FORM_PADDING)) |
| 117 | .gap(px(FORM_GAP)) |
| 118 | .child( |
| 119 | div() |
| 120 | .text_size(px(LABEL_SIZE)) |
| 121 | .text_color(theme::text_soft()) |
| 122 | .child(FIELD_LABEL), |
| 123 | ) |
| 124 | .child(Input::new(&self.input)) |
| 125 | .when_some(problem, |form, problem| { |
| 126 | form.child( |
| 127 | div() |
| 128 | .text_size(px(LABEL_SIZE)) |
| 129 | .text_color(theme::red_soft()) |
| 130 | .child(problem_text(problem)), |
| 131 | ) |
| 132 | }) |
| 133 | .child( |
| 134 | h_flex() |
| 135 | .justify_end() |
| 136 | .gap(px(FORM_GAP)) |
| 137 | .child( |
| 138 | Button::new("group-name-cancel") |
| 139 | .label(tr("common.cancel")) |
| 140 | .on_click(cx.listener(|form, _, window, cx| form.close(window, cx))), |
| 141 | ) |
| 142 | .child( |
| 143 | Button::new("group-name-save") |
| 144 | .primary() |
| 145 | .label(tr("common.save")) |
| 146 | .disabled(problem.is_some()) |
| 147 | .on_click(cx.listener(|form, _, window, cx| form.save(window, cx))), |
| 148 | ), |
| 149 | ) |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | #[derive(Clone, Default)] |
| 154 | pub struct GroupNameSlot(Rc<RefCell<Option<Entity<GroupNameForm>>>>); |
| 155 | |
| 156 | pub fn group_name_button( |
| 157 | app: &Entity<AppState>, |
| 158 | slot: &GroupNameSlot, |
| 159 | chat_id: &str, |
| 160 | title: &str, |
| 161 | ) -> AnyElement { |
| 162 | let (app, chat_id, title) = (app.clone(), chat_id.to_owned(), title.to_owned()); |
| 163 | let form = slot.0.clone(); |
| 164 | let reset = form.clone(); |
| 165 | Popover::new("group-name") |
| 166 | .anchor(Anchor::TopLeft) |
| 167 | .p(px(0.)) |
| 168 | .trigger( |
| 169 | Button::new("group-name-button") |
| 170 | .ghost() |
| 171 | .compact() |
| 172 | .tooltip(BUTTON_TOOLTIP) |
| 173 | .debug_selector(|| GROUP_NAME_BUTTON.into()) |
| 174 | .child(icon(IconName::Pencil, ICON_SIZE, theme::text_soft())), |
| 175 | ) |
| 176 | .on_open_change(move |open, _, _| { |
| 177 | if !*open { |
| 178 | reset.borrow_mut().take(); |
| 179 | } |
| 180 | }) |
| 181 | .content(move |_, window, cx| { |
| 182 | let existing = form.borrow().clone(); |
| 183 | if let Some(existing) = existing.filter(|existing| existing.read(cx).chat_id == chat_id) |
| 184 | { |
| 185 | return existing; |
| 186 | } |
| 187 | let popover = cx.entity().downgrade(); |
| 188 | let (app, chat_id, title) = (app.clone(), chat_id.clone(), title.clone()); |
| 189 | let created = cx.new(|cx| GroupNameForm::new(app, chat_id, title, popover, window, cx)); |
| 190 | *form.borrow_mut() = Some(created.clone()); |
| 191 | created |
| 192 | }) |
| 193 | .into_any_element() |
| 194 | } |
| 195 | |
| 196 | #[cfg(test)] |
| 197 | mod tests { |
| 198 | use super::{TopicProblem, problem_text}; |
| 199 | |
| 200 | #[test] |
| 201 | fn problems_use_the_teams_wording() { |
| 202 | assert_eq!( |
| 203 | problem_text(TopicProblem::TooLong), |
| 204 | "Your group name can't be longer than 250 characters." |
| 205 | ); |
| 206 | assert_eq!( |
| 207 | problem_text(TopicProblem::InvalidCharacter), |
| 208 | "':' is not a valid character in group names." |
| 209 | ); |
| 210 | } |
| 211 | } |