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