BlueGit

ict rusty-teams

Files

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

message_row.rs

message_row.rs 28.1 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| match row.receipt {
464 Receipt::Hidden => meta,
465 Receipt::Sent => meta.child(on_baseline(symbol("done", META_CHECK_SIZE, tint))),
466 Receipt::Read => meta.child(on_baseline(symbol(
467 "done_all",
468 META_CHECK_SIZE,
469 theme::own_read(),
470 ))),
471 Receipt::ReadLive => meta.child(receipt_fade_in(
472 &row.key,
473 on_baseline(symbol("done_all", META_CHECK_SIZE, theme::own_read())),
474 )),
475 },
476 )
477}
478
479fn on_baseline(check: Icon) -> Div {
480 div().relative().top(px(1.)).child(check)
481}
482
483fn pulse<E: IntoElement + Styled + 'static>(id: &'static str, element: E) -> AnimationElement<E> {
484 element.with_animation(
485 id,
486 Animation::new(PULSE_PERIOD)
487 .repeat_synced()
488 .with_easing(pulsating_between(0.35, 1.)),
489 |element, opacity| element.opacity(opacity),
490 )
491}
492
493fn receipt_fade_in(message_key: &str, check: Div) -> AnimationElement<Div> {
494 check.with_animation(
495 SharedString::from(format!("receipt-fade/{message_key}")),
496 Animation::new(RECEIPT_FADE_PERIOD).with_easing(ease_in_out),
497 |check, delta| check.opacity(0.25 + 0.75 * delta),
498 )
499}
500
501pub fn render_skeleton_row(skeleton: &Skeleton, index: usize) -> AnyElement {
502 let corners = bubble_corners(skeleton.own, Default::default());
503 let height = 14. + SKELETON_LINE_HEIGHT * f32::from(skeleton.lines);
504 let shape = div()
505 .w(relative(skeleton.width_ratio))
506 .h(px(height))
507 .rounded_tl(px(corners.top_left))
508 .rounded_tr(px(corners.top_right))
509 .rounded_br(px(corners.bottom_right))
510 .rounded_bl(px(corners.bottom_left))
511 .bg(if skeleton.own {
512 theme::bubble_own().opacity(0.55)
513 } else {
514 theme::bubble_other()
515 });
516 let line = h_flex()
517 .w_full()
518 .when(skeleton.own, |line| line.justify_end())
519 .when(!skeleton.own, |line| {
520 line.gap(px(AVATAR_GAP)).child(
521 div()
522 .size(px(AVATAR_SIZE))
523 .flex_none()
524 .rounded_full()
525 .bg(theme::bubble_other()),
526 )
527 })
528 .child(shape);
529 div()
530 .id(ElementId::Name(format!("row-{index}").into()))
531 .w_full()
532 .px(px(ROW_PADDING))
533 .pt(px(SERIES_START_GAP))
534 .child(pulse("skeleton", line))
535 .into_any_element()
536}
537
538fn meta_room(row: &MessageRow, own: bool, saved: bool) -> usize {
539 let mut width = 12. + row.time.chars().count() as f32 * 6.;
540 if saved {
541 width += SAVED_MARK_SIZE + 4.;
542 }
543 if row.edited {
544 width += 56.;
545 }
546 if own {
547 width += META_CHECK_SIZE + 4.;
548 }
549 (width / FIGURE_SPACE_WIDTH).ceil() as usize
550}
551
552fn with_meta_room(blocks: &[Block], room: usize) -> Option<Vec<Block>> {
553 let mut padded = blocks.to_vec();
554 let inline = padded.last_mut()?.last_inline_mut()?;
555 *inline = inline.with_trailing_room(room);
556 Some(padded)
557}
558
559pub(super) fn has_text(row: &MessageRow) -> bool {
560 !row.blocks.is_empty()
561 || (row.images.is_empty()
562 && row.local_images.is_empty()
563 && row.files.is_empty()
564 && row.adaptive_cards.is_empty())
565}
566
567fn delivery_link(
568 index: usize,
569 name: &str,
570 label: &'static str,
571 action: RowAction,
572) -> Stateful<Div> {
573 div()
574 .id(ElementId::Name(format!("{name}-{index}").into()))
575 .cursor_pointer()
576 .text_color(theme::red_tint())
577 .underline()
578 .child(label)
579 .when_some(action, |link, action| {
580 link.on_click(move |_, _, cx| action(cx))
581 })
582}
583
584pub(super) fn delivery_note(
585 row: &MessageRow,
586 delivery: DeliveryActions,
587 index: usize,
588) -> Option<AnyElement> {
589 let DeliveryActions {
590 retry,
591 delete,
592 send_now,
593 } = delivery;
594 let note = |text: &'static str| {
595 h_flex()
596 .gap(px(4.))
597 .text_size(px(11.))
598 .text_color(theme::red_soft())
599 .child(text)
600 };
601 match &row.delivery {
602 Delivery::Delivered | Delivery::Sending | Delivery::Scheduled(ScheduledState::Waiting) => {
603 None
604 }
605 Delivery::Failed(_) => Some(
606 note("Failed to send.")
607 .child(delivery_link(index, "retry", "Retry", retry))
608 .child(delivery_link(index, "delete", "Delete", delete))
609 .into_any_element(),
610 ),
611 Delivery::Scheduled(ScheduledState::DeliveryFailed) => Some(
612 note("Couldn't send at the scheduled time.")
613 .child(delivery_link(index, "send-now", "Send now", send_now))
614 .child(delivery_link(index, "delete", "Delete", delete))
615 .into_any_element(),
616 ),
617 Delivery::Scheduled(ScheduledState::ChangeFailed) => Some(
618 note("Couldn't change the scheduled message.")
619 .child(delivery_link(index, "retry", "Retry", retry))
620 .into_any_element(),
621 ),
622 }
623}
624
625fn others_row(
626 row: &MessageRow,
627 index: usize,
628 directory: &Directory,
629 bot: Option<&BotIdentity>,
630 delivery: DeliveryActions,
631 mut extras: BubbleExtras,
632 cx: &App,
633) -> Div {
634 let translation = extras.translation.take();
635 let agent_run = extras.agent_run.take();
636 let reaction_proposal = extras.reaction_proposal.take();
637 let first = !row.series.has_prev;
638 let author = bot.map_or_else(|| row.author.clone(), |bot| bot.name.clone());
639 let profile_user = row.sender_id.as_deref().filter(|_| bot.is_none());
640 let mut column = v_flex()
641 .gap(px(2.))
642 .flex_1()
643 .min_w_0()
644 .max_w(bubble_max_width(&extras.timeline, AVATAR_SIZE + AVATAR_GAP))
645 .items_start();
646 if first {
647 column = column.child(
648 h_flex().gap(px(8.)).items_baseline().pl(px(2.)).child(
649 opens_profile(
650 div().id(ElementId::Name(format!("author-{index}").into())),
651 profile_user,
652 )
653 .text_size(px(12.))
654 .font_weight(FontWeight::SEMIBOLD)
655 .text_color(theme::text_soft())
656 .child(author.clone()),
657 ),
658 );
659 }
660 column = column.child(bubble(row, index, false, directory, extras, cx));
661 column = column.children(agent_chip(agent_run));
662 column = column.children(reaction_proposal.map(|pill| reaction_pill(pill, &row.key)));
663 column = column.children(translation_line(row, index, translation.as_ref()));
664 if let Some(note) = delivery_note(row, delivery, index) {
665 column = column.child(note);
666 }
667 let lead = if let Some(bot) = bot.filter(|_| first) {
668 bot_avatar(bot, AVATAR_SIZE)
669 } else if first {
670 person_avatar(
671 directory,
672 row.sender_id.as_deref(),
673 &row.author,
674 AVATAR_SIZE,
675 )
676 } else {
677 div().size(px(AVATAR_SIZE)).into_any_element()
678 };
679 h_flex()
680 .w_full()
681 .gap(px(AVATAR_GAP))
682 .items_start()
683 .child(
684 opens_profile(
685 div().id(ElementId::Name(format!("author-avatar-{index}").into())),
686 profile_user.filter(|_| first),
687 )
688 .w(px(AVATAR_SIZE))
689 .flex_none()
690 .child(lead),
691 )
692 .child(column)
693}
694
695fn own_row(
696 row: &MessageRow,
697 index: usize,
698 directory: &Directory,
699 delivery: DeliveryActions,
700 mut extras: BubbleExtras,
701 cx: &App,
702) -> Div {
703 let translation = extras.translation.take();
704 let agent_run = extras.agent_run.take();
705 let reaction_proposal = extras.reaction_proposal.take();
706 let mut column = v_flex().w_full().items_end().gap(px(2.));
707 column = column.child(
708 h_flex().w_full().justify_end().child(
709 div()
710 .min_w_0()
711 .max_w(bubble_max_width(&extras.timeline, 0.))
712 .child(bubble(row, index, true, directory, extras, cx)),
713 ),
714 );
715 column = column.children(agent_chip(agent_run));
716 column = column.children(reaction_proposal.map(|pill| reaction_pill(pill, &row.key)));
717 column = column.children(translation_line(row, index, translation.as_ref()));
718 if let Some(note) = delivery_note(row, delivery, index) {
719 column = column.child(note);
720 }
721 column
722}
723
724pub fn render_message_row(
725 row: &MessageRow,
726 index: usize,
727 actions: RowActions,
728 directory: &Directory,
729 cx: &App,
730) -> AnyElement {
731 if let Some(call) = &row.call {
732 let selecting = actions.select.is_some();
733 let card = call_card_row(row, call, index, directory, actions.call);
734 return if selecting {
735 div()
736 .w_full()
737 .pl(px(SELECT_BOX_SIZE + SELECT_GAP))
738 .child(card)
739 .into_any_element()
740 } else {
741 card
742 };
743 }
744 let RowActions {
745 select,
746 bot,
747 delivery,
748 reply,
749 hovered,
750 menu,
751 scheduled_menu,
752 react,
753 reaction_controls,
754 add_reaction: _,
755 files,
756 open_image,
757 highlighted,
758 saved,
759 translation,
760 timeline,
761 agent_run,
762 call: _,
763 pinned: _,
764 reaction_proposal,
765 undo_delete,
766 compact,
767 } = actions;
768 let extras = BubbleExtras {
769 hover: hovered,
770 menu,
771 scheduled_menu,
772 react,
773 controls: reaction_controls,
774 files,
775 open_image,
776 saved,
777 translation,
778 timeline,
779 agent_run,
780 reaction_proposal,
781 undo_delete,
782 compact,
783 };
784 let own = row.own;
785 let spacing = px(row_spacing(row.series.has_prev, compact));
786 let mut container = v_flex().w_full().px(px(ROW_PADDING));
787 if let Some(day) = &row.day_header {
788 container = container.child(day_separator(day));
789 }
790 if row.new_marker {
791 container = container.child(new_marker());
792 }
793 let body = if own {
794 own_row(row, index, directory, delivery, extras, cx)
795 } else {
796 others_row(row, index, directory, bot.as_ref(), delivery, extras, cx)
797 };
798 let checked = select.as_ref().is_some_and(|select| select.checked);
799 let body = match select {
800 Some(select) => selectable_body(select, index, body).into_any_element(),
801 None => body.into_any_element(),
802 };
803 container
804 .child(
805 div()
806 .id(ElementId::Name(format!("row-{index}").into()))
807 .w_full()
808 .pt(spacing)
809 .when_some(reply, |element, reply| {
810 element.on_mouse_down(MouseButton::Right, move |_, _, cx| reply(cx))
811 })
812 .child(
813 div()
814 .w_full()
815 .rounded(px(10.))
816 .when(highlighted, |element| {
817 element.bg(theme::accent().opacity(0.2))
818 })
819 .when(checked && !highlighted, |element| {
820 element.bg(theme::accent().opacity(SELECTED_TINT))
821 })
822 .child(body),
823 ),
824 )
825 .into_any_element()
826}
827
828fn selectable_body(select: RowSelect, index: usize, body: impl IntoElement) -> Stateful<Div> {
829 let RowSelect {
830 checked,
831 blocked,
832 toggle,
833 } = select;
834 h_flex()
835 .id(ElementId::Name(format!("forward-select-{index}").into()))
836 .w_full()
837 .items_center()
838 .gap(px(SELECT_GAP))
839 .child(select_box(checked, blocked.is_some(), index))
840 .child(div().flex_1().min_w_0().child(body))
841 .map(|row| match blocked {
842 Some(reason) => row.tooltip(move |window, cx| {
843 gpui_kit::component::tooltip::Tooltip::new(reason).build(window, cx)
844 }),
845 None => row.cursor_pointer().on_click(move |_, _, cx| {
846 cx.stop_propagation();
847 toggle(cx)
848 }),
849 })
850}
851
852pub fn select_box(checked: bool, blocked: bool, index: usize) -> Div {
853 div()
854 .debug_selector(|| select_box_selector(index))
855 .size(px(SELECT_BOX_SIZE))
856 .flex_none()
857 .flex()
858 .items_center()
859 .justify_center()
860 .rounded(px(SELECT_BOX_RADIUS))
861 .map(|element| {
862 if checked {
863 element.bg(theme::accent()).child(icon(
864 IconName::Check,
865 SELECT_CHECK_SIZE,
866 theme::on_accent(),
867 ))
868 } else {
869 element.bg(theme::border_strong())
870 }
871 })
872 .when(blocked, |element| element.opacity(SELECT_BLOCKED_OPACITY))
873}
874
875#[cfg(test)]
876mod density_tests {
877 use super::{META_BOTTOM_INSET, bubble_padding_y, row_spacing};
878
879 #[test]
880 fn compact_density_tightens_gaps_and_bubble_padding() {
881 assert!(row_spacing(false, true) < row_spacing(false, false));
882 assert!(row_spacing(true, true) < row_spacing(true, false));
883 assert!(bubble_padding_y(true) < bubble_padding_y(false));
884 assert!(META_BOTTOM_INSET - bubble_padding_y(true) >= 0.);
885 }
886}