BlueGit

ict rusty-teams

Files

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

status_menu.rs

status_menu.rs 12.9 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 .ghost()
87 .p_0()
88 .size(px(OWN_STATUS_BUTTON_SIZE))
89 .rounded_full()
90 .child(
91 div()
92 .debug_selector(|| "shell/title-bar/own-status/avatar".into())
93 .p(px(PRESENCE_OVERHANG))
94 .child(ringed),
95 )
96 .dropdown_menu_with_anchor(Anchor::TopRight, move |popup, window, cx| {
97 status_menu(popup, window, cx, &context)
98 })
99 .on_open_change(move |open, _, cx| {
100 let open = *open;
101 toggle.update(cx, |state, cx| state.set_status_menu_open(open, cx));
102 }),
103 )
104 .into_any_element(),
105 )
106}
107
108fn status_icon(kind: PresenceKind) -> gpui_kit::component::Icon {
109 let (name, color) = match kind {
110 PresenceKind::Available => ("status_dot", theme::green()),
111 PresenceKind::Busy => ("status_dot", theme::red()),
112 PresenceKind::DoNotDisturb => ("status_dnd", theme::red()),
113 PresenceKind::Away => ("status_away", theme::amber()),
114 PresenceKind::OutOfOffice => ("status_dot", theme::out_of_office()),
115 PresenceKind::Offline => ("status_ring", theme::text_muted()),
116 PresenceKind::Unknown => ("status_ring", theme::text_faint()),
117 };
118 symbol(name, MENU_ICON_SIZE, color)
119}
120
121fn menu_icon(name: IconName) -> gpui_kit::component::Icon {
122 icon(name, MENU_ICON_SIZE, theme::text_muted())
123}
124
125fn header(context: &MenuContext, cx: &App) -> Div {
126 let state = context.app.read(cx);
127 let presence = state.directory.presence_of(&context.person.user_id);
128 let avatar = with_presence(
129 person_avatar(
130 &state.directory,
131 Some(&context.person.user_id),
132 &context.person.display_name,
133 HEADER_AVATAR_SIZE,
134 ),
135 presence,
136 HEADER_AVATAR_SIZE,
137 theme::surface(),
138 );
139 let line = |text: String, color: Hsla| {
140 div()
141 .w_full()
142 .truncate()
143 .text_size(px(12.))
144 .text_color(color)
145 .child(text)
146 };
147 h_flex()
148 .w(px(MENU_WIDTH - HEADER_INSET))
149 .ml(px(-(ICON_COLUMN + 4.)))
150 .py(px(6.))
151 .gap(px(10.))
152 .items_center()
153 .child(avatar)
154 .child(
155 v_flex()
156 .flex_1()
157 .min_w_0()
158 .child(
159 div()
160 .w_full()
161 .truncate()
162 .text_size(px(14.))
163 .font_weight(FontWeight::SEMIBOLD)
164 .text_color(theme::text())
165 .child(context.person.display_name.clone()),
166 )
167 .child(line(context.email.clone(), theme::text_muted()))
168 .children(
169 context
170 .note
171 .clone()
172 .map(|note| line(note, theme::text_soft())),
173 ),
174 )
175}
176
177fn status_menu(
178 popup: PopupMenu,
179 window: &mut Window,
180 cx: &mut Context<PopupMenu>,
181 context: &MenuContext,
182) -> PopupMenu {
183 let header_context = context.clone();
184 let message_app = context.app.clone();
185 let settings_app = context.app.clone();
186 let chat_list_app = context.app.clone();
187 let translation_app = context.app.clone();
188 let general_app = context.app.clone();
189 let call_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
224 .update(cx, |state, cx| state.open_settings(SettingsSection::Chats, cx));
225 }),
226 )
227 .item(
228 PopupMenuItem::new("Translation")
229 .icon(menu_icon(IconName::Languages))
230 .on_click(move |_, _, cx| {
231 translation_app
232 .update(cx, |state, cx| state.open_settings(SettingsSection::General, cx));
233 }),
234 )
235 .item(
236 PopupMenuItem::new("Make a test call")
237 .icon(menu_icon(IconName::Phone))
238 .on_click(move |_, _, cx| {
239 call_app.update(cx, |state, cx| state.start_test_call(cx));
240 }),
241 )
242 .item(
243 PopupMenuItem::new("Notification settings")
244 .icon(menu_icon(IconName::Bell))
245 .on_click(move |_, _, cx| {
246 settings_app.update(cx, |state, cx| {
247 state.open_settings(SettingsSection::Notifications, cx)
248 });
249 }),
250 )
251 .item(
252 PopupMenuItem::new("Settings")
253 .icon(menu_icon(IconName::Settings))
254 .on_click(move |_, _, cx| {
255 general_app
256 .update(cx, |state, cx| state.open_settings(SettingsSection::General, cx));
257 }),
258 );
259 if !demo {
260 return popup;
261 }
262 popup.item(
263 PopupMenuItem::new("Simulate incoming call")
264 .icon(menu_icon(IconName::Phone))
265 .on_click(move |_, _, cx| {
266 ring_app.update(cx, |state, cx| state.demo_incoming_ring(cx));
267 }),
268 )
269}
270
271fn work_location_icon(kind: Option<WorkLocationKind>) -> gpui_kit::component::Icon {
272 menu_icon(match kind {
273 Some(WorkLocationKind::Office) => IconName::Building2,
274 Some(WorkLocationKind::Remote) => IconName::House,
275 None => IconName::MapPin,
276 })
277}
278
279fn work_location_item(
280 context: &MenuContext,
281 window: &mut Window,
282 cx: &mut Context<PopupMenu>,
283) -> PopupMenuItem {
284 let current = context.status.work_location;
285 let app = context.app.clone();
286 let submenu = PopupMenu::build(window, cx, move |menu, _, _| {
287 let pick = |label: &'static str, kind: WorkLocationKind| {
288 let app = app.clone();
289 PopupMenuItem::new(label)
290 .icon(work_location_icon(Some(kind)))
291 .on_click(move |_, _, cx| {
292 app.update(cx, |state, cx| state.set_own_work_location(Some(kind), cx));
293 })
294 };
295 let clear_app = app.clone();
296 let menu = menu
297 .item(PopupMenuItem::label("Set work location for today"))
298 .item(pick("Office", WorkLocationKind::Office))
299 .item(pick("Remote", WorkLocationKind::Remote));
300 if current.is_none() {
301 return menu;
302 }
303 menu.separator().item(
304 PopupMenuItem::new("Clear work location")
305 .icon(menu_icon(IconName::X))
306 .on_click(move |_, _, cx| {
307 clear_app.update(cx, |state, cx| state.set_own_work_location(None, cx));
308 }),
309 )
310 });
311 PopupMenuItem::submenu(work_location_label(current), submenu)
312 .icon(work_location_icon(current.map(|location| location.kind)))
313}
314
315fn state_menu(
316 popup: PopupMenu,
317 window: &mut Window,
318 cx: &mut Context<PopupMenu>,
319 context: &MenuContext,
320) -> PopupMenu {
321 let current = context.status.forced.map(|forced| forced.kind);
322 let mut popup = popup.check_side(Side::Right);
323 for kind in STATUS_CHOICES {
324 let app = context.app.clone();
325 popup = popup.item(
326 PopupMenuItem::new(choice_label(kind))
327 .icon(status_icon(presence_kind_for(kind)))
328 .checked(current == Some(kind))
329 .on_click(move |_, _, cx| {
330 let forced = ForcedAvailability {
331 kind,
332 expires_at: None,
333 };
334 app.update(cx, |state, cx| state.set_own_availability(Some(forced), cx));
335 }),
336 );
337 }
338 let target = duration_target(&context.status);
339 let app = context.app.clone();
340 let durations = PopupMenu::build(window, cx, move |menu, _, _| {
341 STATUS_DURATIONS.iter().fold(menu, |menu, duration| {
342 menu.item(duration_item(&app, target, *duration))
343 })
344 });
345 let reset_app = context.app.clone();
346 popup
347 .separator()
348 .item(
349 PopupMenuItem::submenu("Duration", durations)
350 .icon(menu_icon(IconName::Clock))
351 .disabled(target.is_none()),
352 )
353 .item(
354 PopupMenuItem::new("Reset status")
355 .icon(menu_icon(IconName::RotateCcw))
356 .on_click(move |_, _, cx| {
357 reset_app.update(cx, |state, cx| state.set_own_availability(None, cx));
358 }),
359 )
360}
361
362fn duration_item(
363 app: &Entity<AppState>,
364 target: Option<ForcedKind>,
365 duration: StatusDuration,
366) -> PopupMenuItem {
367 let app = app.clone();
368 PopupMenuItem::new(duration.label())
369 .disabled(target.is_none())
370 .on_click(move |_, _, cx| {
371 let Some(kind) = target else {
372 return;
373 };
374 let forced = ForcedAvailability {
375 kind,
376 expires_at: Some(duration.expires_at(Local::now())),
377 };
378 app.update(cx, |state, cx| state.set_own_availability(Some(forced), cx));
379 })
380}