BlueGit

ict rusty-teams

Files

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

message_row.rs

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