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