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, with_presence_badge};
14use super::widgets::{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 MENU_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, MENU_ICON_SIZE, color)
122}
123
124fn menu_icon(name: IconName) -> gpui_kit::component::Icon {
125 icon(name, MENU_ICON_SIZE, theme::text_muted())
126}
127
128fn header(context: &MenuContext, cx: &App) -> Div {
129 let state = context.app.read(cx);
130 let presence = state.directory.presence_of(&context.person.user_id);
131 let avatar = with_presence(
132 person_avatar(
133 &state.directory,
134 Some(&context.person.user_id),
135 &context.person.display_name,
136 HEADER_AVATAR_SIZE,
137 ),
138 presence,
139 HEADER_AVATAR_SIZE,
140 theme::surface(),
141 );
142 let line = |text: String, color: Hsla| {
143 div()
144 .w_full()
145 .truncate()
146 .text_size(px(12.))
147 .text_color(color)
148 .child(text)
149 };
150 h_flex()
151 .w(px(MENU_WIDTH - HEADER_INSET))
152 .ml(px(-(ICON_COLUMN + 4.)))
153 .py(px(6.))
154 .gap(px(10.))
155 .items_center()
156 .child(avatar)
157 .child(
158 v_flex()
159 .flex_1()
160 .min_w_0()
161 .child(
162 div()
163 .w_full()
164 .truncate()
165 .text_size(px(14.))
166 .font_weight(FontWeight::SEMIBOLD)
167 .text_color(theme::text())
168 .child(context.person.display_name.clone()),
169 )
170 .child(line(context.email.clone(), theme::text_muted()))
171 .children(
172 context
173 .note
174 .clone()
175 .map(|note| line(note, theme::text_soft())),
176 ),
177 )
178}
179
180fn status_menu(
181 popup: PopupMenu,
182 window: &mut Window,
183 cx: &mut Context<PopupMenu>,
184 context: &MenuContext,
185) -> PopupMenu {
186 let header_context = context.clone();
187 let message_app = context.app.clone();
188 let settings_app = context.app.clone();
189 let chat_list_app = context.app.clone();
190 let translation_app = context.app.clone();
191 let general_app = context.app.clone();
192 let call_app = context.app.clone();
193 let zoom_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 let ring_app = context.app.clone();
200 let demo = context.app.read(cx).mode.demo;
201 let popup = popup
202 .min_w(px(MENU_WIDTH))
203 .item(PopupMenuItem::element(move |_, cx| {
204 header(&header_context, cx)
205 }))
206 .separator()
207 .submenu_with_icon(
208 Some(status_icon(context.summary.presence)),
209 row_label,
210 window,
211 cx,
212 move |submenu, window, cx| state_menu(submenu, window, cx, &submenu_context),
213 )
214 .item(work_location_item(context, window, cx))
215 .item(
216 PopupMenuItem::new("Set status message")
217 .icon(menu_icon(IconName::Pencil))
218 .on_click(move |_, _, cx| {
219 message_app.update(cx, |state, cx| state.request_status_message(cx));
220 }),
221 )
222 .separator()
223 .item(
224 PopupMenuItem::new("Chat list")
225 .icon(menu_icon(IconName::LayoutList))
226 .on_click(move |_, _, cx| {
227 chat_list_app.update(cx, |state, cx| {
228 state.open_settings(SettingsSection::Chats, cx)
229 });
230 }),
231 )
232 .item(
233 PopupMenuItem::new("Translation")
234 .icon(menu_icon(IconName::Languages))
235 .on_click(move |_, _, cx| {
236 translation_app.update(cx, |state, cx| {
237 state.open_settings(SettingsSection::General, cx)
238 });
239 }),
240 )
241 .item(
242 PopupMenuItem::new("Make a test call")
243 .icon(menu_icon(IconName::Phone))
244 .on_click(move |_, _, cx| {
245 call_app.update(cx, |state, cx| state.start_test_call(cx));
246 }),
247 )
248 .item(
249 PopupMenuItem::new("Notification settings")
250 .icon(menu_icon(IconName::Bell))
251 .on_click(move |_, _, cx| {
252 settings_app.update(cx, |state, cx| {
253 state.open_settings(SettingsSection::Notifications, cx)
254 });
255 }),
256 )
257 .item(
258 PopupMenuItem::new("Settings")
259 .icon(menu_icon(IconName::Settings))
260 .on_click(move |_, _, cx| {
261 general_app.update(cx, |state, cx| {
262 state.open_settings(SettingsSection::General, cx)
263 });
264 }),
265 )
266 .item(PopupMenuItem::element(move |_, cx| {
267 super::window_zoom::menu_row(&zoom_app, cx)
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}