BlueGit

ict rusty-teams

Files

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

message_row.rs

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