BlueGit

ict rusty-teams

Files

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

message_row.rs

message_row.rs 32.5 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 trailing_content = attachments.is_some()
329 || row.meeting_link.is_some()
330 || row.link_preview.is_some()
331 || !cards.is_empty();
332 let padded_blocks = if has_reactions || trailing_content || jumbo.is_some() {
333 None
334 } else {
335 with_meta_room(&row.blocks, meta_room(row, own, saved))
336 };
337 let meta_inline = has_text(row) && padded_blocks.is_some();
338 if let Some(jumbo) = jumbo {
339 content = content.child(jumbo_row(&row.key, jumbo, own, cx));
340 } else if has_text(row) {
341 content = content.children(message_body(
342 padded_blocks.as_deref().unwrap_or(&row.blocks),
343 &row.images,
344 &row.local_images,
345 &row.sent_previews,
346 &format!("message-{index}"),
347 own,
348 directory,
349 extras.open_image.as_ref(),
350 cx,
351 ));
352 }
353 content = content.children(attachments);
354 content = content.children(
355 row.meeting_link
356 .clone()
357 .map(|url| meeting_join_chip(format!("message-{index}-join"), url)),
358 );
359 content = content.children(row.link_preview.as_ref().map(|preview| {
360 link_preview_card(
361 preview,
362 format!("message-{index}-link"),
363 directory,
364 false,
365 None,
366 )
367 }));
368 content = content.children(cards);
369 if has_reactions {
370 let pills = reaction_pills(
371 &row.reactions,
372 index,
373 own,
374 directory,
375 extras.react.clone(),
376 extras.controls.as_ref(),
377 );
378 content = content.child(
379 h_flex()
380 .gap(px(8.))
381 .items_end()
382 .when(jumbo.is_some() && own, |line| line.justify_end())
383 .child(if jumbo.is_some() {
384 pills
385 } else {
386 pills.flex_auto()
387 })
388 .child(shifted_right(
389 h_flex()
390 .flex_none()
391 .mb(px(META_BOTTOM_INSET - bubble_padding_y(extras.compact)))
392 .child(bubble_meta(row, index, own, saved)),
393 jumbo.is_some(),
394 )),
395 );
396 } else if !meta_inline {
397 content = content.child(shifted_right(
398 h_flex()
399 .justify_end()
400 .child(bubble_meta(row, index, own, saved)),
401 jumbo.is_some(),
402 ));
403 }
404 element = element.child(content);
405 if meta_inline {
406 element = element.child(
407 div()
408 .absolute()
409 .right(px(META_RIGHT_INSET))
410 .bottom(px(META_BOTTOM_INSET))
411 .child(bubble_meta(row, index, own, saved)),
412 );
413 }
414 if let Some(menu) = extras.menu {
415 element = element.child(placed_toolbar(
416 message_toolbar(menu, cx),
417 extras.timeline.clone(),
418 TOOLBAR_RIGHT_INSET,
419 ));
420 }
421 if let Some(menu) = extras.scheduled_menu {
422 element = element.child(placed_toolbar(
423 scheduled_toolbar(menu),
424 extras.timeline.clone(),
425 TOOLBAR_RIGHT_INSET,
426 ));
427 }
428 element
429}
430
431#[derive(Default)]
432struct BubbleExtras {
433 hover: Option<HoverChange>,
434 menu: Option<MessageMenu>,
435 scheduled_menu: Option<ScheduledMenu>,
436 react: Option<PickHandler>,
437 controls: Option<ReactionControls>,
438 files: Option<FileActions>,
439 open_image: Option<ImageActions>,
440 saved: bool,
441 translation: Option<TranslationActions>,
442 timeline: TimelineBounds,
443 agent_run: Option<AgentRun>,
444 reaction_proposal: Option<ReactionPill>,
445 undo_delete: SharedRowAction,
446 compact: bool,
447 highlighted: bool,
448}
449
450fn agent_chip(run: Option<AgentRun>) -> Option<Div> {
451 run.map(|run| div().py(px(2.)).child(run_chip(&run, theme::row_hover())))
452}
453
454/// Teams: bold "Important!" / "Urgent!" in capitals above the message text.
455pub(super) fn importance_header(row: &MessageRow, compact: bool) -> Option<Div> {
456 let label = importance_label(row.importance)?;
457 Some(
458 div()
459 .debug_selector(|| IMPORTANCE_LABEL_TAG.into())
460 .text_size(px(IMPORTANCE_LABEL_SIZE))
461 .line_height(px(if compact {
462 COMPACT_IMPORTANCE_LINE_HEIGHT
463 } else {
464 IMPORTANCE_LINE_HEIGHT
465 }))
466 .font_weight(FontWeight::BOLD)
467 .text_color(theme::red())
468 .child(label),
469 )
470}
471
472pub(super) fn forwarded_header() -> Div {
473 h_flex()
474 .gap(px(4.))
475 .items_center()
476 .text_size(px(FORWARDED_SIZE))
477 .text_color(theme::text_muted())
478 .child(icon(IconName::Forward, FORWARDED_SIZE, theme::text_muted()))
479 .child("Forwarded")
480}
481
482/// Every time and tick ends `META_RIGHT_INSET` from the bubble edge, inside a padded bubble or under a big emoji.
483fn meta_right_shift(jumbo: bool) -> f32 {
484 if jumbo {
485 META_RIGHT_INSET
486 } else {
487 META_RIGHT_INSET - BUBBLE_PADDING_X
488 }
489}
490
491/// A negative margin drops the whole row from taffy's max-content width, so the meta moves into the padding by offset.
492fn shifted_right(meta: Div, jumbo: bool) -> Div {
493 let shift = meta_right_shift(jumbo);
494 if shift < 0. {
495 let into_padding = shift.abs();
496 meta.relative().left(px(into_padding))
497 } else {
498 meta.mr(px(shift))
499 }
500}
501
502fn bubble_meta(row: &MessageRow, index: usize, own: bool, saved: bool) -> Div {
503 let tint = if own {
504 theme::own_meta()
505 } else {
506 theme::text_muted()
507 };
508 h_flex()
509 .when_some(BUBBLE_META_TAGS.get(index).copied(), |meta, tag| {
510 meta.debug_selector(|| tag.into())
511 })
512 .gap(px(4.))
513 .items_center()
514 .text_size(px(META_SIZE))
515 .line_height(px(14.))
516 .italic()
517 .text_color(tint)
518 .when(row.edited, |meta| meta.child("Edited"))
519 .when(saved, |meta| {
520 meta.child(icon(
521 IconName::Bookmark,
522 SAVED_MARK_SIZE,
523 theme::accent_text(),
524 ))
525 })
526 .child(row.time.clone())
527 .when(own && row.delivery == Delivery::Sending, |meta| {
528 meta.child(symbol("schedule", META_CHECK_SIZE, tint))
529 })
530 .when(matches!(row.delivery, Delivery::Scheduled(_)), |meta| {
531 meta.child(symbol("schedule", META_SIZE + 3., tint))
532 })
533 .when(own && row.delivery == Delivery::Delivered, |meta| {
534 let check = match row.receipt {
535 Receipt::Hidden => return meta,
536 Receipt::Sent => symbol("done", META_CHECK_SIZE, tint).into_any_element(),
537 Receipt::Read => {
538 symbol("done_all", META_CHECK_SIZE, theme::own_read()).into_any_element()
539 }
540 Receipt::ReadLive => receipt_fade_in(
541 &row.key,
542 div().child(symbol("done_all", META_CHECK_SIZE, theme::own_read())),
543 )
544 .into_any_element(),
545 };
546 let holder = div()
547 .id(ElementId::Name(format!("{}-receipt", row.key).into()))
548 .child(check);
549 meta.child(match row.receipt_label.clone() {
550 Some(label) => holder.tooltip(move |window, cx| {
551 gpui_kit::component::tooltip::Tooltip::new(label.clone()).build(window, cx)
552 }),
553 None => holder,
554 })
555 })
556}
557
558fn pulse<E: IntoElement + Styled + 'static>(id: &'static str, element: E) -> AnimationElement<E> {
559 element.with_animation(
560 id,
561 Animation::new(PULSE_PERIOD)
562 .repeat_synced()
563 .with_easing(pulsating_between(0.35, 1.)),
564 |element, opacity| element.opacity(opacity),
565 )
566}
567
568fn jump_highlight(index: usize, corners: Corners) -> AnimationElement<Div> {
569 div()
570 .absolute()
571 .inset_0()
572 .rounded_tl(px(corners.top_left))
573 .rounded_tr(px(corners.top_right))
574 .rounded_br(px(corners.bottom_right))
575 .rounded_bl(px(corners.bottom_left))
576 .bg(theme::accent().opacity(HIGHLIGHT_TINT))
577 .with_animation(
578 SharedString::from(format!("jump-highlight/{index}")),
579 Animation::new(JUMP_HIGHLIGHT_PERIOD),
580 |highlight, delta| highlight.opacity(highlight_opacity(delta)),
581 )
582}
583
584fn highlight_opacity(delta: f32) -> f32 {
585 let fade_in = (delta / HIGHLIGHT_FADE_IN).min(1.);
586 let fade_out = ((1. - delta) / HIGHLIGHT_FADE_OUT).min(1.);
587 ease_in_out(fade_in.min(fade_out))
588}
589
590fn receipt_fade_in(message_key: &str, check: Div) -> AnimationElement<Div> {
591 check.with_animation(
592 SharedString::from(format!("receipt-fade/{message_key}")),
593 Animation::new(RECEIPT_FADE_PERIOD).with_easing(ease_in_out),
594 |check, delta| check.opacity(0.25 + 0.75 * delta),
595 )
596}
597
598pub fn render_skeleton_row(skeleton: &Skeleton, index: usize) -> AnyElement {
599 let corners = bubble_corners(skeleton.own, Default::default());
600 let height = 14. + SKELETON_LINE_HEIGHT * f32::from(skeleton.lines);
601 let shape = div()
602 .w(relative(skeleton.width_ratio))
603 .h(px(height))
604 .rounded_tl(px(corners.top_left))
605 .rounded_tr(px(corners.top_right))
606 .rounded_br(px(corners.bottom_right))
607 .rounded_bl(px(corners.bottom_left))
608 .bg(if skeleton.own {
609 theme::bubble_own().opacity(0.55)
610 } else {
611 theme::bubble_other()
612 });
613 let line = h_flex()
614 .w_full()
615 .when(skeleton.own, |line| line.justify_end())
616 .when(!skeleton.own, |line| {
617 line.gap(px(AVATAR_GAP)).child(
618 div()
619 .size(px(AVATAR_SIZE))
620 .flex_none()
621 .rounded_full()
622 .bg(theme::bubble_other()),
623 )
624 })
625 .child(shape);
626 div()
627 .id(ElementId::Name(format!("row-{index}").into()))
628 .w_full()
629 .px(px(ROW_PADDING))
630 .pt(px(SERIES_START_GAP))
631 .child(pulse("skeleton", line))
632 .into_any_element()
633}
634
635fn meta_room(row: &MessageRow, own: bool, saved: bool) -> usize {
636 let mut width = 12. + row.time.chars().count() as f32 * 6.;
637 if saved {
638 width += SAVED_MARK_SIZE + 4.;
639 }
640 if row.edited {
641 width += 56.;
642 }
643 if own {
644 width += META_CHECK_SIZE + 4.;
645 }
646 (width / FIGURE_SPACE_WIDTH).ceil() as usize
647}
648
649fn with_meta_room(blocks: &[Block], room: usize) -> Option<Vec<Block>> {
650 let mut padded = blocks.to_vec();
651 let inline = padded.last_mut()?.last_inline_mut()?;
652 *inline = inline.with_trailing_room(room);
653 Some(padded)
654}
655
656pub(super) fn has_text(row: &MessageRow) -> bool {
657 !row.blocks.is_empty()
658 || (row.images.is_empty()
659 && row.local_images.is_empty()
660 && row.files.is_empty()
661 && row.adaptive_cards.is_empty())
662}
663
664fn delivery_link(
665 index: usize,
666 name: &str,
667 label: &'static str,
668 action: RowAction,
669) -> Stateful<Div> {
670 div()
671 .id(ElementId::Name(format!("{name}-{index}").into()))
672 .cursor_pointer()
673 .text_color(theme::red_tint())
674 .underline()
675 .child(label)
676 .when_some(action, |link, action| {
677 link.on_click(move |_, _, cx| action(cx))
678 })
679}
680
681pub(super) fn delivery_note(
682 row: &MessageRow,
683 delivery: DeliveryActions,
684 index: usize,
685) -> Option<AnyElement> {
686 let DeliveryActions {
687 retry,
688 delete,
689 send_now,
690 } = delivery;
691 let note = |text: &'static str| {
692 h_flex()
693 .gap(px(4.))
694 .text_size(px(11.))
695 .text_color(theme::red_soft())
696 .child(text)
697 };
698 match &row.delivery {
699 Delivery::Delivered | Delivery::Sending | Delivery::Scheduled(ScheduledState::Waiting) => {
700 None
701 }
702 Delivery::Failed(_) => Some(
703 note("Failed to send.")
704 .child(delivery_link(index, "retry", "Retry", retry))
705 .child(delivery_link(index, "delete", "Delete", delete))
706 .into_any_element(),
707 ),
708 Delivery::Scheduled(ScheduledState::DeliveryFailed) => Some(
709 note("Couldn't send at the scheduled time.")
710 .child(delivery_link(index, "send-now", "Send now", send_now))
711 .child(delivery_link(index, "delete", "Delete", delete))
712 .into_any_element(),
713 ),
714 Delivery::Scheduled(ScheduledState::ChangeFailed) => Some(
715 note("Couldn't change the scheduled message.")
716 .child(delivery_link(index, "retry", "Retry", retry))
717 .into_any_element(),
718 ),
719 }
720}
721
722fn others_row(
723 row: &MessageRow,
724 index: usize,
725 directory: &Directory,
726 bot: Option<&BotIdentity>,
727 delivery: DeliveryActions,
728 mut extras: BubbleExtras,
729 cx: &App,
730) -> Div {
731 let translation = extras.translation.take();
732 let agent_run = extras.agent_run.take();
733 let reaction_proposal = extras.reaction_proposal.take();
734 let first = !row.series.has_prev;
735 let author = bot.map_or_else(|| row.author.clone(), |bot| bot.name.clone());
736 let profile_user = row.sender_id.as_deref().filter(|_| bot.is_none());
737 let mut column = v_flex()
738 .gap(px(2.))
739 .flex_1()
740 .min_w_0()
741 .max_w(bubble_max_width(
742 &extras.timeline,
743 AVATAR_SIZE + AVATAR_GAP,
744 extras.compact,
745 ))
746 .items_start();
747 if first {
748 column = column.child(
749 h_flex().gap(px(8.)).items_baseline().pl(px(2.)).child(
750 opens_profile(
751 div().id(ElementId::Name(format!("author-{index}").into())),
752 profile_user,
753 )
754 .text_size(px(12.))
755 .font_weight(FontWeight::SEMIBOLD)
756 .text_color(theme::text_soft())
757 .child(author.clone()),
758 ),
759 );
760 }
761 column = column.child(bubble(row, index, false, directory, extras, cx));
762 column = column.children(agent_chip(agent_run));
763 column = column.children(reaction_proposal.map(|pill| reaction_pill(pill, &row.key)));
764 column = column.children(translation_line(row, index, translation.as_ref()));
765 if let Some(note) = delivery_note(row, delivery, index) {
766 column = column.child(note);
767 }
768 let lead = if let Some(bot) = bot.filter(|_| first) {
769 bot_avatar(bot, AVATAR_SIZE)
770 } else if first {
771 person_avatar(
772 directory,
773 row.sender_id.as_deref(),
774 &row.author,
775 AVATAR_SIZE,
776 )
777 } else {
778 div().size(px(AVATAR_SIZE)).into_any_element()
779 };
780 h_flex()
781 .w_full()
782 .gap(px(AVATAR_GAP))
783 .items_start()
784 .child(
785 opens_profile(
786 div().id(ElementId::Name(format!("author-avatar-{index}").into())),
787 profile_user.filter(|_| first),
788 )
789 .w(px(AVATAR_SIZE))
790 .flex_none()
791 .child(lead),
792 )
793 .child(column)
794}
795
796fn own_row(
797 row: &MessageRow,
798 index: usize,
799 directory: &Directory,
800 delivery: DeliveryActions,
801 mut extras: BubbleExtras,
802 cx: &App,
803) -> Div {
804 let translation = extras.translation.take();
805 let agent_run = extras.agent_run.take();
806 let reaction_proposal = extras.reaction_proposal.take();
807 let mut column = v_flex().w_full().items_end().gap(px(2.));
808 column = column.child(
809 h_flex().w_full().justify_end().child(
810 div()
811 .min_w_0()
812 .max_w(bubble_max_width(&extras.timeline, 0., extras.compact))
813 .child(bubble(row, index, true, directory, extras, cx)),
814 ),
815 );
816 column = column.children(agent_chip(agent_run));
817 column = column.children(reaction_proposal.map(|pill| reaction_pill(pill, &row.key)));
818 column = column.children(translation_line(row, index, translation.as_ref()));
819 if let Some(note) = delivery_note(row, delivery, index) {
820 column = column.child(note);
821 }
822 column
823}
824
825pub fn render_message_row(
826 row: &MessageRow,
827 index: usize,
828 actions: RowActions,
829 directory: &Directory,
830 cx: &App,
831) -> AnyElement {
832 if let Some(call) = &row.call {
833 let selecting = actions.select.is_some();
834 let card = call_card_row(row, call, index, directory, actions.call);
835 return if selecting {
836 div()
837 .w_full()
838 .pl(px(SELECT_BOX_SIZE + SELECT_GAP))
839 .child(card)
840 .into_any_element()
841 } else {
842 card
843 };
844 }
845 let RowActions {
846 select,
847 bot,
848 delivery,
849 reply,
850 open_context,
851 context_menu,
852 hovered,
853 menu,
854 scheduled_menu,
855 react,
856 reaction_controls,
857 add_reaction: _,
858 files,
859 open_image,
860 highlighted,
861 saved,
862 translation,
863 timeline,
864 agent_run,
865 call: _,
866 pinned: _,
867 reaction_proposal,
868 undo_delete,
869 compact,
870 appear: appear_kind,
871 } = actions;
872 let extras = BubbleExtras {
873 hover: hovered,
874 menu,
875 scheduled_menu,
876 react,
877 controls: reaction_controls,
878 files,
879 open_image,
880 saved,
881 translation,
882 timeline,
883 agent_run,
884 reaction_proposal,
885 undo_delete,
886 compact,
887 highlighted,
888 };
889 let own = row.own;
890 let spacing = px(row_spacing(row.series.has_prev, compact));
891 let mut container = v_flex().w_full().px(px(ROW_PADDING));
892 if let Some(day) = &row.day_header {
893 container = container.child(day_separator(day));
894 }
895 if row.new_marker {
896 container = container.child(new_marker());
897 }
898 let body = if own {
899 own_row(row, index, directory, delivery, extras, cx)
900 } else {
901 others_row(row, index, directory, bot.as_ref(), delivery, extras, cx)
902 };
903 let body = match appear_kind.filter(|_| !cx.reduce_motion()) {
904 Some(kind) => appear(body, &row.key, kind).into_any_element(),
905 None => body.into_any_element(),
906 };
907 let checked = select.as_ref().is_some_and(|select| select.checked);
908 let body = match select {
909 Some(select) => selectable_body(select, index, body).into_any_element(),
910 None => body,
911 };
912 container
913 .child(
914 div()
915 .id(ElementId::Name(format!("row-{index}").into()))
916 .mx(px(-ROW_PADDING))
917 .px(px(ROW_PADDING))
918 .pt(spacing)
919 .when(checked && !highlighted, |element| {
920 element.bg(theme::accent().opacity(SELECTED_TINT))
921 })
922 .map(|element| match (open_context, reply) {
923 (Some(open_context), _) => {
924 element.on_mouse_down(MouseButton::Right, move |event, _, cx| {
925 cx.stop_propagation();
926 open_context(event.position, cx)
927 })
928 }
929 (None, Some(reply)) => {
930 element.on_mouse_down(MouseButton::Right, move |_, _, cx| reply(cx))
931 }
932 (None, None) => element,
933 })
934 .children(context_menu)
935 .child(body),
936 )
937 .into_any_element()
938}
939
940fn selectable_body(select: RowSelect, index: usize, body: impl IntoElement) -> Stateful<Div> {
941 let RowSelect {
942 checked,
943 blocked,
944 toggle,
945 } = select;
946 h_flex()
947 .id(ElementId::Name(format!("forward-select-{index}").into()))
948 .w_full()
949 .items_center()
950 .gap(px(SELECT_GAP))
951 .child(select_box(checked, blocked.is_some(), index))
952 .child(div().flex_1().min_w_0().child(body))
953 .map(|row| match blocked {
954 Some(reason) => row.tooltip(move |window, cx| {
955 gpui_kit::component::tooltip::Tooltip::new(reason).build(window, cx)
956 }),
957 None => row.cursor_pointer().on_click(move |_, _, cx| {
958 cx.stop_propagation();
959 toggle(cx)
960 }),
961 })
962}
963
964pub fn select_box(checked: bool, blocked: bool, index: usize) -> Div {
965 div()
966 .debug_selector(|| select_box_selector(index))
967 .size(px(SELECT_BOX_SIZE))
968 .flex_none()
969 .flex()
970 .items_center()
971 .justify_center()
972 .rounded(px(SELECT_BOX_RADIUS))
973 .map(|element| {
974 if checked {
975 element.bg(theme::accent()).child(icon(
976 IconName::Check,
977 SELECT_CHECK_SIZE,
978 theme::on_accent(),
979 ))
980 } else {
981 element.bg(theme::border_strong())
982 }
983 })
984 .when(blocked, |element| element.opacity(SELECT_BLOCKED_OPACITY))
985}
986
987#[cfg(test)]
988mod highlight_tests {
989 use super::highlight_opacity;
990
991 #[test]
992 fn jump_highlight_fades_in_holds_and_fades_out() {
993 assert_eq!(highlight_opacity(0.), 0.);
994 assert_eq!(highlight_opacity(0.5), 1.);
995 assert!(highlight_opacity(0.8) < 1.);
996 assert!(highlight_opacity(0.99) < 0.05);
997 }
998}
999
1000#[cfg(test)]
1001mod density_tests {
1002 use super::{META_BOTTOM_INSET, bubble_padding_y, row_spacing, runway_width};
1003
1004 #[test]
1005 fn compact_density_tightens_gaps_and_bubble_padding() {
1006 assert!(row_spacing(false, true) < row_spacing(false, false));
1007 assert!(row_spacing(true, true) < row_spacing(true, false));
1008 assert!(bubble_padding_y(true) < bubble_padding_y(false));
1009 assert!(META_BOTTOM_INSET - bubble_padding_y(true) >= 0.);
1010 }
1011
1012 #[test]
1013 fn comfy_density_caps_the_runway_like_teams_and_compact_uses_the_full_width() {
1014 assert_eq!(runway_width(2400., false), 1060.);
1015 assert_eq!(runway_width(800., false), 800.);
1016 assert_eq!(runway_width(2400., true), 2400.);
1017 }
1018}