BlueGit

ict rusty-teams

Files

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

message_row.rs

message_row.rs 21.2 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::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().pt(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)),
359 Receipt::Sent => meta.child(symbol("done", META_CHECK_SIZE, tint)),
360 Receipt::Read => meta.child(symbol("done_all", META_CHECK_SIZE, theme::own_read())),
361 },
362 )
363}
364
365fn pulse<E: IntoElement + Styled + 'static>(id: &'static str, element: E) -> AnimationElement<E> {
366 element.with_animation(
367 id,
368 Animation::new(PULSE_PERIOD)
369 .repeat_synced()
370 .with_easing(pulsating_between(0.35, 1.)),
371 |element, opacity| element.opacity(opacity),
372 )
373}
374
375fn receipt_slot(tint: Hsla) -> Div {
376 h_flex()
377 .size(px(META_CHECK_SIZE))
378 .flex_none()
379 .items_center()
380 .justify_center()
381 .child(pulse(
382 "receipt-slot",
383 div()
384 .w(px(13.))
385 .h(px(4.))
386 .rounded(px(2.))
387 .bg(tint.opacity(0.5)),
388 ))
389}
390
391pub fn render_skeleton_row(skeleton: &Skeleton, index: usize) -> AnyElement {
392 let corners = bubble_corners(skeleton.own, Default::default());
393 let height = 14. + SKELETON_LINE_HEIGHT * f32::from(skeleton.lines);
394 let shape = div()
395 .w(relative(skeleton.width_ratio))
396 .h(px(height))
397 .rounded_tl(px(corners.top_left))
398 .rounded_tr(px(corners.top_right))
399 .rounded_br(px(corners.bottom_right))
400 .rounded_bl(px(corners.bottom_left))
401 .bg(if skeleton.own {
402 theme::bubble_own().opacity(0.55)
403 } else {
404 theme::bubble_other()
405 });
406 let line = h_flex()
407 .w_full()
408 .when(skeleton.own, |line| line.justify_end())
409 .when(!skeleton.own, |line| {
410 line.gap(px(AVATAR_GAP)).child(
411 div()
412 .size(px(AVATAR_SIZE))
413 .flex_none()
414 .rounded_full()
415 .bg(theme::bubble_other()),
416 )
417 })
418 .child(shape);
419 div()
420 .id(ElementId::Name(format!("row-{index}").into()))
421 .w_full()
422 .px(px(ROW_PADDING))
423 .pt(px(SERIES_START_GAP))
424 .child(pulse("skeleton", line))
425 .into_any_element()
426}
427
428fn meta_room(row: &MessageRow, own: bool, saved: bool) -> usize {
429 let mut width = 12. + row.time.chars().count() as f32 * 6.;
430 if saved {
431 width += SAVED_MARK_SIZE + 4.;
432 }
433 if row.edited {
434 width += 56.;
435 }
436 if own {
437 width += META_CHECK_SIZE + 4.;
438 }
439 (width / FIGURE_SPACE_WIDTH).ceil() as usize
440}
441
442fn with_meta_room(blocks: &[Block], room: usize) -> Option<Vec<Block>> {
443 let mut padded = blocks.to_vec();
444 let inline = padded.last_mut()?.last_inline_mut()?;
445 *inline = inline.with_trailing_room(room);
446 Some(padded)
447}
448
449pub(super) fn has_text(row: &MessageRow) -> bool {
450 !row.blocks.is_empty()
451 || (row.images.is_empty()
452 && row.local_images.is_empty()
453 && row.files.is_empty()
454 && row.adaptive_cards.is_empty())
455}
456
457fn delivery_link(
458 index: usize,
459 name: &str,
460 label: &'static str,
461 action: RowAction,
462) -> Stateful<Div> {
463 div()
464 .id(ElementId::Name(format!("{name}-{index}").into()))
465 .cursor_pointer()
466 .text_color(theme::red_tint())
467 .underline()
468 .child(label)
469 .when_some(action, |link, action| {
470 link.on_click(move |_, _, cx| action(cx))
471 })
472}
473
474pub(super) fn delivery_note(
475 row: &MessageRow,
476 delivery: DeliveryActions,
477 index: usize,
478) -> Option<AnyElement> {
479 let DeliveryActions {
480 retry,
481 delete,
482 send_now,
483 } = delivery;
484 let note = |text: &'static str| {
485 h_flex()
486 .gap(px(4.))
487 .text_size(px(11.))
488 .text_color(theme::red_soft())
489 .child(text)
490 };
491 match &row.delivery {
492 Delivery::Delivered | Delivery::Sending | Delivery::Scheduled(ScheduledState::Waiting) => {
493 None
494 }
495 Delivery::Failed(_) => Some(
496 note("Failed to send.")
497 .child(delivery_link(index, "retry", "Retry", retry))
498 .child(delivery_link(index, "delete", "Delete", delete))
499 .into_any_element(),
500 ),
501 Delivery::Scheduled(ScheduledState::DeliveryFailed) => Some(
502 note("Couldn't send at the scheduled time.")
503 .child(delivery_link(index, "send-now", "Send now", send_now))
504 .child(delivery_link(index, "delete", "Delete", delete))
505 .into_any_element(),
506 ),
507 Delivery::Scheduled(ScheduledState::ChangeFailed) => Some(
508 note("Couldn't change the scheduled message.")
509 .child(delivery_link(index, "retry", "Retry", retry))
510 .into_any_element(),
511 ),
512 }
513}
514
515fn others_row(
516 row: &MessageRow,
517 index: usize,
518 directory: &Directory,
519 bot: Option<&BotIdentity>,
520 delivery: DeliveryActions,
521 mut extras: BubbleExtras,
522 cx: &App,
523) -> Div {
524 let translation = extras.translation.take();
525 let agent_run = extras.agent_run.take();
526 let reaction_proposal = extras.reaction_proposal.take();
527 let first = !row.series.has_prev;
528 let author = bot.map_or_else(|| row.author.clone(), |bot| bot.name.clone());
529 let profile_user = row.sender_id.as_deref().filter(|_| bot.is_none());
530 let mut column = v_flex()
531 .gap(px(2.))
532 .flex_1()
533 .min_w_0()
534 .max_w(bubble_max_width(&extras.timeline, AVATAR_SIZE + AVATAR_GAP))
535 .items_start();
536 if first {
537 column = column.child(
538 h_flex().gap(px(8.)).items_baseline().pl(px(2.)).child(
539 opens_profile(
540 div().id(ElementId::Name(format!("author-{index}").into())),
541 profile_user,
542 )
543 .text_size(px(12.))
544 .font_weight(FontWeight::SEMIBOLD)
545 .text_color(theme::text_soft())
546 .child(author.clone()),
547 ),
548 );
549 }
550 column = column.child(bubble(row, index, false, directory, extras, cx));
551 column = column.children(agent_chip(agent_run));
552 column = column.children(reaction_proposal.map(|pill| reaction_pill(pill, &row.key)));
553 column = column.children(translation_line(row, index, translation.as_ref()));
554 if let Some(note) = delivery_note(row, delivery, index) {
555 column = column.child(note);
556 }
557 let lead = if let Some(bot) = bot.filter(|_| first) {
558 bot_avatar(bot, AVATAR_SIZE)
559 } else if first {
560 person_avatar(
561 directory,
562 row.sender_id.as_deref(),
563 &row.author,
564 AVATAR_SIZE,
565 )
566 } else {
567 div().size(px(AVATAR_SIZE)).into_any_element()
568 };
569 h_flex()
570 .w_full()
571 .gap(px(AVATAR_GAP))
572 .items_start()
573 .child(
574 opens_profile(
575 div().id(ElementId::Name(format!("author-avatar-{index}").into())),
576 profile_user.filter(|_| first),
577 )
578 .w(px(AVATAR_SIZE))
579 .flex_none()
580 .child(lead),
581 )
582 .child(column)
583}
584
585fn own_row(
586 row: &MessageRow,
587 index: usize,
588 directory: &Directory,
589 delivery: DeliveryActions,
590 mut extras: BubbleExtras,
591 cx: &App,
592) -> Div {
593 let translation = extras.translation.take();
594 let agent_run = extras.agent_run.take();
595 let reaction_proposal = extras.reaction_proposal.take();
596 let mut column = v_flex().w_full().items_end().gap(px(2.));
597 column = column.child(
598 h_flex()
599 .w_full()
600 .justify_end()
601 .child(
602 div()
603 .min_w_0()
604 .max_w(bubble_max_width(&extras.timeline, 0.))
605 .child(bubble(row, index, true, directory, extras, cx)),
606 ),
607 );
608 column = column.children(agent_chip(agent_run));
609 column = column.children(reaction_proposal.map(|pill| reaction_pill(pill, &row.key)));
610 column = column.children(translation_line(row, index, translation.as_ref()));
611 if let Some(note) = delivery_note(row, delivery, index) {
612 column = column.child(note);
613 }
614 column
615}
616
617pub fn render_message_row(
618 row: &MessageRow,
619 index: usize,
620 actions: RowActions,
621 directory: &Directory,
622 cx: &App,
623) -> AnyElement {
624 if let Some(call) = &row.call {
625 return call_card_row(row, call, index, directory, actions.call);
626 }
627 let RowActions {
628 bot,
629 delivery,
630 reply,
631 hovered,
632 menu,
633 scheduled_menu,
634 react,
635 reaction_controls,
636 files,
637 open_image,
638 highlighted,
639 saved,
640 translation,
641 timeline,
642 agent_run,
643 call: _,
644 reaction_proposal,
645 } = actions;
646 let extras = BubbleExtras {
647 hover: hovered,
648 menu,
649 scheduled_menu,
650 react,
651 controls: reaction_controls,
652 files,
653 open_image,
654 saved,
655 translation,
656 timeline,
657 agent_run,
658 reaction_proposal,
659 };
660 let own = row.own;
661 let spacing = if row.series.has_prev {
662 px(2.)
663 } else {
664 px(SERIES_START_GAP)
665 };
666 let mut container = v_flex().w_full().px(px(ROW_PADDING));
667 if let Some(day) = &row.day_header {
668 container = container.child(day_separator(day));
669 }
670 if row.new_marker {
671 container = container.child(new_marker());
672 }
673 let body = if own {
674 own_row(row, index, directory, delivery, extras, cx)
675 } else {
676 others_row(row, index, directory, bot.as_ref(), delivery, extras, cx)
677 };
678 container
679 .child(
680 div()
681 .id(ElementId::Name(format!("row-{index}").into()))
682 .w_full()
683 .pt(spacing)
684 .when_some(reply, |element, reply| {
685 element.on_mouse_down(MouseButton::Right, move |_, _, cx| reply(cx))
686 })
687 .child(
688 div()
689 .w_full()
690 .rounded(px(10.))
691 .when(highlighted, |element| {
692 element.bg(theme::accent().opacity(0.2))
693 })
694 .child(body),
695 ),
696 )
697 .into_any_element()
698}