BlueGit

ict rusty-teams

Files

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

message_row.rs

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