BlueGit

ict rusty-teams

Files

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

message_row.rs

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