BlueGit

ict rusty-teams

Files

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

post_card.rs

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