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