BlueGit

ict rusty-teams

Files

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

message_row.rs

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