BlueGit

ict rusty-teams

Files

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

message_row.rs

message_row.rs 19.2 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::attachments::{FileActions, attachments_view, message_body};
9use super::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::{icon, symbol};
14use crate::data::Directory;
15use crate::render::Block;
16use crate::rows::{Delivery, MessageRow, Receipt, Skeleton, bubble_corners};
17use crate::sidebar_model::DELETED_PREVIEW;
18use crate::theme;
19
20pub type RowAction = Option<Box<dyn Fn(&mut App)>>;
21
22const AVATAR_SIZE: f32 = 28.;
23const AVATAR_GAP: f32 = 8.;
24const SERIES_START_GAP: f32 = 8.;
25const BODY_SIZE: f32 = 13.5;
26const MAX_WIDTH_RATIO: f32 = 0.7;
27const SENDING_OPACITY: f32 = 0.6;
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 open_thread: RowAction,
39 pub retry: RowAction,
40 pub reply: RowAction,
41 pub hovered: Option<HoverChange>,
42 pub menu: Option<MessageMenu>,
43 pub react: Option<PickHandler>,
44 pub reaction_controls: Option<ReactionControls>,
45 pub files: Option<FileActions>,
46 pub highlighted: bool,
47}
48
49const TOOLBAR_LIFT: f32 = 22.;
50
51fn labeled_divider(label: &str, line_color: Hsla, text_color: Hsla) -> Div {
52 h_flex()
53 .w_full()
54 .items_center()
55 .gap(px(12.))
56 .mt(px(14.))
57 .mb(px(6.))
58 .child(div().flex_1().h(px(1.)).bg(line_color))
59 .child(
60 div()
61 .text_size(px(11.))
62 .text_color(text_color)
63 .child(label.to_owned()),
64 )
65 .child(div().flex_1().h(px(1.)).bg(line_color))
66}
67
68fn day_separator(label: &str) -> Div {
69 labeled_divider(label, theme::border(), theme::text_muted())
70}
71
72fn new_marker() -> Div {
73 labeled_divider("New", theme::accent(), theme::accent_text())
74}
75
76fn bubble(
77 row: &MessageRow,
78 index: usize,
79 own: bool,
80 directory: &Directory,
81 extras: BubbleExtras,
82 cx: &App,
83) -> Stateful<Div> {
84 let corners = bubble_corners(own, row.series);
85 let mut element = div()
86 .id(ElementId::Name(format!("bubble-{index}").into()))
87 .when_some(extras.hover.clone(), |element, hover| {
88 element.on_hover(move |is_hovered, _, cx| hover(*is_hovered, cx))
89 })
90 .relative()
91 .rounded_tl(px(corners.top_left))
92 .rounded_tr(px(corners.top_right))
93 .rounded_br(px(corners.bottom_right))
94 .rounded_bl(px(corners.bottom_left))
95 .px(px(11.))
96 .py(px(7.))
97 .text_size(px(BODY_SIZE))
98 .line_height(relative(1.45));
99 if row.deleted {
100 return element
101 .border_1()
102 .border_dashed()
103 .border_color(theme::border_strong())
104 .py(px(6.))
105 .text_size(px(13.))
106 .italic()
107 .text_color(theme::text_muted())
108 .child(DELETED_PREVIEW);
109 }
110 let failed = matches!(row.delivery, Delivery::Failed(_));
111 element = element
112 .bg(if own && !failed {
113 theme::bubble_own()
114 } else {
115 theme::bubble_other()
116 })
117 .when(failed, |bubble| {
118 bubble.border_1().border_color(theme::red())
119 })
120 .when(row.delivery == Delivery::Sending, |bubble| {
121 bubble.opacity(SENDING_OPACITY)
122 });
123 let has_reactions = !row.reactions.is_empty();
124 let mut content = v_flex().gap(px(6.));
125 let padded_blocks = if has_reactions {
126 None
127 } else {
128 with_meta_room(&row.blocks, meta_room(row, own))
129 };
130 let meta_inline = has_text(row) && padded_blocks.is_some();
131 if has_text(row) {
132 content = content.children(message_body(
133 padded_blocks.as_deref().unwrap_or(&row.blocks),
134 &row.images,
135 &row.local_images,
136 &format!("message-{index}"),
137 own,
138 directory,
139 cx,
140 ));
141 }
142 content = content.children(attachments_view(
143 &row.blocks,
144 &row.images,
145 &row.local_images,
146 &row.files,
147 &format!("message-{index}"),
148 directory,
149 extras.files.as_ref(),
150 ));
151 if has_reactions {
152 content = content.child(
153 h_flex()
154 .gap(px(8.))
155 .items_end()
156 .child(
157 reaction_pills(
158 &row.reactions,
159 index,
160 own,
161 directory,
162 extras.react.clone(),
163 extras.controls.as_ref(),
164 )
165 .flex_auto(),
166 )
167 .child(
168 h_flex()
169 .h(px(REACTION_FOOTER_HEIGHT))
170 .flex_none()
171 .items_center()
172 .child(bubble_meta(row, own)),
173 ),
174 );
175 } else if !meta_inline {
176 content = content.child(h_flex().justify_end().child(bubble_meta(row, own)));
177 }
178 element = element.child(content);
179 if meta_inline {
180 element = element.child(
181 div()
182 .absolute()
183 .right(px(9.))
184 .bottom(px(5.))
185 .child(bubble_meta(row, own)),
186 );
187 }
188 if let Some(menu) = extras.menu {
189 element = element.child(
190 div()
191 .absolute()
192 .top(px(-TOOLBAR_LIFT))
193 .right(px(8.))
194 .child(deferred(message_toolbar(menu)).with_priority(1)),
195 );
196 }
197 element
198}
199
200#[derive(Default)]
201struct BubbleExtras {
202 hover: Option<HoverChange>,
203 menu: Option<MessageMenu>,
204 react: Option<PickHandler>,
205 controls: Option<ReactionControls>,
206 files: Option<FileActions>,
207}
208
209fn bubble_meta(row: &MessageRow, own: bool) -> Div {
210 let tint = if own {
211 theme::own_meta()
212 } else {
213 theme::text_muted()
214 };
215 h_flex()
216 .gap(px(4.))
217 .items_center()
218 .text_size(px(META_SIZE))
219 .line_height(px(14.))
220 .italic()
221 .text_color(tint)
222 .when(row.edited, |meta| meta.child("Edited"))
223 .child(row.time.clone())
224 .when(
225 own && row.delivery == Delivery::Delivered,
226 |meta| match row.receipt {
227 Receipt::Hidden => meta,
228 Receipt::Pending => meta.child(receipt_slot(tint)),
229 Receipt::Sent => meta.child(symbol("done", META_CHECK_SIZE, tint)),
230 Receipt::Read => meta.child(symbol("done_all", META_CHECK_SIZE, theme::own_read())),
231 },
232 )
233}
234
235fn pulse<E: IntoElement + Styled + 'static>(id: &'static str, element: E) -> AnimationElement<E> {
236 element.with_animation(
237 id,
238 Animation::new(PULSE_PERIOD)
239 .repeat_synced()
240 .with_easing(pulsating_between(0.35, 1.)),
241 |element, opacity| element.opacity(opacity),
242 )
243}
244
245fn receipt_slot(tint: Hsla) -> Div {
246 h_flex()
247 .size(px(META_CHECK_SIZE))
248 .flex_none()
249 .items_center()
250 .justify_center()
251 .child(pulse(
252 "receipt-slot",
253 div()
254 .w(px(13.))
255 .h(px(4.))
256 .rounded(px(2.))
257 .bg(tint.opacity(0.5)),
258 ))
259}
260
261pub fn render_skeleton_row(skeleton: &Skeleton, index: usize) -> AnyElement {
262 let corners = bubble_corners(skeleton.own, Default::default());
263 let height = 14. + SKELETON_LINE_HEIGHT * f32::from(skeleton.lines);
264 let shape = div()
265 .w(relative(skeleton.width_ratio))
266 .h(px(height))
267 .rounded_tl(px(corners.top_left))
268 .rounded_tr(px(corners.top_right))
269 .rounded_br(px(corners.bottom_right))
270 .rounded_bl(px(corners.bottom_left))
271 .bg(if skeleton.own {
272 theme::bubble_own().opacity(0.55)
273 } else {
274 theme::bubble_other()
275 });
276 let line = h_flex()
277 .w_full()
278 .when(skeleton.own, |line| line.justify_end())
279 .when(!skeleton.own, |line| {
280 line.gap(px(AVATAR_GAP)).child(
281 div()
282 .size(px(AVATAR_SIZE))
283 .flex_none()
284 .rounded_full()
285 .bg(theme::bubble_other()),
286 )
287 })
288 .child(shape);
289 div()
290 .id(ElementId::Name(format!("row-{index}").into()))
291 .w_full()
292 .px(px(24.))
293 .pt(px(SERIES_START_GAP))
294 .child(pulse("skeleton", line))
295 .into_any_element()
296}
297
298fn meta_room(row: &MessageRow, own: bool) -> usize {
299 let mut width = 12. + row.time.chars().count() as f32 * 6.;
300 if row.edited {
301 width += 56.;
302 }
303 if own {
304 width += META_CHECK_SIZE + 4.;
305 }
306 (width / FIGURE_SPACE_WIDTH).ceil() as usize
307}
308
309fn with_meta_room(blocks: &[Block], room: usize) -> Option<Vec<Block>> {
310 let mut padded = blocks.to_vec();
311 let inline = padded.last_mut()?.last_inline_mut()?;
312 *inline = inline.with_trailing_room(room);
313 Some(padded)
314}
315
316fn has_text(row: &MessageRow) -> bool {
317 !row.blocks.is_empty()
318 || (row.images.is_empty() && row.local_images.is_empty() && row.files.is_empty())
319}
320
321fn delivery_note(row: &MessageRow, retry: RowAction, index: usize) -> Option<AnyElement> {
322 match &row.delivery {
323 Delivery::Delivered => None,
324 Delivery::Sending => Some(
325 h_flex()
326 .gap(px(4.))
327 .items_center()
328 .text_size(px(11.))
329 .text_color(theme::text_muted())
330 .child(icon(IconName::Loader, 12., theme::text_muted()))
331 .child("Sending")
332 .into_any_element(),
333 ),
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 if !row.reactions.is_empty() {
482 body = body.child(reaction_pills(
483 &row.reactions,
484 index,
485 false,
486 directory,
487 None,
488 None,
489 ));
490 }
491 }
492 v_flex()
493 .w_full()
494 .gap(px(8.))
495 .p(px(12.))
496 .rounded(px(10.))
497 .bg(theme::surface())
498 .border_1()
499 .border_color(theme::border())
500 .hover(|card| {
501 card.border_color(theme::border_strong())
502 .bg(theme::row_hover())
503 })
504 .child(body)
505 .child(footer)
506}
507
508fn others_row(
509 row: &MessageRow,
510 index: usize,
511 directory: &Directory,
512 retry: RowAction,
513 extras: BubbleExtras,
514 cx: &App,
515) -> Div {
516 let first = !row.series.has_prev;
517 let mut column = v_flex()
518 .gap(px(2.))
519 .max_w(relative(MAX_WIDTH_RATIO))
520 .items_start();
521 if first {
522 column = column.child(
523 h_flex().gap(px(8.)).items_baseline().pl(px(2.)).child(
524 div()
525 .text_size(px(12.))
526 .font_weight(FontWeight::SEMIBOLD)
527 .text_color(theme::text_soft())
528 .child(row.author.clone()),
529 ),
530 );
531 }
532 column = column.child(bubble(row, index, false, directory, extras, cx));
533 if let Some(note) = delivery_note(row, retry, index) {
534 column = column.child(note);
535 }
536 let lead = if first || row.card {
537 person_avatar(
538 directory,
539 row.sender_id.as_deref(),
540 &row.author,
541 AVATAR_SIZE,
542 )
543 } else {
544 div().size(px(AVATAR_SIZE)).into_any_element()
545 };
546 h_flex()
547 .w_full()
548 .gap(px(AVATAR_GAP))
549 .items_start()
550 .child(div().w(px(AVATAR_SIZE)).flex_none().child(lead))
551 .child(column)
552}
553
554fn own_row(
555 row: &MessageRow,
556 index: usize,
557 directory: &Directory,
558 retry: RowAction,
559 extras: BubbleExtras,
560 cx: &App,
561) -> Div {
562 let mut column = v_flex().w_full().items_end().gap(px(2.));
563 column = column.child(
564 div()
565 .max_w(relative(MAX_WIDTH_RATIO))
566 .child(bubble(row, index, true, directory, extras, cx)),
567 );
568 if let Some(note) = delivery_note(row, retry, index) {
569 column = column.child(note);
570 }
571 column
572}
573
574pub fn render_message_row(
575 row: &MessageRow,
576 index: usize,
577 actions: RowActions,
578 directory: &Directory,
579 cx: &App,
580) -> AnyElement {
581 let RowActions {
582 open_thread,
583 retry,
584 reply,
585 hovered,
586 menu,
587 react,
588 reaction_controls,
589 files,
590 highlighted,
591 } = actions;
592 let card_hover = hovered.clone().filter(|_| row.card);
593 let extras = BubbleExtras {
594 hover: hovered,
595 menu,
596 react,
597 controls: reaction_controls,
598 files,
599 };
600 let own = row.own && !row.card;
601 let spacing = if row.card {
602 px(CARD_GAP)
603 } else if row.series.has_prev {
604 px(2.)
605 } else {
606 px(SERIES_START_GAP)
607 };
608 let mut container = v_flex().w_full().px(px(24.));
609 if let Some(day) = &row.day_header {
610 container = container.child(day_separator(day));
611 }
612 if row.new_marker {
613 container = container.child(new_marker());
614 }
615 let body = if row.card {
616 post_card(row, index, directory, extras.files.as_ref(), cx)
617 } else if own {
618 own_row(row, index, directory, retry, extras, cx)
619 } else {
620 others_row(row, index, directory, retry, extras, cx)
621 };
622 container
623 .child(
624 div()
625 .id(ElementId::Name(format!("row-{index}").into()))
626 .w_full()
627 .pt(spacing)
628 .when_some(open_thread, |element, open| {
629 element.cursor_pointer().on_click(move |_, window, cx| {
630 if TextSelection::selected_text(window, cx).is_empty() {
631 open(cx);
632 }
633 })
634 })
635 .when_some(card_hover, |element, hovered| {
636 element.on_hover(move |is_hovered, _, cx| hovered(*is_hovered, cx))
637 })
638 .when_some(reply, |element, reply| {
639 element.on_mouse_down(MouseButton::Right, move |_, _, cx| reply(cx))
640 })
641 .child(
642 div()
643 .w_full()
644 .rounded(px(10.))
645 .when(highlighted, |element| {
646 element.bg(theme::accent().opacity(0.2))
647 })
648 .child(body),
649 ),
650 )
651 .into_any_element()
652}