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