BlueGit

ict rusty-teams

Files

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

message_row.rs

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