BlueGit

ict rusty-teams

Files

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

message_row.rs

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