BlueGit

ict rusty-teams

Files

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

post_card.rs

post_card.rs 19.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::*;
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(crate::kvt::message_cards(
155 &row.blocks,
156 &format!("message-{index}"),
157 cx,
158 ));
159 content = content.children(cards_view(
160 &row.adaptive_cards,
161 &row.conversation_id,
162 &row.key,
163 cx,
164 ));
165 if !row.reactions.is_empty() {
166 content = content.child(
167 h_flex()
168 .gap(px(4.))
169 .flex_wrap()
170 .items_center()
171 .child(reaction_pills(
172 &row.reactions,
173 index,
174 false,
175 directory,
176 actions.react.clone(),
177 actions.reaction_controls.as_ref(),
178 ))
179 .children(
180 actions
181 .add_reaction
182 .clone()
183 .map(|adder| add_reaction_pill(adder, PILL_HEIGHT, cx)),
184 ),
185 );
186 }
187 content
188}
189
190fn message_header(
191 row: &MessageRow,
192 index: usize,
193 author: String,
194 profile_user: Option<&str>,
195 saved: bool,
196 pinned: bool,
197) -> Div {
198 h_flex()
199 .gap(px(6.))
200 .items_center()
201 .child(author_name(index, author, profile_user))
202 .child(
203 div()
204 .text_size(px(11.))
205 .text_color(theme::text_muted())
206 .child(row.time.clone()),
207 )
208 .when(row.edited, |line| line.child(edited_marker()))
209 .when(pinned, |line| line.child(pinned_marker()))
210 .when(saved, |line| {
211 line.child(icon(IconName::Bookmark, 12., theme::accent_text()))
212 })
213 .when(row.delivery == Delivery::Sending, |line| {
214 line.child(symbol("schedule", 13., theme::text_muted()))
215 })
216}
217
218fn author_name(index: usize, author: String, profile_user: Option<&str>) -> Stateful<Div> {
219 opens_profile(
220 div().id(ElementId::Name(format!("post-author-{index}").into())),
221 profile_user,
222 )
223 .text_size(px(13.))
224 .font_weight(FontWeight::SEMIBOLD)
225 .text_color(theme::text())
226 .child(author)
227}
228
229fn message_block(
230 row: &MessageRow,
231 index: usize,
232 role: Role,
233 mut actions: RowActions,
234 directory: &Directory,
235 cx: &App,
236) -> Stateful<Div> {
237 let size = if role == Role::Root {
238 ROOT_AVATAR
239 } else {
240 REPLY_AVATAR
241 };
242 let author = actions
243 .bot
244 .as_ref()
245 .map_or_else(|| row.author.clone(), |bot| bot.name.clone());
246 let avatar = match (&actions.bot, row.sender_id.as_deref()) {
247 (Some(bot), _) => bot_avatar(bot, size),
248 (None, Some(sender_id)) if role == Role::Root => with_presence(
249 person_avatar(directory, Some(sender_id), &row.author, size),
250 directory.presence_of(sender_id),
251 size,
252 theme::row_hover(),
253 )
254 .into_any_element(),
255 (None, sender_id) => person_avatar(directory, sender_id, &row.author, size),
256 };
257 if let (Role::Reply, Some(call)) = (role, &row.call) {
258 let recap = std::mem::replace(&mut actions.call, CallActions::none()).recap;
259 return div()
260 .id(ElementId::Name(format!("post-message-{index}").into()))
261 .w_full()
262 .px(px(CARD_PADDING))
263 .pb(px(CARD_PADDING))
264 .bg(theme::reply_surface())
265 .child(notice_line(call, index, directory, recap));
266 }
267 let profile_user = row.sender_id.as_deref().filter(|_| actions.bot.is_none());
268 let content = match &row.call {
269 Some(call) => v_flex().child(post_call_card(
270 call,
271 index,
272 std::mem::replace(&mut actions.call, CallActions::none()),
273 directory,
274 )),
275 None => message_content(row, index, directory, &actions, cx),
276 };
277 let saved = actions.saved;
278 let pinned = actions.pinned && role == Role::Root;
279 let RowActions {
280 delivery,
281 reply,
282 hovered,
283 menu,
284 scheduled_menu,
285 highlighted,
286 translation: translation_actions,
287 timeline,
288 agent_run,
289 reaction_proposal,
290 open_context,
291 context_menu,
292 ..
293 } = actions;
294 let note = delivery_note(row, delivery, index);
295 let translation = translation_line(row, index, translation_actions.as_ref());
296 let subject = row
297 .subject
298 .clone()
299 .filter(|_| role == Role::Root)
300 .map(|subject| {
301 div()
302 .text_size(px(SUBJECT_SIZE))
303 .font_weight(FontWeight::BOLD)
304 .text_color(theme::text())
305 .child(subject)
306 });
307 let banner = row
308 .banner
309 .as_ref()
310 .filter(|_| role == Role::Root && !row.deleted)
311 .map(|banner| announcement_banner(banner, directory));
312 let details = v_flex()
313 .w_full()
314 .min_w_0()
315 .gap(px(6.))
316 .children(banner)
317 .children(subject)
318 .child(content)
319 .children(translation)
320 .children(note)
321 .children(agent_run.map(|run| run_chip(&run, theme::surface_raised())))
322 .children(reaction_proposal.map(|pill| reaction_pill(pill, &row.key)));
323 let avatar = opens_profile(
324 div().id(ElementId::Name(format!("post-avatar-{index}").into())),
325 profile_user,
326 )
327 .w(px(size))
328 .flex_none()
329 .child(avatar);
330 let layout = match role {
331 Role::Root => v_flex()
332 .w_full()
333 .gap(px(8.))
334 .child(
335 h_flex()
336 .items_center()
337 .gap(px(AVATAR_GAP))
338 .child(avatar.debug_selector(|| POST_AVATAR_TAG.into()))
339 .child(message_header(
340 row,
341 index,
342 author,
343 profile_user,
344 saved,
345 pinned,
346 )),
347 )
348 .child(details.debug_selector(|| POST_BODY_TAG.into())),
349 Role::Reply => h_flex()
350 .items_start()
351 .gap(px(REPLY_AVATAR_GAP))
352 .child(avatar)
353 .child(
354 v_flex()
355 .flex_1()
356 .min_w_0()
357 .gap(px(6.))
358 .child(message_header(
359 row,
360 index,
361 author,
362 profile_user,
363 saved,
364 pinned,
365 ))
366 .child(details),
367 ),
368 };
369 div()
370 .id(ElementId::Name(format!("post-message-{index}").into()))
371 .relative()
372 .w_full()
373 .p(px(CARD_PADDING))
374 .when(role == Role::Reply, |block| {
375 block.bg(theme::reply_surface())
376 })
377 .when(highlighted, |block| block.bg(theme::accent().opacity(0.2)))
378 .when_some(hovered, |block, hovered| {
379 block.on_hover(move |is_hovered, _, cx| hovered(*is_hovered, cx))
380 })
381 .map(|block| match (open_context, reply) {
382 (Some(open_context), _) => {
383 block.on_mouse_down(MouseButton::Right, move |event, _, cx| {
384 cx.stop_propagation();
385 open_context(event.position, cx)
386 })
387 }
388 (None, Some(reply)) => {
389 block.on_mouse_down(MouseButton::Right, move |_, _, cx| reply(cx))
390 }
391 (None, None) => block,
392 })
393 .children(context_menu)
394 .child(layout)
395 .when_some(menu, |block, menu| {
396 block.child(placed_toolbar(
397 message_toolbar(menu, cx),
398 timeline.clone(),
399 TOOLBAR_RIGHT_INSET,
400 ))
401 })
402 .when_some(scheduled_menu, |block, menu| {
403 block.child(placed_toolbar(
404 scheduled_toolbar(menu),
405 timeline.clone(),
406 TOOLBAR_RIGHT_INSET,
407 ))
408 })
409}
410
411fn replies_link(label: String, post_index: usize, open: Option<PostAction>) -> Stateful<Div> {
412 div()
413 .id(ElementId::Name(format!("post-replies-{post_index}").into()))
414 .pl(px(CARD_PADDING + REPLY_AVATAR + REPLY_AVATAR_GAP))
415 .pr(px(CARD_PADDING))
416 .pb(px(8.))
417 .text_size(px(13.))
418 .font_weight(FontWeight::SEMIBOLD)
419 .text_color(theme::accent_text())
420 .when_some(open, |link, open| {
421 link.cursor_pointer()
422 .hover(|link| link.text_color(theme::accent_soft()))
423 .on_click(move |_, window, cx| open(window, cx))
424 })
425 .child(label)
426}
427
428fn reply_footer(
429 post_index: usize,
430 open: Option<PostAction>,
431 editor: Option<AnyElement>,
432 directory: &Directory,
433) -> Option<Div> {
434 let footer = v_flex()
435 .w_full()
436 .px(px(CARD_PADDING - FOOTER_BUTTON_PADDING))
437 .rounded_b(px(CARD_RADIUS))
438 .bg(theme::reply_surface());
439 let own_avatar = directory.me.as_ref().map(|me| {
440 div()
441 .debug_selector(|| POST_FOOTER_AVATAR_TAG.into())
442 .flex_none()
443 .child(person_avatar(
444 directory,
445 Some(&me.user_id),
446 &me.display_name,
447 FOOTER_AVATAR,
448 ))
449 });
450 if let Some(editor) = editor {
451 return Some(
452 footer.px(px(CARD_PADDING)).py(px(8.)).child(
453 h_flex()
454 .w_full()
455 .gap(px(8.))
456 .items_center()
457 .children(own_avatar)
458 .child(div().flex_1().min_w_0().child(editor)),
459 ),
460 );
461 }
462 let open = open?;
463 Some(
464 footer.py(px(CARD_PADDING - FOOTER_BUTTON_VERTICAL)).child(
465 h_flex().child(
466 h_flex()
467 .id(ElementId::Name(format!("post-reply-{post_index}").into()))
468 .gap(px(8.))
469 .px(px(FOOTER_BUTTON_PADDING))
470 .py(px(FOOTER_BUTTON_VERTICAL))
471 .items_center()
472 .rounded(px(6.))
473 .cursor_pointer()
474 .text_size(px(13.))
475 .font_weight(FontWeight::SEMIBOLD)
476 .text_color(theme::text_soft())
477 .hover(|button| {
478 button
479 .bg(theme::row_hover())
480 .text_color(theme::text_strong())
481 })
482 .on_click(move |_, window, cx| open(window, cx))
483 .children(own_avatar)
484 .child(REPLY_IN_THREAD),
485 ),
486 ),
487 )
488}
489
490fn card_action(label: &'static str, glyph: IconName, primary: bool) -> Div {
491 let color = if primary {
492 theme::accent_text()
493 } else {
494 theme::text()
495 };
496 h_flex()
497 .h(px(32.))
498 .px(px(12.))
499 .gap(px(6.))
500 .items_center()
501 .rounded(px(6.))
502 .when(primary, |action| action.bg(theme::reply_surface()))
503 .text_size(px(13.))
504 .font_weight(FontWeight::SEMIBOLD)
505 .text_color(color)
506 .child(icon(glyph, 14., color))
507 .child(label)
508}
509
510pub fn post_in_channel_card(
511 directory: &Directory,
512 open: PostAction,
513 announce: PostAction,
514) -> AnyElement {
515 let own_avatar = directory.me.as_ref().map(|me| {
516 div().flex_none().child(person_avatar(
517 directory,
518 Some(&me.user_id),
519 &me.display_name,
520 ROOT_AVATAR,
521 ))
522 });
523 v_flex()
524 .id(POST_IN_CHANNEL_TAG)
525 .debug_selector(|| POST_IN_CHANNEL_TAG.into())
526 .w_full()
527 .p(px(CARD_PADDING))
528 .gap(px(CARD_GAP))
529 .rounded(px(CARD_RADIUS))
530 .bg(theme::row_hover())
531 .cursor_pointer()
532 .on_click(move |_, window, cx| open(window, cx))
533 .child(
534 h_flex()
535 .gap(px(AVATAR_GAP))
536 .items_center()
537 .children(own_avatar)
538 .child(
539 div()
540 .text_size(px(13.))
541 .font_weight(FontWeight::SEMIBOLD)
542 .text_color(theme::text_strong())
543 .child(POST_IN_CHANNEL),
544 ),
545 )
546 .child(
547 h_flex()
548 .gap(px(4.))
549 .child(card_action(POST_LABEL, IconName::MessageSquare, true))
550 .child(
551 card_action(ANNOUNCEMENT_LABEL, IconName::Megaphone, false)
552 .id(POST_IN_CHANNEL_ANNOUNCE_TAG)
553 .debug_selector(|| POST_IN_CHANNEL_ANNOUNCE_TAG.into())
554 .hover(|action| action.bg(theme::reply_surface()))
555 .on_click(move |_, window, cx| {
556 cx.stop_propagation();
557 announce(window, cx);
558 }),
559 ),
560 )
561 .into_any_element()
562}
563
564fn new_post_badge() -> Div {
565 div()
566 .absolute()
567 .top(px(CARD_PADDING + (ROOT_AVATAR - NEW_BADGE_HEIGHT) / 2.))
568 .right(px(CARD_PADDING))
569 .h(px(NEW_BADGE_HEIGHT))
570 .px(px(8.))
571 .flex()
572 .items_center()
573 .rounded_full()
574 .bg(theme::mention_background(false))
575 .text_size(px(11.))
576 .font_weight(FontWeight::SEMIBOLD)
577 .text_color(theme::accent_text())
578 .child(NEW_LABEL)
579}
580
581fn new_post_stripe() -> Div {
582 div()
583 .absolute()
584 .top_0()
585 .bottom_0()
586 .left_0()
587 .w(px(NEW_STRIPE_WIDTH))
588 .rounded_l(px(CARD_RADIUS))
589 .bg(theme::accent())
590}
591
592pub fn render_post(
593 post: &PostRow,
594 index: usize,
595 actions: PostActions,
596 directory: &Directory,
597 cx: &App,
598) -> AnyElement {
599 let PostActions {
600 root,
601 replies,
602 open_thread,
603 open_reply,
604 reply_editor,
605 } = actions;
606 let mut card = v_flex()
607 .debug_selector(|| POST_CARD_TAG.into())
608 .relative()
609 .w_full()
610 .rounded(px(CARD_RADIUS))
611 .bg(theme::row_hover())
612 .child(message_block(
613 &post.root,
614 message_index(index, 0),
615 Role::Root,
616 root,
617 directory,
618 cx,
619 ));
620 if let Some(label) = post.hidden_replies_label() {
621 card = card.child(replies_link(label, index, open_thread));
622 }
623 let has_footer = open_reply.is_some() || reply_editor.is_some();
624 let reply_count = post.replies.len();
625 for (position, (reply, actions)) in post.replies.iter().zip(replies).enumerate() {
626 let closes_card = !has_footer && position + 1 == reply_count;
627 card = card.child(
628 message_block(
629 reply,
630 message_index(index, position + 1),
631 Role::Reply,
632 actions,
633 directory,
634 cx,
635 )
636 .when(closes_card, |block| block.rounded_b(px(CARD_RADIUS))),
637 );
638 }
639 card = card.children(reply_footer(index, open_reply, reply_editor, directory));
640 if post.is_new {
641 card = card.child(new_post_stripe()).child(new_post_badge());
642 }
643 div()
644 .w_full()
645 .px(px(20.))
646 .pt(px(CARD_GAP))
647 .child(card)
648 .into_any_element()
649}