BlueGit

ict rusty-teams

Files

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

status_menu.rs

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