BlueGit

ict rusty-teams

Files

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

post_card.rs

post_card.rs 19.8 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::*;
5
6use super::adaptive_card::cards_view;
7use super::announcement_banner::announcement_banner;
8use super::attachments::{attachments_view, message_body};
9use super::avatar::{bot_avatar, person_avatar, with_presence};
10use super::call_card::{CallActions, post_call_card};
11use super::call_notice::notice_line;
12use super::link_preview::{link_preview_card, meeting_join_chip};
13use super::message_actions::{add_reaction_pill, message_toolbar};
14use super::message_row::{
15 BODY_SIZE, RowActions, deleted_tombstone, delivery_note, forwarded_header, has_text,
16};
17use super::profile_card::opens_profile;
18use super::reaction_pills::{PILL_HEIGHT, reaction_pills};
19use super::reaction_proposal_pill::reaction_pill;
20use super::scheduled_toolbar::scheduled_toolbar;
21use super::toolbar_placement::placed_toolbar;
22use super::translation_line::translation_line;
23use super::widgets::{icon, symbol};
24use crate::agents::run_chip;
25use crate::data::Directory;
26use crate::rows::{Delivery, MessageRow, PostRow};
27use crate::theme;
28
29const ROOT_AVATAR: f32 = 32.;
30const REPLY_AVATAR: f32 = 20.;
31const REPLY_AVATAR_GAP: f32 = 8.;
32const AVATAR_GAP: f32 = 10.;
33const CARD_PADDING: f32 = 12.;
34const CARD_RADIUS: f32 = 10.;
35const CARD_GAP: f32 = 12.;
36const PINNED_LABEL: &str = "Pinned";
37const SLOTS_PER_POST: usize = 64;
38const TOOLBAR_RIGHT_INSET: f32 = 8.;
39const SUBJECT_SIZE: f32 = 14.;
40const FOOTER_AVATAR: f32 = 20.;
41const FOOTER_BUTTON_PADDING: f32 = 6.;
42const FOOTER_BUTTON_VERTICAL: f32 = 4.;
43const REPLY_IN_THREAD: &str = "Reply in thread";
44const NEW_STRIPE_WIDTH: f32 = 3.;
45const NEW_BADGE_HEIGHT: f32 = 20.;
46const NEW_LABEL: &str = "New";
47const POST_IN_CHANNEL: &str = "Post in channel";
48const POST_LABEL: &str = "Post";
49const ANNOUNCEMENT_LABEL: &str = "Announcement";
50pub const POST_IN_CHANNEL_ANNOUNCE_TAG: &str = "post-in-channel/announcement";
51pub const POST_CARD_TAG: &str = "post-card";
52pub const POST_AVATAR_TAG: &str = "post-card/avatar";
53pub const POST_BODY_TAG: &str = "post-card/body";
54pub const POST_FOOTER_AVATAR_TAG: &str = "post-card/footer-avatar";
55pub const POST_IN_CHANNEL_TAG: &str = "post-in-channel";
56
57pub type PostAction = Box<dyn Fn(&mut Window, &mut App)>;
58
59pub struct PostActions {
60 pub root: RowActions,
61 pub replies: Vec<RowActions>,
62 pub open_thread: Option<PostAction>,
63 pub open_reply: Option<PostAction>,
64 pub reply_editor: Option<AnyElement>,
65}
66
67#[derive(Clone, Copy, PartialEq, Eq)]
68enum Role {
69 Root,
70 Reply,
71}
72
73fn message_index(post_index: usize, slot: usize) -> usize {
74 post_index * SLOTS_PER_POST + slot.min(SLOTS_PER_POST - 1)
75}
76
77fn edited_marker() -> Div {
78 div()
79 .text_size(px(11.))
80 .italic()
81 .text_color(theme::text_muted())
82 .child("Edited")
83}
84
85fn pinned_marker() -> Div {
86 h_flex()
87 .gap(px(3.))
88 .items_center()
89 .text_size(px(11.))
90 .text_color(theme::text_muted())
91 .child(icon(IconName::Pin, 11., theme::text_muted()))
92 .child(PINNED_LABEL)
93}
94
95fn message_content(
96 row: &MessageRow,
97 index: usize,
98 directory: &Directory,
99 actions: &RowActions,
100 cx: &App,
101) -> Div {
102 let mut content = v_flex().gap(px(6.));
103 if row.deleted {
104 return content.child(deleted_tombstone(index, actions.undo_delete.clone()));
105 }
106 if row.forwarded {
107 content = content.child(forwarded_header());
108 }
109 if has_text(row) {
110 content = content.child(
111 v_flex()
112 .gap(px(6.))
113 .text_size(px(BODY_SIZE))
114 .line_height(relative(1.45))
115 .text_color(theme::text_strong())
116 .children(message_body(
117 &row.blocks,
118 &row.images,
119 &row.local_images,
120 &row.sent_previews,
121 &format!("message-{index}"),
122 false,
123 directory,
124 actions.open_image.as_ref(),
125 cx,
126 )),
127 );
128 }
129 content = content.children(attachments_view(
130 &row.blocks,
131 &row.images,
132 &row.local_images,
133 &row.sent_previews,
134 &row.files,
135 &format!("message-{index}"),
136 directory,
137 actions.files.as_ref(),
138 actions.open_image.as_ref(),
139 ));
140 content = content.children(
141 row.meeting_link
142 .clone()
143 .map(|url| meeting_join_chip(format!("message-{index}-join"), url)),
144 );
145 content = content.children(row.link_preview.as_ref().map(|preview| {
146 link_preview_card(
147 preview,
148 format!("message-{index}-link"),
149 directory,
150 false,
151 None,
152 )
153 }));
154 content = content.children(cards_view(
155 &row.adaptive_cards,
156 &row.conversation_id,
157 &row.key,
158 cx,
159 ));
160 if !row.reactions.is_empty() {
161 content = content.child(
162 h_flex()
163 .gap(px(4.))
164 .flex_wrap()
165 .items_center()
166 .child(reaction_pills(
167 &row.reactions,
168 index,
169 false,
170 directory,
171 actions.react.clone(),
172 actions.reaction_controls.as_ref(),
173 ))
174 .children(
175 actions
176 .add_reaction
177 .clone()
178 .map(|adder| add_reaction_pill(adder, PILL_HEIGHT, cx)),
179 ),
180 );
181 }
182 content
183}
184
185fn message_header(
186 row: &MessageRow,
187 index: usize,
188 author: String,
189 profile_user: Option<&str>,
190 saved: bool,
191 pinned: bool,
192) -> Div {
193 h_flex()
194 .gap(px(6.))
195 .items_center()
196 .child(author_name(index, author, profile_user))
197 .child(
198 div()
199 .text_size(px(11.))
200 .text_color(theme::text_muted())
201 .child(row.time.clone()),
202 )
203 .when(row.edited, |line| line.child(edited_marker()))
204 .when(pinned, |line| line.child(pinned_marker()))
205 .when(saved, |line| {
206 line.child(icon(IconName::Bookmark, 12., theme::accent_text()))
207 })
208 .when(row.delivery == Delivery::Sending, |line| {
209 line.child(symbol("schedule", 13., theme::text_muted()))
210 })
211}
212
213fn author_name(index: usize, author: String, profile_user: Option<&str>) -> Stateful<Div> {
214 opens_profile(
215 div().id(ElementId::Name(format!("post-author-{index}").into())),
216 profile_user,
217 )
218 .text_size(px(13.))
219 .font_weight(FontWeight::SEMIBOLD)
220 .text_color(theme::text())
221 .child(author)
222}
223
224fn message_block(
225 row: &MessageRow,
226 index: usize,
227 role: Role,
228 mut actions: RowActions,
229 directory: &Directory,
230 cx: &App,
231) -> Stateful<Div> {
232 let size = if role == Role::Root {
233 ROOT_AVATAR
234 } else {
235 REPLY_AVATAR
236 };
237 let author = actions
238 .bot
239 .as_ref()
240 .map_or_else(|| row.author.clone(), |bot| bot.name.clone());
241 let avatar = match (&actions.bot, row.sender_id.as_deref()) {
242 (Some(bot), _) => bot_avatar(bot, size),
243 (None, Some(sender_id)) if role == Role::Root => with_presence(
244 person_avatar(directory, Some(sender_id), &row.author, size),
245 directory.presence_of(sender_id),
246 size,
247 theme::row_hover(),
248 )
249 .into_any_element(),
250 (None, sender_id) => person_avatar(directory, sender_id, &row.author, size),
251 };
252 if let (Role::Reply, Some(call)) = (role, &row.call) {
253 let recap = std::mem::replace(&mut actions.call, CallActions::none()).recap;
254 return div()
255 .id(ElementId::Name(format!("post-message-{index}").into()))
256 .w_full()
257 .px(px(CARD_PADDING))
258 .pb(px(CARD_PADDING))
259 .bg(theme::reply_surface())
260 .child(notice_line(call, index, directory, recap));
261 }
262 let profile_user = row.sender_id.as_deref().filter(|_| actions.bot.is_none());
263 let content = match &row.call {
264 Some(call) => v_flex().child(post_call_card(
265 call,
266 index,
267 std::mem::replace(&mut actions.call, CallActions::none()),
268 directory,
269 )),
270 None => message_content(row, index, directory, &actions, cx),
271 };
272 let saved = actions.saved;
273 let pinned = actions.pinned && role == Role::Root;
274 let RowActions {
275 delivery,
276 reply,
277 hovered,
278 menu,
279 scheduled_menu,
280 highlighted,
281 translation: translation_actions,
282 timeline,
283 agent_run,
284 reaction_proposal,
285 open_context,
286 context_menu,
287 ..
288 } = actions;
289 let note = delivery_note(row, delivery, index);
290 let translation = translation_line(row, index, translation_actions.as_ref());
291 let subject = row
292 .subject
293 .clone()
294 .filter(|_| role == Role::Root)
295 .map(|subject| {
296 div()
297 .text_size(px(SUBJECT_SIZE))
298 .font_weight(FontWeight::BOLD)
299 .text_color(theme::text())
300 .child(subject)
301 });
302 let banner = row
303 .banner
304 .as_ref()
305 .filter(|_| role == Role::Root && !row.deleted)
306 .map(|banner| announcement_banner(banner, directory));
307 let details = v_flex()
308 .w_full()
309 .min_w_0()
310 .gap(px(6.))
311 .children(banner)
312 .children(subject)
313 .child(content)
314 .children(translation)
315 .children(note)
316 .children(agent_run.map(|run| run_chip(&run, theme::surface_raised())))
317 .children(reaction_proposal.map(|pill| reaction_pill(pill, &row.key)));
318 let avatar = opens_profile(
319 div().id(ElementId::Name(format!("post-avatar-{index}").into())),
320 profile_user,
321 )
322 .w(px(size))
323 .flex_none()
324 .child(avatar);
325 let layout = match role {
326 Role::Root => v_flex()
327 .w_full()
328 .gap(px(8.))
329 .child(
330 h_flex()
331 .items_center()
332 .gap(px(AVATAR_GAP))
333 .child(avatar.debug_selector(|| POST_AVATAR_TAG.into()))
334 .child(message_header(
335 row,
336 index,
337 author,
338 profile_user,
339 saved,
340 pinned,
341 )),
342 )
343 .child(details.debug_selector(|| POST_BODY_TAG.into())),
344 Role::Reply => h_flex()
345 .items_start()
346 .gap(px(REPLY_AVATAR_GAP))
347 .child(avatar)
348 .child(
349 v_flex()
350 .flex_1()
351 .min_w_0()
352 .gap(px(6.))
353 .child(message_header(
354 row,
355 index,
356 author,
357 profile_user,
358 saved,
359 pinned,
360 ))
361 .child(details),
362 ),
363 };
364 div()
365 .id(ElementId::Name(format!("post-message-{index}").into()))
366 .relative()
367 .w_full()
368 .p(px(CARD_PADDING))
369 .when(role == Role::Reply, |block| {
370 block.bg(theme::reply_surface())
371 })
372 .when(highlighted, |block| block.bg(theme::accent().opacity(0.2)))
373 .when_some(hovered, |block, hovered| {
374 block.on_hover(move |is_hovered, _, cx| hovered(*is_hovered, cx))
375 })
376 .map(|block| match (open_context, reply) {
377 (Some(open_context), _) => {
378 block.on_mouse_down(MouseButton::Right, move |event, _, cx| {
379 cx.stop_propagation();
380 open_context(event.position, cx)
381 })
382 }
383 (None, Some(reply)) => {
384 block.on_mouse_down(MouseButton::Right, move |_, _, cx| reply(cx))
385 }
386 (None, None) => block,
387 })
388 .children(context_menu)
389 .child(layout)
390 .when_some(menu, |block, menu| {
391 block.child(placed_toolbar(
392 message_toolbar(menu, cx),
393 timeline.clone(),
394 TOOLBAR_RIGHT_INSET,
395 ))
396 })
397 .when_some(scheduled_menu, |block, menu| {
398 block.child(placed_toolbar(
399 scheduled_toolbar(menu),
400 timeline.clone(),
401 TOOLBAR_RIGHT_INSET,
402 ))
403 })
404}
405
406fn replies_link(label: String, post_index: usize, open: Option<PostAction>) -> Stateful<Div> {
407 div()
408 .id(ElementId::Name(format!("post-replies-{post_index}").into()))
409 .pl(px(CARD_PADDING + REPLY_AVATAR + REPLY_AVATAR_GAP))
410 .pr(px(CARD_PADDING))
411 .pb(px(8.))
412 .text_size(px(13.))
413 .font_weight(FontWeight::SEMIBOLD)
414 .text_color(theme::accent_text())
415 .when_some(open, |link, open| {
416 link.cursor_pointer()
417 .hover(|link| link.text_color(theme::accent_soft()))
418 .on_click(move |_, window, cx| open(window, cx))
419 })
420 .child(label)
421}
422
423fn reply_footer(
424 post_index: usize,
425 open: Option<PostAction>,
426 editor: Option<AnyElement>,
427 directory: &Directory,
428) -> Option<Div> {
429 let footer = v_flex()
430 .w_full()
431 .px(px(CARD_PADDING - FOOTER_BUTTON_PADDING))
432 .rounded_b(px(CARD_RADIUS))
433 .bg(theme::reply_surface());
434 let own_avatar = directory.me.as_ref().map(|me| {
435 div()
436 .debug_selector(|| POST_FOOTER_AVATAR_TAG.into())
437 .flex_none()
438 .child(person_avatar(
439 directory,
440 Some(&me.user_id),
441 &me.display_name,
442 FOOTER_AVATAR,
443 ))
444 });
445 if let Some(editor) = editor {
446 return Some(
447 footer.px(px(CARD_PADDING)).py(px(8.)).child(
448 h_flex()
449 .w_full()
450 .gap(px(8.))
451 .items_center()
452 .children(own_avatar)
453 .child(div().flex_1().min_w_0().child(editor)),
454 ),
455 );
456 }
457 let open = open?;
458 Some(
459 footer.py(px(CARD_PADDING - FOOTER_BUTTON_VERTICAL)).child(
460 h_flex().child(
461 h_flex()
462 .id(ElementId::Name(format!("post-reply-{post_index}").into()))
463 .gap(px(8.))
464 .px(px(FOOTER_BUTTON_PADDING))
465 .py(px(FOOTER_BUTTON_VERTICAL))
466 .items_center()
467 .rounded(px(6.))
468 .cursor_pointer()
469 .text_size(px(13.))
470 .font_weight(FontWeight::SEMIBOLD)
471 .text_color(theme::text_soft())
472 .hover(|button| {
473 button
474 .bg(theme::row_hover())
475 .text_color(theme::text_strong())
476 })
477 .on_click(move |_, window, cx| open(window, cx))
478 .children(own_avatar)
479 .child(REPLY_IN_THREAD),
480 ),
481 ),
482 )
483}
484
485fn card_action(label: &'static str, glyph: IconName, primary: bool) -> Div {
486 let color = if primary {
487 theme::accent_text()
488 } else {
489 theme::text()
490 };
491 h_flex()
492 .h(px(32.))
493 .px(px(12.))
494 .gap(px(6.))
495 .items_center()
496 .rounded(px(6.))
497 .when(primary, |action| action.bg(theme::reply_surface()))
498 .text_size(px(13.))
499 .font_weight(FontWeight::SEMIBOLD)
500 .text_color(color)
501 .child(icon(glyph, 14., color))
502 .child(label)
503}
504
505pub fn post_in_channel_card(
506 directory: &Directory,
507 open: PostAction,
508 announce: PostAction,
509) -> AnyElement {
510 let own_avatar = directory.me.as_ref().map(|me| {
511 div().flex_none().child(person_avatar(
512 directory,
513 Some(&me.user_id),
514 &me.display_name,
515 ROOT_AVATAR,
516 ))
517 });
518 v_flex()
519 .id(POST_IN_CHANNEL_TAG)
520 .debug_selector(|| POST_IN_CHANNEL_TAG.into())
521 .w_full()
522 .p(px(CARD_PADDING))
523 .gap(px(CARD_GAP))
524 .rounded(px(CARD_RADIUS))
525 .bg(theme::row_hover())
526 .cursor_pointer()
527 .on_click(move |_, window, cx| open(window, cx))
528 .child(
529 h_flex()
530 .gap(px(AVATAR_GAP))
531 .items_center()
532 .children(own_avatar)
533 .child(
534 div()
535 .text_size(px(13.))
536 .font_weight(FontWeight::SEMIBOLD)
537 .text_color(theme::text_strong())
538 .child(POST_IN_CHANNEL),
539 ),
540 )
541 .child(
542 h_flex()
543 .gap(px(4.))
544 .child(card_action(POST_LABEL, IconName::MessageSquare, true))
545 .child(
546 card_action(ANNOUNCEMENT_LABEL, IconName::Megaphone, false)
547 .id(POST_IN_CHANNEL_ANNOUNCE_TAG)
548 .debug_selector(|| POST_IN_CHANNEL_ANNOUNCE_TAG.into())
549 .hover(|action| action.bg(theme::reply_surface()))
550 .on_click(move |_, window, cx| {
551 cx.stop_propagation();
552 announce(window, cx);
553 }),
554 ),
555 )
556 .into_any_element()
557}
558
559fn new_post_badge() -> Div {
560 div()
561 .absolute()
562 .top(px(CARD_PADDING + (ROOT_AVATAR - NEW_BADGE_HEIGHT) / 2.))
563 .right(px(CARD_PADDING))
564 .h(px(NEW_BADGE_HEIGHT))
565 .px(px(8.))
566 .flex()
567 .items_center()
568 .rounded_full()
569 .bg(theme::mention_background(false))
570 .text_size(px(11.))
571 .font_weight(FontWeight::SEMIBOLD)
572 .text_color(theme::accent_text())
573 .child(NEW_LABEL)
574}
575
576fn new_post_stripe() -> Div {
577 div()
578 .absolute()
579 .top_0()
580 .bottom_0()
581 .left_0()
582 .w(px(NEW_STRIPE_WIDTH))
583 .rounded_l(px(CARD_RADIUS))
584 .bg(theme::accent())
585}
586
587pub fn render_post(
588 post: &PostRow,
589 index: usize,
590 actions: PostActions,
591 directory: &Directory,
592 cx: &App,
593) -> AnyElement {
594 let PostActions {
595 root,
596 replies,
597 open_thread,
598 open_reply,
599 reply_editor,
600 } = actions;
601 let mut card = v_flex()
602 .debug_selector(|| POST_CARD_TAG.into())
603 .relative()
604 .w_full()
605 .rounded(px(CARD_RADIUS))
606 .bg(theme::row_hover())
607 .child(message_block(
608 &post.root,
609 message_index(index, 0),
610 Role::Root,
611 root,
612 directory,
613 cx,
614 ));
615 if let Some(label) = post.hidden_replies_label() {
616 card = card.child(replies_link(label, index, open_thread));
617 }
618 let has_footer = open_reply.is_some() || reply_editor.is_some();
619 let reply_count = post.replies.len();
620 for (position, (reply, actions)) in post.replies.iter().zip(replies).enumerate() {
621 let closes_card = !has_footer && position + 1 == reply_count;
622 card = card.child(
623 message_block(
624 reply,
625 message_index(index, position + 1),
626 Role::Reply,
627 actions,
628 directory,
629 cx,
630 )
631 .when(closes_card, |block| block.rounded_b(px(CARD_RADIUS))),
632 );
633 }
634 card = card.children(reply_footer(index, open_reply, reply_editor, directory));
635 if post.is_new {
636 card = card.child(new_post_stripe()).child(new_post_badge());
637 }
638 div()
639 .w_full()
640 .px(px(20.))
641 .pt(px(CARD_GAP))
642 .child(card)
643 .into_any_element()
644}