BlueGit

ict rusty-teams

Files

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

message_row.rs

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