BlueGit

ict rusty-teams

Files

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

message_row.rs

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