BlueGit

ict rusty-teams

Files

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

message_row.rs

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