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