BlueGit

ict rusty-teams

Files

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

message_row.rs

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