BlueGit

ict rusty-teams

Files

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

message_row.rs

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