BlueGit

ict rusty-teams

Files

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

message_row.rs

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