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, status_glyph, 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) = status_glyph(kind).unwrap_or(("status_ring", theme::text_faint()));
113 symbol(name, PRESENCE_ICON_SIZE, color)
114}
115
116fn header(context: &MenuContext, cx: &App) -> Div {
117 let state = context.app.read(cx);
118 let presence = state.directory.presence_of(&context.person.user_id);
119 let avatar = with_presence(
120 person_avatar(
121 &state.directory,
122 Some(&context.person.user_id),
123 &context.person.display_name,
124 HEADER_AVATAR_SIZE,
125 ),
126 presence,
127 HEADER_AVATAR_SIZE,
128 theme::surface(),
129 );
130 let line = |text: String, color: Hsla| {
131 div()
132 .w_full()
133 .truncate()
134 .text_size(px(12.))
135 .text_color(color)
136 .child(text)
137 };
138 h_flex()
139 .w(px(MENU_WIDTH - HEADER_INSET))
140 .ml(px(-(ICON_COLUMN + 4.)))
141 .py(px(6.))
142 .gap(px(10.))
143 .items_center()
144 .child(avatar)
145 .child(
146 v_flex()
147 .flex_1()
148 .min_w_0()
149 .child(
150 div()
151 .w_full()
152 .truncate()
153 .text_size(px(14.))
154 .font_weight(FontWeight::SEMIBOLD)
155 .text_color(theme::text())
156 .child(context.person.display_name.clone()),
157 )
158 .child(line(context.email.clone(), theme::text_muted()))
159 .children(
160 context
161 .note
162 .clone()
163 .map(|note| line(note, theme::text_soft())),
164 ),
165 )
166}
167
168fn status_menu(
169 popup: PopupMenu,
170 window: &mut Window,
171 cx: &mut Context<PopupMenu>,
172 context: &MenuContext,
173) -> PopupMenu {
174 let header_context = context.clone();
175 let message_app = context.app.clone();
176 let settings_app = context.app.clone();
177 let chat_list_app = context.app.clone();
178 let translation_app = context.app.clone();
179 let general_app = context.app.clone();
180 let call_app = context.app.clone();
181 let zoom_app = context.app.clone();
182 let row_label = match &context.summary.until {
183 Some(until) => format!("{} {until}", context.summary.label),
184 None => context.summary.label.to_owned(),
185 };
186 let submenu_context = context.clone();
187 let ring_app = context.app.clone();
188 let demo = context.app.read(cx).mode.demo;
189 let popup = popup
190 .min_w(px(MENU_WIDTH))
191 .item(PopupMenuItem::element(move |_, cx| {
192 header(&header_context, cx)
193 }))
194 .separator()
195 .submenu_with_icon(
196 Some(status_icon(context.summary.presence)),
197 row_label,
198 window,
199 cx,
200 move |submenu, window, cx| state_menu(submenu, window, cx, &submenu_context),
201 )
202 .item(work_location_item(context, window, cx))
203 .item(
204 PopupMenuItem::new("Set status message")
205 .icon(menu_icon(IconName::Pencil))
206 .on_click(move |_, _, cx| {
207 message_app.update(cx, |state, cx| state.request_status_message(cx));
208 }),
209 )
210 .separator()
211 .item(
212 PopupMenuItem::new("Chat list")
213 .icon(menu_icon(IconName::LayoutList))
214 .on_click(move |_, _, cx| {
215 chat_list_app.update(cx, |state, cx| {
216 state.open_settings(SettingsSection::Chats, cx)
217 });
218 }),
219 )
220 .item(
221 PopupMenuItem::new("Translation")
222 .icon(menu_icon(IconName::Languages))
223 .on_click(move |_, _, cx| {
224 translation_app.update(cx, |state, cx| {
225 state.open_settings(SettingsSection::General, cx)
226 });
227 }),
228 )
229 .item(
230 PopupMenuItem::new("Make a test call")
231 .icon(menu_icon(IconName::Phone))
232 .on_click(move |_, _, cx| {
233 call_app.update(cx, |state, cx| state.start_test_call(cx));
234 }),
235 )
236 .item(
237 PopupMenuItem::new("Notification settings")
238 .icon(menu_icon(IconName::Bell))
239 .on_click(move |_, _, cx| {
240 settings_app.update(cx, |state, cx| {
241 state.open_settings(SettingsSection::Notifications, cx)
242 });
243 }),
244 )
245 .item(
246 PopupMenuItem::new("Settings")
247 .icon(menu_icon(IconName::Settings))
248 .on_click(move |_, _, cx| {
249 general_app.update(cx, |state, cx| {
250 state.open_settings(SettingsSection::General, cx)
251 });
252 }),
253 )
254 .item(PopupMenuItem::element(move |_, cx| {
255 super::window_zoom::menu_row(&zoom_app, cx)
256 }));
257 if !demo {
258 return popup;
259 }
260 popup.item(
261 PopupMenuItem::new("Simulate incoming call")
262 .icon(menu_icon(IconName::Phone))
263 .on_click(move |_, _, cx| {
264 ring_app.update(cx, |state, cx| state.demo_incoming_ring(cx));
265 }),
266 )
267}
268
269fn work_location_icon(kind: Option<WorkLocationKind>) -> gpui_kit::component::Icon {
270 menu_icon(match kind {
271 Some(WorkLocationKind::Office) => IconName::Building2,
272 Some(WorkLocationKind::Remote) => IconName::House,
273 None => IconName::MapPin,
274 })
275}
276
277fn work_location_item(
278 context: &MenuContext,
279 window: &mut Window,
280 cx: &mut Context<PopupMenu>,
281) -> PopupMenuItem {
282 let current = context.status.work_location;
283 let app = context.app.clone();
284 let submenu = PopupMenu::build(window, cx, move |menu, _, _| {
285 let pick = |label: &'static str, kind: WorkLocationKind| {
286 let app = app.clone();
287 PopupMenuItem::new(label)
288 .icon(work_location_icon(Some(kind)))
289 .on_click(move |_, _, cx| {
290 app.update(cx, |state, cx| state.set_own_work_location(Some(kind), cx));
291 })
292 };
293 let clear_app = app.clone();
294 let menu = menu
295 .item(PopupMenuItem::label("Set work location for today"))
296 .item(pick("Office", WorkLocationKind::Office))
297 .item(pick("Remote", WorkLocationKind::Remote));
298 if current.is_none() {
299 return menu;
300 }
301 menu.separator().item(
302 PopupMenuItem::new("Clear work location")
303 .icon(menu_icon(IconName::X))
304 .on_click(move |_, _, cx| {
305 clear_app.update(cx, |state, cx| state.set_own_work_location(None, cx));
306 }),
307 )
308 });
309 PopupMenuItem::submenu(work_location_label(current), submenu)
310 .icon(work_location_icon(current.map(|location| location.kind)))
311}
312
313fn state_menu(
314 popup: PopupMenu,
315 window: &mut Window,
316 cx: &mut Context<PopupMenu>,
317 context: &MenuContext,
318) -> PopupMenu {
319 let current = context.status.forced.map(|forced| forced.kind);
320 let mut popup = popup.check_side(Side::Right);
321 for kind in STATUS_CHOICES {
322 let app = context.app.clone();
323 popup = popup.item(
324 PopupMenuItem::new(choice_label(kind))
325 .icon(status_icon(presence_kind_for(kind)))
326 .checked(current == Some(kind))
327 .on_click(move |_, _, cx| {
328 let forced = ForcedAvailability {
329 kind,
330 expires_at: None,
331 };
332 app.update(cx, |state, cx| state.set_own_availability(Some(forced), cx));
333 }),
334 );
335 }
336 let target = duration_target(&context.status);
337 let app = context.app.clone();
338 let durations = PopupMenu::build(window, cx, move |menu, _, _| {
339 STATUS_DURATIONS.iter().fold(menu, |menu, duration| {
340 menu.item(duration_item(&app, target, *duration))
341 })
342 });
343 let reset_app = context.app.clone();
344 popup
345 .separator()
346 .item(
347 PopupMenuItem::submenu("Duration", durations)
348 .icon(menu_icon(IconName::Clock))
349 .disabled(target.is_none()),
350 )
351 .item(
352 PopupMenuItem::new("Reset status")
353 .icon(menu_icon(IconName::RotateCcw))
354 .on_click(move |_, _, cx| {
355 reset_app.update(cx, |state, cx| state.set_own_availability(None, cx));
356 }),
357 )
358}
359
360fn duration_item(
361 app: &Entity<AppState>,
362 target: Option<ForcedKind>,
363 duration: StatusDuration,
364) -> PopupMenuItem {
365 let app = app.clone();
366 PopupMenuItem::new(duration.label())
367 .disabled(target.is_none())
368 .on_click(move |_, _, cx| {
369 let Some(kind) = target else {
370 return;
371 };
372 let forced = ForcedAvailability {
373 kind,
374 expires_at: Some(duration.expires_at(Local::now())),
375 };
376 app.update(cx, |state, cx| state.set_own_availability(Some(forced), cx));
377 })
378}