BlueGit

ict rusty-teams

Files

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

message_row.rs

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