BlueGit

ict rusty-teams

Files

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

message_row.rs

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