BlueGit

ict rusty-teams

Files

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

message_row.rs

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