BlueGit

ict rusty-teams

Files

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

message_row.rs

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