BlueGit

ict rusty-teams

Files

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

message_row.rs

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