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