BlueGit

ict rusty-teams

Files

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

message_row.rs

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