BlueGit

ict rusty-teams

Files

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

message_row.rs

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