BlueGit

ict rusty-teams

Files

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

post_card.rs

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