BlueGit

ict rusty-teams

Files

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

message_row.rs

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