BlueGit

ict rusty-teams

Files

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

message_row.rs

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