BlueGit

ict rusty-teams

Files

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

message_row.rs

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