BlueGit

ict rusty-teams

Files

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

message_row.rs

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