BlueGit

ict rusty-teams

Files

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

message_row.rs

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