BlueGit

ict rusty-teams

Files

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

status_menu.rs

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