BlueGit

ict rusty-teams

Files

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

status_menu.rs

status_menu.rs 13.2 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 row_label = match &context.summary.until {
201 Some(until) => format!("{} {until}", context.summary.label),
202 None => context.summary.label.to_owned(),
203 };
204 let submenu_context = context.clone();
205 let ring_app = context.app.clone();
206 let demo = context.app.read(cx).mode.demo;
207 let popup = popup
208 .min_w(px(MENU_WIDTH))
209 .item(PopupMenuItem::element(move |_, cx| {
210 header(&header_context, cx)
211 }))
212 .separator()
213 .submenu_with_icon(
214 Some(status_icon(context.summary.presence)),
215 row_label,
216 window,
217 cx,
218 move |submenu, window, cx| state_menu(submenu, window, cx, &submenu_context),
219 )
220 .item(work_location_item(context, window, cx))
221 .item(
222 PopupMenuItem::new("Set status message")
223 .icon(menu_icon(IconName::Pencil))
224 .on_click(move |_, _, cx| {
225 message_app.update(cx, |state, cx| state.request_status_message(cx));
226 }),
227 )
228 .separator()
229 .item(
230 PopupMenuItem::new("Chat list")
231 .icon(menu_icon(IconName::LayoutList))
232 .on_click(move |_, _, cx| {
233 chat_list_app
234 .update(cx, |state, cx| state.open_settings(SettingsSection::Chats, cx));
235 }),
236 )
237 .item(
238 PopupMenuItem::new("Translation")
239 .icon(menu_icon(IconName::Languages))
240 .on_click(move |_, _, cx| {
241 translation_app
242 .update(cx, |state, cx| state.open_settings(SettingsSection::General, cx));
243 }),
244 )
245 .item(
246 PopupMenuItem::new("Make a test call")
247 .icon(menu_icon(IconName::Phone))
248 .on_click(move |_, _, cx| {
249 call_app.update(cx, |state, cx| state.start_test_call(cx));
250 }),
251 )
252 .item(
253 PopupMenuItem::new("Notification settings")
254 .icon(menu_icon(IconName::Bell))
255 .on_click(move |_, _, cx| {
256 settings_app.update(cx, |state, cx| {
257 state.open_settings(SettingsSection::Notifications, cx)
258 });
259 }),
260 )
261 .item(
262 PopupMenuItem::new("Settings")
263 .icon(menu_icon(IconName::Settings))
264 .on_click(move |_, _, cx| {
265 general_app
266 .update(cx, |state, cx| state.open_settings(SettingsSection::General, cx));
267 }),
268 );
269 if !demo {
270 return popup;
271 }
272 popup.item(
273 PopupMenuItem::new("Simulate incoming call")
274 .icon(menu_icon(IconName::Phone))
275 .on_click(move |_, _, cx| {
276 ring_app.update(cx, |state, cx| state.demo_incoming_ring(cx));
277 }),
278 )
279}
280
281fn work_location_icon(kind: Option<WorkLocationKind>) -> gpui_kit::component::Icon {
282 menu_icon(match kind {
283 Some(WorkLocationKind::Office) => IconName::Building2,
284 Some(WorkLocationKind::Remote) => IconName::House,
285 None => IconName::MapPin,
286 })
287}
288
289fn work_location_item(
290 context: &MenuContext,
291 window: &mut Window,
292 cx: &mut Context<PopupMenu>,
293) -> PopupMenuItem {
294 let current = context.status.work_location;
295 let app = context.app.clone();
296 let submenu = PopupMenu::build(window, cx, move |menu, _, _| {
297 let pick = |label: &'static str, kind: WorkLocationKind| {
298 let app = app.clone();
299 PopupMenuItem::new(label)
300 .icon(work_location_icon(Some(kind)))
301 .on_click(move |_, _, cx| {
302 app.update(cx, |state, cx| state.set_own_work_location(Some(kind), cx));
303 })
304 };
305 let clear_app = app.clone();
306 let menu = menu
307 .item(PopupMenuItem::label("Set work location for today"))
308 .item(pick("Office", WorkLocationKind::Office))
309 .item(pick("Remote", WorkLocationKind::Remote));
310 if current.is_none() {
311 return menu;
312 }
313 menu.separator().item(
314 PopupMenuItem::new("Clear work location")
315 .icon(menu_icon(IconName::X))
316 .on_click(move |_, _, cx| {
317 clear_app.update(cx, |state, cx| state.set_own_work_location(None, cx));
318 }),
319 )
320 });
321 PopupMenuItem::submenu(work_location_label(current), submenu)
322 .icon(work_location_icon(current.map(|location| location.kind)))
323}
324
325fn state_menu(
326 popup: PopupMenu,
327 window: &mut Window,
328 cx: &mut Context<PopupMenu>,
329 context: &MenuContext,
330) -> PopupMenu {
331 let current = context.status.forced.map(|forced| forced.kind);
332 let mut popup = popup.check_side(Side::Right);
333 for kind in STATUS_CHOICES {
334 let app = context.app.clone();
335 popup = popup.item(
336 PopupMenuItem::new(choice_label(kind))
337 .icon(status_icon(presence_kind_for(kind)))
338 .checked(current == Some(kind))
339 .on_click(move |_, _, cx| {
340 let forced = ForcedAvailability {
341 kind,
342 expires_at: None,
343 };
344 app.update(cx, |state, cx| state.set_own_availability(Some(forced), cx));
345 }),
346 );
347 }
348 let target = duration_target(&context.status);
349 let app = context.app.clone();
350 let durations = PopupMenu::build(window, cx, move |menu, _, _| {
351 STATUS_DURATIONS.iter().fold(menu, |menu, duration| {
352 menu.item(duration_item(&app, target, *duration))
353 })
354 });
355 let reset_app = context.app.clone();
356 popup
357 .separator()
358 .item(
359 PopupMenuItem::submenu("Duration", durations)
360 .icon(menu_icon(IconName::Clock))
361 .disabled(target.is_none()),
362 )
363 .item(
364 PopupMenuItem::new("Reset status")
365 .icon(menu_icon(IconName::RotateCcw))
366 .on_click(move |_, _, cx| {
367 reset_app.update(cx, |state, cx| state.set_own_availability(None, cx));
368 }),
369 )
370}
371
372fn duration_item(
373 app: &Entity<AppState>,
374 target: Option<ForcedKind>,
375 duration: StatusDuration,
376) -> PopupMenuItem {
377 let app = app.clone();
378 PopupMenuItem::new(duration.label())
379 .disabled(target.is_none())
380 .on_click(move |_, _, cx| {
381 let Some(kind) = target else {
382 return;
383 };
384 let forced = ForcedAvailability {
385 kind,
386 expires_at: Some(duration.expires_at(Local::now())),
387 };
388 app.update(cx, |state, cx| state.set_own_availability(Some(forced), cx));
389 })
390}