BlueGit

ict rusty-teams

Files

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

status_menu.rs

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