BlueGit

ict rusty-teams

Files

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

message_row.rs

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