BlueGit

ict rusty-teams

Files

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

status_menu.rs

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