BlueGit

ict rusty-teams

Files

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

post_card.rs

chore: cargo fmt the workspace
metis Daniel Däschle · pushed by metis · 2 weeks ago
a6592765 History
post_card.rs 19.4 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 ..
286 } = actions;
287 let note = delivery_note(row, delivery, index);
288 let translation = translation_line(row, index, translation_actions.as_ref());
289 let subject = row
290 .subject
291 .clone()
292 .filter(|_| role == Role::Root)
293 .map(|subject| {
294 div()
295 .text_size(px(SUBJECT_SIZE))
296 .font_weight(FontWeight::BOLD)
297 .text_color(theme::text())
298 .child(subject)
299 });
300 let banner = row
301 .banner
302 .as_ref()
303 .filter(|_| role == Role::Root && !row.deleted)
304 .map(|banner| announcement_banner(banner, directory));
305 let details = v_flex()
306 .w_full()
307 .min_w_0()
308 .gap(px(6.))
309 .children(banner)
310 .children(subject)
311 .child(content)
312 .children(translation)
313 .children(note)
314 .children(agent_run.map(|run| run_chip(&run, theme::surface_raised())))
315 .children(reaction_proposal.map(|pill| reaction_pill(pill, &row.key)));
316 let avatar = opens_profile(
317 div().id(ElementId::Name(format!("post-avatar-{index}").into())),
318 profile_user,
319 )
320 .w(px(size))
321 .flex_none()
322 .child(avatar);
323 let layout = match role {
324 Role::Root => v_flex()
325 .w_full()
326 .gap(px(8.))
327 .child(
328 h_flex()
329 .items_center()
330 .gap(px(AVATAR_GAP))
331 .child(avatar.debug_selector(|| POST_AVATAR_TAG.into()))
332 .child(message_header(
333 row,
334 index,
335 author,
336 profile_user,
337 saved,
338 pinned,
339 )),
340 )
341 .child(details.debug_selector(|| POST_BODY_TAG.into())),
342 Role::Reply => h_flex()
343 .items_start()
344 .gap(px(REPLY_AVATAR_GAP))
345 .child(avatar)
346 .child(
347 v_flex()
348 .flex_1()
349 .min_w_0()
350 .gap(px(6.))
351 .child(message_header(
352 row,
353 index,
354 author,
355 profile_user,
356 saved,
357 pinned,
358 ))
359 .child(details),
360 ),
361 };
362 div()
363 .id(ElementId::Name(format!("post-message-{index}").into()))
364 .relative()
365 .w_full()
366 .p(px(CARD_PADDING))
367 .when(role == Role::Reply, |block| {
368 block.bg(theme::reply_surface())
369 })
370 .when(highlighted, |block| block.bg(theme::accent().opacity(0.2)))
371 .when_some(hovered, |block, hovered| {
372 block.on_hover(move |is_hovered, _, cx| hovered(*is_hovered, cx))
373 })
374 .when_some(reply, |block, reply| {
375 block.on_mouse_down(MouseButton::Right, move |_, _, cx| reply(cx))
376 })
377 .child(layout)
378 .when_some(menu, |block, menu| {
379 block.child(placed_toolbar(
380 message_toolbar(menu, cx),
381 timeline.clone(),
382 TOOLBAR_RIGHT_INSET,
383 ))
384 })
385 .when_some(scheduled_menu, |block, menu| {
386 block.child(placed_toolbar(
387 scheduled_toolbar(menu),
388 timeline.clone(),
389 TOOLBAR_RIGHT_INSET,
390 ))
391 })
392}
393
394fn replies_link(label: String, post_index: usize, open: Option<PostAction>) -> Stateful<Div> {
395 div()
396 .id(ElementId::Name(format!("post-replies-{post_index}").into()))
397 .pl(px(CARD_PADDING + REPLY_AVATAR + REPLY_AVATAR_GAP))
398 .pr(px(CARD_PADDING))
399 .pb(px(8.))
400 .text_size(px(13.))
401 .font_weight(FontWeight::SEMIBOLD)
402 .text_color(theme::accent_text())
403 .when_some(open, |link, open| {
404 link.cursor_pointer()
405 .hover(|link| link.text_color(theme::accent_soft()))
406 .on_click(move |_, window, cx| open(window, cx))
407 })
408 .child(label)
409}
410
411fn reply_footer(
412 post_index: usize,
413 open: Option<PostAction>,
414 editor: Option<AnyElement>,
415 directory: &Directory,
416) -> Option<Div> {
417 let footer = v_flex()
418 .w_full()
419 .px(px(CARD_PADDING - FOOTER_BUTTON_PADDING))
420 .rounded_b(px(CARD_RADIUS))
421 .bg(theme::reply_surface());
422 let own_avatar = directory.me.as_ref().map(|me| {
423 div()
424 .debug_selector(|| POST_FOOTER_AVATAR_TAG.into())
425 .flex_none()
426 .child(person_avatar(
427 directory,
428 Some(&me.user_id),
429 &me.display_name,
430 FOOTER_AVATAR,
431 ))
432 });
433 if let Some(editor) = editor {
434 return Some(
435 footer.px(px(CARD_PADDING)).py(px(8.)).child(
436 h_flex()
437 .w_full()
438 .gap(px(8.))
439 .items_center()
440 .children(own_avatar)
441 .child(div().flex_1().min_w_0().child(editor)),
442 ),
443 );
444 }
445 let open = open?;
446 Some(
447 footer.py(px(CARD_PADDING - FOOTER_BUTTON_VERTICAL)).child(
448 h_flex().child(
449 h_flex()
450 .id(ElementId::Name(format!("post-reply-{post_index}").into()))
451 .gap(px(8.))
452 .px(px(FOOTER_BUTTON_PADDING))
453 .py(px(FOOTER_BUTTON_VERTICAL))
454 .items_center()
455 .rounded(px(6.))
456 .cursor_pointer()
457 .text_size(px(13.))
458 .font_weight(FontWeight::SEMIBOLD)
459 .text_color(theme::text_soft())
460 .hover(|button| {
461 button
462 .bg(theme::row_hover())
463 .text_color(theme::text_strong())
464 })
465 .on_click(move |_, window, cx| open(window, cx))
466 .children(own_avatar)
467 .child(REPLY_IN_THREAD),
468 ),
469 ),
470 )
471}
472
473fn card_action(label: &'static str, glyph: IconName, primary: bool) -> Div {
474 let color = if primary {
475 theme::accent_text()
476 } else {
477 theme::text()
478 };
479 h_flex()
480 .h(px(32.))
481 .px(px(12.))
482 .gap(px(6.))
483 .items_center()
484 .rounded(px(6.))
485 .when(primary, |action| action.bg(theme::reply_surface()))
486 .text_size(px(13.))
487 .font_weight(FontWeight::SEMIBOLD)
488 .text_color(color)
489 .child(icon(glyph, 14., color))
490 .child(label)
491}
492
493pub fn post_in_channel_card(
494 directory: &Directory,
495 open: PostAction,
496 announce: PostAction,
497) -> AnyElement {
498 let own_avatar = directory.me.as_ref().map(|me| {
499 div().flex_none().child(person_avatar(
500 directory,
501 Some(&me.user_id),
502 &me.display_name,
503 ROOT_AVATAR,
504 ))
505 });
506 v_flex()
507 .id(POST_IN_CHANNEL_TAG)
508 .debug_selector(|| POST_IN_CHANNEL_TAG.into())
509 .w_full()
510 .p(px(CARD_PADDING))
511 .gap(px(CARD_GAP))
512 .rounded(px(CARD_RADIUS))
513 .bg(theme::row_hover())
514 .cursor_pointer()
515 .on_click(move |_, window, cx| open(window, cx))
516 .child(
517 h_flex()
518 .gap(px(AVATAR_GAP))
519 .items_center()
520 .children(own_avatar)
521 .child(
522 div()
523 .text_size(px(13.))
524 .font_weight(FontWeight::SEMIBOLD)
525 .text_color(theme::text_strong())
526 .child(POST_IN_CHANNEL),
527 ),
528 )
529 .child(
530 h_flex()
531 .gap(px(4.))
532 .child(card_action(POST_LABEL, IconName::MessageSquare, true))
533 .child(
534 card_action(ANNOUNCEMENT_LABEL, IconName::Megaphone, false)
535 .id(POST_IN_CHANNEL_ANNOUNCE_TAG)
536 .debug_selector(|| POST_IN_CHANNEL_ANNOUNCE_TAG.into())
537 .hover(|action| action.bg(theme::reply_surface()))
538 .on_click(move |_, window, cx| {
539 cx.stop_propagation();
540 announce(window, cx);
541 }),
542 ),
543 )
544 .into_any_element()
545}
546
547fn new_post_badge() -> Div {
548 div()
549 .absolute()
550 .top(px(CARD_PADDING + (ROOT_AVATAR - NEW_BADGE_HEIGHT) / 2.))
551 .right(px(CARD_PADDING))
552 .h(px(NEW_BADGE_HEIGHT))
553 .px(px(8.))
554 .flex()
555 .items_center()
556 .rounded_full()
557 .bg(theme::mention_background(false))
558 .text_size(px(11.))
559 .font_weight(FontWeight::SEMIBOLD)
560 .text_color(theme::accent_text())
561 .child(NEW_LABEL)
562}
563
564fn new_post_stripe() -> Div {
565 div()
566 .absolute()
567 .top_0()
568 .bottom_0()
569 .left_0()
570 .w(px(NEW_STRIPE_WIDTH))
571 .rounded_l(px(CARD_RADIUS))
572 .bg(theme::accent())
573}
574
575pub fn render_post(
576 post: &PostRow,
577 index: usize,
578 actions: PostActions,
579 directory: &Directory,
580 cx: &App,
581) -> AnyElement {
582 let PostActions {
583 root,
584 replies,
585 open_thread,
586 open_reply,
587 reply_editor,
588 } = actions;
589 let mut card = v_flex()
590 .debug_selector(|| POST_CARD_TAG.into())
591 .relative()
592 .w_full()
593 .rounded(px(CARD_RADIUS))
594 .bg(theme::row_hover())
595 .child(message_block(
596 &post.root,
597 message_index(index, 0),
598 Role::Root,
599 root,
600 directory,
601 cx,
602 ));
603 if let Some(label) = post.hidden_replies_label() {
604 card = card.child(replies_link(label, index, open_thread));
605 }
606 let has_footer = open_reply.is_some() || reply_editor.is_some();
607 let reply_count = post.replies.len();
608 for (position, (reply, actions)) in post.replies.iter().zip(replies).enumerate() {
609 let closes_card = !has_footer && position + 1 == reply_count;
610 card = card.child(
611 message_block(
612 reply,
613 message_index(index, position + 1),
614 Role::Reply,
615 actions,
616 directory,
617 cx,
618 )
619 .when(closes_card, |block| block.rounded_b(px(CARD_RADIUS))),
620 );
621 }
622 card = card.children(reply_footer(index, open_reply, reply_editor, directory));
623 if post.is_new {
624 card = card.child(new_post_stripe()).child(new_post_badge());
625 }
626 div()
627 .w_full()
628 .px(px(20.))
629 .pt(px(CARD_GAP))
630 .child(card)
631 .into_any_element()
632}