| 1 | use gpui_kit::assets::IconName; |
| 2 | use gpui_kit::component::{h_flex, v_flex}; |
| 3 | use gpui_kit::prelude::FluentBuilder as _; |
| 4 | use gpui_kit::*; |
| 5 | use std::time::Duration; |
| 6 | |
| 7 | use super::attachments::attachments_view; |
| 8 | use super::avatar::{member_stack, person_avatar}; |
| 9 | use super::widgets::{icon, symbol}; |
| 10 | use crate::data::Directory; |
| 11 | use crate::render::{Block, render_blocks}; |
| 12 | use crate::rows::{Delivery, MessageRow, ReactionChip, Receipt, Skeleton, bubble_corners}; |
| 13 | use crate::sidebar_model::DELETED_PREVIEW; |
| 14 | use crate::theme; |
| 15 | |
| 16 | pub type RowAction = Option<Box<dyn Fn(&mut App)>>; |
| 17 | |
| 18 | const AVATAR_SIZE: f32 = 28.; |
| 19 | const AVATAR_GAP: f32 = 8.; |
| 20 | const SERIES_START_GAP: f32 = 8.; |
| 21 | const BODY_SIZE: f32 = 13.5; |
| 22 | const MAX_WIDTH_RATIO: f32 = 0.7; |
| 23 | const SENDING_OPACITY: f32 = 0.6; |
| 24 | const REACTION_OVERHANG: f32 = 12.; |
| 25 | const CARD_AVATAR_SIZE: f32 = 32.; |
| 26 | const CARD_GAP: f32 = 12.; |
| 27 | const META_SIZE: f32 = 11.; |
| 28 | const META_CHECK_SIZE: f32 = 15.; |
| 29 | const FIGURE_SPACE_WIDTH: f32 = 7.4; |
| 30 | const PULSE_PERIOD: Duration = Duration::from_millis(1600); |
| 31 | const SKELETON_LINE_HEIGHT: f32 = 20.; |
| 32 | |
| 33 | pub struct RowActions { |
| 34 | pub open_thread: RowAction, |
| 35 | pub retry: RowAction, |
| 36 | pub reply: RowAction, |
| 37 | pub hovered: RowAction, |
| 38 | pub highlighted: bool, |
| 39 | } |
| 40 | |
| 41 | fn reaction_chip(chip: &ReactionChip) -> Div { |
| 42 | let (background, border, foreground) = if chip.mine { |
| 43 | (theme::bubble_own(), theme::accent(), theme::accent_tint()) |
| 44 | } else { |
| 45 | (theme::surface(), theme::border_strong(), theme::text_soft()) |
| 46 | }; |
| 47 | h_flex() |
| 48 | .h(px(22.)) |
| 49 | .px(px(8.)) |
| 50 | .gap(px(4.)) |
| 51 | .items_center() |
| 52 | .rounded(px(11.)) |
| 53 | .bg(background) |
| 54 | .border_1() |
| 55 | .border_color(border) |
| 56 | .text_size(px(12.)) |
| 57 | .text_color(foreground) |
| 58 | .child(chip.label.clone()) |
| 59 | .child(chip.count.to_string()) |
| 60 | } |
| 61 | |
| 62 | fn day_separator(label: &str) -> Div { |
| 63 | h_flex() |
| 64 | .w_full() |
| 65 | .items_center() |
| 66 | .gap(px(12.)) |
| 67 | .mt(px(14.)) |
| 68 | .mb(px(6.)) |
| 69 | .child(div().flex_1().h(px(1.)).bg(theme::border())) |
| 70 | .child( |
| 71 | div() |
| 72 | .text_size(px(11.)) |
| 73 | .text_color(theme::text_muted()) |
| 74 | .child(label.to_owned()), |
| 75 | ) |
| 76 | .child(div().flex_1().h(px(1.)).bg(theme::border())) |
| 77 | } |
| 78 | |
| 79 | fn new_marker() -> Div { |
| 80 | h_flex() |
| 81 | .w_full() |
| 82 | .items_center() |
| 83 | .gap(px(10.)) |
| 84 | .mt(px(12.)) |
| 85 | .mb(px(2.)) |
| 86 | .child(div().flex_1().h(px(1.)).bg(theme::accent())) |
| 87 | .child( |
| 88 | div() |
| 89 | .text_size(px(11.)) |
| 90 | .font_weight(FontWeight::SEMIBOLD) |
| 91 | .text_color(theme::accent_text()) |
| 92 | .child("New"), |
| 93 | ) |
| 94 | } |
| 95 | |
| 96 | fn bubble(row: &MessageRow, index: usize, own: bool, directory: &Directory, cx: &App) -> Div { |
| 97 | let corners = bubble_corners(own, row.series); |
| 98 | let mut element = div() |
| 99 | .relative() |
| 100 | .rounded_tl(px(corners.top_left)) |
| 101 | .rounded_tr(px(corners.top_right)) |
| 102 | .rounded_br(px(corners.bottom_right)) |
| 103 | .rounded_bl(px(corners.bottom_left)) |
| 104 | .px(px(11.)) |
| 105 | .py(px(7.)) |
| 106 | .text_size(px(BODY_SIZE)) |
| 107 | .line_height(relative(1.45)); |
| 108 | if row.deleted { |
| 109 | return element |
| 110 | .border_1() |
| 111 | .border_dashed() |
| 112 | .border_color(theme::border_strong()) |
| 113 | .py(px(6.)) |
| 114 | .text_size(px(13.)) |
| 115 | .italic() |
| 116 | .text_color(theme::text_muted()) |
| 117 | .child(DELETED_PREVIEW); |
| 118 | } |
| 119 | let failed = matches!(row.delivery, Delivery::Failed(_)); |
| 120 | element = element |
| 121 | .bg(if own && !failed { |
| 122 | theme::bubble_own() |
| 123 | } else { |
| 124 | theme::bubble_other() |
| 125 | }) |
| 126 | .when(failed, |bubble| { |
| 127 | bubble.border_1().border_color(theme::red()) |
| 128 | }) |
| 129 | .when(row.delivery == Delivery::Sending, |bubble| { |
| 130 | bubble.opacity(SENDING_OPACITY) |
| 131 | }) |
| 132 | .when(!row.reactions.is_empty(), |bubble| { |
| 133 | bubble.mb(px(REACTION_OVERHANG)) |
| 134 | }); |
| 135 | let mut content = v_flex().gap(px(6.)); |
| 136 | let padded_blocks = with_meta_room(&row.blocks, meta_room(row, own)); |
| 137 | let meta_inline = has_text(row) && padded_blocks.is_some(); |
| 138 | if has_text(row) { |
| 139 | content = content.child(render_blocks( |
| 140 | padded_blocks.as_deref().unwrap_or(&row.blocks), |
| 141 | &format!("message-{index}"), |
| 142 | own, |
| 143 | cx, |
| 144 | )); |
| 145 | } |
| 146 | content = content.children(attachments_view( |
| 147 | &row.images, |
| 148 | &row.files, |
| 149 | &format!("message-{index}"), |
| 150 | directory, |
| 151 | )); |
| 152 | if !meta_inline { |
| 153 | content = content.child(h_flex().justify_end().child(bubble_meta(row, own))); |
| 154 | } |
| 155 | element = element.child(content); |
| 156 | if meta_inline { |
| 157 | element = element.child( |
| 158 | div() |
| 159 | .absolute() |
| 160 | .right(px(9.)) |
| 161 | .bottom(px(5.)) |
| 162 | .child(bubble_meta(row, own)), |
| 163 | ); |
| 164 | } |
| 165 | if !row.reactions.is_empty() { |
| 166 | element = element.child( |
| 167 | h_flex() |
| 168 | .absolute() |
| 169 | .when(own, |chips| chips.right(px(11.))) |
| 170 | .when(!own, |chips| chips.left(px(11.))) |
| 171 | .bottom(px(-14.)) |
| 172 | .gap(px(4.)) |
| 173 | .children(row.reactions.iter().map(reaction_chip)), |
| 174 | ); |
| 175 | } |
| 176 | element |
| 177 | } |
| 178 | |
| 179 | fn bubble_meta(row: &MessageRow, own: bool) -> Div { |
| 180 | let tint = if own { |
| 181 | theme::own_meta() |
| 182 | } else { |
| 183 | theme::text_muted() |
| 184 | }; |
| 185 | h_flex() |
| 186 | .gap(px(4.)) |
| 187 | .items_center() |
| 188 | .text_size(px(META_SIZE)) |
| 189 | .line_height(px(14.)) |
| 190 | .italic() |
| 191 | .text_color(tint) |
| 192 | .when(row.edited, |meta| meta.child("Edited")) |
| 193 | .child(row.time.clone()) |
| 194 | .when( |
| 195 | own && row.delivery == Delivery::Delivered, |
| 196 | |meta| match row.receipt { |
| 197 | Receipt::Hidden => meta, |
| 198 | Receipt::Pending => meta.child(receipt_slot(tint)), |
| 199 | Receipt::Sent => meta.child(symbol("done", META_CHECK_SIZE, tint)), |
| 200 | Receipt::Read => meta.child(symbol("done_all", META_CHECK_SIZE, theme::own_read())), |
| 201 | }, |
| 202 | ) |
| 203 | } |
| 204 | |
| 205 | fn pulse<E: IntoElement + Styled + 'static>(id: &'static str, element: E) -> AnimationElement<E> { |
| 206 | element.with_animation( |
| 207 | id, |
| 208 | Animation::new(PULSE_PERIOD) |
| 209 | .repeat_synced() |
| 210 | .with_easing(pulsating_between(0.35, 1.)), |
| 211 | |element, opacity| element.opacity(opacity), |
| 212 | ) |
| 213 | } |
| 214 | |
| 215 | fn receipt_slot(tint: Hsla) -> Div { |
| 216 | h_flex() |
| 217 | .size(px(META_CHECK_SIZE)) |
| 218 | .flex_none() |
| 219 | .items_center() |
| 220 | .justify_center() |
| 221 | .child(pulse( |
| 222 | "receipt-slot", |
| 223 | div() |
| 224 | .w(px(13.)) |
| 225 | .h(px(4.)) |
| 226 | .rounded(px(2.)) |
| 227 | .bg(tint.opacity(0.5)), |
| 228 | )) |
| 229 | } |
| 230 | |
| 231 | pub fn render_skeleton_row(skeleton: &Skeleton, index: usize) -> AnyElement { |
| 232 | let corners = bubble_corners(skeleton.own, Default::default()); |
| 233 | let height = 14. + SKELETON_LINE_HEIGHT * f32::from(skeleton.lines); |
| 234 | let shape = div() |
| 235 | .w(relative(skeleton.width_ratio)) |
| 236 | .h(px(height)) |
| 237 | .rounded_tl(px(corners.top_left)) |
| 238 | .rounded_tr(px(corners.top_right)) |
| 239 | .rounded_br(px(corners.bottom_right)) |
| 240 | .rounded_bl(px(corners.bottom_left)) |
| 241 | .bg(if skeleton.own { |
| 242 | theme::bubble_own().opacity(0.55) |
| 243 | } else { |
| 244 | theme::bubble_other() |
| 245 | }); |
| 246 | let line = h_flex() |
| 247 | .w_full() |
| 248 | .when(skeleton.own, |line| line.justify_end()) |
| 249 | .when(!skeleton.own, |line| { |
| 250 | line.gap(px(AVATAR_GAP)).child( |
| 251 | div() |
| 252 | .size(px(AVATAR_SIZE)) |
| 253 | .flex_none() |
| 254 | .rounded_full() |
| 255 | .bg(theme::bubble_other()), |
| 256 | ) |
| 257 | }) |
| 258 | .child(shape); |
| 259 | div() |
| 260 | .id(ElementId::Name(format!("row-{index}").into())) |
| 261 | .w_full() |
| 262 | .px(px(24.)) |
| 263 | .pt(px(SERIES_START_GAP)) |
| 264 | .child(pulse("skeleton", line)) |
| 265 | .into_any_element() |
| 266 | } |
| 267 | |
| 268 | fn meta_room(row: &MessageRow, own: bool) -> usize { |
| 269 | let mut width = 12. + row.time.chars().count() as f32 * 6.; |
| 270 | if row.edited { |
| 271 | width += 56.; |
| 272 | } |
| 273 | if own { |
| 274 | width += META_CHECK_SIZE + 4.; |
| 275 | } |
| 276 | (width / FIGURE_SPACE_WIDTH).ceil() as usize |
| 277 | } |
| 278 | |
| 279 | fn with_meta_room(blocks: &[Block], room: usize) -> Option<Vec<Block>> { |
| 280 | let mut padded = blocks.to_vec(); |
| 281 | match padded.last_mut()? { |
| 282 | Block::Paragraph(inline) | Block::ListItem(inline) => { |
| 283 | *inline = inline.with_trailing_room(room); |
| 284 | Some(padded) |
| 285 | } |
| 286 | Block::Code(_) | Block::Quote(_) => None, |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | fn has_text(row: &MessageRow) -> bool { |
| 291 | !row.blocks.is_empty() || (row.images.is_empty() && row.files.is_empty()) |
| 292 | } |
| 293 | |
| 294 | fn delivery_note(row: &MessageRow, retry: RowAction, index: usize) -> Option<AnyElement> { |
| 295 | match &row.delivery { |
| 296 | Delivery::Delivered => None, |
| 297 | Delivery::Sending => Some( |
| 298 | h_flex() |
| 299 | .gap(px(4.)) |
| 300 | .items_center() |
| 301 | .text_size(px(11.)) |
| 302 | .text_color(theme::text_muted()) |
| 303 | .child(icon(IconName::Loader, 12., theme::text_muted())) |
| 304 | .child("Sending") |
| 305 | .into_any_element(), |
| 306 | ), |
| 307 | Delivery::Failed(_) => Some( |
| 308 | h_flex() |
| 309 | .gap(px(4.)) |
| 310 | .text_size(px(11.)) |
| 311 | .text_color(theme::red_soft()) |
| 312 | .child("Failed to send.") |
| 313 | .child( |
| 314 | div() |
| 315 | .id(ElementId::Name(format!("retry-{index}").into())) |
| 316 | .cursor_pointer() |
| 317 | .text_color(theme::red_tint()) |
| 318 | .underline() |
| 319 | .child("Retry") |
| 320 | .when_some(retry, |link, retry| { |
| 321 | link.on_click(move |_, _, cx| retry(cx)) |
| 322 | }), |
| 323 | ) |
| 324 | .into_any_element(), |
| 325 | ), |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | fn edited_marker() -> Div { |
| 330 | div() |
| 331 | .text_size(px(11.)) |
| 332 | .italic() |
| 333 | .text_color(theme::text_muted()) |
| 334 | .child("Edited") |
| 335 | } |
| 336 | |
| 337 | fn post_card(row: &MessageRow, index: usize, directory: &Directory, cx: &App) -> Div { |
| 338 | let header = h_flex() |
| 339 | .gap(px(10.)) |
| 340 | .items_center() |
| 341 | .child(person_avatar( |
| 342 | directory, |
| 343 | row.sender_id.as_deref(), |
| 344 | &row.author, |
| 345 | CARD_AVATAR_SIZE, |
| 346 | )) |
| 347 | .child( |
| 348 | v_flex() |
| 349 | .child( |
| 350 | div() |
| 351 | .text_size(px(13.)) |
| 352 | .font_weight(FontWeight::SEMIBOLD) |
| 353 | .text_color(theme::text()) |
| 354 | .child(row.author.clone()), |
| 355 | ) |
| 356 | .child( |
| 357 | h_flex() |
| 358 | .gap(px(6.)) |
| 359 | .text_size(px(11.)) |
| 360 | .text_color(theme::text_muted()) |
| 361 | .child(row.time.clone()) |
| 362 | .when(row.edited, |line| line.child(edited_marker())), |
| 363 | ), |
| 364 | ); |
| 365 | let count = row.reply_count.unwrap_or(0); |
| 366 | let footer = h_flex() |
| 367 | .gap(px(8.)) |
| 368 | .items_center() |
| 369 | .pt(px(8.)) |
| 370 | .border_t_1() |
| 371 | .border_color(theme::border()) |
| 372 | .when(count > 0, |line| { |
| 373 | line.child(member_stack( |
| 374 | directory, |
| 375 | &row.reply_faces, |
| 376 | row.reply_faces.len(), |
| 377 | )) |
| 378 | }) |
| 379 | .child( |
| 380 | div() |
| 381 | .text_size(px(12.5)) |
| 382 | .font_weight(if count > 0 { |
| 383 | FontWeight::SEMIBOLD |
| 384 | } else { |
| 385 | FontWeight::NORMAL |
| 386 | }) |
| 387 | .text_color(if count > 0 { |
| 388 | theme::accent_text() |
| 389 | } else { |
| 390 | theme::text_muted() |
| 391 | }) |
| 392 | .child(match count { |
| 393 | 0 => "No replies".to_owned(), |
| 394 | 1 => "1 reply".to_owned(), |
| 395 | count => format!("{count} replies"), |
| 396 | }), |
| 397 | ) |
| 398 | .children(row.last_reply_time.as_ref().map(|time| { |
| 399 | div() |
| 400 | .text_size(px(11.5)) |
| 401 | .text_color(theme::text_muted()) |
| 402 | .child(format!("Last reply {time}")) |
| 403 | })) |
| 404 | .child(div().flex_1()) |
| 405 | .child( |
| 406 | div() |
| 407 | .text_size(px(12.5)) |
| 408 | .text_color(theme::text_soft()) |
| 409 | .child("Reply"), |
| 410 | ); |
| 411 | let mut body = v_flex().gap(px(8.)).child(header); |
| 412 | if row.deleted { |
| 413 | body = body.child( |
| 414 | div() |
| 415 | .text_size(px(13.)) |
| 416 | .italic() |
| 417 | .text_color(theme::text_muted()) |
| 418 | .child(DELETED_PREVIEW), |
| 419 | ); |
| 420 | } else { |
| 421 | if has_text(row) { |
| 422 | body = body.child( |
| 423 | div() |
| 424 | .text_size(px(BODY_SIZE)) |
| 425 | .line_height(relative(1.45)) |
| 426 | .text_color(theme::text_strong()) |
| 427 | .child(render_blocks( |
| 428 | &row.blocks, |
| 429 | &format!("message-{index}"), |
| 430 | false, |
| 431 | cx, |
| 432 | )), |
| 433 | ); |
| 434 | } |
| 435 | body = body.children(attachments_view( |
| 436 | &row.images, |
| 437 | &row.files, |
| 438 | &format!("message-{index}"), |
| 439 | directory, |
| 440 | )); |
| 441 | if !row.reactions.is_empty() { |
| 442 | body = body.child( |
| 443 | h_flex() |
| 444 | .gap(px(4.)) |
| 445 | .flex_wrap() |
| 446 | .children(row.reactions.iter().map(reaction_chip)), |
| 447 | ); |
| 448 | } |
| 449 | } |
| 450 | v_flex() |
| 451 | .w_full() |
| 452 | .gap(px(8.)) |
| 453 | .p(px(12.)) |
| 454 | .rounded(px(10.)) |
| 455 | .bg(theme::surface()) |
| 456 | .border_1() |
| 457 | .border_color(theme::border()) |
| 458 | .hover(|card| { |
| 459 | card.border_color(theme::border_strong()) |
| 460 | .bg(theme::row_hover()) |
| 461 | }) |
| 462 | .child(body) |
| 463 | .child(footer) |
| 464 | } |
| 465 | |
| 466 | fn others_row( |
| 467 | row: &MessageRow, |
| 468 | index: usize, |
| 469 | directory: &Directory, |
| 470 | retry: RowAction, |
| 471 | cx: &App, |
| 472 | ) -> Div { |
| 473 | let first = !row.series.has_prev; |
| 474 | let mut column = v_flex() |
| 475 | .gap(px(2.)) |
| 476 | .max_w(relative(MAX_WIDTH_RATIO)) |
| 477 | .items_start(); |
| 478 | if first { |
| 479 | column = column.child( |
| 480 | h_flex().gap(px(8.)).items_baseline().pl(px(2.)).child( |
| 481 | div() |
| 482 | .text_size(px(12.)) |
| 483 | .font_weight(FontWeight::SEMIBOLD) |
| 484 | .text_color(theme::text_soft()) |
| 485 | .child(row.author.clone()), |
| 486 | ), |
| 487 | ); |
| 488 | } |
| 489 | column = column.child(bubble(row, index, false, directory, cx)); |
| 490 | if let Some(note) = delivery_note(row, retry, index) { |
| 491 | column = column.child(note); |
| 492 | } |
| 493 | let lead = if first || row.card { |
| 494 | person_avatar( |
| 495 | directory, |
| 496 | row.sender_id.as_deref(), |
| 497 | &row.author, |
| 498 | AVATAR_SIZE, |
| 499 | ) |
| 500 | } else { |
| 501 | div().size(px(AVATAR_SIZE)).into_any_element() |
| 502 | }; |
| 503 | h_flex() |
| 504 | .w_full() |
| 505 | .gap(px(AVATAR_GAP)) |
| 506 | .items_start() |
| 507 | .child(div().w(px(AVATAR_SIZE)).flex_none().child(lead)) |
| 508 | .child(column) |
| 509 | } |
| 510 | |
| 511 | fn own_row( |
| 512 | row: &MessageRow, |
| 513 | index: usize, |
| 514 | directory: &Directory, |
| 515 | retry: RowAction, |
| 516 | cx: &App, |
| 517 | ) -> Div { |
| 518 | let mut column = v_flex().w_full().items_end().gap(px(2.)); |
| 519 | column = column.child( |
| 520 | div() |
| 521 | .max_w(relative(MAX_WIDTH_RATIO)) |
| 522 | .child(bubble(row, index, true, directory, cx)), |
| 523 | ); |
| 524 | if let Some(note) = delivery_note(row, retry, index) { |
| 525 | column = column.child(note); |
| 526 | } |
| 527 | column |
| 528 | } |
| 529 | |
| 530 | pub fn render_message_row( |
| 531 | row: &MessageRow, |
| 532 | index: usize, |
| 533 | actions: RowActions, |
| 534 | directory: &Directory, |
| 535 | cx: &App, |
| 536 | ) -> AnyElement { |
| 537 | let RowActions { |
| 538 | open_thread, |
| 539 | retry, |
| 540 | reply, |
| 541 | hovered, |
| 542 | highlighted, |
| 543 | } = actions; |
| 544 | let own = row.own && !row.card; |
| 545 | let spacing = if row.card { |
| 546 | px(CARD_GAP) |
| 547 | } else if row.series.has_prev { |
| 548 | px(2.) |
| 549 | } else { |
| 550 | px(SERIES_START_GAP) |
| 551 | }; |
| 552 | let mut container = v_flex().w_full().px(px(24.)); |
| 553 | if let Some(day) = &row.day_header { |
| 554 | container = container.child(day_separator(day)); |
| 555 | } |
| 556 | if row.new_marker { |
| 557 | container = container.child(new_marker()); |
| 558 | } |
| 559 | let body = if row.card { |
| 560 | post_card(row, index, directory, cx) |
| 561 | } else if own { |
| 562 | own_row(row, index, directory, retry, cx) |
| 563 | } else { |
| 564 | others_row(row, index, directory, retry, cx) |
| 565 | }; |
| 566 | container |
| 567 | .child( |
| 568 | div() |
| 569 | .id(ElementId::Name(format!("row-{index}").into())) |
| 570 | .w_full() |
| 571 | .pt(spacing) |
| 572 | .when_some(open_thread, |element, open| { |
| 573 | element.cursor_pointer().on_click(move |_, _, cx| open(cx)) |
| 574 | }) |
| 575 | .when_some(hovered, |element, hovered| { |
| 576 | element.on_hover(move |is_hovered, _, cx| { |
| 577 | if *is_hovered { |
| 578 | hovered(cx); |
| 579 | } |
| 580 | }) |
| 581 | }) |
| 582 | .when_some(reply, |element, reply| { |
| 583 | element.on_mouse_down(MouseButton::Right, move |_, _, cx| reply(cx)) |
| 584 | }) |
| 585 | .child( |
| 586 | div() |
| 587 | .w_full() |
| 588 | .rounded(px(10.)) |
| 589 | .when(highlighted, |element| { |
| 590 | element.bg(theme::accent().opacity(0.2)) |
| 591 | }) |
| 592 | .child(body), |
| 593 | ), |
| 594 | ) |
| 595 | .into_any_element() |
| 596 | } |