BlueGit

ict rusty-teams

Files

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

message_row.rs

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