BlueGit

ict rusty-teams

Files

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

status_menu.rs

status_menu.rs 16.8 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 });
257 PopupMenuItem::submenu("Chat list", submenu).icon(menu_icon(IconName::LayoutList))
258}
259
260fn translation_item(
261 context: &MenuContext,
262 window: &mut Window,
263 cx: &mut Context<PopupMenu>,
264) -> PopupMenuItem {
265 let app = context.app.clone();
266 let submenu = PopupMenu::build(window, cx, move |menu, window, cx| {
267 translation_menu(menu, window, cx, &app)
268 });
269 PopupMenuItem::submenu("Translation", submenu).icon(menu_icon(IconName::Languages))
270}
271
272fn translation_menu(
273 menu: PopupMenu,
274 window: &mut Window,
275 cx: &mut Context<PopupMenu>,
276 app: &Entity<AppState>,
277) -> PopupMenu {
278 let state = app.read(cx);
279 if state.translation.settings.is_none() {
280 return menu.item(PopupMenuItem::label("Translation settings not loaded yet"));
281 }
282 let target = state.translation.target();
283 let behavior = state.translation.behavior();
284 let languages = state.translation.languages.clone();
285 let never: Vec<(String, String)> = state
286 .translation
287 .display_codes()
288 .into_iter()
289 .filter(|code| *code != target)
290 .map(|code| {
291 let name = state.translation.language_name(&code);
292 (code, name)
293 })
294 .collect();
295 let target_label = format!("Translate to {}", state.translation.language_name(&target));
296 let target_app = app.clone();
297 let mut menu = menu
298 .check_side(Side::Right)
299 .min_w(px(MENU_WIDTH))
300 .submenu_with_icon(None, target_label, window, cx, move |submenu, _, _| {
301 languages.iter().fold(
302 submenu
303 .check_side(Side::Right)
304 .max_h(px(LANGUAGE_LIST_HEIGHT))
305 .scrollable(true),
306 |submenu, language| {
307 let (app, code) = (target_app.clone(), language.code.clone());
308 submenu.item(
309 PopupMenuItem::new(language.name.clone())
310 .checked(language.code == target)
311 .on_click(move |_, _, cx| {
312 app.update(cx, |state, cx| state.set_translation_target(&code, cx));
313 }),
314 )
315 },
316 )
317 });
318 menu = menu.separator();
319 for (label, choice) in [
320 ("Ask me before translating", TranslationBehavior::Ask),
321 ("Auto-translate all messages", TranslationBehavior::Auto),
322 ("Never translate", TranslationBehavior::Never),
323 ] {
324 let app = app.clone();
325 menu = menu.item(
326 PopupMenuItem::new(label)
327 .checked(behavior == choice)
328 .on_click(move |_, _, cx| {
329 app.update(cx, |state, cx| state.set_translation_behavior(choice, cx));
330 }),
331 );
332 }
333 menu = menu
334 .separator()
335 .item(PopupMenuItem::label("Never translate messages in"));
336 if never.is_empty() {
337 return menu.item(PopupMenuItem::label("No languages"));
338 }
339 for (code, name) in never {
340 let app = app.clone();
341 menu = menu.item(
342 PopupMenuItem::element(move |_, _| {
343 h_flex()
344 .w(px(TOGGLE_LABEL_WIDTH))
345 .gap(px(12.))
346 .items_center()
347 .justify_between()
348 .child(div().flex_1().min_w_0().truncate().child(name.clone()))
349 .child(menu_icon(IconName::X))
350 })
351 .on_click(move |_, _, cx| {
352 app.update(cx, |state, cx| state.allow_translating_language(&code, cx));
353 }),
354 );
355 }
356 menu
357}
358
359fn section_toggle(
360 app: &Entity<AppState>,
361 section: ChatSection,
362 label: &'static str,
363 enabled: bool,
364) -> PopupMenuItem {
365 let app = app.clone();
366 PopupMenuItem::element(move |_, _| {
367 h_flex()
368 .w(px(TOGGLE_LABEL_WIDTH))
369 .gap(px(12.))
370 .items_center()
371 .justify_between()
372 .child(div().flex_1().min_w_0().truncate().child(label))
373 .child(switch(enabled))
374 })
375 .on_click(move |_, _, cx| {
376 app.update(cx, |state, cx| {
377 state.set_chat_section(section, !enabled, cx)
378 });
379 })
380}
381
382fn switch(enabled: bool) -> Div {
383 let thumb = div()
384 .size(px(SWITCH_THUMB))
385 .rounded_full()
386 .bg(theme::text_strong());
387 h_flex()
388 .w(px(SWITCH_WIDTH))
389 .h(px(SWITCH_HEIGHT))
390 .flex_none()
391 .p(px(SWITCH_PADDING))
392 .items_center()
393 .rounded_full()
394 .bg(if enabled {
395 theme::accent()
396 } else {
397 theme::badge_muted()
398 })
399 .when(enabled, |track| track.justify_end())
400 .child(thumb)
401}
402
403fn work_location_icon(kind: Option<WorkLocationKind>) -> gpui_kit::component::Icon {
404 menu_icon(match kind {
405 Some(WorkLocationKind::Office) => IconName::Building2,
406 Some(WorkLocationKind::Remote) => IconName::House,
407 None => IconName::MapPin,
408 })
409}
410
411fn work_location_item(
412 context: &MenuContext,
413 window: &mut Window,
414 cx: &mut Context<PopupMenu>,
415) -> PopupMenuItem {
416 let current = context.status.work_location;
417 let app = context.app.clone();
418 let submenu = PopupMenu::build(window, cx, move |menu, _, _| {
419 let pick = |label: &'static str, kind: WorkLocationKind| {
420 let app = app.clone();
421 PopupMenuItem::new(label)
422 .icon(work_location_icon(Some(kind)))
423 .on_click(move |_, _, cx| {
424 app.update(cx, |state, cx| state.set_own_work_location(Some(kind), cx));
425 })
426 };
427 let clear_app = app.clone();
428 let menu = menu
429 .item(PopupMenuItem::label("Set work location for today"))
430 .item(pick("Office", WorkLocationKind::Office))
431 .item(pick("Remote", WorkLocationKind::Remote));
432 if current.is_none() {
433 return menu;
434 }
435 menu.separator().item(
436 PopupMenuItem::new("Clear work location")
437 .icon(menu_icon(IconName::X))
438 .on_click(move |_, _, cx| {
439 clear_app.update(cx, |state, cx| state.set_own_work_location(None, cx));
440 }),
441 )
442 });
443 PopupMenuItem::submenu(work_location_label(current), submenu)
444 .icon(work_location_icon(current.map(|location| location.kind)))
445}
446
447fn state_menu(
448 popup: PopupMenu,
449 window: &mut Window,
450 cx: &mut Context<PopupMenu>,
451 context: &MenuContext,
452) -> PopupMenu {
453 let current = context.status.forced.map(|forced| forced.kind);
454 let mut popup = popup.check_side(Side::Right);
455 for kind in STATUS_CHOICES {
456 let app = context.app.clone();
457 popup = popup.item(
458 PopupMenuItem::new(choice_label(kind))
459 .icon(status_icon(presence_kind_for(kind)))
460 .checked(current == Some(kind))
461 .on_click(move |_, _, cx| {
462 let forced = ForcedAvailability {
463 kind,
464 expires_at: None,
465 };
466 app.update(cx, |state, cx| state.set_own_availability(Some(forced), cx));
467 }),
468 );
469 }
470 let target = duration_target(&context.status);
471 let app = context.app.clone();
472 let durations = PopupMenu::build(window, cx, move |menu, _, _| {
473 STATUS_DURATIONS.iter().fold(menu, |menu, duration| {
474 menu.item(duration_item(&app, target, *duration))
475 })
476 });
477 let reset_app = context.app.clone();
478 popup
479 .separator()
480 .item(
481 PopupMenuItem::submenu("Duration", durations)
482 .icon(menu_icon(IconName::Clock))
483 .disabled(target.is_none()),
484 )
485 .item(
486 PopupMenuItem::new("Reset status")
487 .icon(menu_icon(IconName::RotateCcw))
488 .on_click(move |_, _, cx| {
489 reset_app.update(cx, |state, cx| state.set_own_availability(None, cx));
490 }),
491 )
492}
493
494fn duration_item(
495 app: &Entity<AppState>,
496 target: Option<ForcedKind>,
497 duration: StatusDuration,
498) -> PopupMenuItem {
499 let app = app.clone();
500 PopupMenuItem::new(duration.label())
501 .disabled(target.is_none())
502 .on_click(move |_, _, cx| {
503 let Some(kind) = target else {
504 return;
505 };
506 let forced = ForcedAvailability {
507 kind,
508 expires_at: Some(duration.expires_at(Local::now())),
509 };
510 app.update(cx, |state, cx| state.set_own_availability(Some(forced), cx));
511 })
512}