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