BlueGit

ict rusty-teams

Files

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

status_menu.rs

status_menu.rs 13.4 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::*;
11use teams_core::{ForcedAvailability, ForcedKind, PresenceStatus, WorkLocationKind};
12
13use super::avatar::{person_avatar, with_presence};
14use super::widgets::{icon, symbol};
15use crate::app_state::AppState;
16use crate::data::{Person, PresenceKind};
17use crate::settings_section::SettingsSection;
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::theme;
23
24pub const MENU_WIDTH: f32 = 250.;
25const TITLE_AVATAR_SIZE: f32 = 26.;
26const HEADER_AVATAR_SIZE: f32 = 40.;
27const RING_WIDTH: f32 = 2.;
28const PRESENCE_OVERHANG: f32 = 2.;
29const OWN_STATUS_BUTTON_SIZE: f32 = TITLE_AVATAR_SIZE + 2. * (1. + RING_WIDTH + PRESENCE_OVERHANG);
30const MENU_ICON_SIZE: f32 = 14.;
31const ICON_COLUMN: f32 = 16.;
32const HEADER_INSET: f32 = 24.;
33
34#[derive(Clone)]
35struct MenuContext {
36 app: Entity<AppState>,
37 person: Person,
38 email: String,
39 note: Option<String>,
40 status: PresenceStatus,
41 summary: StateSummary,
42}
43
44pub fn own_status_button(app: &Entity<AppState>, state: &AppState) -> Option<AnyElement> {
45 let person = state.directory.me.clone()?;
46 let presence = state.directory.presence_of(&person.user_id);
47 let open = state.status_menu_open;
48 let summary = state_summary(&state.own_status, state.own_presence_kind(), &Local::now());
49 let context = MenuContext {
50 app: app.clone(),
51 email: state.own_email.clone(),
52 note: state.own_status.note.as_ref().map(|note| note.text.clone()),
53 status: state.own_status.clone(),
54 summary,
55 person: person.clone(),
56 };
57 let ringed = div()
58 .p(px(1.))
59 .rounded_full()
60 .border(px(RING_WIDTH))
61 .border_color(if open {
62 theme::accent()
63 } else {
64 transparent_black()
65 })
66 .child(person_avatar(
67 &state.directory,
68 Some(&person.user_id),
69 &person.display_name,
70 TITLE_AVATAR_SIZE,
71 ))
72 .into_any_element();
73 let ringed = with_presence(
74 ringed,
75 presence,
76 TITLE_AVATAR_SIZE + 2. * (1. + RING_WIDTH),
77 theme::background(),
78 );
79 let toggle = app.clone();
80 Some(
81 div()
82 .debug_selector(|| "shell/title-bar/own-status".into())
83 .flex_none()
84 .child(
85 Button::new("title-own-status")
86 .tooltip(version_tooltip())
87 .ghost()
88 .p_0()
89 .size(px(OWN_STATUS_BUTTON_SIZE))
90 .rounded_full()
91 .child(
92 div()
93 .debug_selector(|| "shell/title-bar/own-status/avatar".into())
94 .p(px(PRESENCE_OVERHANG))
95 .child(ringed),
96 )
97 .dropdown_menu_with_anchor(Anchor::TopRight, move |popup, window, cx| {
98 status_menu(popup, window, cx, &context)
99 })
100 .on_open_change(move |open, _, cx| {
101 let open = *open;
102 toggle.update(cx, |state, cx| state.set_status_menu_open(open, cx));
103 }),
104 )
105 .into_any_element(),
106 )
107}
108
109fn version_tooltip() -> String {
110 let build_id = crate::updater::running_version();
111 format!(
112 "{} {}\nBuild {build_id}",
113 app_base::APP_NAME,
114 crate::format::short_version(build_id, env!("CARGO_PKG_VERSION")),
115 )
116}
117
118fn status_icon(kind: PresenceKind) -> gpui_kit::component::Icon {
119 let (name, color) = match kind {
120 PresenceKind::Available => ("status_dot", theme::green()),
121 PresenceKind::Busy => ("status_dot", theme::red()),
122 PresenceKind::DoNotDisturb => ("status_dnd", theme::red()),
123 PresenceKind::Away => ("status_away", theme::amber()),
124 PresenceKind::OutOfOffice => ("status_dot", theme::out_of_office()),
125 PresenceKind::Offline => ("status_ring", theme::text_muted()),
126 PresenceKind::Unknown => ("status_ring", theme::text_faint()),
127 };
128 symbol(name, MENU_ICON_SIZE, color)
129}
130
131fn menu_icon(name: IconName) -> gpui_kit::component::Icon {
132 icon(name, MENU_ICON_SIZE, theme::text_muted())
133}
134
135fn header(context: &MenuContext, cx: &App) -> Div {
136 let state = context.app.read(cx);
137 let presence = state.directory.presence_of(&context.person.user_id);
138 let avatar = with_presence(
139 person_avatar(
140 &state.directory,
141 Some(&context.person.user_id),
142 &context.person.display_name,
143 HEADER_AVATAR_SIZE,
144 ),
145 presence,
146 HEADER_AVATAR_SIZE,
147 theme::surface(),
148 );
149 let line = |text: String, color: Hsla| {
150 div()
151 .w_full()
152 .truncate()
153 .text_size(px(12.))
154 .text_color(color)
155 .child(text)
156 };
157 h_flex()
158 .w(px(MENU_WIDTH - HEADER_INSET))
159 .ml(px(-(ICON_COLUMN + 4.)))
160 .py(px(6.))
161 .gap(px(10.))
162 .items_center()
163 .child(avatar)
164 .child(
165 v_flex()
166 .flex_1()
167 .min_w_0()
168 .child(
169 div()
170 .w_full()
171 .truncate()
172 .text_size(px(14.))
173 .font_weight(FontWeight::SEMIBOLD)
174 .text_color(theme::text())
175 .child(context.person.display_name.clone()),
176 )
177 .child(line(context.email.clone(), theme::text_muted()))
178 .children(
179 context
180 .note
181 .clone()
182 .map(|note| line(note, theme::text_soft())),
183 ),
184 )
185}
186
187fn status_menu(
188 popup: PopupMenu,
189 window: &mut Window,
190 cx: &mut Context<PopupMenu>,
191 context: &MenuContext,
192) -> PopupMenu {
193 let header_context = context.clone();
194 let message_app = context.app.clone();
195 let settings_app = context.app.clone();
196 let chat_list_app = context.app.clone();
197 let translation_app = context.app.clone();
198 let general_app = context.app.clone();
199 let call_app = context.app.clone();
200 let zoom_app = context.app.clone();
201 let row_label = match &context.summary.until {
202 Some(until) => format!("{} {until}", context.summary.label),
203 None => context.summary.label.to_owned(),
204 };
205 let submenu_context = context.clone();
206 let ring_app = context.app.clone();
207 let demo = context.app.read(cx).mode.demo;
208 let popup = popup
209 .min_w(px(MENU_WIDTH))
210 .item(PopupMenuItem::element(move |_, cx| {
211 header(&header_context, cx)
212 }))
213 .separator()
214 .submenu_with_icon(
215 Some(status_icon(context.summary.presence)),
216 row_label,
217 window,
218 cx,
219 move |submenu, window, cx| state_menu(submenu, window, cx, &submenu_context),
220 )
221 .item(work_location_item(context, window, cx))
222 .item(
223 PopupMenuItem::new("Set status message")
224 .icon(menu_icon(IconName::Pencil))
225 .on_click(move |_, _, cx| {
226 message_app.update(cx, |state, cx| state.request_status_message(cx));
227 }),
228 )
229 .separator()
230 .item(
231 PopupMenuItem::new("Chat list")
232 .icon(menu_icon(IconName::LayoutList))
233 .on_click(move |_, _, cx| {
234 chat_list_app
235 .update(cx, |state, cx| state.open_settings(SettingsSection::Chats, cx));
236 }),
237 )
238 .item(
239 PopupMenuItem::new("Translation")
240 .icon(menu_icon(IconName::Languages))
241 .on_click(move |_, _, cx| {
242 translation_app
243 .update(cx, |state, cx| state.open_settings(SettingsSection::General, cx));
244 }),
245 )
246 .item(
247 PopupMenuItem::new("Make a test call")
248 .icon(menu_icon(IconName::Phone))
249 .on_click(move |_, _, cx| {
250 call_app.update(cx, |state, cx| state.start_test_call(cx));
251 }),
252 )
253 .item(
254 PopupMenuItem::new("Notification settings")
255 .icon(menu_icon(IconName::Bell))
256 .on_click(move |_, _, cx| {
257 settings_app.update(cx, |state, cx| {
258 state.open_settings(SettingsSection::Notifications, cx)
259 });
260 }),
261 )
262 .item(
263 PopupMenuItem::new("Settings")
264 .icon(menu_icon(IconName::Settings))
265 .on_click(move |_, _, cx| {
266 general_app
267 .update(cx, |state, cx| state.open_settings(SettingsSection::General, cx));
268 }),
269 )
270 .item(PopupMenuItem::element(move |_, cx| {
271 super::window_zoom::menu_row(&zoom_app, cx)
272 }));
273 if !demo {
274 return popup;
275 }
276 popup.item(
277 PopupMenuItem::new("Simulate incoming call")
278 .icon(menu_icon(IconName::Phone))
279 .on_click(move |_, _, cx| {
280 ring_app.update(cx, |state, cx| state.demo_incoming_ring(cx));
281 }),
282 )
283}
284
285fn work_location_icon(kind: Option<WorkLocationKind>) -> gpui_kit::component::Icon {
286 menu_icon(match kind {
287 Some(WorkLocationKind::Office) => IconName::Building2,
288 Some(WorkLocationKind::Remote) => IconName::House,
289 None => IconName::MapPin,
290 })
291}
292
293fn work_location_item(
294 context: &MenuContext,
295 window: &mut Window,
296 cx: &mut Context<PopupMenu>,
297) -> PopupMenuItem {
298 let current = context.status.work_location;
299 let app = context.app.clone();
300 let submenu = PopupMenu::build(window, cx, move |menu, _, _| {
301 let pick = |label: &'static str, kind: WorkLocationKind| {
302 let app = app.clone();
303 PopupMenuItem::new(label)
304 .icon(work_location_icon(Some(kind)))
305 .on_click(move |_, _, cx| {
306 app.update(cx, |state, cx| state.set_own_work_location(Some(kind), cx));
307 })
308 };
309 let clear_app = app.clone();
310 let menu = menu
311 .item(PopupMenuItem::label("Set work location for today"))
312 .item(pick("Office", WorkLocationKind::Office))
313 .item(pick("Remote", WorkLocationKind::Remote));
314 if current.is_none() {
315 return menu;
316 }
317 menu.separator().item(
318 PopupMenuItem::new("Clear work location")
319 .icon(menu_icon(IconName::X))
320 .on_click(move |_, _, cx| {
321 clear_app.update(cx, |state, cx| state.set_own_work_location(None, cx));
322 }),
323 )
324 });
325 PopupMenuItem::submenu(work_location_label(current), submenu)
326 .icon(work_location_icon(current.map(|location| location.kind)))
327}
328
329fn state_menu(
330 popup: PopupMenu,
331 window: &mut Window,
332 cx: &mut Context<PopupMenu>,
333 context: &MenuContext,
334) -> PopupMenu {
335 let current = context.status.forced.map(|forced| forced.kind);
336 let mut popup = popup.check_side(Side::Right);
337 for kind in STATUS_CHOICES {
338 let app = context.app.clone();
339 popup = popup.item(
340 PopupMenuItem::new(choice_label(kind))
341 .icon(status_icon(presence_kind_for(kind)))
342 .checked(current == Some(kind))
343 .on_click(move |_, _, cx| {
344 let forced = ForcedAvailability {
345 kind,
346 expires_at: None,
347 };
348 app.update(cx, |state, cx| state.set_own_availability(Some(forced), cx));
349 }),
350 );
351 }
352 let target = duration_target(&context.status);
353 let app = context.app.clone();
354 let durations = PopupMenu::build(window, cx, move |menu, _, _| {
355 STATUS_DURATIONS.iter().fold(menu, |menu, duration| {
356 menu.item(duration_item(&app, target, *duration))
357 })
358 });
359 let reset_app = context.app.clone();
360 popup
361 .separator()
362 .item(
363 PopupMenuItem::submenu("Duration", durations)
364 .icon(menu_icon(IconName::Clock))
365 .disabled(target.is_none()),
366 )
367 .item(
368 PopupMenuItem::new("Reset status")
369 .icon(menu_icon(IconName::RotateCcw))
370 .on_click(move |_, _, cx| {
371 reset_app.update(cx, |state, cx| state.set_own_availability(None, cx));
372 }),
373 )
374}
375
376fn duration_item(
377 app: &Entity<AppState>,
378 target: Option<ForcedKind>,
379 duration: StatusDuration,
380) -> PopupMenuItem {
381 let app = app.clone();
382 PopupMenuItem::new(duration.label())
383 .disabled(target.is_none())
384 .on_click(move |_, _, cx| {
385 let Some(kind) = target else {
386 return;
387 };
388 let forced = ForcedAvailability {
389 kind,
390 expires_at: Some(duration.expires_at(Local::now())),
391 };
392 app.update(cx, |state, cx| state.set_own_availability(Some(forced), cx));
393 })
394}