| 1 | use std::sync::Arc; |
| 2 | |
| 3 | use chrono::Utc; |
| 4 | use gpui_kit::assets::IconName; |
| 5 | use gpui_kit::base::GlobalState; |
| 6 | use gpui_kit::component::scroll::ScrollableElement as _; |
| 7 | use gpui_kit::component::{h_flex, tooltip::Tooltip, v_flex}; |
| 8 | use gpui_kit::prelude::FluentBuilder as _; |
| 9 | use gpui_kit::*; |
| 10 | use teams_core::{OrgPerson, PROFILE_LINK_PREFIX, PersonProfile, WorkLocationKind}; |
| 11 | |
| 12 | use super::avatar::{person_avatar, with_presence}; |
| 13 | use super::new_chat::Pick; |
| 14 | use super::new_meeting_dialog::open_new_meeting_with; |
| 15 | use super::widgets::icon; |
| 16 | use crate::app_state::{AppHandle, AppState}; |
| 17 | use crate::data::{Presence, PresenceKind, presence_kind_of}; |
| 18 | use crate::people::resolve_names; |
| 19 | use crate::presence_follow::{NOTIFY_WHEN_AVAILABLE, TURN_OFF_NOTIFICATIONS}; |
| 20 | use crate::profile_model::{ |
| 21 | ContactKind, ContactRow, StatusBox, contact_rows, has_organization, local_time_label, |
| 22 | mailto_url, status_box, status_line, work_location_label, |
| 23 | }; |
| 24 | use crate::theme::{self, ElevatedSurface as _}; |
| 25 | |
| 26 | const CARD_WIDTH: f32 = 340.; |
| 27 | const CARD_RADIUS: f32 = 10.; |
| 28 | const CARD_PADDING: f32 = 16.; |
| 29 | const CARD_GAP: f32 = 12.; |
| 30 | const CARD_MARGIN: f32 = 8.; |
| 31 | const CLICK_OFFSET: f32 = 4.; |
| 32 | const PHOTO_SIZE: f32 = 56.; |
| 33 | const PERSON_ROW_AVATAR: f32 = 22.; |
| 34 | const STATUS_LINES: usize = 3; |
| 35 | const REPORTS_MAX_HEIGHT: f32 = 168.; |
| 36 | const ACTION_HEIGHT: f32 = 30.; |
| 37 | const SKELETON_HEIGHT: f32 = 10.; |
| 38 | const SCHEDULE_MEETING: &str = "Schedule meeting"; |
| 39 | const EMAIL: &str = "Email"; |
| 40 | const COPY_EMAIL: &str = "Copy email"; |
| 41 | |
| 42 | pub enum ProfileCardEvent { |
| 43 | Close, |
| 44 | } |
| 45 | |
| 46 | pub struct ProfileCard { |
| 47 | app: Entity<AppState>, |
| 48 | user_id: String, |
| 49 | history: Vec<String>, |
| 50 | anchor: Point<Pixels>, |
| 51 | contact_open: bool, |
| 52 | organization_open: bool, |
| 53 | reports_open: bool, |
| 54 | focus_handle: FocusHandle, |
| 55 | _observation: Subscription, |
| 56 | } |
| 57 | |
| 58 | impl EventEmitter<ProfileCardEvent> for ProfileCard {} |
| 59 | |
| 60 | pub fn open_profile(user_id: &str, position: Point<Pixels>, cx: &mut App) { |
| 61 | let Some(handle) = cx.try_global::<AppHandle>() else { |
| 62 | return; |
| 63 | }; |
| 64 | let (app, user_id) = (handle.0.clone(), user_id.to_owned()); |
| 65 | app.update(cx, |state, cx| { |
| 66 | state.open_profile_card(&user_id, position, cx) |
| 67 | }); |
| 68 | } |
| 69 | |
| 70 | /// `true` when the link was a profile link and has been handled. |
| 71 | pub fn open_profile_link(url: &str, position: Point<Pixels>, cx: &mut App) -> bool { |
| 72 | match url.strip_prefix(PROFILE_LINK_PREFIX) { |
| 73 | Some(user_id) => { |
| 74 | open_profile(user_id, position, cx); |
| 75 | true |
| 76 | } |
| 77 | None => false, |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | pub fn opens_profile(element: Stateful<Div>, user_id: Option<&str>) -> Stateful<Div> { |
| 82 | match user_id { |
| 83 | Some(user_id) => { |
| 84 | let user_id = user_id.to_owned(); |
| 85 | element.cursor_pointer().on_click(move |event, _, cx| { |
| 86 | cx.stop_propagation(); |
| 87 | open_profile(&user_id, event.position(), cx) |
| 88 | }) |
| 89 | } |
| 90 | None => element, |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | impl ProfileCard { |
| 95 | pub fn new( |
| 96 | app: Entity<AppState>, |
| 97 | user_id: String, |
| 98 | anchor: Point<Pixels>, |
| 99 | window: &mut Window, |
| 100 | cx: &mut Context<Self>, |
| 101 | ) -> Self { |
| 102 | let focus_handle = cx.focus_handle(); |
| 103 | window.focus(&focus_handle, cx); |
| 104 | let observation = cx.observe(&app, |_, _, cx| cx.notify()); |
| 105 | ProfileCard { |
| 106 | app, |
| 107 | user_id, |
| 108 | history: Vec::new(), |
| 109 | anchor, |
| 110 | contact_open: true, |
| 111 | organization_open: true, |
| 112 | reports_open: false, |
| 113 | focus_handle, |
| 114 | _observation: observation, |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | pub fn move_to(&mut self, user_id: String, anchor: Point<Pixels>, cx: &mut Context<Self>) { |
| 119 | self.history.clear(); |
| 120 | self.anchor = anchor; |
| 121 | self.show(user_id, false, cx); |
| 122 | } |
| 123 | |
| 124 | fn show(&mut self, user_id: String, remember: bool, cx: &mut Context<Self>) { |
| 125 | if remember { |
| 126 | self.history |
| 127 | .push(std::mem::replace(&mut self.user_id, user_id)); |
| 128 | } else { |
| 129 | self.user_id = user_id; |
| 130 | } |
| 131 | self.reports_open = false; |
| 132 | let id = self.user_id.clone(); |
| 133 | self.app.update(cx, |state, cx| { |
| 134 | state.request_profile(&id, false, cx); |
| 135 | state.request_avatars(vec![id.clone()], cx); |
| 136 | }); |
| 137 | cx.notify(); |
| 138 | } |
| 139 | |
| 140 | fn go_back(&mut self, cx: &mut Context<Self>) { |
| 141 | if let Some(previous) = self.history.pop() { |
| 142 | self.show(previous, false, cx); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | fn on_key_down(&mut self, event: &KeyDownEvent, _: &mut Window, cx: &mut Context<Self>) { |
| 147 | if event.keystroke.key == "escape" { |
| 148 | cx.emit(ProfileCardEvent::Close); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | fn display_name(&self, profile: Option<&PersonProfile>, cx: &App) -> String { |
| 153 | profile |
| 154 | .and_then(|profile| profile.display_name.clone()) |
| 155 | .or_else(|| { |
| 156 | resolve_names(self.app.read(cx), std::slice::from_ref(&self.user_id)) |
| 157 | .remove(&self.user_id) |
| 158 | }) |
| 159 | .unwrap_or_else(|| "Unknown person".to_owned()) |
| 160 | } |
| 161 | |
| 162 | fn presence(&self, profile: Option<&PersonProfile>, cx: &App) -> Presence { |
| 163 | let directory = self.app.read(cx).directory.presence_of(&self.user_id); |
| 164 | match (directory, profile.and_then(|profile| profile.availability)) { |
| 165 | (Presence::Live(kind), _) if kind != PresenceKind::Unknown => directory, |
| 166 | (_, Some(availability)) => Presence::Live(presence_kind_of(availability)), |
| 167 | _ => directory, |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | fn header(&self, profile: Option<&PersonProfile>, failed: bool, cx: &App) -> Div { |
| 172 | let name = self.display_name(profile, cx); |
| 173 | let presence = self.presence(profile, cx); |
| 174 | let directory = &self.app.read(cx).directory; |
| 175 | let photo = with_presence( |
| 176 | person_avatar(directory, Some(&self.user_id), &name, PHOTO_SIZE), |
| 177 | presence, |
| 178 | PHOTO_SIZE, |
| 179 | theme::surface_raised(), |
| 180 | ); |
| 181 | let availability = directory.status_label(&self.user_id, presence); |
| 182 | let local_time = profile.and_then(|profile| local_time_label(profile, Utc::now())); |
| 183 | let subline = status_line(availability, local_time.as_deref()); |
| 184 | let details = v_flex() |
| 185 | .gap(px(1.)) |
| 186 | .children(profile.into_iter().flat_map(|profile| { |
| 187 | [&profile.job_title, &profile.department] |
| 188 | .into_iter() |
| 189 | .flatten() |
| 190 | .filter(|text| !text.trim().is_empty()) |
| 191 | .map(|text| muted_line(text.clone())) |
| 192 | .collect::<Vec<_>>() |
| 193 | })); |
| 194 | let work_location = profile |
| 195 | .and_then(|profile| profile.work_location) |
| 196 | .map(|location| { |
| 197 | let glyph = match location.kind { |
| 198 | WorkLocationKind::Office => IconName::Building2, |
| 199 | WorkLocationKind::Remote => IconName::House, |
| 200 | }; |
| 201 | h_flex() |
| 202 | .gap(px(5.)) |
| 203 | .items_center() |
| 204 | .child(icon(glyph, 13., theme::text_muted())) |
| 205 | .child(muted_line(work_location_label(location).to_owned())) |
| 206 | }); |
| 207 | let loading = profile.is_none() && !failed; |
| 208 | h_flex().gap(px(12.)).items_center().child(photo).child( |
| 209 | v_flex() |
| 210 | .flex_1() |
| 211 | .min_w_0() |
| 212 | .gap(px(2.)) |
| 213 | .child( |
| 214 | div() |
| 215 | .truncate() |
| 216 | .text_size(px(15.)) |
| 217 | .font_weight(FontWeight::SEMIBOLD) |
| 218 | .text_color(theme::text()) |
| 219 | .child(name), |
| 220 | ) |
| 221 | .when(!subline.is_empty(), |column| { |
| 222 | column.child(muted_line(subline)) |
| 223 | }) |
| 224 | .children(work_location) |
| 225 | .child(details) |
| 226 | .when(loading, |column| column.child(skeleton(0.6))), |
| 227 | ) |
| 228 | } |
| 229 | |
| 230 | fn status_box(&self, status: &StatusBox) -> AnyElement { |
| 231 | let (background, foreground) = if status.out_of_office { |
| 232 | (theme::amber().opacity(0.12), theme::amber()) |
| 233 | } else { |
| 234 | (theme::surface(), theme::text_soft()) |
| 235 | }; |
| 236 | let full_text = status.text.clone(); |
| 237 | div() |
| 238 | .id("profile-status") |
| 239 | .w_full() |
| 240 | .px(px(10.)) |
| 241 | .py(px(8.)) |
| 242 | .rounded(px(6.)) |
| 243 | .bg(background) |
| 244 | .text_color(foreground) |
| 245 | .text_size(px(12.)) |
| 246 | .line_height(relative(1.4)) |
| 247 | .child( |
| 248 | div() |
| 249 | .line_clamp(STATUS_LINES) |
| 250 | .text_ellipsis() |
| 251 | .child(status.text.clone()), |
| 252 | ) |
| 253 | .tooltip(move |window, cx| Tooltip::new(full_text.clone()).build(window, cx)) |
| 254 | .into_any_element() |
| 255 | } |
| 256 | |
| 257 | fn actions(&self, email: Option<String>, cx: &mut Context<Self>) -> Div { |
| 258 | let is_me = self |
| 259 | .app |
| 260 | .read(cx) |
| 261 | .directory |
| 262 | .me |
| 263 | .as_ref() |
| 264 | .is_some_and(|me| me.user_id == self.user_id); |
| 265 | let profile = self |
| 266 | .app |
| 267 | .read(cx) |
| 268 | .profiles |
| 269 | .get(&self.user_id) |
| 270 | .and_then(|entry| entry.profile.clone()); |
| 271 | let name = self.display_name(profile.as_deref(), cx); |
| 272 | let user_id = self.user_id.clone(); |
| 273 | let (mail_email, copy_email) = (email.clone(), email.clone()); |
| 274 | let invitee = Pick { |
| 275 | user_id: user_id.clone(), |
| 276 | name: name.clone(), |
| 277 | mail: email, |
| 278 | organization: None, |
| 279 | }; |
| 280 | let chat_id = user_id.clone(); |
| 281 | let (video_id, audio_id) = (user_id.clone(), user_id.clone()); |
| 282 | let followed = self.app.read(cx).is_presence_followed(&user_id); |
| 283 | let (follow_id, follow_name) = (user_id.clone(), name.clone()); |
| 284 | h_flex() |
| 285 | .gap(px(8.)) |
| 286 | .when(!is_me, |row| { |
| 287 | row.child( |
| 288 | action_button("profile-chat", "Chat", IconName::MessageSquare, true).on_click( |
| 289 | cx.listener(move |this, _, _, cx| { |
| 290 | let user_id = chat_id.clone(); |
| 291 | this.app |
| 292 | .update(cx, |state, cx| state.open_chat_with(&user_id, cx)); |
| 293 | cx.emit(ProfileCardEvent::Close); |
| 294 | }), |
| 295 | ), |
| 296 | ) |
| 297 | .child( |
| 298 | icon_action( |
| 299 | "profile-video-call", |
| 300 | IconName::Video, |
| 301 | video_call_tooltip(&name), |
| 302 | ) |
| 303 | .on_click(cx.listener(move |this, _, _, cx| { |
| 304 | let user_id = video_id.clone(); |
| 305 | this.app |
| 306 | .update(cx, |state, cx| state.call_person(&user_id, true, cx)); |
| 307 | cx.emit(ProfileCardEvent::Close); |
| 308 | })), |
| 309 | ) |
| 310 | .child( |
| 311 | icon_action( |
| 312 | "profile-audio-call", |
| 313 | IconName::Phone, |
| 314 | audio_call_tooltip(&name), |
| 315 | ) |
| 316 | .on_click(cx.listener(move |this, _, _, cx| { |
| 317 | let user_id = audio_id.clone(); |
| 318 | this.app |
| 319 | .update(cx, |state, cx| state.call_person(&user_id, false, cx)); |
| 320 | cx.emit(ProfileCardEvent::Close); |
| 321 | })), |
| 322 | ) |
| 323 | .child( |
| 324 | icon_action( |
| 325 | "profile-schedule-meeting", |
| 326 | IconName::Calendar, |
| 327 | SCHEDULE_MEETING.to_owned(), |
| 328 | ) |
| 329 | .on_click(cx.listener(move |this, _, window, cx| { |
| 330 | open_new_meeting_with(this.app.clone(), vec![invitee.clone()], window, cx); |
| 331 | cx.emit(ProfileCardEvent::Close); |
| 332 | })), |
| 333 | ) |
| 334 | .child( |
| 335 | icon_action( |
| 336 | "profile-follow-presence", |
| 337 | if followed { |
| 338 | IconName::BellOff |
| 339 | } else { |
| 340 | IconName::BellRing |
| 341 | }, |
| 342 | if followed { |
| 343 | TURN_OFF_NOTIFICATIONS |
| 344 | } else { |
| 345 | NOTIFY_WHEN_AVAILABLE |
| 346 | } |
| 347 | .to_owned(), |
| 348 | ) |
| 349 | .on_click(cx.listener(move |this, _, _, cx| { |
| 350 | let (user_id, name) = (follow_id.clone(), follow_name.clone()); |
| 351 | this.app.update(cx, |state, cx| { |
| 352 | state.set_presence_followed(&user_id, &name, !followed, cx) |
| 353 | }); |
| 354 | })), |
| 355 | ) |
| 356 | }) |
| 357 | .when_some(mail_email, |row, address| { |
| 358 | row.child( |
| 359 | icon_action("profile-email", IconName::Mail, EMAIL.to_owned()).on_click( |
| 360 | cx.listener(move |_, _, _, cx| { |
| 361 | app_base::open_link::open_url(cx, &mailto_url(&address)); |
| 362 | cx.emit(ProfileCardEvent::Close); |
| 363 | }), |
| 364 | ), |
| 365 | ) |
| 366 | }) |
| 367 | .when_some(copy_email, |row, address| { |
| 368 | row.child( |
| 369 | icon_action("profile-copy-email", IconName::Copy, COPY_EMAIL.to_owned()) |
| 370 | .on_click(cx.listener(move |this, _, _, cx| { |
| 371 | this.app |
| 372 | .update(cx, |state, cx| state.copy_profile_email(&address, cx)); |
| 373 | cx.emit(ProfileCardEvent::Close); |
| 374 | })), |
| 375 | ) |
| 376 | }) |
| 377 | } |
| 378 | |
| 379 | fn section_header( |
| 380 | &self, |
| 381 | id: &'static str, |
| 382 | label: &'static str, |
| 383 | open: bool, |
| 384 | cx: &mut Context<Self>, |
| 385 | ) -> Stateful<Div> { |
| 386 | h_flex() |
| 387 | .id(id) |
| 388 | .gap(px(4.)) |
| 389 | .items_center() |
| 390 | .cursor_pointer() |
| 391 | .text_size(px(12.)) |
| 392 | .font_weight(FontWeight::SEMIBOLD) |
| 393 | .text_color(theme::text_muted()) |
| 394 | .child(icon( |
| 395 | if open { |
| 396 | IconName::ChevronDown |
| 397 | } else { |
| 398 | IconName::ChevronRight |
| 399 | }, |
| 400 | 14., |
| 401 | theme::text_muted(), |
| 402 | )) |
| 403 | .child(label) |
| 404 | .on_click(cx.listener(move |this, _, _, cx| { |
| 405 | match id { |
| 406 | "profile-contact-header" => this.contact_open = !this.contact_open, |
| 407 | _ => this.organization_open = !this.organization_open, |
| 408 | } |
| 409 | cx.notify(); |
| 410 | })) |
| 411 | } |
| 412 | |
| 413 | fn contact_section(&self, rows: Vec<ContactRow>, cx: &mut Context<Self>) -> Div { |
| 414 | let open = self.contact_open; |
| 415 | v_flex() |
| 416 | .gap(px(6.)) |
| 417 | .child(self.section_header("profile-contact-header", "Contact", open, cx)) |
| 418 | .when(open, |section| { |
| 419 | section.children(rows.into_iter().enumerate().map(|(index, row)| { |
| 420 | let value = row.value.clone(); |
| 421 | let app = self.app.clone(); |
| 422 | contact_row(index, row).on_click(move |_, _, cx| { |
| 423 | app.update(cx, |state, cx| state.copy_profile_detail(&value, cx)); |
| 424 | }) |
| 425 | })) |
| 426 | }) |
| 427 | } |
| 428 | |
| 429 | fn person_row( |
| 430 | &self, |
| 431 | id: String, |
| 432 | person: &OrgPerson, |
| 433 | caption: Option<&'static str>, |
| 434 | cx: &mut Context<Self>, |
| 435 | ) -> Stateful<Div> { |
| 436 | let name = person.display_name.clone().unwrap_or_default(); |
| 437 | let subtitle = caption |
| 438 | .map(str::to_owned) |
| 439 | .or_else(|| person.job_title.clone()) |
| 440 | .filter(|text| !text.is_empty()); |
| 441 | let target = person.id.clone(); |
| 442 | let directory = &self.app.read(cx).directory; |
| 443 | h_flex() |
| 444 | .id(ElementId::Name(id.into())) |
| 445 | .gap(px(8.)) |
| 446 | .px(px(4.)) |
| 447 | .py(px(3.)) |
| 448 | .items_center() |
| 449 | .rounded(px(6.)) |
| 450 | .cursor_pointer() |
| 451 | .hover(|row| row.bg(theme::row_hover())) |
| 452 | .child(person_avatar( |
| 453 | directory, |
| 454 | Some(&person.id), |
| 455 | &name, |
| 456 | PERSON_ROW_AVATAR, |
| 457 | )) |
| 458 | .child( |
| 459 | div() |
| 460 | .truncate() |
| 461 | .text_size(px(12.5)) |
| 462 | .text_color(theme::text()) |
| 463 | .child(name), |
| 464 | ) |
| 465 | .children( |
| 466 | subtitle.map(|text| div().flex_1().min_w_0().truncate().child(muted_text(text))), |
| 467 | ) |
| 468 | .on_click(cx.listener(move |this, _, _, cx| this.show(target.clone(), true, cx))) |
| 469 | } |
| 470 | |
| 471 | fn organization_section(&self, profile: &PersonProfile, cx: &mut Context<Self>) -> Div { |
| 472 | let open = self.organization_open; |
| 473 | let mut section = v_flex().gap(px(4.)).child(self.section_header( |
| 474 | "profile-organization-header", |
| 475 | "Organization", |
| 476 | open, |
| 477 | cx, |
| 478 | )); |
| 479 | if !open { |
| 480 | return section; |
| 481 | } |
| 482 | if let Some(manager) = &profile.manager { |
| 483 | section = section.child(self.person_row( |
| 484 | "profile-manager".to_owned(), |
| 485 | manager, |
| 486 | Some("Manager"), |
| 487 | cx, |
| 488 | )); |
| 489 | } |
| 490 | let count = profile.direct_reports.len(); |
| 491 | if count > 0 { |
| 492 | let label = match count { |
| 493 | 1 => "1 direct report".to_owned(), |
| 494 | count => format!("{count} direct reports"), |
| 495 | }; |
| 496 | let reports_open = self.reports_open; |
| 497 | section = section.child( |
| 498 | h_flex() |
| 499 | .id("profile-reports-toggle") |
| 500 | .gap(px(4.)) |
| 501 | .px(px(4.)) |
| 502 | .py(px(3.)) |
| 503 | .items_center() |
| 504 | .cursor_pointer() |
| 505 | .text_size(px(12.5)) |
| 506 | .text_color(theme::text_soft()) |
| 507 | .child(label) |
| 508 | .child(icon( |
| 509 | if reports_open { |
| 510 | IconName::ChevronDown |
| 511 | } else { |
| 512 | IconName::ChevronRight |
| 513 | }, |
| 514 | 14., |
| 515 | theme::text_muted(), |
| 516 | )) |
| 517 | .on_click(cx.listener(|this, _, _, cx| { |
| 518 | this.reports_open = !this.reports_open; |
| 519 | cx.notify(); |
| 520 | })), |
| 521 | ); |
| 522 | if reports_open { |
| 523 | let rows: Vec<Stateful<Div>> = profile |
| 524 | .direct_reports |
| 525 | .iter() |
| 526 | .enumerate() |
| 527 | .map(|(index, person)| { |
| 528 | self.person_row(format!("profile-report-{index}"), person, None, cx) |
| 529 | }) |
| 530 | .collect(); |
| 531 | section = section.child( |
| 532 | v_flex() |
| 533 | .id("profile-reports") |
| 534 | .max_h(px(REPORTS_MAX_HEIGHT)) |
| 535 | .overflow_y_scrollbar() |
| 536 | .children(rows), |
| 537 | ); |
| 538 | } |
| 539 | } |
| 540 | section |
| 541 | } |
| 542 | |
| 543 | fn loading_sections(&self) -> Div { |
| 544 | v_flex() |
| 545 | .gap(px(8.)) |
| 546 | .child(skeleton(1.)) |
| 547 | .child(skeleton(0.8)) |
| 548 | .child(skeleton(0.5)) |
| 549 | } |
| 550 | |
| 551 | fn failure(&self, cx: &mut Context<Self>) -> Div { |
| 552 | h_flex() |
| 553 | .gap(px(6.)) |
| 554 | .text_size(px(12.)) |
| 555 | .text_color(theme::red_soft()) |
| 556 | .child("Profile details could not be loaded.") |
| 557 | .child( |
| 558 | div() |
| 559 | .id("profile-retry") |
| 560 | .cursor_pointer() |
| 561 | .underline() |
| 562 | .text_color(theme::red_tint()) |
| 563 | .child("Retry") |
| 564 | .on_click(cx.listener(|this, _, _, cx| { |
| 565 | let id = this.user_id.clone(); |
| 566 | this.app |
| 567 | .update(cx, |state, cx| state.request_profile(&id, true, cx)); |
| 568 | })), |
| 569 | ) |
| 570 | } |
| 571 | |
| 572 | fn body(&self, entry_state: EntryState, cx: &mut Context<Self>) -> Vec<AnyElement> { |
| 573 | let EntryState { profile, failed } = entry_state; |
| 574 | let mut parts: Vec<AnyElement> = Vec::new(); |
| 575 | let Some(profile) = profile else { |
| 576 | parts.push(self.actions(None, cx).into_any_element()); |
| 577 | parts.push(if failed { |
| 578 | self.failure(cx).into_any_element() |
| 579 | } else { |
| 580 | self.loading_sections().into_any_element() |
| 581 | }); |
| 582 | return parts; |
| 583 | }; |
| 584 | if let Some(status) = status_box(&profile) { |
| 585 | parts.push(self.status_box(&status)); |
| 586 | } |
| 587 | parts.push(self.actions(profile.email.clone(), cx).into_any_element()); |
| 588 | let rows = contact_rows(&profile); |
| 589 | if !rows.is_empty() { |
| 590 | parts.push(self.contact_section(rows, cx).into_any_element()); |
| 591 | } |
| 592 | if has_organization(&profile) { |
| 593 | parts.push(self.organization_section(&profile, cx).into_any_element()); |
| 594 | } |
| 595 | if failed { |
| 596 | parts.push(self.failure(cx).into_any_element()); |
| 597 | } |
| 598 | parts |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | struct EntryState { |
| 603 | profile: Option<Arc<PersonProfile>>, |
| 604 | failed: bool, |
| 605 | } |
| 606 | |
| 607 | fn muted_text(text: String) -> Div { |
| 608 | div() |
| 609 | .text_size(px(12.)) |
| 610 | .text_color(theme::text_muted()) |
| 611 | .child(text) |
| 612 | } |
| 613 | |
| 614 | fn muted_line(text: String) -> Div { |
| 615 | muted_text(text).truncate() |
| 616 | } |
| 617 | |
| 618 | fn skeleton(width_ratio: f32) -> Div { |
| 619 | div() |
| 620 | .h(px(SKELETON_HEIGHT)) |
| 621 | .w(relative(width_ratio)) |
| 622 | .rounded(px(4.)) |
| 623 | .bg(theme::popup_hover()) |
| 624 | } |
| 625 | |
| 626 | /// Klikbaar: kopieert de waarde (KVT issue #6; tekst selecteren kan niet in de kaart). |
| 627 | fn contact_row(index: usize, row: ContactRow) -> Stateful<Div> { |
| 628 | let glyph = match row.kind { |
| 629 | ContactKind::Email => IconName::Mail, |
| 630 | ContactKind::WorkPhone => IconName::Phone, |
| 631 | ContactKind::MobilePhone => IconName::Smartphone, |
| 632 | ContactKind::Office => IconName::MapPin, |
| 633 | }; |
| 634 | h_flex() |
| 635 | .id(ElementId::Name(format!("profile-contact-{index}").into())) |
| 636 | .gap(px(8.)) |
| 637 | .px(px(4.)) |
| 638 | .items_center() |
| 639 | .rounded(px(6.)) |
| 640 | .cursor_pointer() |
| 641 | .hover(|row| row.bg(theme::row_hover())) |
| 642 | .tooltip(|window, cx| Tooltip::new(crate::i18n::tr("kvt.profile.copy")).build(window, cx)) |
| 643 | .child(icon(glyph, 14., theme::text_muted())) |
| 644 | .child( |
| 645 | div() |
| 646 | .flex_1() |
| 647 | .min_w_0() |
| 648 | .truncate() |
| 649 | .text_size(px(12.5)) |
| 650 | .text_color(theme::text()) |
| 651 | .child(row.value), |
| 652 | ) |
| 653 | } |
| 654 | |
| 655 | pub fn video_call_tooltip(name: &str) -> String { |
| 656 | format!("Start a video call with {name}") |
| 657 | } |
| 658 | |
| 659 | pub fn audio_call_tooltip(name: &str) -> String { |
| 660 | format!("Start an audio call with {name}") |
| 661 | } |
| 662 | |
| 663 | fn icon_action(id: &'static str, glyph: IconName, tooltip: String) -> Stateful<Div> { |
| 664 | div() |
| 665 | .id(id) |
| 666 | .size(px(ACTION_HEIGHT)) |
| 667 | .flex() |
| 668 | .flex_none() |
| 669 | .items_center() |
| 670 | .justify_center() |
| 671 | .rounded(px(6.)) |
| 672 | .cursor_pointer() |
| 673 | .bg(theme::popup_hover()) |
| 674 | .hover(|button| button.opacity(0.9)) |
| 675 | .child(icon(glyph, 14., theme::text_soft())) |
| 676 | .tooltip(move |window, cx| Tooltip::new(tooltip.clone()).build(window, cx)) |
| 677 | } |
| 678 | |
| 679 | fn action_button( |
| 680 | id: &'static str, |
| 681 | label: &'static str, |
| 682 | glyph: IconName, |
| 683 | filled: bool, |
| 684 | ) -> Stateful<Div> { |
| 685 | let (foreground, background) = if filled { |
| 686 | (theme::on_accent(), theme::accent()) |
| 687 | } else { |
| 688 | (theme::text_soft(), theme::popup_hover()) |
| 689 | }; |
| 690 | h_flex() |
| 691 | .id(id) |
| 692 | .h(px(ACTION_HEIGHT)) |
| 693 | .px(px(10.)) |
| 694 | .gap(px(6.)) |
| 695 | .items_center() |
| 696 | .justify_center() |
| 697 | .rounded(px(6.)) |
| 698 | .cursor_pointer() |
| 699 | .bg(background) |
| 700 | .text_color(foreground) |
| 701 | .text_size(px(12.5)) |
| 702 | .font_weight(FontWeight::SEMIBOLD) |
| 703 | .hover(|button| button.opacity(0.9)) |
| 704 | .child(icon(glyph, 14., foreground)) |
| 705 | .child(label) |
| 706 | } |
| 707 | |
| 708 | impl Render for ProfileCard { |
| 709 | fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { |
| 710 | let entry_state = self |
| 711 | .app |
| 712 | .read(cx) |
| 713 | .profiles |
| 714 | .get(&self.user_id) |
| 715 | .map(|entry| EntryState { |
| 716 | profile: entry.profile.clone(), |
| 717 | failed: entry.failed, |
| 718 | }) |
| 719 | .unwrap_or(EntryState { |
| 720 | profile: None, |
| 721 | failed: false, |
| 722 | }); |
| 723 | let profile = entry_state.profile.clone(); |
| 724 | let header = self.header(profile.as_deref(), entry_state.failed, cx); |
| 725 | let body = self.body(entry_state, cx); |
| 726 | let back = (!self.history.is_empty()).then(|| { |
| 727 | h_flex() |
| 728 | .id("profile-back") |
| 729 | .gap(px(4.)) |
| 730 | .items_center() |
| 731 | .cursor_pointer() |
| 732 | .text_size(px(12.)) |
| 733 | .text_color(theme::text_muted()) |
| 734 | .hover(|button| button.text_color(theme::text())) |
| 735 | .child(icon(IconName::ArrowLeft, 14., theme::text_muted())) |
| 736 | .child("Back") |
| 737 | .on_click(cx.listener(|this, _, _, cx| this.go_back(cx))) |
| 738 | }); |
| 739 | let card = v_flex() |
| 740 | .id("profile-card") |
| 741 | .track_focus(&self.focus_handle) |
| 742 | .w(px(CARD_WIDTH)) |
| 743 | .p(px(CARD_PADDING)) |
| 744 | .gap(px(CARD_GAP)) |
| 745 | .rounded(px(CARD_RADIUS)) |
| 746 | .flyout_surface() |
| 747 | .text_color(theme::text()) |
| 748 | .occlude() |
| 749 | .on_key_down(cx.listener(Self::on_key_down)) |
| 750 | .on_mouse_down(MouseButton::Left, |_, _, cx| { |
| 751 | GlobalState::suppress_text_selection(cx); |
| 752 | cx.stop_propagation(); |
| 753 | }) |
| 754 | .on_mouse_down(MouseButton::Right, |_, _, cx| cx.stop_propagation()) |
| 755 | .children(back) |
| 756 | .child(header) |
| 757 | .children(body); |
| 758 | let offset = point(px(CLICK_OFFSET), px(CLICK_OFFSET)); |
| 759 | div() |
| 760 | .id("profile-layer") |
| 761 | .absolute() |
| 762 | .top_0() |
| 763 | .left_0() |
| 764 | .size_full() |
| 765 | .occlude() |
| 766 | .on_mouse_down( |
| 767 | MouseButton::Left, |
| 768 | cx.listener(|_, _, _, cx| cx.emit(ProfileCardEvent::Close)), |
| 769 | ) |
| 770 | .on_mouse_down( |
| 771 | MouseButton::Right, |
| 772 | cx.listener(|_, _, _, cx| cx.emit(ProfileCardEvent::Close)), |
| 773 | ) |
| 774 | .child( |
| 775 | deferred( |
| 776 | anchored() |
| 777 | .position(self.anchor + offset) |
| 778 | .anchor(Anchor::TopLeft) |
| 779 | .snap_to_window_with_margin(px(CARD_MARGIN)) |
| 780 | .child(card), |
| 781 | ) |
| 782 | .with_priority(2), |
| 783 | ) |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | #[cfg(test)] |
| 788 | mod tests { |
| 789 | use super::{audio_call_tooltip, video_call_tooltip}; |
| 790 | |
| 791 | #[test] |
| 792 | fn call_tooltips_use_the_teams_wording() { |
| 793 | assert_eq!(video_call_tooltip("Ann"), "Start a video call with Ann"); |
| 794 | assert_eq!(audio_call_tooltip("Ann"), "Start an audio call with Ann"); |
| 795 | } |
| 796 | } |