BlueGit

ict rusty-teams

Files

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

message_row.rs

4378439a History
message_row.rs 31.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::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_appear::{AppearKind, appear};
15use super::message_actions::{MessageMenu, ReactionAdder, message_toolbar};
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_TAGS: [&str; 12] = [
41 "message-bubble/0",
42 "message-bubble/1",
43 "message-bubble/2",
44 "message-bubble/3",
45 "message-bubble/4",
46 "message-bubble/5",
47 "message-bubble/6",
48 "message-bubble/7",
49 "message-bubble/8",
50 "message-bubble/9",
51 "message-bubble/10",
52 "message-bubble/11",
53];
54pub const IMPORTANCE_LABEL_TAG: &str = "message-importance-label";
55pub(super) const AVATAR_SIZE: f32 = 28.;
56pub(super) const AVATAR_GAP: f32 = 8.;
57const SERIES_START_GAP: f32 = 8.;
58const SERIES_GAP: f32 = 2.;
59const COMPACT_SERIES_START_GAP: f32 = 4.;
60const COMPACT_SERIES_GAP: f32 = 1.;
61const COMPACT_BUBBLE_PADDING_Y: f32 = 4.;
62pub(super) const BODY_SIZE: f32 = 13.5;
63const MAX_WIDTH_RATIO: f32 = 0.7;
64const NARROW_BUBBLE_WIDTH: f32 = 300.;
65const BUBBLE_PADDING_Y: f32 = 8.;
66const META_BOTTOM_INSET: f32 = 4.;
67const META_SIZE: f32 = 11.;
68const META_CHECK_SIZE: f32 = 15.;
69const TICK_BASELINE_DROP: f32 = 0.5;
70const SAVED_MARK_SIZE: f32 = 12.;
71const FORWARDED_SIZE: f32 = 11.;
72const IMPORTANCE_LABEL_SIZE: f32 = 12.;
73const IMPORTANCE_LINE_HEIGHT: f32 = 16.;
74const COMPACT_IMPORTANCE_LINE_HEIGHT: f32 = 14.;
75pub(super) const BUBBLE_PADDING_X: f32 = 12.;
76const FIGURE_SPACE_WIDTH: f32 = 7.4;
77const PULSE_PERIOD: Duration = Duration::from_millis(1600);
78const RECEIPT_FADE_PERIOD: Duration = Duration::from_millis(600);
79pub const JUMP_HIGHLIGHT_PERIOD: Duration = Duration::from_millis(2400);
80const HIGHLIGHT_FADE_IN: f32 = 0.1;
81const HIGHLIGHT_FADE_OUT: f32 = 0.4;
82const HIGHLIGHT_TINT: f32 = 0.3;
83const SKELETON_LINE_HEIGHT: f32 = 20.;
84pub const SELECT_BOX_SIZE: f32 = 16.;
85pub const SELECT_GAP: f32 = 12.;
86const SELECT_CHECK_SIZE: f32 = 12.;
87const SELECT_BOX_RADIUS: f32 = 4.;
88const SELECT_BLOCKED_OPACITY: f32 = 0.4;
89const SELECTED_TINT: f32 = 0.08;
90pub fn select_box_selector(index: usize) -> String {
91 format!("forward-check/{index}")
92}
93
94pub struct DeliveryActions {
95 pub retry: RowAction,
96 pub delete: RowAction,
97 pub send_now: RowAction,
98}
99
100pub struct RowSelect {
101 pub checked: bool,
102 pub blocked: Option<&'static str>,
103 pub toggle: std::rc::Rc<dyn Fn(&mut App)>,
104}
105
106pub struct RowActions {
107 pub select: Option<RowSelect>,
108 pub bot: Option<BotIdentity>,
109 pub delivery: DeliveryActions,
110 pub reply: RowAction,
111 pub open_context: Option<Rc<dyn Fn(Point<Pixels>, &mut App)>>,
112 pub context_menu: Option<AnyElement>,
113 pub hovered: Option<HoverChange>,
114 pub menu: Option<MessageMenu>,
115 pub scheduled_menu: Option<ScheduledMenu>,
116 pub react: Option<PickHandler>,
117 pub reaction_controls: Option<ReactionControls>,
118 pub add_reaction: Option<ReactionAdder>,
119 pub files: Option<FileActions>,
120 pub open_image: Option<ImageActions>,
121 pub highlighted: bool,
122 pub saved: bool,
123 pub pinned: bool,
124 pub translation: Option<TranslationActions>,
125 pub timeline: TimelineBounds,
126 pub agent_run: Option<AgentRun>,
127 pub call: CallActions,
128 pub reaction_proposal: Option<ReactionPill>,
129 pub undo_delete: SharedRowAction,
130 pub compact: bool,
131 pub appear: Option<AppearKind>,
132}
133
134pub fn row_spacing(has_previous: bool, compact: bool) -> f32 {
135 match (has_previous, compact) {
136 (true, false) => SERIES_GAP,
137 (true, true) => COMPACT_SERIES_GAP,
138 (false, false) => SERIES_START_GAP,
139 (false, true) => COMPACT_SERIES_START_GAP,
140 }
141}
142
143pub fn bubble_padding_y(compact: bool) -> f32 {
144 if compact {
145 COMPACT_BUBBLE_PADDING_Y
146 } else {
147 BUBBLE_PADDING_Y
148 }
149}
150
151const TOOLBAR_RIGHT_INSET: f32 = 0.;
152
153fn separator_pill(label: &str, fill: Hsla, text_color: Hsla) -> Div {
154 h_flex()
155 .w_full()
156 .justify_center()
157 .mt(px(12.))
158 .mb(px(6.))
159 .child(
160 div()
161 .h(px(24.))
162 .px(px(8.))
163 .flex()
164 .items_center()
165 .rounded(px(12.))
166 .bg(fill)
167 .text_size(px(11.))
168 .text_color(text_color)
169 .child(label.to_owned()),
170 )
171}
172
173pub(super) fn day_separator(label: &str) -> Div {
174 separator_pill(label, theme::row_hover(), theme::text_muted())
175}
176
177pub(super) fn new_marker() -> Div {
178 separator_pill(
179 "New",
180 theme::mention_background(false),
181 theme::accent_text(),
182 )
183}
184
185pub(super) fn deleted_tombstone(index: usize, undo_delete: SharedRowAction) -> Div {
186 let line =
187 h_flex()
188 .gap(px(8.))
189 .items_baseline()
190 .text_size(px(13.))
191 .child(div().italic().text_color(theme::text_muted()).child(
192 if undo_delete.is_some() {
193 DELETED_TOMBSTONE
194 } else {
195 DELETED_PREVIEW
196 },
197 ));
198 match undo_delete {
199 Some(undo) => line.child(
200 div()
201 .id(ElementId::Name(format!("undo-delete-{index}").into()))
202 .cursor_pointer()
203 .font_weight(FontWeight::SEMIBOLD)
204 .text_color(theme::accent_text())
205 .hover(|link| link.underline())
206 .child(UNDO_DELETE_LABEL)
207 .on_click(move |_, _, cx| undo(cx)),
208 ),
209 None => line,
210 }
211}
212
213fn bubble_max_width(timeline: &TimelineBounds, reserved: f32, compact: bool) -> Length {
214 let Some(bounds) = timeline.get() else {
215 return relative(MAX_WIDTH_RATIO).into();
216 };
217 let runway = runway_width(f32::from(bounds.size.width), compact);
218 let row_width = (runway - 2. * ROW_PADDING - reserved).max(0.);
219 px((row_width * MAX_WIDTH_RATIO).max(row_width.min(NARROW_BUBBLE_WIDTH))).into()
220}
221
222pub fn runway_width(timeline_width: f32, compact: bool) -> f32 {
223 if compact {
224 timeline_width
225 } else {
226 timeline_width.min(COMFY_RUNWAY_WIDTH)
227 }
228}
229
230fn bubble(
231 row: &MessageRow,
232 index: usize,
233 own: bool,
234 directory: &Directory,
235 extras: BubbleExtras,
236 cx: &App,
237) -> Stateful<Div> {
238 let corners = bubble_corners(own, row.series);
239 let jumbo = row.jumbo.as_deref().filter(|_| {
240 !row.deleted && matches!(row.delivery, Delivery::Delivered | Delivery::Sending)
241 });
242 let mut element = div()
243 .id(ElementId::Name(format!("bubble-{index}").into()))
244 .when_some(extras.hover.clone(), |element, hover| {
245 element.on_hover(move |is_hovered, _, cx| hover(*is_hovered, cx))
246 })
247 .relative()
248 .max_w_full()
249 .when_some(BUBBLE_TAGS.get(index).copied(), |element, tag| {
250 element.debug_selector(|| tag.into())
251 })
252 .when(jumbo.is_none(), |element| {
253 element
254 .rounded_tl(px(corners.top_left))
255 .rounded_tr(px(corners.top_right))
256 .rounded_br(px(corners.bottom_right))
257 .rounded_bl(px(corners.bottom_left))
258 .px(px(BUBBLE_PADDING_X))
259 .py(px(bubble_padding_y(extras.compact)))
260 })
261 .text_size(px(BODY_SIZE))
262 .line_height(relative(1.45));
263 if row.deleted {
264 return element
265 .border_1()
266 .border_color(theme::border_strong())
267 .py(px(6.))
268 .child(deleted_tombstone(index, extras.undo_delete));
269 }
270 let failed = matches!(row.delivery, Delivery::Failed(_));
271 let scheduled = matches!(row.delivery, Delivery::Scheduled(_));
272 element = element
273 .when(!scheduled && jumbo.is_none(), |bubble| {
274 bubble.bg(if own && !failed {
275 theme::bubble_own()
276 } else {
277 theme::bubble_other()
278 })
279 })
280 .when(failed, |bubble| {
281 bubble.border_1().border_color(theme::red())
282 })
283 .when(scheduled, |bubble| {
284 bubble.border_1().border_color(
285 if row.delivery == Delivery::Scheduled(ScheduledState::DeliveryFailed) {
286 theme::red()
287 } else {
288 theme::accent()
289 },
290 )
291 });
292 element = element.when(extras.highlighted, |bubble| {
293 bubble.child(jump_highlight(index, corners))
294 });
295 let has_reactions = !row.reactions.is_empty();
296 let saved = extras.saved;
297 let mut content = v_flex().gap(px(6.));
298 content = content.children(importance_header(row, extras.compact));
299 if row.forwarded {
300 content = content.child(forwarded_header());
301 }
302 let attachments = attachments_view(
303 &row.blocks,
304 &row.images,
305 &row.local_images,
306 &row.files,
307 &format!("message-{index}"),
308 directory,
309 extras.files.as_ref(),
310 extras.open_image.as_ref(),
311 );
312 let cards = cards_view(&row.adaptive_cards, &row.conversation_id, &row.key, cx);
313 let trailing_content = attachments.is_some()
314 || row.meeting_link.is_some()
315 || row.link_preview.is_some()
316 || !cards.is_empty();
317 let padded_blocks = if has_reactions || trailing_content || jumbo.is_some() {
318 None
319 } else {
320 with_meta_room(&row.blocks, meta_room(row, own, saved))
321 };
322 let meta_inline = has_text(row) && padded_blocks.is_some();
323 if let Some(jumbo) = jumbo {
324 content = content.child(jumbo_row(&row.key, jumbo, own));
325 } else if has_text(row) {
326 content = content.children(message_body(
327 padded_blocks.as_deref().unwrap_or(&row.blocks),
328 &row.images,
329 &row.local_images,
330 &format!("message-{index}"),
331 own,
332 directory,
333 extras.open_image.as_ref(),
334 cx,
335 ));
336 }
337 content = content.children(attachments);
338 content = content.children(
339 row.meeting_link
340 .clone()
341 .map(|url| meeting_join_chip(format!("message-{index}-join"), url)),
342 );
343 content = content.children(row.link_preview.as_ref().map(|preview| {
344 link_preview_card(
345 preview,
346 format!("message-{index}-link"),
347 directory,
348 false,
349 None,
350 )
351 }));
352 content = content.children(cards);
353 if has_reactions {
354 let pills = reaction_pills(
355 &row.reactions,
356 index,
357 own,
358 directory,
359 extras.react.clone(),
360 extras.controls.as_ref(),
361 );
362 content = content.child(
363 h_flex()
364 .gap(px(8.))
365 .items_end()
366 .when(jumbo.is_some() && own, |line| line.justify_end())
367 .child(if jumbo.is_some() { pills } else { pills.flex_auto() })
368 .child(
369 h_flex()
370 .flex_none()
371 .mb(px(META_BOTTOM_INSET - bubble_padding_y(extras.compact)))
372 .child(bubble_meta(row, own, saved)),
373 ),
374 );
375 } else if !meta_inline {
376 content = content.child(h_flex().justify_end().child(bubble_meta(row, own, saved)));
377 }
378 element = element.child(content);
379 if meta_inline {
380 element = element.child(
381 div()
382 .absolute()
383 .right(px(8.))
384 .bottom(px(META_BOTTOM_INSET))
385 .child(bubble_meta(row, own, saved)),
386 );
387 }
388 if let Some(menu) = extras.menu {
389 element = element.child(placed_toolbar(
390 message_toolbar(menu, cx),
391 extras.timeline.clone(),
392 TOOLBAR_RIGHT_INSET,
393 ));
394 }
395 if let Some(menu) = extras.scheduled_menu {
396 element = element.child(placed_toolbar(
397 scheduled_toolbar(menu),
398 extras.timeline.clone(),
399 TOOLBAR_RIGHT_INSET,
400 ));
401 }
402 element
403}
404
405#[derive(Default)]
406struct BubbleExtras {
407 hover: Option<HoverChange>,
408 menu: Option<MessageMenu>,
409 scheduled_menu: Option<ScheduledMenu>,
410 react: Option<PickHandler>,
411 controls: Option<ReactionControls>,
412 files: Option<FileActions>,
413 open_image: Option<ImageActions>,
414 saved: bool,
415 translation: Option<TranslationActions>,
416 timeline: TimelineBounds,
417 agent_run: Option<AgentRun>,
418 reaction_proposal: Option<ReactionPill>,
419 undo_delete: SharedRowAction,
420 compact: bool,
421 highlighted: bool,
422}
423
424fn agent_chip(run: Option<AgentRun>) -> Option<Div> {
425 run.map(|run| div().py(px(2.)).child(run_chip(&run, theme::row_hover())))
426}
427
428/// Teams: bold "Important!" / "Urgent!" in capitals above the message text.
429pub(super) fn importance_header(row: &MessageRow, compact: bool) -> Option<Div> {
430 let label = importance_label(row.importance)?;
431 Some(
432 div()
433 .debug_selector(|| IMPORTANCE_LABEL_TAG.into())
434 .text_size(px(IMPORTANCE_LABEL_SIZE))
435 .line_height(px(if compact {
436 COMPACT_IMPORTANCE_LINE_HEIGHT
437 } else {
438 IMPORTANCE_LINE_HEIGHT
439 }))
440 .font_weight(FontWeight::BOLD)
441 .text_color(theme::red())
442 .child(label),
443 )
444}
445
446pub(super) fn forwarded_header() -> Div {
447 h_flex()
448 .gap(px(4.))
449 .items_center()
450 .text_size(px(FORWARDED_SIZE))
451 .text_color(theme::text_muted())
452 .child(icon(IconName::Forward, FORWARDED_SIZE, theme::text_muted()))
453 .child("Forwarded")
454}
455
456fn bubble_meta(row: &MessageRow, own: bool, saved: bool) -> Div {
457 let tint = if own {
458 theme::own_meta()
459 } else {
460 theme::text_muted()
461 };
462 h_flex()
463 .gap(px(4.))
464 .items_center()
465 .text_size(px(META_SIZE))
466 .line_height(px(14.))
467 .italic()
468 .text_color(tint)
469 .when(row.edited, |meta| meta.child("Edited"))
470 .when(saved, |meta| {
471 meta.child(icon(
472 IconName::Bookmark,
473 SAVED_MARK_SIZE,
474 theme::accent_text(),
475 ))
476 })
477 .child(row.time.clone())
478 .when(own && row.delivery == Delivery::Sending, |meta| {
479 meta.child(symbol("schedule", META_CHECK_SIZE, tint))
480 })
481 .when(matches!(row.delivery, Delivery::Scheduled(_)), |meta| {
482 meta.child(symbol("schedule", META_SIZE + 3., tint))
483 })
484 .when(
485 own && row.delivery == Delivery::Delivered,
486 |meta| {
487 let check = match row.receipt {
488 Receipt::Hidden => return meta,
489 Receipt::Sent => symbol("done", META_CHECK_SIZE, tint).into_any_element(),
490 Receipt::Read => symbol("done_all", META_CHECK_SIZE, theme::own_read()).into_any_element(),
491 Receipt::ReadLive => receipt_fade_in(
492 &row.key,
493 div().child(symbol("done_all", META_CHECK_SIZE, theme::own_read())),
494 )
495 .into_any_element(),
496 };
497 // The tick glyph ends about half a pixel above the time baseline (measured at 100 % and 125 %).
498 let holder = div()
499 .id(ElementId::Name(format!("{}-receipt", row.key).into()))
500 .relative()
501 .top(px(TICK_BASELINE_DROP))
502 .child(check);
503 meta.child(match row.receipt_label.clone() {
504 Some(label) => holder.tooltip(move |window, cx| {
505 gpui_kit::component::tooltip::Tooltip::new(label.clone()).build(window, cx)
506 }),
507 None => holder,
508 })
509 },
510 )
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 open_context,
802 context_menu,
803 hovered,
804 menu,
805 scheduled_menu,
806 react,
807 reaction_controls,
808 add_reaction: _,
809 files,
810 open_image,
811 highlighted,
812 saved,
813 translation,
814 timeline,
815 agent_run,
816 call: _,
817 pinned: _,
818 reaction_proposal,
819 undo_delete,
820 compact,
821 appear: appear_kind,
822 } = actions;
823 let extras = BubbleExtras {
824 hover: hovered,
825 menu,
826 scheduled_menu,
827 react,
828 controls: reaction_controls,
829 files,
830 open_image,
831 saved,
832 translation,
833 timeline,
834 agent_run,
835 reaction_proposal,
836 undo_delete,
837 compact,
838 highlighted,
839 };
840 let own = row.own;
841 let spacing = px(row_spacing(row.series.has_prev, compact));
842 let mut container = v_flex().w_full().px(px(ROW_PADDING));
843 if let Some(day) = &row.day_header {
844 container = container.child(day_separator(day));
845 }
846 if row.new_marker {
847 container = container.child(new_marker());
848 }
849 let body = if own {
850 own_row(row, index, directory, delivery, extras, cx)
851 } else {
852 others_row(row, index, directory, bot.as_ref(), delivery, extras, cx)
853 };
854 let body = match appear_kind.filter(|_| !cx.reduce_motion()) {
855 Some(kind) => appear(body, &row.key, kind).into_any_element(),
856 None => body.into_any_element(),
857 };
858 let checked = select.as_ref().is_some_and(|select| select.checked);
859 let body = match select {
860 Some(select) => selectable_body(select, index, body).into_any_element(),
861 None => body,
862 };
863 container
864 .child(
865 div()
866 .id(ElementId::Name(format!("row-{index}").into()))
867 .mx(px(-ROW_PADDING))
868 .px(px(ROW_PADDING))
869 .pt(spacing)
870 .when(checked && !highlighted, |element| {
871 element.bg(theme::accent().opacity(SELECTED_TINT))
872 })
873 .map(|element| match (open_context, reply) {
874 (Some(open_context), _) => element.on_mouse_down(MouseButton::Right, move |event, _, cx| {
875 cx.stop_propagation();
876 open_context(event.position, cx)
877 }),
878 (None, Some(reply)) => element.on_mouse_down(MouseButton::Right, move |_, _, cx| reply(cx)),
879 (None, None) => element,
880 })
881 .children(context_menu)
882 .child(body),
883 )
884 .into_any_element()
885}
886
887fn selectable_body(select: RowSelect, index: usize, body: impl IntoElement) -> Stateful<Div> {
888 let RowSelect {
889 checked,
890 blocked,
891 toggle,
892 } = select;
893 h_flex()
894 .id(ElementId::Name(format!("forward-select-{index}").into()))
895 .w_full()
896 .items_center()
897 .gap(px(SELECT_GAP))
898 .child(select_box(checked, blocked.is_some(), index))
899 .child(div().flex_1().min_w_0().child(body))
900 .map(|row| match blocked {
901 Some(reason) => row.tooltip(move |window, cx| {
902 gpui_kit::component::tooltip::Tooltip::new(reason).build(window, cx)
903 }),
904 None => row.cursor_pointer().on_click(move |_, _, cx| {
905 cx.stop_propagation();
906 toggle(cx)
907 }),
908 })
909}
910
911pub fn select_box(checked: bool, blocked: bool, index: usize) -> Div {
912 div()
913 .debug_selector(|| select_box_selector(index))
914 .size(px(SELECT_BOX_SIZE))
915 .flex_none()
916 .flex()
917 .items_center()
918 .justify_center()
919 .rounded(px(SELECT_BOX_RADIUS))
920 .map(|element| {
921 if checked {
922 element.bg(theme::accent()).child(icon(
923 IconName::Check,
924 SELECT_CHECK_SIZE,
925 theme::on_accent(),
926 ))
927 } else {
928 element.bg(theme::border_strong())
929 }
930 })
931 .when(blocked, |element| element.opacity(SELECT_BLOCKED_OPACITY))
932}
933
934#[cfg(test)]
935mod highlight_tests {
936 use super::highlight_opacity;
937
938 #[test]
939 fn jump_highlight_fades_in_holds_and_fades_out() {
940 assert_eq!(highlight_opacity(0.), 0.);
941 assert_eq!(highlight_opacity(0.5), 1.);
942 assert!(highlight_opacity(0.8) < 1.);
943 assert!(highlight_opacity(0.99) < 0.05);
944 }
945}
946
947#[cfg(test)]
948mod density_tests {
949 use super::{META_BOTTOM_INSET, bubble_padding_y, row_spacing, runway_width};
950
951 #[test]
952 fn compact_density_tightens_gaps_and_bubble_padding() {
953 assert!(row_spacing(false, true) < row_spacing(false, false));
954 assert!(row_spacing(true, true) < row_spacing(true, false));
955 assert!(bubble_padding_y(true) < bubble_padding_y(false));
956 assert!(META_BOTTOM_INSET - bubble_padding_y(true) >= 0.);
957 }
958
959 #[test]
960 fn comfy_density_caps_the_runway_like_teams_and_compact_uses_the_full_width() {
961 assert_eq!(runway_width(2400., false), 1060.);
962 assert_eq!(runway_width(800., false), 800.);
963 assert_eq!(runway_width(2400., true), 2400.);
964 }
965}