BlueGit

ict rusty-teams

Files

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

message_row.rs

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