| 1 | use chrono::Local; |
| 2 | use gpui_kit::assets::IconName; |
| 3 | use gpui_kit::component::{ |
| 4 | Side, |
| 5 | button::{Button, ButtonVariants as _}, |
| 6 | h_flex, |
| 7 | menu::{DropdownMenu as _, PopupMenu, PopupMenuItem}, |
| 8 | v_flex, |
| 9 | }; |
| 10 | use gpui_kit::*; |
| 11 | use teams_core::{ForcedAvailability, ForcedKind, PresenceStatus, WorkLocationKind}; |
| 12 | |
| 13 | use super::avatar::{person_avatar, with_presence}; |
| 14 | use super::widgets::{icon, symbol}; |
| 15 | use crate::app_state::AppState; |
| 16 | use crate::data::{Person, PresenceKind}; |
| 17 | use crate::own_status::{ |
| 18 | STATUS_CHOICES, STATUS_DURATIONS, StateSummary, StatusDuration, choice_label, duration_target, |
| 19 | presence_kind_for, state_summary, work_location_label, |
| 20 | }; |
| 21 | use crate::theme; |
| 22 | |
| 23 | pub const MENU_WIDTH: f32 = 250.; |
| 24 | const TITLE_AVATAR_SIZE: f32 = 26.; |
| 25 | const HEADER_AVATAR_SIZE: f32 = 40.; |
| 26 | const RING_WIDTH: f32 = 2.; |
| 27 | const MENU_ICON_SIZE: f32 = 14.; |
| 28 | const ICON_COLUMN: f32 = 16.; |
| 29 | const HEADER_INSET: f32 = 24.; |
| 30 | |
| 31 | #[derive(Clone)] |
| 32 | struct MenuContext { |
| 33 | app: Entity<AppState>, |
| 34 | person: Person, |
| 35 | email: String, |
| 36 | note: Option<String>, |
| 37 | status: PresenceStatus, |
| 38 | summary: StateSummary, |
| 39 | } |
| 40 | |
| 41 | pub fn own_status_button(app: &Entity<AppState>, state: &AppState) -> Option<AnyElement> { |
| 42 | let person = state.directory.me.clone()?; |
| 43 | let presence = state.directory.presence_of(&person.user_id); |
| 44 | let open = state.status_menu_open; |
| 45 | let summary = state_summary(&state.own_status, state.own_presence_kind(), &Local::now()); |
| 46 | let context = MenuContext { |
| 47 | app: app.clone(), |
| 48 | email: state.own_email.clone(), |
| 49 | note: state.own_status.note.as_ref().map(|note| note.text.clone()), |
| 50 | status: state.own_status.clone(), |
| 51 | summary, |
| 52 | person: person.clone(), |
| 53 | }; |
| 54 | let avatar = with_presence( |
| 55 | person_avatar( |
| 56 | &state.directory, |
| 57 | Some(&person.user_id), |
| 58 | &person.display_name, |
| 59 | TITLE_AVATAR_SIZE, |
| 60 | ), |
| 61 | presence, |
| 62 | TITLE_AVATAR_SIZE, |
| 63 | theme::background(), |
| 64 | ); |
| 65 | let ringed = div() |
| 66 | .p(px(1.)) |
| 67 | .rounded_full() |
| 68 | .border(px(RING_WIDTH)) |
| 69 | .border_color(if open { |
| 70 | theme::accent() |
| 71 | } else { |
| 72 | transparent_black() |
| 73 | }) |
| 74 | .child(avatar); |
| 75 | let toggle = app.clone(); |
| 76 | Some( |
| 77 | Button::new("title-own-status") |
| 78 | .ghost() |
| 79 | .p_0() |
| 80 | .rounded_full() |
| 81 | .child(ringed) |
| 82 | .dropdown_menu_with_anchor(Anchor::TopRight, move |popup, window, cx| { |
| 83 | status_menu(popup, window, cx, &context) |
| 84 | }) |
| 85 | .on_open_change(move |open, _, cx| { |
| 86 | let open = *open; |
| 87 | toggle.update(cx, |state, cx| state.set_status_menu_open(open, cx)); |
| 88 | }) |
| 89 | .into_any_element(), |
| 90 | ) |
| 91 | } |
| 92 | |
| 93 | fn status_icon(kind: PresenceKind) -> gpui_kit::component::Icon { |
| 94 | let (name, color) = match kind { |
| 95 | PresenceKind::Available => ("status_dot", theme::green()), |
| 96 | PresenceKind::Busy => ("status_dot", theme::red()), |
| 97 | PresenceKind::DoNotDisturb => ("status_dnd", theme::red()), |
| 98 | PresenceKind::Away => ("status_away", theme::amber()), |
| 99 | PresenceKind::Offline => ("status_ring", theme::text_muted()), |
| 100 | PresenceKind::Unknown => ("status_ring", theme::text_faint()), |
| 101 | }; |
| 102 | symbol(name, MENU_ICON_SIZE, color) |
| 103 | } |
| 104 | |
| 105 | fn menu_icon(name: IconName) -> gpui_kit::component::Icon { |
| 106 | icon(name, MENU_ICON_SIZE, theme::text_muted()) |
| 107 | } |
| 108 | |
| 109 | fn header(context: &MenuContext, cx: &App) -> Div { |
| 110 | let state = context.app.read(cx); |
| 111 | let presence = state.directory.presence_of(&context.person.user_id); |
| 112 | let avatar = with_presence( |
| 113 | person_avatar( |
| 114 | &state.directory, |
| 115 | Some(&context.person.user_id), |
| 116 | &context.person.display_name, |
| 117 | HEADER_AVATAR_SIZE, |
| 118 | ), |
| 119 | presence, |
| 120 | HEADER_AVATAR_SIZE, |
| 121 | theme::surface(), |
| 122 | ); |
| 123 | let line = |text: String, color: Hsla| { |
| 124 | div() |
| 125 | .w_full() |
| 126 | .truncate() |
| 127 | .text_size(px(12.)) |
| 128 | .text_color(color) |
| 129 | .child(text) |
| 130 | }; |
| 131 | h_flex() |
| 132 | .w(px(MENU_WIDTH - HEADER_INSET)) |
| 133 | .ml(px(-(ICON_COLUMN + 4.))) |
| 134 | .py(px(6.)) |
| 135 | .gap(px(10.)) |
| 136 | .items_center() |
| 137 | .child(avatar) |
| 138 | .child( |
| 139 | v_flex() |
| 140 | .flex_1() |
| 141 | .min_w_0() |
| 142 | .child( |
| 143 | div() |
| 144 | .w_full() |
| 145 | .truncate() |
| 146 | .text_size(px(14.)) |
| 147 | .font_weight(FontWeight::SEMIBOLD) |
| 148 | .text_color(theme::text()) |
| 149 | .child(context.person.display_name.clone()), |
| 150 | ) |
| 151 | .child(line(context.email.clone(), theme::text_muted())) |
| 152 | .children( |
| 153 | context |
| 154 | .note |
| 155 | .clone() |
| 156 | .map(|note| line(note, theme::text_soft())), |
| 157 | ), |
| 158 | ) |
| 159 | } |
| 160 | |
| 161 | fn status_menu( |
| 162 | popup: PopupMenu, |
| 163 | window: &mut Window, |
| 164 | cx: &mut Context<PopupMenu>, |
| 165 | context: &MenuContext, |
| 166 | ) -> PopupMenu { |
| 167 | let header_context = context.clone(); |
| 168 | let message_app = context.app.clone(); |
| 169 | let settings_app = context.app.clone(); |
| 170 | let row_label = match &context.summary.until { |
| 171 | Some(until) => format!("{} {until}", context.summary.label), |
| 172 | None => context.summary.label.to_owned(), |
| 173 | }; |
| 174 | let submenu_context = context.clone(); |
| 175 | popup |
| 176 | .min_w(px(MENU_WIDTH)) |
| 177 | .item(PopupMenuItem::element(move |_, cx| { |
| 178 | header(&header_context, cx) |
| 179 | })) |
| 180 | .separator() |
| 181 | .submenu_with_icon( |
| 182 | Some(status_icon(context.summary.presence)), |
| 183 | row_label, |
| 184 | window, |
| 185 | cx, |
| 186 | move |submenu, window, cx| state_menu(submenu, window, cx, &submenu_context), |
| 187 | ) |
| 188 | .item(work_location_item(context, window, cx)) |
| 189 | .item( |
| 190 | PopupMenuItem::new("Set status message") |
| 191 | .icon(menu_icon(IconName::Pencil)) |
| 192 | .on_click(move |_, _, cx| { |
| 193 | message_app.update(cx, |state, cx| state.request_status_message(cx)); |
| 194 | }), |
| 195 | ) |
| 196 | .separator() |
| 197 | .item( |
| 198 | PopupMenuItem::new("Notification settings") |
| 199 | .icon(menu_icon(IconName::Settings)) |
| 200 | .on_click(move |_, _, cx| { |
| 201 | settings_app.update(cx, |state, cx| state.open_notification_settings(cx)); |
| 202 | }), |
| 203 | ) |
| 204 | } |
| 205 | |
| 206 | fn work_location_icon(kind: Option<WorkLocationKind>) -> gpui_kit::component::Icon { |
| 207 | menu_icon(match kind { |
| 208 | Some(WorkLocationKind::Office) => IconName::Building2, |
| 209 | Some(WorkLocationKind::Remote) => IconName::House, |
| 210 | None => IconName::MapPin, |
| 211 | }) |
| 212 | } |
| 213 | |
| 214 | fn work_location_item( |
| 215 | context: &MenuContext, |
| 216 | window: &mut Window, |
| 217 | cx: &mut Context<PopupMenu>, |
| 218 | ) -> PopupMenuItem { |
| 219 | let current = context.status.work_location; |
| 220 | let app = context.app.clone(); |
| 221 | let submenu = PopupMenu::build(window, cx, move |menu, _, _| { |
| 222 | let pick = |label: &'static str, kind: WorkLocationKind| { |
| 223 | let app = app.clone(); |
| 224 | PopupMenuItem::new(label) |
| 225 | .icon(work_location_icon(Some(kind))) |
| 226 | .on_click(move |_, _, cx| { |
| 227 | app.update(cx, |state, cx| state.set_own_work_location(Some(kind), cx)); |
| 228 | }) |
| 229 | }; |
| 230 | let clear_app = app.clone(); |
| 231 | let menu = menu |
| 232 | .item(PopupMenuItem::label("Set work location for today")) |
| 233 | .item(pick("Office", WorkLocationKind::Office)) |
| 234 | .item(pick("Remote", WorkLocationKind::Remote)); |
| 235 | if current.is_none() { |
| 236 | return menu; |
| 237 | } |
| 238 | menu.separator().item( |
| 239 | PopupMenuItem::new("Clear work location") |
| 240 | .icon(menu_icon(IconName::X)) |
| 241 | .on_click(move |_, _, cx| { |
| 242 | clear_app.update(cx, |state, cx| state.set_own_work_location(None, cx)); |
| 243 | }), |
| 244 | ) |
| 245 | }); |
| 246 | PopupMenuItem::submenu(work_location_label(current), submenu) |
| 247 | .icon(work_location_icon(current.map(|location| location.kind))) |
| 248 | } |
| 249 | |
| 250 | fn state_menu( |
| 251 | popup: PopupMenu, |
| 252 | window: &mut Window, |
| 253 | cx: &mut Context<PopupMenu>, |
| 254 | context: &MenuContext, |
| 255 | ) -> PopupMenu { |
| 256 | let current = context.status.forced.map(|forced| forced.kind); |
| 257 | let mut popup = popup.check_side(Side::Right); |
| 258 | for kind in STATUS_CHOICES { |
| 259 | let app = context.app.clone(); |
| 260 | popup = popup.item( |
| 261 | PopupMenuItem::new(choice_label(kind)) |
| 262 | .icon(status_icon(presence_kind_for(kind))) |
| 263 | .checked(current == Some(kind)) |
| 264 | .on_click(move |_, _, cx| { |
| 265 | let forced = ForcedAvailability { |
| 266 | kind, |
| 267 | expires_at: None, |
| 268 | }; |
| 269 | app.update(cx, |state, cx| state.set_own_availability(Some(forced), cx)); |
| 270 | }), |
| 271 | ); |
| 272 | } |
| 273 | let target = duration_target(&context.status); |
| 274 | let app = context.app.clone(); |
| 275 | let durations = PopupMenu::build(window, cx, move |menu, _, _| { |
| 276 | STATUS_DURATIONS.iter().fold(menu, |menu, duration| { |
| 277 | menu.item(duration_item(&app, target, *duration)) |
| 278 | }) |
| 279 | }); |
| 280 | let reset_app = context.app.clone(); |
| 281 | popup |
| 282 | .separator() |
| 283 | .item( |
| 284 | PopupMenuItem::submenu("Duration", durations) |
| 285 | .icon(menu_icon(IconName::Clock)) |
| 286 | .disabled(target.is_none()), |
| 287 | ) |
| 288 | .item( |
| 289 | PopupMenuItem::new("Reset status") |
| 290 | .icon(menu_icon(IconName::RotateCcw)) |
| 291 | .on_click(move |_, _, cx| { |
| 292 | reset_app.update(cx, |state, cx| state.set_own_availability(None, cx)); |
| 293 | }), |
| 294 | ) |
| 295 | } |
| 296 | |
| 297 | fn duration_item( |
| 298 | app: &Entity<AppState>, |
| 299 | target: Option<ForcedKind>, |
| 300 | duration: StatusDuration, |
| 301 | ) -> PopupMenuItem { |
| 302 | let app = app.clone(); |
| 303 | PopupMenuItem::new(duration.label()) |
| 304 | .disabled(target.is_none()) |
| 305 | .on_click(move |_, _, cx| { |
| 306 | let Some(kind) = target else { |
| 307 | return; |
| 308 | }; |
| 309 | let forced = ForcedAvailability { |
| 310 | kind, |
| 311 | expires_at: Some(duration.expires_at(Local::now())), |
| 312 | }; |
| 313 | app.update(cx, |state, cx| state.set_own_availability(Some(forced), cx)); |
| 314 | }) |
| 315 | } |