BlueGit

ict rusty-teams

Files

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

status_menu.rs

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