BlueGit

ict rusty-teams

Files

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

status_message_dialog.rs

status_message_dialog.rs 11.0 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 )
211 }
212
213 fn done_button(&self, enabled: bool, cx: &mut Context<Self>) -> Stateful<Div> {
214 let button = h_flex()
215 .id("status-message-done")
216 .h(px(BUTTON_HEIGHT))
217 .px(px(16.))
218 .items_center()
219 .rounded(px(6.))
220 .bg(theme::accent())
221 .text_color(theme::on_accent())
222 .text_size(px(13.))
223 .font_weight(FontWeight::SEMIBOLD)
224 .child("Done");
225 if enabled {
226 button
227 .cursor_pointer()
228 .hover(|button| button.bg(theme::accent_text()))
229 .on_click(cx.listener(|this, _, _, cx| this.finish(cx)))
230 } else {
231 button.opacity(DISABLED_OPACITY)
232 }
233 }
234}
235
236impl Render for StatusMessageDialog {
237 fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
238 let enabled = can_finish(self.length);
239 div()
240 .id("status-message-layer")
241 .absolute()
242 .top_0()
243 .left_0()
244 .size_full()
245 .flex()
246 .items_center()
247 .justify_center()
248 .child(
249 div()
250 .id("status-message-backdrop")
251 .absolute()
252 .top_0()
253 .left_0()
254 .size_full()
255 .bg(black().opacity(BACKDROP_OPACITY))
256 .occlude()
257 .on_click(cx.listener(|_, _, _, cx| cx.emit(StatusMessageDialogEvent::Close))),
258 )
259 .child(
260 v_flex()
261 .id("status-message-card")
262 .w(px(CARD_WIDTH))
263 .max_w(relative(1.))
264 .max_h(relative(1.))
265 .m(px(CARD_MARGIN))
266 .p(px(CARD_PADDING))
267 .gap(px(SECTION_GAP))
268 .rounded(px(CARD_RADIUS))
269 .dialog_surface()
270 .text_color(theme::text())
271 .occlude()
272 .capture_key_down(cx.listener(Self::on_key_down))
273 .child(self.header(cx))
274 .child(self.field())
275 .child(
276 Checkbox::new("status-message-pinned")
277 .label("Show when people message me")
278 .checked(self.show_when_messaged)
279 .on_click(cx.listener(|this, checked: &bool, _, cx| {
280 this.show_when_messaged = *checked;
281 cx.notify();
282 })),
283 )
284 .child(self.expiry_row(cx))
285 .child(
286 h_flex()
287 .w_full()
288 .items_center()
289 .gap(px(8.))
290 .when(self.has_message, |row| {
291 row.child(
292 Button::new("status-message-clear")
293 .ghost()
294 .label("Clear message")
295 .on_click(cx.listener(|this, _, _, cx| this.clear(cx))),
296 )
297 })
298 .child(div().flex_1())
299 .child(
300 Button::new("status-message-cancel")
301 .ghost()
302 .label("Cancel")
303 .on_click(cx.listener(|_, _, _, cx| {
304 cx.emit(StatusMessageDialogEvent::Close)
305 })),
306 )
307 .child(self.done_button(enabled, cx)),
308 ),
309 )
310 }
311}
312
313#[cfg(test)]
314mod tests {
315 use super::can_finish;
316
317 #[test]
318 fn done_needs_at_least_one_character() {
319 assert!(!can_finish(0));
320 assert!(can_finish(1));
321 }
322}