BlueGit

ict rusty-teams

Files

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

message_row.rs

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