BlueGit

ict rusty-teams

Files

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

message_row.rs

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