BlueGit

ict rusty-teams

Files

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

message_row.rs

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