BlueGit

ict rusty-teams

Files

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

group_name.rs

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