BlueGit

ict rusty-teams

Files

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

message_row.rs

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