BlueGit

ict rusty-teams

Files

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

message_row.rs

message_row.rs 28.6 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, ReactionAdder, message_toolbar};
14use super::profile_card::opens_profile;
15use super::reaction_picker::PickHandler;
16use super::reaction_pills::{PillLook, 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::importance::importance_label;
26use crate::render::Block;
27use crate::restorable_deletes::{DELETED_TOMBSTONE, UNDO_DELETE_LABEL};
28use crate::rows::{Delivery, MessageRow, Receipt, ScheduledState, Skeleton, bubble_corners};
29use crate::sidebar_model::DELETED_PREVIEW;
30use crate::theme;
31use app_ui_kit::layout::ROW_PADDING;
32
33pub type RowAction = Option<Box<dyn Fn(&mut App)>>;
34pub type SharedRowAction = Option<std::rc::Rc<dyn Fn(&mut App)>>;
35
36pub const BUBBLE_TAGS: [&str; 12] = [
37 "message-bubble/0",
38 "message-bubble/1",
39 "message-bubble/2",
40 "message-bubble/3",
41 "message-bubble/4",
42 "message-bubble/5",
43 "message-bubble/6",
44 "message-bubble/7",
45 "message-bubble/8",
46 "message-bubble/9",
47 "message-bubble/10",
48 "message-bubble/11",
49];
50pub const IMPORTANCE_LABEL_TAG: &str = "message-importance-label";
51pub(super) const AVATAR_SIZE: f32 = 28.;
52pub(super) const AVATAR_GAP: f32 = 8.;
53const SERIES_START_GAP: f32 = 8.;
54const SERIES_GAP: f32 = 2.;
55const COMPACT_SERIES_START_GAP: f32 = 4.;
56const COMPACT_SERIES_GAP: f32 = 1.;
57const COMPACT_BUBBLE_PADDING_Y: f32 = 4.;
58pub(super) const BODY_SIZE: f32 = 13.5;
59const MAX_WIDTH_RATIO: f32 = 0.7;
60const NARROW_BUBBLE_WIDTH: f32 = 300.;
61const BUBBLE_PADDING_Y: f32 = 8.;
62const META_BOTTOM_INSET: f32 = 4.;
63const META_SIZE: f32 = 11.;
64const META_CHECK_SIZE: f32 = 15.;
65const SAVED_MARK_SIZE: f32 = 12.;
66const FORWARDED_SIZE: f32 = 11.;
67const IMPORTANCE_LABEL_SIZE: f32 = 12.;
68const IMPORTANCE_LINE_HEIGHT: f32 = 16.;
69const COMPACT_IMPORTANCE_LINE_HEIGHT: f32 = 14.;
70pub(super) const BUBBLE_PADDING_X: f32 = 12.;
71const FIGURE_SPACE_WIDTH: f32 = 7.4;
72const PULSE_PERIOD: Duration = Duration::from_millis(1600);
73const RECEIPT_FADE_PERIOD: Duration = Duration::from_millis(600);
74const SKELETON_LINE_HEIGHT: f32 = 20.;
75pub const SELECT_BOX_SIZE: f32 = 16.;
76pub const SELECT_GAP: f32 = 12.;
77const SELECT_CHECK_SIZE: f32 = 12.;
78const SELECT_BOX_RADIUS: f32 = 4.;
79const SELECT_BLOCKED_OPACITY: f32 = 0.4;
80const SELECTED_TINT: f32 = 0.08;
81pub fn select_box_selector(index: usize) -> String {
82 format!("forward-check/{index}")
83}
84
85pub struct DeliveryActions {
86 pub retry: RowAction,
87 pub delete: RowAction,
88 pub send_now: RowAction,
89}
90
91pub struct RowSelect {
92 pub checked: bool,
93 pub blocked: Option<&'static str>,
94 pub toggle: std::rc::Rc<dyn Fn(&mut App)>,
95}
96
97pub struct RowActions {
98 pub select: Option<RowSelect>,
99 pub bot: Option<BotIdentity>,
100 pub delivery: DeliveryActions,
101 pub reply: RowAction,
102 pub hovered: Option<HoverChange>,
103 pub menu: Option<MessageMenu>,
104 pub scheduled_menu: Option<ScheduledMenu>,
105 pub react: Option<PickHandler>,
106 pub reaction_controls: Option<ReactionControls>,
107 pub add_reaction: Option<ReactionAdder>,
108 pub files: Option<FileActions>,
109 pub open_image: Option<ImageActions>,
110 pub highlighted: bool,
111 pub saved: bool,
112 pub pinned: bool,
113 pub translation: Option<TranslationActions>,
114 pub timeline: TimelineBounds,
115 pub agent_run: Option<AgentRun>,
116 pub call: CallActions,
117 pub reaction_proposal: Option<ReactionPill>,
118 pub undo_delete: SharedRowAction,
119 pub compact: bool,
120}
121
122pub fn row_spacing(has_previous: bool, compact: bool) -> f32 {
123 match (has_previous, compact) {
124 (true, false) => SERIES_GAP,
125 (true, true) => COMPACT_SERIES_GAP,
126 (false, false) => SERIES_START_GAP,
127 (false, true) => COMPACT_SERIES_START_GAP,
128 }
129}
130
131pub fn bubble_padding_y(compact: bool) -> f32 {
132 if compact {
133 COMPACT_BUBBLE_PADDING_Y
134 } else {
135 BUBBLE_PADDING_Y
136 }
137}
138
139const TOOLBAR_RIGHT_INSET: f32 = 0.;
140
141fn separator_pill(label: &str, fill: Hsla, text_color: Hsla) -> Div {
142 h_flex()
143 .w_full()
144 .justify_center()
145 .mt(px(12.))
146 .mb(px(6.))
147 .child(
148 div()
149 .h(px(24.))
150 .px(px(8.))
151 .flex()
152 .items_center()
153 .rounded(px(12.))
154 .bg(fill)
155 .text_size(px(11.))
156 .text_color(text_color)
157 .child(label.to_owned()),
158 )
159}
160
161pub(super) fn day_separator(label: &str) -> Div {
162 separator_pill(label, theme::row_hover(), theme::text_muted())
163}
164
165pub(super) fn new_marker() -> Div {
166 separator_pill(
167 "New",
168 theme::mention_background(false),
169 theme::accent_text(),
170 )
171}
172
173pub(super) fn deleted_tombstone(index: usize, undo_delete: SharedRowAction) -> Div {
174 let line =
175 h_flex()
176 .gap(px(8.))
177 .items_baseline()
178 .text_size(px(13.))
179 .child(div().italic().text_color(theme::text_muted()).child(
180 if undo_delete.is_some() {
181 DELETED_TOMBSTONE
182 } else {
183 DELETED_PREVIEW
184 },
185 ));
186 match undo_delete {
187 Some(undo) => line.child(
188 div()
189 .id(ElementId::Name(format!("undo-delete-{index}").into()))
190 .cursor_pointer()
191 .font_weight(FontWeight::SEMIBOLD)
192 .text_color(theme::accent_text())
193 .hover(|link| link.underline())
194 .child(UNDO_DELETE_LABEL)
195 .on_click(move |_, _, cx| undo(cx)),
196 ),
197 None => line,
198 }
199}
200
201fn bubble_max_width(timeline: &TimelineBounds, reserved: f32) -> Length {
202 let Some(bounds) = timeline.get() else {
203 return relative(MAX_WIDTH_RATIO).into();
204 };
205 let row_width = (f32::from(bounds.size.width) - 2. * ROW_PADDING - reserved).max(0.);
206 px((row_width * MAX_WIDTH_RATIO).max(row_width.min(NARROW_BUBBLE_WIDTH))).into()
207}
208
209fn bubble(
210 row: &MessageRow,
211 index: usize,
212 own: bool,
213 directory: &Directory,
214 extras: BubbleExtras,
215 cx: &App,
216) -> Stateful<Div> {
217 let corners = bubble_corners(own, row.series);
218 let jumbo = row.jumbo.as_deref().filter(|_| {
219 !row.deleted && matches!(row.delivery, Delivery::Delivered | Delivery::Sending)
220 });
221 let mut element = div()
222 .id(ElementId::Name(format!("bubble-{index}").into()))
223 .when_some(extras.hover.clone(), |element, hover| {
224 element.on_hover(move |is_hovered, _, cx| hover(*is_hovered, cx))
225 })
226 .relative()
227 .max_w_full()
228 .when_some(BUBBLE_TAGS.get(index).copied(), |element, tag| {
229 element.debug_selector(|| tag.into())
230 })
231 .when(jumbo.is_none(), |element| {
232 element
233 .rounded_tl(px(corners.top_left))
234 .rounded_tr(px(corners.top_right))
235 .rounded_br(px(corners.bottom_right))
236 .rounded_bl(px(corners.bottom_left))
237 .px(px(BUBBLE_PADDING_X))
238 .py(px(bubble_padding_y(extras.compact)))
239 })
240 .text_size(px(BODY_SIZE))
241 .line_height(relative(1.45));
242 if row.deleted {
243 return element
244 .border_1()
245 .border_color(theme::border_strong())
246 .py(px(6.))
247 .child(deleted_tombstone(index, extras.undo_delete));
248 }
249 let failed = matches!(row.delivery, Delivery::Failed(_));
250 let scheduled = matches!(row.delivery, Delivery::Scheduled(_));
251 element = element
252 .when(!scheduled && jumbo.is_none(), |bubble| {
253 bubble.bg(if own && !failed {
254 theme::bubble_own()
255 } else {
256 theme::bubble_other()
257 })
258 })
259 .when(failed, |bubble| {
260 bubble.border_1().border_color(theme::red())
261 })
262 .when(scheduled, |bubble| {
263 bubble.border_1().border_color(
264 if row.delivery == Delivery::Scheduled(ScheduledState::DeliveryFailed) {
265 theme::red()
266 } else {
267 theme::accent()
268 },
269 )
270 });
271 let has_reactions = !row.reactions.is_empty();
272 let saved = extras.saved;
273 let mut content = v_flex().gap(px(6.));
274 content = content.children(importance_header(row, extras.compact));
275 if row.forwarded {
276 content = content.child(forwarded_header());
277 }
278 let attachments = attachments_view(
279 &row.blocks,
280 &row.images,
281 &row.local_images,
282 &row.files,
283 &format!("message-{index}"),
284 directory,
285 extras.files.as_ref(),
286 extras.open_image.as_ref(),
287 );
288 let cards = cards_view(&row.adaptive_cards, &row.conversation_id, &row.key, cx);
289 let trailing_content = attachments.is_some()
290 || row.meeting_link.is_some()
291 || row.link_preview.is_some()
292 || !cards.is_empty();
293 let padded_blocks = if has_reactions || trailing_content || jumbo.is_some() {
294 None
295 } else {
296 with_meta_room(&row.blocks, meta_room(row, own, saved))
297 };
298 let meta_inline = has_text(row) && padded_blocks.is_some();
299 if let Some(jumbo) = jumbo {
300 content = content.child(jumbo_row(&row.key, jumbo, own));
301 } else if has_text(row) {
302 content = content.children(message_body(
303 padded_blocks.as_deref().unwrap_or(&row.blocks),
304 &row.images,
305 &row.local_images,
306 &format!("message-{index}"),
307 own,
308 directory,
309 extras.open_image.as_ref(),
310 cx,
311 ));
312 }
313 content = content.children(attachments);
314 content = content.children(
315 row.meeting_link
316 .clone()
317 .map(|url| meeting_join_chip(format!("message-{index}-join"), url)),
318 );
319 content = content.children(row.link_preview.as_ref().map(|preview| {
320 link_preview_card(
321 preview,
322 format!("message-{index}-link"),
323 directory,
324 false,
325 None,
326 )
327 }));
328 content = content.children(cards);
329 if has_reactions {
330 content = content.child(
331 h_flex()
332 .gap(px(8.))
333 .items_end()
334 .child(
335 reaction_pills(
336 &row.reactions,
337 index,
338 own,
339 PillLook::Faces,
340 directory,
341 extras.react.clone(),
342 extras.controls.as_ref(),
343 )
344 .flex_auto(),
345 )
346 .child(
347 h_flex()
348 .flex_none()
349 .mb(px(META_BOTTOM_INSET - bubble_padding_y(extras.compact)))
350 .child(bubble_meta(row, own, saved)),
351 ),
352 );
353 } else if !meta_inline {
354 content = content.child(h_flex().justify_end().child(bubble_meta(row, own, saved)));
355 }
356 element = element.child(content);
357 if meta_inline {
358 element = element.child(
359 div()
360 .absolute()
361 .right(px(8.))
362 .bottom(px(META_BOTTOM_INSET))
363 .child(bubble_meta(row, own, saved)),
364 );
365 }
366 if let Some(menu) = extras.menu {
367 element = element.child(placed_toolbar(
368 message_toolbar(menu, cx),
369 extras.timeline.clone(),
370 TOOLBAR_RIGHT_INSET,
371 ));
372 }
373 if let Some(menu) = extras.scheduled_menu {
374 element = element.child(placed_toolbar(
375 scheduled_toolbar(menu),
376 extras.timeline.clone(),
377 TOOLBAR_RIGHT_INSET,
378 ));
379 }
380 element
381}
382
383#[derive(Default)]
384struct BubbleExtras {
385 hover: Option<HoverChange>,
386 menu: Option<MessageMenu>,
387 scheduled_menu: Option<ScheduledMenu>,
388 react: Option<PickHandler>,
389 controls: Option<ReactionControls>,
390 files: Option<FileActions>,
391 open_image: Option<ImageActions>,
392 saved: bool,
393 translation: Option<TranslationActions>,
394 timeline: TimelineBounds,
395 agent_run: Option<AgentRun>,
396 reaction_proposal: Option<ReactionPill>,
397 undo_delete: SharedRowAction,
398 compact: bool,
399}
400
401fn agent_chip(run: Option<AgentRun>) -> Option<Div> {
402 run.map(|run| div().py(px(2.)).child(run_chip(&run, theme::row_hover())))
403}
404
405/// Teams: bold "Important!" / "Urgent!" in capitals above the message text.
406pub(super) fn importance_header(row: &MessageRow, compact: bool) -> Option<Div> {
407 let label = importance_label(row.importance)?;
408 Some(
409 div()
410 .debug_selector(|| IMPORTANCE_LABEL_TAG.into())
411 .text_size(px(IMPORTANCE_LABEL_SIZE))
412 .line_height(px(if compact {
413 COMPACT_IMPORTANCE_LINE_HEIGHT
414 } else {
415 IMPORTANCE_LINE_HEIGHT
416 }))
417 .font_weight(FontWeight::BOLD)
418 .text_color(theme::red())
419 .child(label),
420 )
421}
422
423pub(super) fn forwarded_header() -> Div {
424 h_flex()
425 .gap(px(4.))
426 .items_center()
427 .text_size(px(FORWARDED_SIZE))
428 .text_color(theme::text_muted())
429 .child(icon(IconName::Forward, FORWARDED_SIZE, theme::text_muted()))
430 .child("Forwarded")
431}
432
433fn bubble_meta(row: &MessageRow, own: bool, saved: bool) -> Div {
434 let tint = if own {
435 theme::own_meta()
436 } else {
437 theme::text_muted()
438 };
439 h_flex()
440 .gap(px(4.))
441 .items_center()
442 .text_size(px(META_SIZE))
443 .line_height(px(14.))
444 .italic()
445 .text_color(tint)
446 .when(row.edited, |meta| meta.child("Edited"))
447 .when(saved, |meta| {
448 meta.child(icon(
449 IconName::Bookmark,
450 SAVED_MARK_SIZE,
451 theme::accent_text(),
452 ))
453 })
454 .child(row.time.clone())
455 .when(own && row.delivery == Delivery::Sending, |meta| {
456 meta.child(symbol("schedule", META_CHECK_SIZE, tint))
457 })
458 .when(matches!(row.delivery, Delivery::Scheduled(_)), |meta| {
459 meta.child(symbol("schedule", META_SIZE + 3., tint))
460 })
461 .when(
462 own && row.delivery == Delivery::Delivered,
463 |meta| {
464 let check = match row.receipt {
465 Receipt::Hidden => return meta,
466 Receipt::Sent => on_baseline(symbol("done", META_CHECK_SIZE, tint)).into_any_element(),
467 Receipt::Read => on_baseline(symbol(
468 "done_all",
469 META_CHECK_SIZE,
470 theme::own_read(),
471 ))
472 .into_any_element(),
473 Receipt::ReadLive => receipt_fade_in(
474 &row.key,
475 on_baseline(symbol("done_all", META_CHECK_SIZE, theme::own_read())),
476 )
477 .into_any_element(),
478 };
479 let holder = div().id(ElementId::Name(format!("{}-receipt", row.key).into())).child(check);
480 meta.child(match row.receipt_label.clone() {
481 Some(label) => holder.tooltip(move |window, cx| {
482 gpui_kit::component::tooltip::Tooltip::new(label.clone()).build(window, cx)
483 }),
484 None => holder,
485 })
486 },
487 )
488}
489
490fn on_baseline(check: Icon) -> Div {
491 div().relative().top(px(1.)).child(check)
492}
493
494fn pulse<E: IntoElement + Styled + 'static>(id: &'static str, element: E) -> AnimationElement<E> {
495 element.with_animation(
496 id,
497 Animation::new(PULSE_PERIOD)
498 .repeat_synced()
499 .with_easing(pulsating_between(0.35, 1.)),
500 |element, opacity| element.opacity(opacity),
501 )
502}
503
504fn receipt_fade_in(message_key: &str, check: Div) -> AnimationElement<Div> {
505 check.with_animation(
506 SharedString::from(format!("receipt-fade/{message_key}")),
507 Animation::new(RECEIPT_FADE_PERIOD).with_easing(ease_in_out),
508 |check, delta| check.opacity(0.25 + 0.75 * delta),
509 )
510}
511
512pub fn render_skeleton_row(skeleton: &Skeleton, index: usize) -> AnyElement {
513 let corners = bubble_corners(skeleton.own, Default::default());
514 let height = 14. + SKELETON_LINE_HEIGHT * f32::from(skeleton.lines);
515 let shape = div()
516 .w(relative(skeleton.width_ratio))
517 .h(px(height))
518 .rounded_tl(px(corners.top_left))
519 .rounded_tr(px(corners.top_right))
520 .rounded_br(px(corners.bottom_right))
521 .rounded_bl(px(corners.bottom_left))
522 .bg(if skeleton.own {
523 theme::bubble_own().opacity(0.55)
524 } else {
525 theme::bubble_other()
526 });
527 let line = h_flex()
528 .w_full()
529 .when(skeleton.own, |line| line.justify_end())
530 .when(!skeleton.own, |line| {
531 line.gap(px(AVATAR_GAP)).child(
532 div()
533 .size(px(AVATAR_SIZE))
534 .flex_none()
535 .rounded_full()
536 .bg(theme::bubble_other()),
537 )
538 })
539 .child(shape);
540 div()
541 .id(ElementId::Name(format!("row-{index}").into()))
542 .w_full()
543 .px(px(ROW_PADDING))
544 .pt(px(SERIES_START_GAP))
545 .child(pulse("skeleton", line))
546 .into_any_element()
547}
548
549fn meta_room(row: &MessageRow, own: bool, saved: bool) -> usize {
550 let mut width = 12. + row.time.chars().count() as f32 * 6.;
551 if saved {
552 width += SAVED_MARK_SIZE + 4.;
553 }
554 if row.edited {
555 width += 56.;
556 }
557 if own {
558 width += META_CHECK_SIZE + 4.;
559 }
560 (width / FIGURE_SPACE_WIDTH).ceil() as usize
561}
562
563fn with_meta_room(blocks: &[Block], room: usize) -> Option<Vec<Block>> {
564 let mut padded = blocks.to_vec();
565 let inline = padded.last_mut()?.last_inline_mut()?;
566 *inline = inline.with_trailing_room(room);
567 Some(padded)
568}
569
570pub(super) fn has_text(row: &MessageRow) -> bool {
571 !row.blocks.is_empty()
572 || (row.images.is_empty()
573 && row.local_images.is_empty()
574 && row.files.is_empty()
575 && row.adaptive_cards.is_empty())
576}
577
578fn delivery_link(
579 index: usize,
580 name: &str,
581 label: &'static str,
582 action: RowAction,
583) -> Stateful<Div> {
584 div()
585 .id(ElementId::Name(format!("{name}-{index}").into()))
586 .cursor_pointer()
587 .text_color(theme::red_tint())
588 .underline()
589 .child(label)
590 .when_some(action, |link, action| {
591 link.on_click(move |_, _, cx| action(cx))
592 })
593}
594
595pub(super) fn delivery_note(
596 row: &MessageRow,
597 delivery: DeliveryActions,
598 index: usize,
599) -> Option<AnyElement> {
600 let DeliveryActions {
601 retry,
602 delete,
603 send_now,
604 } = delivery;
605 let note = |text: &'static str| {
606 h_flex()
607 .gap(px(4.))
608 .text_size(px(11.))
609 .text_color(theme::red_soft())
610 .child(text)
611 };
612 match &row.delivery {
613 Delivery::Delivered | Delivery::Sending | Delivery::Scheduled(ScheduledState::Waiting) => {
614 None
615 }
616 Delivery::Failed(_) => Some(
617 note("Failed to send.")
618 .child(delivery_link(index, "retry", "Retry", retry))
619 .child(delivery_link(index, "delete", "Delete", delete))
620 .into_any_element(),
621 ),
622 Delivery::Scheduled(ScheduledState::DeliveryFailed) => Some(
623 note("Couldn't send at the scheduled time.")
624 .child(delivery_link(index, "send-now", "Send now", send_now))
625 .child(delivery_link(index, "delete", "Delete", delete))
626 .into_any_element(),
627 ),
628 Delivery::Scheduled(ScheduledState::ChangeFailed) => Some(
629 note("Couldn't change the scheduled message.")
630 .child(delivery_link(index, "retry", "Retry", retry))
631 .into_any_element(),
632 ),
633 }
634}
635
636fn others_row(
637 row: &MessageRow,
638 index: usize,
639 directory: &Directory,
640 bot: Option<&BotIdentity>,
641 delivery: DeliveryActions,
642 mut extras: BubbleExtras,
643 cx: &App,
644) -> Div {
645 let translation = extras.translation.take();
646 let agent_run = extras.agent_run.take();
647 let reaction_proposal = extras.reaction_proposal.take();
648 let first = !row.series.has_prev;
649 let author = bot.map_or_else(|| row.author.clone(), |bot| bot.name.clone());
650 let profile_user = row.sender_id.as_deref().filter(|_| bot.is_none());
651 let mut column = v_flex()
652 .gap(px(2.))
653 .flex_1()
654 .min_w_0()
655 .max_w(bubble_max_width(&extras.timeline, AVATAR_SIZE + AVATAR_GAP))
656 .items_start();
657 if first {
658 column = column.child(
659 h_flex().gap(px(8.)).items_baseline().pl(px(2.)).child(
660 opens_profile(
661 div().id(ElementId::Name(format!("author-{index}").into())),
662 profile_user,
663 )
664 .text_size(px(12.))
665 .font_weight(FontWeight::SEMIBOLD)
666 .text_color(theme::text_soft())
667 .child(author.clone()),
668 ),
669 );
670 }
671 column = column.child(bubble(row, index, false, directory, extras, cx));
672 column = column.children(agent_chip(agent_run));
673 column = column.children(reaction_proposal.map(|pill| reaction_pill(pill, &row.key)));
674 column = column.children(translation_line(row, index, translation.as_ref()));
675 if let Some(note) = delivery_note(row, delivery, index) {
676 column = column.child(note);
677 }
678 let lead = if let Some(bot) = bot.filter(|_| first) {
679 bot_avatar(bot, AVATAR_SIZE)
680 } else if first {
681 person_avatar(
682 directory,
683 row.sender_id.as_deref(),
684 &row.author,
685 AVATAR_SIZE,
686 )
687 } else {
688 div().size(px(AVATAR_SIZE)).into_any_element()
689 };
690 h_flex()
691 .w_full()
692 .gap(px(AVATAR_GAP))
693 .items_start()
694 .child(
695 opens_profile(
696 div().id(ElementId::Name(format!("author-avatar-{index}").into())),
697 profile_user.filter(|_| first),
698 )
699 .w(px(AVATAR_SIZE))
700 .flex_none()
701 .child(lead),
702 )
703 .child(column)
704}
705
706fn own_row(
707 row: &MessageRow,
708 index: usize,
709 directory: &Directory,
710 delivery: DeliveryActions,
711 mut extras: BubbleExtras,
712 cx: &App,
713) -> Div {
714 let translation = extras.translation.take();
715 let agent_run = extras.agent_run.take();
716 let reaction_proposal = extras.reaction_proposal.take();
717 let mut column = v_flex().w_full().items_end().gap(px(2.));
718 column = column.child(
719 h_flex().w_full().justify_end().child(
720 div()
721 .min_w_0()
722 .max_w(bubble_max_width(&extras.timeline, 0.))
723 .child(bubble(row, index, true, directory, extras, cx)),
724 ),
725 );
726 column = column.children(agent_chip(agent_run));
727 column = column.children(reaction_proposal.map(|pill| reaction_pill(pill, &row.key)));
728 column = column.children(translation_line(row, index, translation.as_ref()));
729 if let Some(note) = delivery_note(row, delivery, index) {
730 column = column.child(note);
731 }
732 column
733}
734
735pub fn render_message_row(
736 row: &MessageRow,
737 index: usize,
738 actions: RowActions,
739 directory: &Directory,
740 cx: &App,
741) -> AnyElement {
742 if let Some(call) = &row.call {
743 let selecting = actions.select.is_some();
744 let card = call_card_row(row, call, index, directory, actions.call);
745 return if selecting {
746 div()
747 .w_full()
748 .pl(px(SELECT_BOX_SIZE + SELECT_GAP))
749 .child(card)
750 .into_any_element()
751 } else {
752 card
753 };
754 }
755 let RowActions {
756 select,
757 bot,
758 delivery,
759 reply,
760 hovered,
761 menu,
762 scheduled_menu,
763 react,
764 reaction_controls,
765 add_reaction: _,
766 files,
767 open_image,
768 highlighted,
769 saved,
770 translation,
771 timeline,
772 agent_run,
773 call: _,
774 pinned: _,
775 reaction_proposal,
776 undo_delete,
777 compact,
778 } = actions;
779 let extras = BubbleExtras {
780 hover: hovered,
781 menu,
782 scheduled_menu,
783 react,
784 controls: reaction_controls,
785 files,
786 open_image,
787 saved,
788 translation,
789 timeline,
790 agent_run,
791 reaction_proposal,
792 undo_delete,
793 compact,
794 };
795 let own = row.own;
796 let spacing = px(row_spacing(row.series.has_prev, compact));
797 let mut container = v_flex().w_full().px(px(ROW_PADDING));
798 if let Some(day) = &row.day_header {
799 container = container.child(day_separator(day));
800 }
801 if row.new_marker {
802 container = container.child(new_marker());
803 }
804 let body = if own {
805 own_row(row, index, directory, delivery, extras, cx)
806 } else {
807 others_row(row, index, directory, bot.as_ref(), delivery, extras, cx)
808 };
809 let checked = select.as_ref().is_some_and(|select| select.checked);
810 let body = match select {
811 Some(select) => selectable_body(select, index, body).into_any_element(),
812 None => body.into_any_element(),
813 };
814 container
815 .child(
816 div()
817 .id(ElementId::Name(format!("row-{index}").into()))
818 .w_full()
819 .pt(spacing)
820 .when_some(reply, |element, reply| {
821 element.on_mouse_down(MouseButton::Right, move |_, _, cx| reply(cx))
822 })
823 .child(
824 div()
825 .w_full()
826 .rounded(px(10.))
827 .when(highlighted, |element| {
828 element.bg(theme::accent().opacity(0.2))
829 })
830 .when(checked && !highlighted, |element| {
831 element.bg(theme::accent().opacity(SELECTED_TINT))
832 })
833 .child(body),
834 ),
835 )
836 .into_any_element()
837}
838
839fn selectable_body(select: RowSelect, index: usize, body: impl IntoElement) -> Stateful<Div> {
840 let RowSelect {
841 checked,
842 blocked,
843 toggle,
844 } = select;
845 h_flex()
846 .id(ElementId::Name(format!("forward-select-{index}").into()))
847 .w_full()
848 .items_center()
849 .gap(px(SELECT_GAP))
850 .child(select_box(checked, blocked.is_some(), index))
851 .child(div().flex_1().min_w_0().child(body))
852 .map(|row| match blocked {
853 Some(reason) => row.tooltip(move |window, cx| {
854 gpui_kit::component::tooltip::Tooltip::new(reason).build(window, cx)
855 }),
856 None => row.cursor_pointer().on_click(move |_, _, cx| {
857 cx.stop_propagation();
858 toggle(cx)
859 }),
860 })
861}
862
863pub fn select_box(checked: bool, blocked: bool, index: usize) -> Div {
864 div()
865 .debug_selector(|| select_box_selector(index))
866 .size(px(SELECT_BOX_SIZE))
867 .flex_none()
868 .flex()
869 .items_center()
870 .justify_center()
871 .rounded(px(SELECT_BOX_RADIUS))
872 .map(|element| {
873 if checked {
874 element.bg(theme::accent()).child(icon(
875 IconName::Check,
876 SELECT_CHECK_SIZE,
877 theme::on_accent(),
878 ))
879 } else {
880 element.bg(theme::border_strong())
881 }
882 })
883 .when(blocked, |element| element.opacity(SELECT_BLOCKED_OPACITY))
884}
885
886#[cfg(test)]
887mod density_tests {
888 use super::{META_BOTTOM_INSET, bubble_padding_y, row_spacing};
889
890 #[test]
891 fn compact_density_tightens_gaps_and_bubble_padding() {
892 assert!(row_spacing(false, true) < row_spacing(false, false));
893 assert!(row_spacing(true, true) < row_spacing(true, false));
894 assert!(bubble_padding_y(true) < bubble_padding_y(false));
895 assert!(META_BOTTOM_INSET - bubble_padding_y(true) >= 0.);
896 }
897}