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