BlueGit

ict rusty-teams

Files

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

message_row.rs

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