| 1 | use chrono::Local; |
| 2 | use gpui_kit::assets::IconName; |
| 3 | use gpui_kit::component::{ |
| 4 | Side, |
| 5 | button::{Button, ButtonVariants as _}, |
| 6 | h_flex, |
| 7 | menu::{DropdownMenu as _, PopupMenu, PopupMenuItem}, |
| 8 | v_flex, |
| 9 | }; |
| 10 | use gpui_kit::prelude::FluentBuilder as _; |
| 11 | use gpui_kit::*; |
| 12 | use teams_core::{ |
| 13 | ChatSection, ForcedAvailability, ForcedKind, PresenceStatus, TranslationBehavior, |
| 14 | WorkLocationKind, |
| 15 | }; |
| 16 | |
| 17 | use super::avatar::{person_avatar, with_presence}; |
| 18 | use super::widgets::{icon, symbol}; |
| 19 | use crate::app_state::AppState; |
| 20 | use crate::data::{Person, PresenceKind}; |
| 21 | use crate::own_status::{ |
| 22 | STATUS_CHOICES, STATUS_DURATIONS, StateSummary, StatusDuration, choice_label, duration_target, |
| 23 | presence_kind_for, state_summary, work_location_label, |
| 24 | }; |
| 25 | use crate::theme; |
| 26 | |
| 27 | pub const MENU_WIDTH: f32 = 250.; |
| 28 | const TITLE_AVATAR_SIZE: f32 = 26.; |
| 29 | const HEADER_AVATAR_SIZE: f32 = 40.; |
| 30 | const RING_WIDTH: f32 = 2.; |
| 31 | const PRESENCE_OVERHANG: f32 = 2.; |
| 32 | const OWN_STATUS_BUTTON_SIZE: f32 = TITLE_AVATAR_SIZE + 2. * (1. + RING_WIDTH + PRESENCE_OVERHANG); |
| 33 | const MENU_ICON_SIZE: f32 = 14.; |
| 34 | const ICON_COLUMN: f32 = 16.; |
| 35 | const HEADER_INSET: f32 = 24.; |
| 36 | const SWITCH_WIDTH: f32 = 28.; |
| 37 | const SWITCH_HEIGHT: f32 = 16.; |
| 38 | const SWITCH_THUMB: f32 = 12.; |
| 39 | const SWITCH_PADDING: f32 = 2.; |
| 40 | const TOGGLE_LABEL_WIDTH: f32 = 270.; |
| 41 | const LANGUAGE_LIST_HEIGHT: f32 = 320.; |
| 42 | |
| 43 | #[derive(Clone)] |
| 44 | struct MenuContext { |
| 45 | app: Entity<AppState>, |
| 46 | person: Person, |
| 47 | email: String, |
| 48 | note: Option<String>, |
| 49 | status: PresenceStatus, |
| 50 | summary: StateSummary, |
| 51 | } |
| 52 | |
| 53 | pub fn own_status_button(app: &Entity<AppState>, state: &AppState) -> Option<AnyElement> { |
| 54 | let person = state.directory.me.clone()?; |
| 55 | let presence = state.directory.presence_of(&person.user_id); |
| 56 | let open = state.status_menu_open; |
| 57 | let summary = state_summary(&state.own_status, state.own_presence_kind(), &Local::now()); |
| 58 | let context = MenuContext { |
| 59 | app: app.clone(), |
| 60 | email: state.own_email.clone(), |
| 61 | note: state.own_status.note.as_ref().map(|note| note.text.clone()), |
| 62 | status: state.own_status.clone(), |
| 63 | summary, |
| 64 | person: person.clone(), |
| 65 | }; |
| 66 | let ringed = div() |
| 67 | .p(px(1.)) |
| 68 | .rounded_full() |
| 69 | .border(px(RING_WIDTH)) |
| 70 | .border_color(if open { |
| 71 | theme::accent() |
| 72 | } else { |
| 73 | transparent_black() |
| 74 | }) |
| 75 | .child(person_avatar( |
| 76 | &state.directory, |
| 77 | Some(&person.user_id), |
| 78 | &person.display_name, |
| 79 | TITLE_AVATAR_SIZE, |
| 80 | )) |
| 81 | .into_any_element(); |
| 82 | let ringed = with_presence( |
| 83 | ringed, |
| 84 | presence, |
| 85 | TITLE_AVATAR_SIZE + 2. * (1. + RING_WIDTH), |
| 86 | theme::background(), |
| 87 | ); |
| 88 | let toggle = app.clone(); |
| 89 | Some( |
| 90 | div() |
| 91 | .debug_selector(|| "shell/title-bar/own-status".into()) |
| 92 | .flex_none() |
| 93 | .child( |
| 94 | Button::new("title-own-status") |
| 95 | .ghost() |
| 96 | .p_0() |
| 97 | .size(px(OWN_STATUS_BUTTON_SIZE)) |
| 98 | .rounded_full() |
| 99 | .child( |
| 100 | div() |
| 101 | .debug_selector(|| "shell/title-bar/own-status/avatar".into()) |
| 102 | .p(px(PRESENCE_OVERHANG)) |
| 103 | .child(ringed), |
| 104 | ) |
| 105 | .dropdown_menu_with_anchor(Anchor::TopRight, move |popup, window, cx| { |
| 106 | status_menu(popup, window, cx, &context) |
| 107 | }) |
| 108 | .on_open_change(move |open, _, cx| { |
| 109 | let open = *open; |
| 110 | toggle.update(cx, |state, cx| state.set_status_menu_open(open, cx)); |
| 111 | }), |
| 112 | ) |
| 113 | .into_any_element(), |
| 114 | ) |
| 115 | } |
| 116 | |
| 117 | fn status_icon(kind: PresenceKind) -> gpui_kit::component::Icon { |
| 118 | let (name, color) = match kind { |
| 119 | PresenceKind::Available => ("status_dot", theme::green()), |
| 120 | PresenceKind::Busy => ("status_dot", theme::red()), |
| 121 | PresenceKind::DoNotDisturb => ("status_dnd", theme::red()), |
| 122 | PresenceKind::Away => ("status_away", theme::amber()), |
| 123 | PresenceKind::Offline => ("status_ring", theme::text_muted()), |
| 124 | PresenceKind::Unknown => ("status_ring", theme::text_faint()), |
| 125 | }; |
| 126 | symbol(name, MENU_ICON_SIZE, color) |
| 127 | } |
| 128 | |
| 129 | fn menu_icon(name: IconName) -> gpui_kit::component::Icon { |
| 130 | icon(name, MENU_ICON_SIZE, theme::text_muted()) |
| 131 | } |
| 132 | |
| 133 | fn header(context: &MenuContext, cx: &App) -> Div { |
| 134 | let state = context.app.read(cx); |
| 135 | let presence = state.directory.presence_of(&context.person.user_id); |
| 136 | let avatar = with_presence( |
| 137 | person_avatar( |
| 138 | &state.directory, |
| 139 | Some(&context.person.user_id), |
| 140 | &context.person.display_name, |
| 141 | HEADER_AVATAR_SIZE, |
| 142 | ), |
| 143 | presence, |
| 144 | HEADER_AVATAR_SIZE, |
| 145 | theme::surface(), |
| 146 | ); |
| 147 | let line = |text: String, color: Hsla| { |
| 148 | div() |
| 149 | .w_full() |
| 150 | .truncate() |
| 151 | .text_size(px(12.)) |
| 152 | .text_color(color) |
| 153 | .child(text) |
| 154 | }; |
| 155 | h_flex() |
| 156 | .w(px(MENU_WIDTH - HEADER_INSET)) |
| 157 | .ml(px(-(ICON_COLUMN + 4.))) |
| 158 | .py(px(6.)) |
| 159 | .gap(px(10.)) |
| 160 | .items_center() |
| 161 | .child(avatar) |
| 162 | .child( |
| 163 | v_flex() |
| 164 | .flex_1() |
| 165 | .min_w_0() |
| 166 | .child( |
| 167 | div() |
| 168 | .w_full() |
| 169 | .truncate() |
| 170 | .text_size(px(14.)) |
| 171 | .font_weight(FontWeight::SEMIBOLD) |
| 172 | .text_color(theme::text()) |
| 173 | .child(context.person.display_name.clone()), |
| 174 | ) |
| 175 | .child(line(context.email.clone(), theme::text_muted())) |
| 176 | .children( |
| 177 | context |
| 178 | .note |
| 179 | .clone() |
| 180 | .map(|note| line(note, theme::text_soft())), |
| 181 | ), |
| 182 | ) |
| 183 | } |
| 184 | |
| 185 | fn status_menu( |
| 186 | popup: PopupMenu, |
| 187 | window: &mut Window, |
| 188 | cx: &mut Context<PopupMenu>, |
| 189 | context: &MenuContext, |
| 190 | ) -> PopupMenu { |
| 191 | let header_context = context.clone(); |
| 192 | let message_app = context.app.clone(); |
| 193 | let settings_app = context.app.clone(); |
| 194 | let call_app = context.app.clone(); |
| 195 | let row_label = match &context.summary.until { |
| 196 | Some(until) => format!("{} {until}", context.summary.label), |
| 197 | None => context.summary.label.to_owned(), |
| 198 | }; |
| 199 | let submenu_context = context.clone(); |
| 200 | let ring_app = context.app.clone(); |
| 201 | let demo = context.app.read(cx).mode.demo; |
| 202 | let popup = popup |
| 203 | .min_w(px(MENU_WIDTH)) |
| 204 | .item(PopupMenuItem::element(move |_, cx| { |
| 205 | header(&header_context, cx) |
| 206 | })) |
| 207 | .separator() |
| 208 | .submenu_with_icon( |
| 209 | Some(status_icon(context.summary.presence)), |
| 210 | row_label, |
| 211 | window, |
| 212 | cx, |
| 213 | move |submenu, window, cx| state_menu(submenu, window, cx, &submenu_context), |
| 214 | ) |
| 215 | .item(work_location_item(context, window, cx)) |
| 216 | .item( |
| 217 | PopupMenuItem::new("Set status message") |
| 218 | .icon(menu_icon(IconName::Pencil)) |
| 219 | .on_click(move |_, _, cx| { |
| 220 | message_app.update(cx, |state, cx| state.request_status_message(cx)); |
| 221 | }), |
| 222 | ) |
| 223 | .separator() |
| 224 | .item(chat_list_item(context, window, cx)) |
| 225 | .item(translation_item(context, window, cx)) |
| 226 | .item( |
| 227 | PopupMenuItem::new("Make a test call") |
| 228 | .icon(menu_icon(IconName::Phone)) |
| 229 | .on_click(move |_, _, cx| { |
| 230 | call_app.update(cx, |state, cx| state.start_test_call(cx)); |
| 231 | }), |
| 232 | ) |
| 233 | .item( |
| 234 | PopupMenuItem::new("Notification settings") |
| 235 | .icon(menu_icon(IconName::Settings)) |
| 236 | .on_click(move |_, _, cx| { |
| 237 | settings_app.update(cx, |state, cx| state.open_notification_settings(cx)); |
| 238 | }), |
| 239 | ); |
| 240 | if !demo { |
| 241 | return popup; |
| 242 | } |
| 243 | popup.item( |
| 244 | PopupMenuItem::new("Simulate incoming call") |
| 245 | .icon(menu_icon(IconName::Phone)) |
| 246 | .on_click(move |_, _, cx| { |
| 247 | ring_app.update(cx, |state, cx| state.demo_incoming_ring(cx)); |
| 248 | }), |
| 249 | ) |
| 250 | } |
| 251 | |
| 252 | fn chat_list_item( |
| 253 | context: &MenuContext, |
| 254 | window: &mut Window, |
| 255 | cx: &mut Context<PopupMenu>, |
| 256 | ) -> PopupMenuItem { |
| 257 | let app = context.app.clone(); |
| 258 | let submenu = PopupMenu::build(window, cx, move |menu, _, cx| { |
| 259 | let settings = app.read(cx).directory.section_settings; |
| 260 | menu.item(section_toggle( |
| 261 | &app, |
| 262 | ChatSection::Muted, |
| 263 | "Show muted chats in own section", |
| 264 | settings.enabled(ChatSection::Muted), |
| 265 | )) |
| 266 | }); |
| 267 | PopupMenuItem::submenu("Chat list", submenu).icon(menu_icon(IconName::LayoutList)) |
| 268 | } |
| 269 | |
| 270 | fn translation_item( |
| 271 | context: &MenuContext, |
| 272 | window: &mut Window, |
| 273 | cx: &mut Context<PopupMenu>, |
| 274 | ) -> PopupMenuItem { |
| 275 | let app = context.app.clone(); |
| 276 | let submenu = PopupMenu::build(window, cx, move |menu, window, cx| { |
| 277 | translation_menu(menu, window, cx, &app) |
| 278 | }); |
| 279 | PopupMenuItem::submenu("Translation", submenu).icon(menu_icon(IconName::Languages)) |
| 280 | } |
| 281 | |
| 282 | fn translation_menu( |
| 283 | menu: PopupMenu, |
| 284 | window: &mut Window, |
| 285 | cx: &mut Context<PopupMenu>, |
| 286 | app: &Entity<AppState>, |
| 287 | ) -> PopupMenu { |
| 288 | let state = app.read(cx); |
| 289 | if state.translation.settings.is_none() { |
| 290 | return menu.item(PopupMenuItem::label("Translation settings not loaded yet")); |
| 291 | } |
| 292 | let target = state.translation.target(); |
| 293 | let behavior = state.translation.behavior(); |
| 294 | let languages = state.translation.languages.clone(); |
| 295 | let never: Vec<(String, String)> = state |
| 296 | .translation |
| 297 | .display_codes() |
| 298 | .into_iter() |
| 299 | .filter(|code| *code != target) |
| 300 | .map(|code| { |
| 301 | let name = state.translation.language_name(&code); |
| 302 | (code, name) |
| 303 | }) |
| 304 | .collect(); |
| 305 | let target_label = format!("Translate to {}", state.translation.language_name(&target)); |
| 306 | let target_app = app.clone(); |
| 307 | let mut menu = menu |
| 308 | .check_side(Side::Right) |
| 309 | .min_w(px(MENU_WIDTH)) |
| 310 | .submenu_with_icon(None, target_label, window, cx, move |submenu, _, _| { |
| 311 | languages.iter().fold( |
| 312 | submenu |
| 313 | .check_side(Side::Right) |
| 314 | .max_h(px(LANGUAGE_LIST_HEIGHT)) |
| 315 | .scrollable(true), |
| 316 | |submenu, language| { |
| 317 | let (app, code) = (target_app.clone(), language.code.clone()); |
| 318 | submenu.item( |
| 319 | PopupMenuItem::new(language.name.clone()) |
| 320 | .checked(language.code == target) |
| 321 | .on_click(move |_, _, cx| { |
| 322 | app.update(cx, |state, cx| state.set_translation_target(&code, cx)); |
| 323 | }), |
| 324 | ) |
| 325 | }, |
| 326 | ) |
| 327 | }); |
| 328 | menu = menu.separator(); |
| 329 | for (label, choice) in [ |
| 330 | ("Ask me before translating", TranslationBehavior::Ask), |
| 331 | ("Auto-translate all messages", TranslationBehavior::Auto), |
| 332 | ("Never translate", TranslationBehavior::Never), |
| 333 | ] { |
| 334 | let app = app.clone(); |
| 335 | menu = menu.item( |
| 336 | PopupMenuItem::new(label) |
| 337 | .checked(behavior == choice) |
| 338 | .on_click(move |_, _, cx| { |
| 339 | app.update(cx, |state, cx| state.set_translation_behavior(choice, cx)); |
| 340 | }), |
| 341 | ); |
| 342 | } |
| 343 | menu = menu |
| 344 | .separator() |
| 345 | .item(PopupMenuItem::label("Never translate messages in")); |
| 346 | if never.is_empty() { |
| 347 | return menu.item(PopupMenuItem::label("No languages")); |
| 348 | } |
| 349 | for (code, name) in never { |
| 350 | let app = app.clone(); |
| 351 | menu = menu.item( |
| 352 | PopupMenuItem::element(move |_, _| { |
| 353 | h_flex() |
| 354 | .w(px(TOGGLE_LABEL_WIDTH)) |
| 355 | .gap(px(12.)) |
| 356 | .items_center() |
| 357 | .justify_between() |
| 358 | .child(div().flex_1().min_w_0().truncate().child(name.clone())) |
| 359 | .child(menu_icon(IconName::X)) |
| 360 | }) |
| 361 | .on_click(move |_, _, cx| { |
| 362 | app.update(cx, |state, cx| state.allow_translating_language(&code, cx)); |
| 363 | }), |
| 364 | ); |
| 365 | } |
| 366 | menu |
| 367 | } |
| 368 | |
| 369 | fn section_toggle( |
| 370 | app: &Entity<AppState>, |
| 371 | section: ChatSection, |
| 372 | label: &'static str, |
| 373 | enabled: bool, |
| 374 | ) -> PopupMenuItem { |
| 375 | let app = app.clone(); |
| 376 | PopupMenuItem::element(move |_, _| { |
| 377 | h_flex() |
| 378 | .w(px(TOGGLE_LABEL_WIDTH)) |
| 379 | .gap(px(12.)) |
| 380 | .items_center() |
| 381 | .justify_between() |
| 382 | .child(div().flex_1().min_w_0().truncate().child(label)) |
| 383 | .child(switch(enabled)) |
| 384 | }) |
| 385 | .on_click(move |_, _, cx| { |
| 386 | app.update(cx, |state, cx| { |
| 387 | state.set_chat_section(section, !enabled, cx) |
| 388 | }); |
| 389 | }) |
| 390 | } |
| 391 | |
| 392 | fn switch(enabled: bool) -> Div { |
| 393 | let thumb = div() |
| 394 | .size(px(SWITCH_THUMB)) |
| 395 | .rounded_full() |
| 396 | .bg(theme::text_strong()); |
| 397 | h_flex() |
| 398 | .w(px(SWITCH_WIDTH)) |
| 399 | .h(px(SWITCH_HEIGHT)) |
| 400 | .flex_none() |
| 401 | .p(px(SWITCH_PADDING)) |
| 402 | .items_center() |
| 403 | .rounded_full() |
| 404 | .bg(if enabled { |
| 405 | theme::accent() |
| 406 | } else { |
| 407 | theme::badge_muted() |
| 408 | }) |
| 409 | .when(enabled, |track| track.justify_end()) |
| 410 | .child(thumb) |
| 411 | } |
| 412 | |
| 413 | fn work_location_icon(kind: Option<WorkLocationKind>) -> gpui_kit::component::Icon { |
| 414 | menu_icon(match kind { |
| 415 | Some(WorkLocationKind::Office) => IconName::Building2, |
| 416 | Some(WorkLocationKind::Remote) => IconName::House, |
| 417 | None => IconName::MapPin, |
| 418 | }) |
| 419 | } |
| 420 | |
| 421 | fn work_location_item( |
| 422 | context: &MenuContext, |
| 423 | window: &mut Window, |
| 424 | cx: &mut Context<PopupMenu>, |
| 425 | ) -> PopupMenuItem { |
| 426 | let current = context.status.work_location; |
| 427 | let app = context.app.clone(); |
| 428 | let submenu = PopupMenu::build(window, cx, move |menu, _, _| { |
| 429 | let pick = |label: &'static str, kind: WorkLocationKind| { |
| 430 | let app = app.clone(); |
| 431 | PopupMenuItem::new(label) |
| 432 | .icon(work_location_icon(Some(kind))) |
| 433 | .on_click(move |_, _, cx| { |
| 434 | app.update(cx, |state, cx| state.set_own_work_location(Some(kind), cx)); |
| 435 | }) |
| 436 | }; |
| 437 | let clear_app = app.clone(); |
| 438 | let menu = menu |
| 439 | .item(PopupMenuItem::label("Set work location for today")) |
| 440 | .item(pick("Office", WorkLocationKind::Office)) |
| 441 | .item(pick("Remote", WorkLocationKind::Remote)); |
| 442 | if current.is_none() { |
| 443 | return menu; |
| 444 | } |
| 445 | menu.separator().item( |
| 446 | PopupMenuItem::new("Clear work location") |
| 447 | .icon(menu_icon(IconName::X)) |
| 448 | .on_click(move |_, _, cx| { |
| 449 | clear_app.update(cx, |state, cx| state.set_own_work_location(None, cx)); |
| 450 | }), |
| 451 | ) |
| 452 | }); |
| 453 | PopupMenuItem::submenu(work_location_label(current), submenu) |
| 454 | .icon(work_location_icon(current.map(|location| location.kind))) |
| 455 | } |
| 456 | |
| 457 | fn state_menu( |
| 458 | popup: PopupMenu, |
| 459 | window: &mut Window, |
| 460 | cx: &mut Context<PopupMenu>, |
| 461 | context: &MenuContext, |
| 462 | ) -> PopupMenu { |
| 463 | let current = context.status.forced.map(|forced| forced.kind); |
| 464 | let mut popup = popup.check_side(Side::Right); |
| 465 | for kind in STATUS_CHOICES { |
| 466 | let app = context.app.clone(); |
| 467 | popup = popup.item( |
| 468 | PopupMenuItem::new(choice_label(kind)) |
| 469 | .icon(status_icon(presence_kind_for(kind))) |
| 470 | .checked(current == Some(kind)) |
| 471 | .on_click(move |_, _, cx| { |
| 472 | let forced = ForcedAvailability { |
| 473 | kind, |
| 474 | expires_at: None, |
| 475 | }; |
| 476 | app.update(cx, |state, cx| state.set_own_availability(Some(forced), cx)); |
| 477 | }), |
| 478 | ); |
| 479 | } |
| 480 | let target = duration_target(&context.status); |
| 481 | let app = context.app.clone(); |
| 482 | let durations = PopupMenu::build(window, cx, move |menu, _, _| { |
| 483 | STATUS_DURATIONS.iter().fold(menu, |menu, duration| { |
| 484 | menu.item(duration_item(&app, target, *duration)) |
| 485 | }) |
| 486 | }); |
| 487 | let reset_app = context.app.clone(); |
| 488 | popup |
| 489 | .separator() |
| 490 | .item( |
| 491 | PopupMenuItem::submenu("Duration", durations) |
| 492 | .icon(menu_icon(IconName::Clock)) |
| 493 | .disabled(target.is_none()), |
| 494 | ) |
| 495 | .item( |
| 496 | PopupMenuItem::new("Reset status") |
| 497 | .icon(menu_icon(IconName::RotateCcw)) |
| 498 | .on_click(move |_, _, cx| { |
| 499 | reset_app.update(cx, |state, cx| state.set_own_availability(None, cx)); |
| 500 | }), |
| 501 | ) |
| 502 | } |
| 503 | |
| 504 | fn duration_item( |
| 505 | app: &Entity<AppState>, |
| 506 | target: Option<ForcedKind>, |
| 507 | duration: StatusDuration, |
| 508 | ) -> PopupMenuItem { |
| 509 | let app = app.clone(); |
| 510 | PopupMenuItem::new(duration.label()) |
| 511 | .disabled(target.is_none()) |
| 512 | .on_click(move |_, _, cx| { |
| 513 | let Some(kind) = target else { |
| 514 | return; |
| 515 | }; |
| 516 | let forced = ForcedAvailability { |
| 517 | kind, |
| 518 | expires_at: Some(duration.expires_at(Local::now())), |
| 519 | }; |
| 520 | app.update(cx, |state, cx| state.set_own_availability(Some(forced), cx)); |
| 521 | }) |
| 522 | } |