BlueGit

ict rusty-teams

Files

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

status_menu.rs

status_menu.rs 15.8 KiB Download Raw
1use chrono::{Local, Utc};
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::*;
11use teams_core::{ForcedAvailability, ForcedKind, PresenceStatus, WorkLocationKind};
12
13use super::avatar::{person_avatar, status_glyph, with_presence, with_presence_badge};
14use super::widgets::{menu_icon, symbol};
15use crate::app_state::AppState;
16use crate::notify::pause;
17use crate::data::{Person, PresenceKind};
18use crate::own_status::{
19 STATUS_CHOICES, STATUS_DURATIONS, StateSummary, StatusDuration, choice_label, duration_target,
20 presence_kind_for, state_summary, work_location_label,
21};
22use crate::settings_section::SettingsSection;
23use crate::theme;
24
25pub const MENU_WIDTH: f32 = 250.;
26pub(crate) const TITLE_AVATAR_SIZE: f32 = 28.;
27pub(crate) const OWN_AVATAR_DISC: &str = "shell/title-bar/own-status/avatar/disc";
28pub(crate) const SETTINGS_MORE_TAG: &str = "shell/title-bar/settings-more";
29const SETTINGS_MORE_TITLE: &str = "Settings and more";
30const VIEW_ACCOUNT_LABEL: &str = "View account";
31const VIEW_ACCOUNT_URL: &str = "https://myaccount.microsoft.com";
32const TITLE_PRESENCE_SIZE: f32 = 14.;
33const HEADER_AVATAR_SIZE: f32 = 40.;
34const PRESENCE_OVERHANG: f32 = 2.;
35const TITLE_BUTTON_INSET: f32 = 2.;
36const OWN_STATUS_BUTTON_SIZE: f32 =
37 TITLE_AVATAR_SIZE + 2. * (PRESENCE_OVERHANG + TITLE_BUTTON_INSET);
38const PRESENCE_ICON_SIZE: f32 = 14.;
39const ICON_COLUMN: f32 = 16.;
40const HEADER_INSET: f32 = 24.;
41
42#[derive(Clone)]
43struct MenuContext {
44 app: Entity<AppState>,
45 person: Person,
46 email: String,
47 note: Option<String>,
48 status: PresenceStatus,
49 summary: StateSummary,
50}
51
52pub fn own_status_button(app: &Entity<AppState>, state: &AppState) -> Option<AnyElement> {
53 let person = state.directory.me.clone()?;
54 let presence = state.directory.presence_of(&person.user_id);
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_badge(
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 TITLE_PRESENCE_SIZE,
74 theme::background(),
75 )
76 .debug_selector(|| OWN_AVATAR_DISC.into());
77 let toggle = app.clone();
78 Some(
79 div()
80 .debug_selector(|| "shell/title-bar/own-status".into())
81 .flex_none()
82 .child(
83 Button::new("title-own-status")
84 .tooltip(version_tooltip())
85 .ghost()
86 .p_0()
87 .size(px(OWN_STATUS_BUTTON_SIZE))
88 .rounded_full()
89 .child(
90 div()
91 .debug_selector(|| "shell/title-bar/own-status/avatar".into())
92 .p(px(PRESENCE_OVERHANG))
93 .child(avatar),
94 )
95 .dropdown_menu_with_anchor(Anchor::TopRight, move |popup, window, cx| {
96 status_menu(popup, window, cx, &context)
97 })
98 .on_open_change(move |open, _, cx| {
99 let open = *open;
100 toggle.update(cx, |state, cx| state.set_status_menu_open(open, cx));
101 }),
102 )
103 .into_any_element(),
104 )
105}
106
107fn version_tooltip() -> String {
108 let build_id = crate::updater::running_version();
109 format!(
110 "{} {}\nBuild {build_id}",
111 app_base::APP_NAME,
112 crate::format::short_version(build_id, env!("CARGO_PKG_VERSION")),
113 )
114}
115
116fn status_icon(kind: PresenceKind) -> gpui_kit::component::Icon {
117 let (name, color) = status_glyph(kind).unwrap_or(("status_ring", theme::text_faint()));
118 symbol(name, PRESENCE_ICON_SIZE, color)
119}
120
121fn header(context: &MenuContext, cx: &App) -> Div {
122 let state = context.app.read(cx);
123 let presence = state.directory.presence_of(&context.person.user_id);
124 let avatar = with_presence(
125 person_avatar(
126 &state.directory,
127 Some(&context.person.user_id),
128 &context.person.display_name,
129 HEADER_AVATAR_SIZE,
130 ),
131 presence,
132 HEADER_AVATAR_SIZE,
133 theme::surface(),
134 );
135 let line = |text: String, color: Hsla| {
136 div()
137 .w_full()
138 .truncate()
139 .text_size(px(12.))
140 .text_color(color)
141 .child(text)
142 };
143 h_flex()
144 .w(px(MENU_WIDTH - HEADER_INSET))
145 .ml(px(-(ICON_COLUMN + 4.)))
146 .py(px(6.))
147 .gap(px(10.))
148 .items_center()
149 .child(avatar)
150 .child(
151 v_flex()
152 .flex_1()
153 .min_w_0()
154 .child(
155 div()
156 .w_full()
157 .truncate()
158 .text_size(px(14.))
159 .font_weight(FontWeight::SEMIBOLD)
160 .text_color(theme::text())
161 .child(context.person.display_name.clone()),
162 )
163 .child(line(context.email.clone(), theme::text_muted()))
164 .child(
165 h_flex()
166 .id("status-menu-view-account")
167 .gap(px(4.))
168 .items_center()
169 .cursor_pointer()
170 .text_size(px(12.))
171 .text_color(theme::text_soft())
172 .hover(|link| link.text_color(theme::accent_text()))
173 .child(VIEW_ACCOUNT_LABEL)
174 .child(menu_icon(IconName::ExternalLink).size(px(12.)))
175 .on_click(|_, _, cx| cx.open_url(VIEW_ACCOUNT_URL)),
176 )
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 row_label = match &context.summary.until {
195 Some(until) => format!("{} {until}", context.summary.label),
196 None => context.summary.label.to_owned(),
197 };
198 let submenu_context = context.clone();
199 popup
200 .min_w(px(MENU_WIDTH))
201 .item(PopupMenuItem::element(move |_, cx| {
202 header(&header_context, cx)
203 }))
204 .separator()
205 .submenu_with_icon(
206 Some(status_icon(context.summary.presence)),
207 row_label,
208 window,
209 cx,
210 move |submenu, window, cx| state_menu(submenu, window, cx, &submenu_context),
211 )
212 .item(pause_item(&context.app, window, cx))
213 .item(work_location_item(context, window, cx))
214 .item(
215 PopupMenuItem::new("Set status message")
216 .icon(menu_icon(IconName::Pencil))
217 .on_click(move |_, _, cx| {
218 message_app.update(cx, |state, cx| state.request_status_message(cx));
219 }),
220 )
221}
222
223fn pause_item(
224 app: &Entity<AppState>,
225 window: &mut Window,
226 cx: &mut Context<PopupMenu>,
227) -> PopupMenuItem {
228 let paused_until = app.read(cx).notifications_paused_until;
229 let paused = pause::is_paused(paused_until, Utc::now());
230 let label = pause::menu_label(paused_until, Local::now());
231 let app = app.clone();
232 let submenu = PopupMenu::build(window, cx, move |menu, _, _| {
233 let resume_app = app.clone();
234 let menu = STATUS_DURATIONS.iter().fold(menu, |menu, duration| {
235 let app = app.clone();
236 let duration = *duration;
237 menu.item(PopupMenuItem::new(duration.label()).on_click(move |_, _, cx| {
238 let until = duration.expires_at(Local::now());
239 app.update(cx, |state, cx| state.pause_notifications(until, cx));
240 }))
241 });
242 if !paused {
243 return menu;
244 }
245 menu.separator().item(
246 PopupMenuItem::new("Resume notifications")
247 .icon(menu_icon(IconName::BellRing))
248 .on_click(move |_, _, cx| {
249 resume_app.update(cx, |state, cx| state.resume_notifications(cx));
250 }),
251 )
252 });
253 PopupMenuItem::submenu(label, submenu).icon(menu_icon(if paused {
254 IconName::BellOff
255 } else {
256 IconName::Bell
257 }))
258}
259
260pub fn settings_more_button(app: &Entity<AppState>) -> AnyElement {
261 let app = app.clone();
262 div()
263 .debug_selector(|| SETTINGS_MORE_TAG.into())
264 .flex_none()
265 .child(
266 Button::new("title-settings-more")
267 .ghost()
268 .icon(IconName::Ellipsis)
269 .tooltip(SETTINGS_MORE_TITLE)
270 .dropdown_menu_with_anchor(Anchor::TopRight, move |popup, window, cx| {
271 settings_more_menu(popup, window, cx, &app)
272 }),
273 )
274 .into_any_element()
275}
276
277fn settings_more_menu(
278 popup: PopupMenu,
279 _window: &mut Window,
280 cx: &mut Context<PopupMenu>,
281 app: &Entity<AppState>,
282) -> PopupMenu {
283 let settings_app = app.clone();
284 let chat_list_app = app.clone();
285 let translation_app = app.clone();
286 let notification_app = app.clone();
287 let call_app = app.clone();
288 let zoom_app = app.clone();
289 let ring_app = app.clone();
290 let demo = app.read(cx).mode.demo;
291 let popup = popup
292 .min_w(px(MENU_WIDTH))
293 .item(
294 PopupMenuItem::new("Settings")
295 .icon(menu_icon(IconName::Settings))
296 .on_click(move |_, _, cx| {
297 settings_app.update(cx, |state, cx| {
298 state.open_settings(SettingsSection::General, cx)
299 });
300 }),
301 )
302 .separator()
303 .item(
304 PopupMenuItem::new("Chat list")
305 .icon(menu_icon(IconName::LayoutList))
306 .on_click(move |_, _, cx| {
307 chat_list_app.update(cx, |state, cx| {
308 state.open_settings(SettingsSection::Chats, cx)
309 });
310 }),
311 )
312 .item(
313 PopupMenuItem::new("Translation")
314 .icon(menu_icon(IconName::Languages))
315 .on_click(move |_, _, cx| {
316 translation_app.update(cx, |state, cx| {
317 state.open_settings(SettingsSection::General, cx)
318 });
319 }),
320 )
321 .item(
322 PopupMenuItem::new("Notification settings")
323 .icon(menu_icon(IconName::Bell))
324 .on_click(move |_, _, cx| {
325 notification_app.update(cx, |state, cx| {
326 state.open_settings(SettingsSection::Notifications, cx)
327 });
328 }),
329 )
330 .item(
331 PopupMenuItem::new("Make a test call")
332 .icon(menu_icon(IconName::Phone))
333 .on_click(move |_, _, cx| {
334 call_app.update(cx, |state, cx| state.start_test_call(cx));
335 }),
336 )
337 .item(PopupMenuItem::element(move |_, cx| {
338 super::window_zoom::menu_row(&zoom_app, cx)
339 }));
340 if !demo {
341 return popup;
342 }
343 popup.item(
344 PopupMenuItem::new("Simulate incoming call")
345 .icon(menu_icon(IconName::Phone))
346 .on_click(move |_, _, cx| {
347 ring_app.update(cx, |state, cx| state.demo_incoming_ring(cx));
348 }),
349 )
350}
351
352fn work_location_icon(kind: Option<WorkLocationKind>) -> gpui_kit::component::Icon {
353 menu_icon(match kind {
354 Some(WorkLocationKind::Office) => IconName::Building2,
355 Some(WorkLocationKind::Remote) => IconName::House,
356 None => IconName::MapPin,
357 })
358}
359
360fn work_location_item(
361 context: &MenuContext,
362 window: &mut Window,
363 cx: &mut Context<PopupMenu>,
364) -> PopupMenuItem {
365 let current = context.status.work_location;
366 let app = context.app.clone();
367 let submenu = PopupMenu::build(window, cx, move |menu, _, _| {
368 let pick = |label: &'static str, kind: WorkLocationKind| {
369 let app = app.clone();
370 PopupMenuItem::new(label)
371 .icon(work_location_icon(Some(kind)))
372 .on_click(move |_, _, cx| {
373 app.update(cx, |state, cx| state.set_own_work_location(Some(kind), cx));
374 })
375 };
376 let clear_app = app.clone();
377 let menu = menu
378 .item(PopupMenuItem::label("Set work location for today"))
379 .item(pick("Office", WorkLocationKind::Office))
380 .item(pick("Remote", WorkLocationKind::Remote));
381 if current.is_none() {
382 return menu;
383 }
384 menu.separator().item(
385 PopupMenuItem::new("Clear work location")
386 .icon(menu_icon(IconName::X))
387 .on_click(move |_, _, cx| {
388 clear_app.update(cx, |state, cx| state.set_own_work_location(None, cx));
389 }),
390 )
391 });
392 PopupMenuItem::submenu(work_location_label(current), submenu)
393 .icon(work_location_icon(current.map(|location| location.kind)))
394}
395
396fn state_menu(
397 popup: PopupMenu,
398 window: &mut Window,
399 cx: &mut Context<PopupMenu>,
400 context: &MenuContext,
401) -> PopupMenu {
402 let current = context.status.forced.map(|forced| forced.kind);
403 let mut popup = popup.check_side(Side::Right);
404 for kind in STATUS_CHOICES {
405 let app = context.app.clone();
406 popup = popup.item(
407 PopupMenuItem::new(choice_label(kind))
408 .icon(status_icon(presence_kind_for(kind)))
409 .checked(current == Some(kind))
410 .on_click(move |_, _, cx| {
411 let forced = ForcedAvailability {
412 kind,
413 expires_at: None,
414 };
415 app.update(cx, |state, cx| state.set_own_availability(Some(forced), cx));
416 }),
417 );
418 }
419 let target = duration_target(&context.status);
420 let app = context.app.clone();
421 let durations = PopupMenu::build(window, cx, move |menu, _, _| {
422 STATUS_DURATIONS.iter().fold(menu, |menu, duration| {
423 menu.item(duration_item(&app, target, *duration))
424 })
425 });
426 let reset_app = context.app.clone();
427 popup
428 .separator()
429 .item(
430 PopupMenuItem::submenu("Duration", durations)
431 .icon(menu_icon(IconName::Clock))
432 .disabled(target.is_none()),
433 )
434 .item(
435 PopupMenuItem::new("Reset status")
436 .icon(menu_icon(IconName::RotateCcw))
437 .on_click(move |_, _, cx| {
438 reset_app.update(cx, |state, cx| state.set_own_availability(None, cx));
439 }),
440 )
441}
442
443fn duration_item(
444 app: &Entity<AppState>,
445 target: Option<ForcedKind>,
446 duration: StatusDuration,
447) -> PopupMenuItem {
448 let app = app.clone();
449 PopupMenuItem::new(duration.label())
450 .disabled(target.is_none())
451 .on_click(move |_, _, cx| {
452 let Some(kind) = target else {
453 return;
454 };
455 let forced = ForcedAvailability {
456 kind,
457 expires_at: Some(duration.expires_at(Local::now())),
458 };
459 app.update(cx, |state, cx| state.set_own_availability(Some(forced), cx));
460 })
461}