BlueGit

ict rusty-teams

Files

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

message_row.rs

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