BlueGit

ict rusty-teams

Files

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

message_row.rs

message_row.rs 18.7 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;
6
7use super::adaptive_card::cards_view;
8use super::attachments::{FileActions, ImageOpen, attachments_view, message_body};
9use super::avatar::{bot_avatar, person_avatar};
10use super::link_preview::{link_preview_card, meeting_join_chip};
11use super::message_actions::{HoverChange, MessageMenu, message_toolbar};
12use super::profile_card::opens_profile;
13use super::reaction_picker::PickHandler;
14use super::reaction_pills::{ReactionControls, reaction_pills};
15use super::scheduled_toolbar::{ScheduledMenu, scheduled_toolbar};
16use super::translation_line::{TranslationActions, translation_line};
17use super::widgets::{icon, symbol};
18use crate::card_state::BotIdentity;
19use crate::data::Directory;
20use crate::render::Block;
21use crate::rows::{Delivery, MessageRow, Receipt, ScheduledState, Skeleton, bubble_corners};
22use crate::sidebar_model::DELETED_PREVIEW;
23use crate::theme;
24
25pub type RowAction = Option<Box<dyn Fn(&mut App)>>;
26
27pub(super) const AVATAR_SIZE: f32 = 28.;
28pub(super) const AVATAR_GAP: f32 = 8.;
29const SERIES_START_GAP: f32 = 8.;
30pub(super) const BODY_SIZE: f32 = 13.5;
31const MAX_WIDTH_RATIO: f32 = 0.7;
32const REACTION_FOOTER_HEIGHT: f32 = 24.;
33const META_SIZE: f32 = 11.;
34const META_CHECK_SIZE: f32 = 15.;
35const SAVED_MARK_SIZE: f32 = 12.;
36const FORWARDED_SIZE: f32 = 11.;
37const FIGURE_SPACE_WIDTH: f32 = 7.4;
38const PULSE_PERIOD: Duration = Duration::from_millis(1600);
39const SKELETON_LINE_HEIGHT: f32 = 20.;
40
41pub struct DeliveryActions {
42 pub retry: RowAction,
43 pub delete: RowAction,
44 pub send_now: RowAction,
45}
46
47pub struct RowActions {
48 pub bot: Option<BotIdentity>,
49 pub delivery: DeliveryActions,
50 pub reply: RowAction,
51 pub hovered: Option<HoverChange>,
52 pub menu: Option<MessageMenu>,
53 pub scheduled_menu: Option<ScheduledMenu>,
54 pub react: Option<PickHandler>,
55 pub reaction_controls: Option<ReactionControls>,
56 pub files: Option<FileActions>,
57 pub open_image: Option<ImageOpen>,
58 pub highlighted: bool,
59 pub saved: bool,
60 pub translation: Option<TranslationActions>,
61}
62
63const TOOLBAR_LIFT: f32 = 22.;
64
65fn separator_pill(label: &str, fill: Hsla, text_color: Hsla) -> Div {
66 h_flex().w_full().justify_center().mt(px(12.)).mb(px(6.)).child(
67 div()
68 .h(px(24.))
69 .px(px(8.))
70 .flex()
71 .items_center()
72 .rounded(px(12.))
73 .bg(fill)
74 .text_size(px(11.))
75 .text_color(text_color)
76 .child(label.to_owned()),
77 )
78}
79
80fn day_separator(label: &str) -> Div {
81 separator_pill(label, theme::row_hover(), theme::text_muted())
82}
83
84fn new_marker() -> Div {
85 separator_pill("New", theme::mention_background(false), theme::accent_text())
86}
87
88fn bubble(
89 row: &MessageRow,
90 index: usize,
91 own: bool,
92 directory: &Directory,
93 extras: BubbleExtras,
94 cx: &App,
95) -> Stateful<Div> {
96 let corners = bubble_corners(own, row.series);
97 let mut element = div()
98 .id(ElementId::Name(format!("bubble-{index}").into()))
99 .when_some(extras.hover.clone(), |element, hover| {
100 element.on_hover(move |is_hovered, _, cx| hover(*is_hovered, cx))
101 })
102 .relative()
103 .rounded_tl(px(corners.top_left))
104 .rounded_tr(px(corners.top_right))
105 .rounded_br(px(corners.bottom_right))
106 .rounded_bl(px(corners.bottom_left))
107 .px(px(12.))
108 .py(px(8.))
109 .text_size(px(BODY_SIZE))
110 .line_height(relative(1.45));
111 if row.deleted {
112 return element
113 .border_1()
114 .border_dashed()
115 .border_color(theme::border_strong())
116 .py(px(6.))
117 .text_size(px(13.))
118 .italic()
119 .text_color(theme::text_muted())
120 .child(DELETED_PREVIEW);
121 }
122 let failed = matches!(row.delivery, Delivery::Failed(_));
123 let scheduled = matches!(row.delivery, Delivery::Scheduled(_));
124 element = element
125 .when(!scheduled, |bubble| {
126 bubble.bg(if own && !failed {
127 theme::bubble_own()
128 } else {
129 theme::bubble_other()
130 })
131 })
132 .when(failed, |bubble| {
133 bubble.border_1().border_color(theme::red())
134 })
135 .when(scheduled, |bubble| {
136 bubble
137 .border_1()
138 .border_dashed()
139 .border_color(if row.delivery == Delivery::Scheduled(ScheduledState::DeliveryFailed) {
140 theme::red()
141 } else {
142 theme::accent()
143 })
144 });
145 let has_reactions = !row.reactions.is_empty();
146 let saved = extras.saved;
147 let mut content = v_flex().gap(px(6.));
148 if row.forwarded {
149 content = content.child(forwarded_header());
150 }
151 let padded_blocks = if has_reactions {
152 None
153 } else {
154 with_meta_room(&row.blocks, meta_room(row, own, saved))
155 };
156 let meta_inline = has_text(row) && padded_blocks.is_some();
157 if has_text(row) {
158 content = content.children(message_body(
159 padded_blocks.as_deref().unwrap_or(&row.blocks),
160 &row.images,
161 &row.local_images,
162 &format!("message-{index}"),
163 own,
164 directory,
165 extras.open_image.as_ref(),
166 cx,
167 ));
168 }
169 content = content.children(attachments_view(
170 &row.blocks,
171 &row.images,
172 &row.local_images,
173 &row.files,
174 &format!("message-{index}"),
175 directory,
176 extras.files.as_ref(),
177 extras.open_image.as_ref(),
178 ));
179 content = content.children(
180 row.meeting_link
181 .clone()
182 .map(|url| meeting_join_chip(format!("message-{index}-join"), url)),
183 );
184 content = content.children(row.link_preview.as_ref().map(|preview| {
185 link_preview_card(
186 preview,
187 format!("message-{index}-link"),
188 directory,
189 false,
190 None,
191 )
192 }));
193 content = content.children(cards_view(
194 &row.adaptive_cards,
195 &row.conversation_id,
196 &row.key,
197 cx,
198 ));
199 if has_reactions {
200 content = content.child(
201 h_flex()
202 .gap(px(8.))
203 .items_end()
204 .child(
205 reaction_pills(
206 &row.reactions,
207 index,
208 own,
209 directory,
210 extras.react.clone(),
211 extras.controls.as_ref(),
212 )
213 .flex_auto(),
214 )
215 .child(
216 h_flex()
217 .h(px(REACTION_FOOTER_HEIGHT))
218 .flex_none()
219 .items_center()
220 .child(bubble_meta(row, own, saved)),
221 ),
222 );
223 } else if !meta_inline {
224 content = content.child(h_flex().justify_end().child(bubble_meta(row, own, saved)));
225 }
226 element = element.child(content);
227 if meta_inline {
228 element = element.child(
229 div()
230 .absolute()
231 .right(px(8.))
232 .bottom(px(4.))
233 .child(bubble_meta(row, own, saved)),
234 );
235 }
236 if let Some(menu) = extras.menu {
237 element = element.child(
238 div()
239 .absolute()
240 .top(px(-TOOLBAR_LIFT))
241 .right(px(8.))
242 .child(deferred(message_toolbar(menu, cx)).with_priority(1)),
243 );
244 }
245 if let Some(menu) = extras.scheduled_menu {
246 element = element.child(
247 div()
248 .absolute()
249 .top(px(-TOOLBAR_LIFT))
250 .right(px(8.))
251 .child(deferred(scheduled_toolbar(menu)).with_priority(1)),
252 );
253 }
254 element
255}
256
257#[derive(Default)]
258struct BubbleExtras {
259 hover: Option<HoverChange>,
260 menu: Option<MessageMenu>,
261 scheduled_menu: Option<ScheduledMenu>,
262 react: Option<PickHandler>,
263 controls: Option<ReactionControls>,
264 files: Option<FileActions>,
265 open_image: Option<ImageOpen>,
266 saved: bool,
267 translation: Option<TranslationActions>,
268}
269
270pub(super) fn forwarded_header() -> Div {
271 h_flex()
272 .gap(px(4.))
273 .items_center()
274 .text_size(px(FORWARDED_SIZE))
275 .text_color(theme::text_muted())
276 .child(icon(IconName::Forward, FORWARDED_SIZE, theme::text_muted()))
277 .child("Forwarded")
278}
279
280fn bubble_meta(row: &MessageRow, own: bool, saved: bool) -> Div {
281 let tint = if own {
282 theme::own_meta()
283 } else {
284 theme::text_muted()
285 };
286 h_flex()
287 .gap(px(4.))
288 .items_center()
289 .text_size(px(META_SIZE))
290 .line_height(px(14.))
291 .italic()
292 .text_color(tint)
293 .when(row.edited, |meta| meta.child("Edited"))
294 .when(saved, |meta| {
295 meta.child(icon(
296 IconName::Bookmark,
297 SAVED_MARK_SIZE,
298 theme::accent_text(),
299 ))
300 })
301 .child(row.time.clone())
302 .when(own && row.delivery == Delivery::Sending, |meta| {
303 meta.child(symbol("schedule", META_CHECK_SIZE, tint))
304 })
305 .when(matches!(row.delivery, Delivery::Scheduled(_)), |meta| {
306 meta.child(symbol("schedule", META_SIZE + 3., tint))
307 })
308 .when(
309 own && row.delivery == Delivery::Delivered,
310 |meta| match row.receipt {
311 Receipt::Hidden => meta,
312 Receipt::Pending => meta.child(receipt_slot(tint)),
313 Receipt::Sent => meta.child(symbol("done", META_CHECK_SIZE, tint)),
314 Receipt::Read => meta.child(symbol("done_all", META_CHECK_SIZE, theme::own_read())),
315 },
316 )
317}
318
319fn pulse<E: IntoElement + Styled + 'static>(id: &'static str, element: E) -> AnimationElement<E> {
320 element.with_animation(
321 id,
322 Animation::new(PULSE_PERIOD)
323 .repeat_synced()
324 .with_easing(pulsating_between(0.35, 1.)),
325 |element, opacity| element.opacity(opacity),
326 )
327}
328
329fn receipt_slot(tint: Hsla) -> Div {
330 h_flex()
331 .size(px(META_CHECK_SIZE))
332 .flex_none()
333 .items_center()
334 .justify_center()
335 .child(pulse(
336 "receipt-slot",
337 div()
338 .w(px(13.))
339 .h(px(4.))
340 .rounded(px(2.))
341 .bg(tint.opacity(0.5)),
342 ))
343}
344
345pub fn render_skeleton_row(skeleton: &Skeleton, index: usize) -> AnyElement {
346 let corners = bubble_corners(skeleton.own, Default::default());
347 let height = 14. + SKELETON_LINE_HEIGHT * f32::from(skeleton.lines);
348 let shape = div()
349 .w(relative(skeleton.width_ratio))
350 .h(px(height))
351 .rounded_tl(px(corners.top_left))
352 .rounded_tr(px(corners.top_right))
353 .rounded_br(px(corners.bottom_right))
354 .rounded_bl(px(corners.bottom_left))
355 .bg(if skeleton.own {
356 theme::bubble_own().opacity(0.55)
357 } else {
358 theme::bubble_other()
359 });
360 let line = h_flex()
361 .w_full()
362 .when(skeleton.own, |line| line.justify_end())
363 .when(!skeleton.own, |line| {
364 line.gap(px(AVATAR_GAP)).child(
365 div()
366 .size(px(AVATAR_SIZE))
367 .flex_none()
368 .rounded_full()
369 .bg(theme::bubble_other()),
370 )
371 })
372 .child(shape);
373 div()
374 .id(ElementId::Name(format!("row-{index}").into()))
375 .w_full()
376 .px(px(20.))
377 .pt(px(SERIES_START_GAP))
378 .child(pulse("skeleton", line))
379 .into_any_element()
380}
381
382fn meta_room(row: &MessageRow, own: bool, saved: bool) -> usize {
383 let mut width = 12. + row.time.chars().count() as f32 * 6.;
384 if saved {
385 width += SAVED_MARK_SIZE + 4.;
386 }
387 if row.edited {
388 width += 56.;
389 }
390 if own {
391 width += META_CHECK_SIZE + 4.;
392 }
393 (width / FIGURE_SPACE_WIDTH).ceil() as usize
394}
395
396fn with_meta_room(blocks: &[Block], room: usize) -> Option<Vec<Block>> {
397 let mut padded = blocks.to_vec();
398 let inline = padded.last_mut()?.last_inline_mut()?;
399 *inline = inline.with_trailing_room(room);
400 Some(padded)
401}
402
403pub(super) fn has_text(row: &MessageRow) -> bool {
404 !row.blocks.is_empty()
405 || (row.images.is_empty()
406 && row.local_images.is_empty()
407 && row.files.is_empty()
408 && row.adaptive_cards.is_empty())
409}
410
411fn delivery_link(index: usize, name: &str, label: &'static str, action: RowAction) -> Stateful<Div> {
412 div()
413 .id(ElementId::Name(format!("{name}-{index}").into()))
414 .cursor_pointer()
415 .text_color(theme::red_tint())
416 .underline()
417 .child(label)
418 .when_some(action, |link, action| {
419 link.on_click(move |_, _, cx| action(cx))
420 })
421}
422
423pub(super) fn delivery_note(
424 row: &MessageRow,
425 delivery: DeliveryActions,
426 index: usize,
427) -> Option<AnyElement> {
428 let DeliveryActions {
429 retry,
430 delete,
431 send_now,
432 } = delivery;
433 let note = |text: &'static str| {
434 h_flex()
435 .gap(px(4.))
436 .text_size(px(11.))
437 .text_color(theme::red_soft())
438 .child(text)
439 };
440 match &row.delivery {
441 Delivery::Delivered | Delivery::Sending | Delivery::Scheduled(ScheduledState::Waiting) => {
442 None
443 }
444 Delivery::Failed(_) => Some(
445 note("Failed to send.")
446 .child(delivery_link(index, "retry", "Retry", retry))
447 .child(delivery_link(index, "delete", "Delete", delete))
448 .into_any_element(),
449 ),
450 Delivery::Scheduled(ScheduledState::DeliveryFailed) => Some(
451 note("Couldn't send at the scheduled time.")
452 .child(delivery_link(index, "send-now", "Send now", send_now))
453 .child(delivery_link(index, "delete", "Delete", delete))
454 .into_any_element(),
455 ),
456 Delivery::Scheduled(ScheduledState::ChangeFailed) => Some(
457 note("Couldn't change the scheduled message.")
458 .child(delivery_link(index, "retry", "Retry", retry))
459 .into_any_element(),
460 ),
461 }
462}
463
464fn others_row(
465 row: &MessageRow,
466 index: usize,
467 directory: &Directory,
468 bot: Option<&BotIdentity>,
469 delivery: DeliveryActions,
470 mut extras: BubbleExtras,
471 cx: &App,
472) -> Div {
473 let translation = extras.translation.take();
474 let first = !row.series.has_prev;
475 let author = bot.map_or_else(|| row.author.clone(), |bot| bot.name.clone());
476 let profile_user = row.sender_id.as_deref().filter(|_| bot.is_none());
477 let mut column = v_flex()
478 .gap(px(2.))
479 .max_w(relative(MAX_WIDTH_RATIO))
480 .items_start();
481 if first {
482 column = column.child(
483 h_flex().gap(px(8.)).items_baseline().pl(px(2.)).child(
484 opens_profile(
485 div().id(ElementId::Name(format!("author-{index}").into())),
486 profile_user,
487 )
488 .text_size(px(12.))
489 .font_weight(FontWeight::SEMIBOLD)
490 .text_color(theme::text_soft())
491 .child(author.clone()),
492 ),
493 );
494 }
495 column = column.child(bubble(row, index, false, directory, extras, cx));
496 column = column.children(translation_line(row, index, translation.as_ref()));
497 if let Some(note) = delivery_note(row, delivery, index) {
498 column = column.child(note);
499 }
500 let lead = if let Some(bot) = bot.filter(|_| first) {
501 bot_avatar(bot, AVATAR_SIZE)
502 } else if first {
503 person_avatar(
504 directory,
505 row.sender_id.as_deref(),
506 &row.author,
507 AVATAR_SIZE,
508 )
509 } else {
510 div().size(px(AVATAR_SIZE)).into_any_element()
511 };
512 h_flex()
513 .w_full()
514 .gap(px(AVATAR_GAP))
515 .items_start()
516 .child(
517 opens_profile(
518 div().id(ElementId::Name(format!("author-avatar-{index}").into())),
519 profile_user.filter(|_| first),
520 )
521 .w(px(AVATAR_SIZE))
522 .flex_none()
523 .child(lead),
524 )
525 .child(column)
526}
527
528fn own_row(
529 row: &MessageRow,
530 index: usize,
531 directory: &Directory,
532 delivery: DeliveryActions,
533 mut extras: BubbleExtras,
534 cx: &App,
535) -> Div {
536 let translation = extras.translation.take();
537 let mut column = v_flex().w_full().items_end().gap(px(2.));
538 column = column.child(
539 div()
540 .max_w(relative(MAX_WIDTH_RATIO))
541 .child(bubble(row, index, true, directory, extras, cx)),
542 );
543 column = column.children(translation_line(row, index, translation.as_ref()));
544 if let Some(note) = delivery_note(row, delivery, index) {
545 column = column.child(note);
546 }
547 column
548}
549
550pub fn render_message_row(
551 row: &MessageRow,
552 index: usize,
553 actions: RowActions,
554 directory: &Directory,
555 cx: &App,
556) -> AnyElement {
557 let RowActions {
558 bot,
559 delivery,
560 reply,
561 hovered,
562 menu,
563 scheduled_menu,
564 react,
565 reaction_controls,
566 files,
567 open_image,
568 highlighted,
569 saved,
570 translation,
571 } = actions;
572 let extras = BubbleExtras {
573 hover: hovered,
574 menu,
575 scheduled_menu,
576 react,
577 controls: reaction_controls,
578 files,
579 open_image,
580 saved,
581 translation,
582 };
583 let own = row.own;
584 let spacing = if row.series.has_prev {
585 px(2.)
586 } else {
587 px(SERIES_START_GAP)
588 };
589 let mut container = v_flex().w_full().px(px(20.));
590 if let Some(day) = &row.day_header {
591 container = container.child(day_separator(day));
592 }
593 if row.new_marker {
594 container = container.child(new_marker());
595 }
596 let body = if own {
597 own_row(row, index, directory, delivery, extras, cx)
598 } else {
599 others_row(row, index, directory, bot.as_ref(), delivery, extras, cx)
600 };
601 container
602 .child(
603 div()
604 .id(ElementId::Name(format!("row-{index}").into()))
605 .w_full()
606 .pt(spacing)
607 .when_some(reply, |element, reply| {
608 element.on_mouse_down(MouseButton::Right, move |_, _, cx| reply(cx))
609 })
610 .child(
611 div()
612 .w_full()
613 .rounded(px(10.))
614 .when(highlighted, |element| {
615 element.bg(theme::accent().opacity(0.2))
616 })
617 .child(body),
618 ),
619 )
620 .into_any_element()
621}