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