| 1 | use chatsvc::{Reply, ResponseType}; |
| 2 | use chrono::{Local, Offset, Utc}; |
| 3 | use gpui_kit::assets::IconName; |
| 4 | use gpui_kit::component::Sizable as _; |
| 5 | use gpui_kit::component::button::{Button, ButtonVariants as _}; |
| 6 | use gpui_kit::component::menu::{ContextMenuExt as _, DropdownMenu as _, PopupMenuItem}; |
| 7 | use gpui_kit::component::{h_flex, v_flex}; |
| 8 | use gpui_kit::prelude::FluentBuilder as _; |
| 9 | use gpui_kit::*; |
| 10 | |
| 11 | use super::avatar::person_avatar; |
| 12 | use super::join_dialog::open_join_with_id; |
| 13 | use super::new_meeting_dialog::open_new_meeting; |
| 14 | use super::scroll_edge::with_top_edge; |
| 15 | use super::sidebar::SIDEBAR_SCROLL_DIVIDER; |
| 16 | use super::widgets::icon; |
| 17 | use crate::app_state::AppState; |
| 18 | use crate::call::{ |
| 19 | AGENDA_ROW_HEIGHT, AgendaRow, CALL_ROW_HEIGHT, CallRow, DAY_HEIGHT, EMPTY_HEIGHT, ListEntry, |
| 20 | SECTION_HEIGHT, join_available, response_label, |
| 21 | }; |
| 22 | use crate::theme; |
| 23 | |
| 24 | const FALLBACK_VIEWPORT: f32 = 900.; |
| 25 | const VIEWPORT_MARGIN: f32 = 300.; |
| 26 | const TIME_COLUMN_WIDTH: f32 = 96.; |
| 27 | const AVATAR_SIZE: f32 = 36.; |
| 28 | const MENU_ICON_SIZE: f32 = 15.; |
| 29 | |
| 30 | fn meeting_avatar() -> Div { |
| 31 | div() |
| 32 | .size(px(AVATAR_SIZE)) |
| 33 | .flex_none() |
| 34 | .flex() |
| 35 | .items_center() |
| 36 | .justify_center() |
| 37 | .rounded_full() |
| 38 | .bg(theme::border_strong()) |
| 39 | .child(icon(IconName::Video, 16., theme::text())) |
| 40 | } |
| 41 | |
| 42 | fn call_row( |
| 43 | state: &Entity<AppState>, |
| 44 | row: &CallRow, |
| 45 | can_open_chat: bool, |
| 46 | directory: &crate::data::Directory, |
| 47 | ) -> AnyElement { |
| 48 | let avatar = if row.meeting { |
| 49 | meeting_avatar().into_any_element() |
| 50 | } else { |
| 51 | person_avatar(directory, row.user_id.as_deref(), &row.title, AVATAR_SIZE) |
| 52 | }; |
| 53 | let title_color = if row.missed { |
| 54 | theme::red_soft() |
| 55 | } else { |
| 56 | theme::text() |
| 57 | }; |
| 58 | let click_state = state.clone(); |
| 59 | let click_row = row.clone(); |
| 60 | let menu_state = state.clone(); |
| 61 | let menu_row = row.clone(); |
| 62 | let can_call_back = !row.meeting && row.user_id.is_some(); |
| 63 | h_flex() |
| 64 | .id(SharedString::from(format!("call-{}", row.key))) |
| 65 | .mx(px(6.)) |
| 66 | .h(px(CALL_ROW_HEIGHT)) |
| 67 | .flex_none() |
| 68 | .px(px(10.)) |
| 69 | .gap(px(12.)) |
| 70 | .items_center() |
| 71 | .rounded(px(8.)) |
| 72 | .cursor_pointer() |
| 73 | .hover(|row| row.bg(theme::row_hover())) |
| 74 | .child(avatar) |
| 75 | .child( |
| 76 | v_flex() |
| 77 | .flex_1() |
| 78 | .min_w_0() |
| 79 | .gap(px(2.)) |
| 80 | .child( |
| 81 | h_flex() |
| 82 | .gap(px(8.)) |
| 83 | .items_baseline() |
| 84 | .child( |
| 85 | div() |
| 86 | .flex_1() |
| 87 | .min_w_0() |
| 88 | .truncate() |
| 89 | .text_size(px(14.)) |
| 90 | .text_color(title_color) |
| 91 | .child(row.title.clone()), |
| 92 | ) |
| 93 | .child( |
| 94 | div() |
| 95 | .flex_none() |
| 96 | .text_size(px(11.)) |
| 97 | .text_color(theme::text_muted()) |
| 98 | .child(row.time_label.clone()), |
| 99 | ), |
| 100 | ) |
| 101 | .child( |
| 102 | div() |
| 103 | .truncate() |
| 104 | .text_size(px(12.5)) |
| 105 | .text_color(if row.missed { |
| 106 | theme::red_soft() |
| 107 | } else { |
| 108 | theme::text_muted() |
| 109 | }) |
| 110 | .child(row.subline.clone()), |
| 111 | ), |
| 112 | ) |
| 113 | .on_click(move |_, _, cx| { |
| 114 | click_state.update(cx, |state, cx| state.activate_call_row(&click_row, cx)); |
| 115 | }) |
| 116 | .context_menu(move |popup, _, _| { |
| 117 | let call_state = menu_state.clone(); |
| 118 | let call_row = menu_row.clone(); |
| 119 | let chat_state = menu_state.clone(); |
| 120 | let chat_row = menu_row.clone(); |
| 121 | popup |
| 122 | .when(can_call_back, |popup| { |
| 123 | popup.item( |
| 124 | PopupMenuItem::new("Call back") |
| 125 | .icon(icon(IconName::Phone, MENU_ICON_SIZE, theme::text_muted())) |
| 126 | .on_click(move |_, _, cx| { |
| 127 | call_state.update(cx, |state, cx| { |
| 128 | state.call_back_from_history(&call_row, cx) |
| 129 | }); |
| 130 | }), |
| 131 | ) |
| 132 | }) |
| 133 | .when(can_open_chat, |popup| { |
| 134 | popup.item( |
| 135 | PopupMenuItem::new("Open chat") |
| 136 | .icon(icon( |
| 137 | IconName::MessageSquare, |
| 138 | MENU_ICON_SIZE, |
| 139 | theme::text_muted(), |
| 140 | )) |
| 141 | .on_click(move |_, _, cx| { |
| 142 | chat_state.update(cx, |state, cx| { |
| 143 | state.open_chat_of_call_row(&chat_row, cx) |
| 144 | }); |
| 145 | }), |
| 146 | ) |
| 147 | }) |
| 148 | }) |
| 149 | .into_any_element() |
| 150 | } |
| 151 | |
| 152 | fn join_button(id: &'static str, label: &'static str, primary: bool) -> Stateful<Div> { |
| 153 | let (fill, hover_fill, text_color) = if primary { |
| 154 | (theme::accent(), theme::accent_text(), theme::on_accent()) |
| 155 | } else { |
| 156 | ( |
| 157 | theme::surface_raised(), |
| 158 | theme::popup_hover(), |
| 159 | theme::text_strong(), |
| 160 | ) |
| 161 | }; |
| 162 | div() |
| 163 | .id(id) |
| 164 | .h(px(28.)) |
| 165 | .px(px(12.)) |
| 166 | .flex() |
| 167 | .items_center() |
| 168 | .rounded(px(7.)) |
| 169 | .bg(fill) |
| 170 | .cursor_pointer() |
| 171 | .hover(move |button| button.bg(hover_fill)) |
| 172 | .text_size(px(12.5)) |
| 173 | .font_weight(FontWeight::SEMIBOLD) |
| 174 | .text_color(text_color) |
| 175 | .child(label) |
| 176 | } |
| 177 | |
| 178 | fn join_buttons(state: &Entity<AppState>, meet_now_pending: bool) -> impl IntoElement { |
| 179 | let meet_state = state.clone(); |
| 180 | let new_state = state.clone(); |
| 181 | let id_state = state.clone(); |
| 182 | let paste_state = state.clone(); |
| 183 | h_flex() |
| 184 | .debug_selector(|| "shell/islands/sidebar/calls-actions".into()) |
| 185 | .w_full() |
| 186 | .px(px(16.)) |
| 187 | .pb(px(8.)) |
| 188 | .gap(px(8.)) |
| 189 | .flex_wrap() |
| 190 | .child( |
| 191 | join_button("meet-now", "Meet now", true) |
| 192 | .debug_selector(|| "shell/islands/sidebar/calls-actions/meet-now".into()) |
| 193 | .when(meet_now_pending, |button| button.opacity(0.5)) |
| 194 | .when(!meet_now_pending, |button| { |
| 195 | button.on_click(move |_, _, cx| { |
| 196 | meet_state.update(cx, |state, cx| state.start_meet_now(cx)); |
| 197 | }) |
| 198 | }), |
| 199 | ) |
| 200 | .child( |
| 201 | join_button("new-meeting", "New meeting", false) |
| 202 | .debug_selector(|| "shell/islands/sidebar/calls-actions/new-meeting".into()) |
| 203 | .on_click(move |_, window, cx| open_new_meeting(new_state.clone(), window, cx)), |
| 204 | ) |
| 205 | .child( |
| 206 | join_button("join-with-id", "Join with ID", false) |
| 207 | .on_click(move |_, window, cx| open_join_with_id(id_state.clone(), window, cx)), |
| 208 | ) |
| 209 | .child( |
| 210 | join_button("paste-meeting-link", "Paste a meeting link", false).on_click( |
| 211 | move |_, _, cx| { |
| 212 | paste_state.update(cx, |state, cx| state.join_meeting_from_clipboard(cx)); |
| 213 | }, |
| 214 | ), |
| 215 | ) |
| 216 | } |
| 217 | |
| 218 | fn section_header(title: &'static str) -> AnyElement { |
| 219 | div() |
| 220 | .h(px(SECTION_HEIGHT)) |
| 221 | .px(px(16.)) |
| 222 | .flex() |
| 223 | .items_end() |
| 224 | .pb(px(4.)) |
| 225 | .text_size(px(12.)) |
| 226 | .font_weight(FontWeight::SEMIBOLD) |
| 227 | .text_color(theme::text_muted()) |
| 228 | .child(title) |
| 229 | .into_any_element() |
| 230 | } |
| 231 | |
| 232 | fn day_header(label: &str) -> AnyElement { |
| 233 | div() |
| 234 | .h(px(DAY_HEIGHT)) |
| 235 | .px(px(16.)) |
| 236 | .flex() |
| 237 | .items_center() |
| 238 | .text_size(px(11.5)) |
| 239 | .text_color(theme::text_faint()) |
| 240 | .child(label.to_owned()) |
| 241 | .into_any_element() |
| 242 | } |
| 243 | |
| 244 | fn empty_row(text: &'static str) -> AnyElement { |
| 245 | div() |
| 246 | .h(px(EMPTY_HEIGHT)) |
| 247 | .flex() |
| 248 | .items_center() |
| 249 | .justify_center() |
| 250 | .text_size(px(13.)) |
| 251 | .text_color(theme::text_muted()) |
| 252 | .child(text) |
| 253 | .into_any_element() |
| 254 | } |
| 255 | |
| 256 | fn rsvp_menu_item( |
| 257 | state: &Entity<AppState>, |
| 258 | event_key: &str, |
| 259 | label: &'static str, |
| 260 | reply: Reply, |
| 261 | chosen: bool, |
| 262 | ) -> PopupMenuItem { |
| 263 | let state = state.clone(); |
| 264 | let event_key = event_key.to_owned(); |
| 265 | PopupMenuItem::new(label) |
| 266 | .checked(chosen) |
| 267 | .on_click(move |_, _, cx| { |
| 268 | state.update(cx, |state, cx| { |
| 269 | state.respond_to_event(&event_key, reply, cx) |
| 270 | }); |
| 271 | }) |
| 272 | } |
| 273 | |
| 274 | fn rsvp_control(state: &Entity<AppState>, row: &AgendaRow, send_response: bool) -> AnyElement { |
| 275 | let menu_state = state.clone(); |
| 276 | let key = row.key.clone(); |
| 277 | let response = row.response; |
| 278 | div() |
| 279 | .id(SharedString::from(format!("agenda-rsvp-{}", row.key))) |
| 280 | .flex_none() |
| 281 | .on_click(|_, _, cx| cx.stop_propagation()) |
| 282 | .child( |
| 283 | Button::new(SharedString::from(format!( |
| 284 | "agenda-rsvp-button-{}", |
| 285 | row.key |
| 286 | ))) |
| 287 | .xsmall() |
| 288 | .outline() |
| 289 | .label(response_label(response)) |
| 290 | .icon(IconName::ChevronDown) |
| 291 | .dropdown_menu(move |menu, _, _| { |
| 292 | let toggle_state = menu_state.clone(); |
| 293 | menu.item(rsvp_menu_item( |
| 294 | &menu_state, |
| 295 | &key, |
| 296 | "Accept", |
| 297 | Reply::Accept, |
| 298 | response == ResponseType::Accepted, |
| 299 | )) |
| 300 | .item(rsvp_menu_item( |
| 301 | &menu_state, |
| 302 | &key, |
| 303 | "Tentative", |
| 304 | Reply::Tentative, |
| 305 | response == ResponseType::Tentative, |
| 306 | )) |
| 307 | .item(rsvp_menu_item( |
| 308 | &menu_state, |
| 309 | &key, |
| 310 | "Decline", |
| 311 | Reply::Decline, |
| 312 | response == ResponseType::Declined, |
| 313 | )) |
| 314 | .separator() |
| 315 | .item( |
| 316 | PopupMenuItem::new("Send a response") |
| 317 | .checked(send_response) |
| 318 | .on_click(move |_, _, cx| { |
| 319 | toggle_state |
| 320 | .update(cx, |state, cx| state.toggle_rsvp_send_response(cx)); |
| 321 | }), |
| 322 | ) |
| 323 | }), |
| 324 | ) |
| 325 | .into_any_element() |
| 326 | } |
| 327 | |
| 328 | fn agenda_row( |
| 329 | state: &Entity<AppState>, |
| 330 | row: &AgendaRow, |
| 331 | joinable: bool, |
| 332 | send_response: bool, |
| 333 | ) -> AnyElement { |
| 334 | let click_state = state.clone(); |
| 335 | let click_row = row.clone(); |
| 336 | let join_state = state.clone(); |
| 337 | let join_row = row.clone(); |
| 338 | h_flex() |
| 339 | .id(SharedString::from(format!("agenda-{}", row.key))) |
| 340 | .mx(px(6.)) |
| 341 | .h(px(AGENDA_ROW_HEIGHT)) |
| 342 | .flex_none() |
| 343 | .px(px(10.)) |
| 344 | .gap(px(12.)) |
| 345 | .items_center() |
| 346 | .rounded(px(8.)) |
| 347 | .cursor_pointer() |
| 348 | .hover(|row| row.bg(theme::row_hover())) |
| 349 | .child( |
| 350 | div() |
| 351 | .w(px(TIME_COLUMN_WIDTH)) |
| 352 | .flex_none() |
| 353 | .text_size(px(12.)) |
| 354 | .text_color(theme::text_muted()) |
| 355 | .child(row.time_label.clone()), |
| 356 | ) |
| 357 | .child( |
| 358 | v_flex() |
| 359 | .flex_1() |
| 360 | .min_w_0() |
| 361 | .gap(px(2.)) |
| 362 | .child( |
| 363 | div() |
| 364 | .truncate() |
| 365 | .text_size(px(14.)) |
| 366 | .text_color(theme::text()) |
| 367 | .child(row.subject.clone()), |
| 368 | ) |
| 369 | .when(!row.detail.is_empty() || row.can_respond, |column| { |
| 370 | column.child( |
| 371 | h_flex() |
| 372 | .gap(px(6.)) |
| 373 | .items_center() |
| 374 | .child( |
| 375 | div() |
| 376 | .flex_1() |
| 377 | .min_w_0() |
| 378 | .truncate() |
| 379 | .text_size(px(12.5)) |
| 380 | .text_color(theme::text_muted()) |
| 381 | .child(row.detail.clone()), |
| 382 | ) |
| 383 | .when(row.can_respond, |line| { |
| 384 | line.child(rsvp_control(state, row, send_response)) |
| 385 | }), |
| 386 | ) |
| 387 | }), |
| 388 | ) |
| 389 | .when(joinable, |line| { |
| 390 | line.child( |
| 391 | Button::new(SharedString::from(format!("agenda-join-{}", row.key))) |
| 392 | .small() |
| 393 | .primary() |
| 394 | .label("Join") |
| 395 | .on_click(move |_, _, cx| { |
| 396 | cx.stop_propagation(); |
| 397 | join_state.update(cx, |state, cx| state.join_agenda_row(&join_row, cx)); |
| 398 | }), |
| 399 | ) |
| 400 | }) |
| 401 | .on_click(move |_, _, cx| { |
| 402 | click_state.update(cx, |state, cx| state.open_agenda_chat(&click_row, cx)); |
| 403 | }) |
| 404 | .into_any_element() |
| 405 | } |
| 406 | |
| 407 | pub fn calls_body( |
| 408 | state: &Entity<AppState>, |
| 409 | scroll: &ScrollHandle, |
| 410 | window_height: f32, |
| 411 | cx: &mut App, |
| 412 | ) -> AnyElement { |
| 413 | let now = Utc::now(); |
| 414 | let offset = Local::now().offset().fix(); |
| 415 | if state.read(cx).calls_list.needs_rebuild(now, offset) { |
| 416 | let rebuild_state = state.clone(); |
| 417 | cx.defer(move |cx| rebuild_state.update(cx, |state, cx| state.rebuild_calls_list(cx))); |
| 418 | } |
| 419 | let top = -f32::from(scroll.offset().y); |
| 420 | let measured = f32::from(scroll.bounds().size.height); |
| 421 | let height = if measured > 1. { |
| 422 | measured |
| 423 | } else { |
| 424 | window_height.max(FALLBACK_VIEWPORT) |
| 425 | }; |
| 426 | let app = state.read(cx); |
| 427 | let window = app |
| 428 | .calls_list |
| 429 | .window(top - VIEWPORT_MARGIN, top + height + VIEWPORT_MARGIN); |
| 430 | let entries = &app.calls_list.entries()[window.range.clone()]; |
| 431 | let mut list = v_flex().w_full().pb(px(12.)); |
| 432 | if window.before > 0. { |
| 433 | list = list.child(div().h(px(window.before)).flex_none()); |
| 434 | } |
| 435 | for entry in entries { |
| 436 | list = list.child(match entry { |
| 437 | ListEntry::Section(title) => section_header(title), |
| 438 | ListEntry::Day(label) => day_header(label), |
| 439 | ListEntry::Empty(text) => empty_row(text), |
| 440 | ListEntry::Agenda(row) => agenda_row( |
| 441 | state, |
| 442 | row, |
| 443 | join_available(row.start, row.end, now) && row.join_url.is_some(), |
| 444 | app.rsvp_send_response, |
| 445 | ), |
| 446 | ListEntry::Call(row) => call_row( |
| 447 | state, |
| 448 | row, |
| 449 | app.can_open_chat_of_call_row(row), |
| 450 | &app.directory, |
| 451 | ), |
| 452 | }); |
| 453 | } |
| 454 | if window.after > 0. { |
| 455 | list = list.child(div().h(px(window.after)).flex_none()); |
| 456 | } |
| 457 | v_flex() |
| 458 | .flex_1() |
| 459 | .min_h_0() |
| 460 | .child(join_buttons(state, app.meet_now_pending)) |
| 461 | .child(with_top_edge( |
| 462 | div() |
| 463 | .id("calls-scroll") |
| 464 | .flex_1() |
| 465 | .min_h_0() |
| 466 | .overflow_y_scroll() |
| 467 | .track_scroll(scroll) |
| 468 | .child(list), |
| 469 | scroll, |
| 470 | SIDEBAR_SCROLL_DIVIDER, |
| 471 | )) |
| 472 | .into_any_element() |
| 473 | } |