BlueGit

ict rusty-teams

Files

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

status_message_dialog.rs

status_message_dialog.rs 11.1 KiB Download Raw
1use chrono::Local;
2use gpui_kit::assets::IconName;
3use gpui_kit::component::{
4 button::{Button, ButtonVariants as _},
5 checkbox::Checkbox,
6 h_flex,
7 input::{InputEvent, Textarea, TextareaState},
8 menu::{DropdownMenu as _, PopupMenuItem},
9 v_flex,
10};
11use gpui_kit::prelude::FluentBuilder as _;
12use gpui_kit::*;
13use teams_core::StatusNote;
14
15use super::widgets::symbol;
16use crate::app_state::AppState;
17use crate::own_status::{NOTE_DURATIONS, NOTE_LIMIT, NoteDuration, clamp_note};
18use crate::theme::{self, ElevatedSurface as _};
19
20const CARD_WIDTH: f32 = 440.;
21const CARD_RADIUS: f32 = 10.;
22const CARD_PADDING: f32 = 16.;
23const CARD_MARGIN: f32 = 32.;
24const SECTION_GAP: f32 = 12.;
25const BACKDROP_OPACITY: f32 = 0.55;
26const CLOSE_SIZE: f32 = 28.;
27const BUTTON_HEIGHT: f32 = 32.;
28const DISABLED_OPACITY: f32 = 0.4;
29const FIELD_MIN_ROWS: usize = 3;
30const FIELD_MAX_ROWS: usize = 6;
31const EXPIRY_WIDTH: f32 = 160.;
32
33pub enum StatusMessageDialogEvent {
34 Close,
35}
36
37pub struct StatusMessageDialog {
38 app: Entity<AppState>,
39 input: Entity<TextareaState>,
40 show_when_messaged: bool,
41 expiry: NoteDuration,
42 has_message: bool,
43 length: usize,
44 _subscription: Subscription,
45}
46
47impl EventEmitter<StatusMessageDialogEvent> for StatusMessageDialog {}
48
49pub fn can_finish(length: usize) -> bool {
50 length > 0
51}
52
53impl StatusMessageDialog {
54 pub fn new(app: Entity<AppState>, window: &mut Window, cx: &mut Context<Self>) -> Self {
55 let note = app.read(cx).own_status.note.clone();
56 let text = note
57 .as_ref()
58 .map(|note| note.text.clone())
59 .unwrap_or_default();
60 let input = cx.new(|cx| {
61 TextareaState::new(window, cx)
62 .auto_grow(FIELD_MIN_ROWS, FIELD_MAX_ROWS)
63 .placeholder("What's your status message?")
64 .default_value(text.clone())
65 });
66 let subscription = cx.subscribe_in(&input, window, Self::on_input_event);
67 input.update(cx, |input, cx| input.focus(window, cx));
68 StatusMessageDialog {
69 app,
70 input,
71 show_when_messaged: note.as_ref().is_some_and(|note| note.show_when_messaged),
72 expiry: NoteDuration::DEFAULT,
73 has_message: note.is_some(),
74 length: text.trim().chars().count(),
75 _subscription: subscription,
76 }
77 }
78
79 fn on_input_event(
80 &mut self,
81 input: &Entity<TextareaState>,
82 event: &InputEvent,
83 window: &mut Window,
84 cx: &mut Context<Self>,
85 ) {
86 if !matches!(event, InputEvent::Change) {
87 return;
88 }
89 let value = input.read(cx).value().to_string();
90 let clamped = clamp_note(&value);
91 if clamped != value {
92 input.update(cx, |input, cx| input.set_value(clamped.clone(), window, cx));
93 }
94 self.length = clamped.trim().chars().count();
95 cx.notify();
96 }
97
98 fn finish(&mut self, cx: &mut Context<Self>) {
99 let text = clamp_note(self.input.read(cx).value().trim());
100 if !can_finish(text.chars().count()) {
101 return;
102 }
103 let note = StatusNote {
104 text,
105 show_when_messaged: self.show_when_messaged,
106 expires_at: self.expiry.expires_at(Local::now()),
107 };
108 self.app
109 .update(cx, |state, cx| state.set_own_status_note(Some(note), cx));
110 cx.emit(StatusMessageDialogEvent::Close);
111 }
112
113 fn clear(&mut self, cx: &mut Context<Self>) {
114 self.app
115 .update(cx, |state, cx| state.set_own_status_note(None, cx));
116 cx.emit(StatusMessageDialogEvent::Close);
117 }
118
119 fn on_key_down(&mut self, event: &KeyDownEvent, _: &mut Window, cx: &mut Context<Self>) {
120 match event.keystroke.key.as_str() {
121 "escape" => cx.emit(StatusMessageDialogEvent::Close),
122 "enter" if event.keystroke.modifiers.control => self.finish(cx),
123 _ => return,
124 }
125 cx.stop_propagation();
126 }
127
128 fn header(&self, cx: &mut Context<Self>) -> Div {
129 h_flex()
130 .w_full()
131 .items_center()
132 .gap(px(SECTION_GAP))
133 .child(
134 div()
135 .flex_1()
136 .text_size(px(15.))
137 .font_weight(FontWeight::SEMIBOLD)
138 .child("Set status message"),
139 )
140 .child(
141 div()
142 .id("status-message-close")
143 .size(px(CLOSE_SIZE))
144 .flex_none()
145 .flex()
146 .items_center()
147 .justify_center()
148 .rounded_full()
149 .cursor_pointer()
150 .hover(|button| button.bg(theme::popup_hover()))
151 .on_click(cx.listener(|_, _, _, cx| cx.emit(StatusMessageDialogEvent::Close)))
152 .child(symbol("close", 16., theme::text_muted())),
153 )
154 }
155
156 fn field(&self) -> Div {
157 v_flex()
158 .w_full()
159 .gap(px(4.))
160 .child(
161 div()
162 .w_full()
163 .px(px(10.))
164 .py(px(6.))
165 .rounded(px(6.))
166 .bg(theme::popup_hover())
167 .child(Textarea::new(&self.input).appearance(false).bordered(false)),
168 )
169 .child(
170 div()
171 .w_full()
172 .flex()
173 .justify_end()
174 .text_size(px(11.))
175 .text_color(theme::text_muted())
176 .child(format!("{}/{NOTE_LIMIT}", self.length)),
177 )
178 }
179
180 fn expiry_row(&self, cx: &mut Context<Self>) -> Div {
181 let dialog = cx.entity();
182 let current = self.expiry;
183 h_flex()
184 .w_full()
185 .items_center()
186 .justify_between()
187 .gap(px(SECTION_GAP))
188 .child(div().text_size(px(13.)).child("Clear status message after"))
189 .child(
190 Button::new("status-message-expiry")
191 .outline()
192 .w(px(EXPIRY_WIDTH))
193 .label(current.label())
194 .icon(IconName::ChevronDown)
195 .dropdown_menu(move |menu, _, _| {
196 NOTE_DURATIONS.iter().fold(menu, |menu, duration| {
197 let (dialog, duration) = (dialog.clone(), *duration);
198 menu.item(
199 PopupMenuItem::new(duration.label())
200 .checked(duration == current)
201 .on_click(move |_, _, cx| {
202 dialog.update(cx, |this, cx| {
203 this.expiry = duration;
204 cx.notify();
205 });
206 }),
207 )
208 })
209 })
210 .listbox(),
211 )
212 }
213
214 fn done_button(&self, enabled: bool, cx: &mut Context<Self>) -> Stateful<Div> {
215 let button = h_flex()
216 .id("status-message-done")
217 .h(px(BUTTON_HEIGHT))
218 .px(px(16.))
219 .items_center()
220 .rounded(px(6.))
221 .bg(theme::accent())
222 .text_color(theme::on_accent())
223 .text_size(px(13.))
224 .font_weight(FontWeight::SEMIBOLD)
225 .child("Done");
226 if enabled {
227 button
228 .cursor_pointer()
229 .hover(|button| button.bg(theme::accent_text()))
230 .on_click(cx.listener(|this, _, _, cx| this.finish(cx)))
231 } else {
232 button.opacity(DISABLED_OPACITY)
233 }
234 }
235}
236
237impl Render for StatusMessageDialog {
238 fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
239 let enabled = can_finish(self.length);
240 div()
241 .id("status-message-layer")
242 .absolute()
243 .top_0()
244 .left_0()
245 .size_full()
246 .flex()
247 .items_center()
248 .justify_center()
249 .child(
250 div()
251 .id("status-message-backdrop")
252 .absolute()
253 .top_0()
254 .left_0()
255 .size_full()
256 .bg(black().opacity(BACKDROP_OPACITY))
257 .occlude()
258 .on_click(cx.listener(|_, _, _, cx| cx.emit(StatusMessageDialogEvent::Close))),
259 )
260 .child(
261 v_flex()
262 .id("status-message-card")
263 .w(px(CARD_WIDTH))
264 .max_w(relative(1.))
265 .max_h(relative(1.))
266 .m(px(CARD_MARGIN))
267 .p(px(CARD_PADDING))
268 .gap(px(SECTION_GAP))
269 .rounded(px(CARD_RADIUS))
270 .dialog_surface()
271 .text_color(theme::text())
272 .occlude()
273 .capture_key_down(cx.listener(Self::on_key_down))
274 .child(self.header(cx))
275 .child(self.field())
276 .child(
277 Checkbox::new("status-message-pinned")
278 .label("Show when people message me")
279 .checked(self.show_when_messaged)
280 .on_click(cx.listener(|this, checked: &bool, _, cx| {
281 this.show_when_messaged = *checked;
282 cx.notify();
283 })),
284 )
285 .child(self.expiry_row(cx))
286 .child(
287 h_flex()
288 .w_full()
289 .items_center()
290 .gap(px(8.))
291 .when(self.has_message, |row| {
292 row.child(
293 Button::new("status-message-clear")
294 .ghost()
295 .label("Clear message")
296 .on_click(cx.listener(|this, _, _, cx| this.clear(cx))),
297 )
298 })
299 .child(div().flex_1())
300 .child(
301 Button::new("status-message-cancel")
302 .ghost()
303 .label("Cancel")
304 .on_click(cx.listener(|_, _, _, cx| {
305 cx.emit(StatusMessageDialogEvent::Close)
306 })),
307 )
308 .child(self.done_button(enabled, cx)),
309 ),
310 )
311 }
312}
313
314#[cfg(test)]
315mod tests {
316 use super::can_finish;
317
318 #[test]
319 fn done_needs_at_least_one_character() {
320 assert!(!can_finish(0));
321 assert!(can_finish(1));
322 }
323}