BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / views / message_row.rs

message_row.rs

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