BlueGit

ict rusty-teams

Files

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

post_card.rs

post_card.rs 18.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, PillLook, 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 = 24.;
31const AVATAR_GAP: f32 = 10.;
32const CARD_PADDING: f32 = 12.;
33const CARD_RADIUS: f32 = 10.;
34const CARD_GAP: f32 = 12.;
35const PINNED_LABEL: &str = "Pinned";
36const SLOTS_PER_POST: usize = 64;
37const TOOLBAR_RIGHT_INSET: f32 = 8.;
38const SUBJECT_SIZE: f32 = 14.;
39const FOOTER_AVATAR: f32 = 20.;
40const FOOTER_BUTTON_PADDING: f32 = 6.;
41const FOOTER_BUTTON_VERTICAL: f32 = 4.;
42const REPLY_IN_THREAD: &str = "Reply in thread";
43const NEW_STRIPE_WIDTH: f32 = 3.;
44const NEW_BADGE_HEIGHT: f32 = 20.;
45const NEW_LABEL: &str = "New";
46const POST_IN_CHANNEL: &str = "Post in channel";
47const POST_LABEL: &str = "Post";
48pub const POST_CARD_TAG: &str = "post-card";
49pub const POST_AVATAR_TAG: &str = "post-card/avatar";
50pub const POST_BODY_TAG: &str = "post-card/body";
51pub const POST_FOOTER_AVATAR_TAG: &str = "post-card/footer-avatar";
52pub const POST_IN_CHANNEL_TAG: &str = "post-in-channel";
53
54pub type PostAction = Box<dyn Fn(&mut Window, &mut App)>;
55
56pub struct PostActions {
57 pub root: RowActions,
58 pub replies: Vec<RowActions>,
59 pub open_thread: Option<PostAction>,
60 pub open_reply: Option<PostAction>,
61 pub reply_editor: Option<AnyElement>,
62}
63
64#[derive(Clone, Copy, PartialEq, Eq)]
65enum Role {
66 Root,
67 Reply,
68}
69
70fn message_index(post_index: usize, slot: usize) -> usize {
71 post_index * SLOTS_PER_POST + slot.min(SLOTS_PER_POST - 1)
72}
73
74fn edited_marker() -> Div {
75 div()
76 .text_size(px(11.))
77 .italic()
78 .text_color(theme::text_muted())
79 .child("Edited")
80}
81
82fn pinned_marker() -> Div {
83 h_flex()
84 .gap(px(3.))
85 .items_center()
86 .text_size(px(11.))
87 .text_color(theme::text_muted())
88 .child(icon(IconName::Pin, 11., theme::text_muted()))
89 .child(PINNED_LABEL)
90}
91
92fn message_content(
93 row: &MessageRow,
94 index: usize,
95 directory: &Directory,
96 actions: &RowActions,
97 cx: &App,
98) -> Div {
99 let mut content = v_flex().gap(px(6.));
100 if row.deleted {
101 return content.child(deleted_tombstone(index, actions.undo_delete.clone()));
102 }
103 if row.forwarded {
104 content = content.child(forwarded_header());
105 }
106 if has_text(row) {
107 content = content.child(
108 v_flex()
109 .gap(px(6.))
110 .text_size(px(BODY_SIZE))
111 .line_height(relative(1.45))
112 .text_color(theme::text_strong())
113 .children(message_body(
114 &row.blocks,
115 &row.images,
116 &row.local_images,
117 &format!("message-{index}"),
118 false,
119 directory,
120 actions.open_image.as_ref(),
121 cx,
122 )),
123 );
124 }
125 content = content.children(attachments_view(
126 &row.blocks,
127 &row.images,
128 &row.local_images,
129 &row.files,
130 &format!("message-{index}"),
131 directory,
132 actions.files.as_ref(),
133 actions.open_image.as_ref(),
134 ));
135 content = content.children(
136 row.meeting_link
137 .clone()
138 .map(|url| meeting_join_chip(format!("message-{index}-join"), url)),
139 );
140 content = content.children(row.link_preview.as_ref().map(|preview| {
141 link_preview_card(
142 preview,
143 format!("message-{index}-link"),
144 directory,
145 false,
146 None,
147 )
148 }));
149 content = content.children(cards_view(
150 &row.adaptive_cards,
151 &row.conversation_id,
152 &row.key,
153 cx,
154 ));
155 if !row.reactions.is_empty() {
156 content = content.child(
157 h_flex()
158 .gap(px(4.))
159 .flex_wrap()
160 .items_center()
161 .child(reaction_pills(
162 &row.reactions,
163 index,
164 false,
165 PillLook::Count,
166 directory,
167 actions.react.clone(),
168 actions.reaction_controls.as_ref(),
169 ))
170 .children(
171 actions
172 .add_reaction
173 .clone()
174 .map(|adder| add_reaction_pill(adder, PILL_HEIGHT, cx)),
175 ),
176 );
177 }
178 content
179}
180
181fn message_header(
182 row: &MessageRow,
183 index: usize,
184 author: String,
185 profile_user: Option<&str>,
186 saved: bool,
187 pinned: bool,
188) -> Div {
189 h_flex()
190 .gap(px(6.))
191 .items_center()
192 .child(author_name(index, author, profile_user))
193 .child(
194 div()
195 .text_size(px(11.))
196 .text_color(theme::text_muted())
197 .child(row.time.clone()),
198 )
199 .when(row.edited, |line| line.child(edited_marker()))
200 .when(pinned, |line| line.child(pinned_marker()))
201 .when(saved, |line| {
202 line.child(icon(IconName::Bookmark, 12., theme::accent_text()))
203 })
204 .when(row.delivery == Delivery::Sending, |line| {
205 line.child(symbol("schedule", 13., theme::text_muted()))
206 })
207}
208
209fn author_name(index: usize, author: String, profile_user: Option<&str>) -> Stateful<Div> {
210 opens_profile(
211 div().id(ElementId::Name(format!("post-author-{index}").into())),
212 profile_user,
213 )
214 .text_size(px(13.))
215 .font_weight(FontWeight::SEMIBOLD)
216 .text_color(theme::text())
217 .child(author)
218}
219
220fn message_block(
221 row: &MessageRow,
222 index: usize,
223 role: Role,
224 mut actions: RowActions,
225 directory: &Directory,
226 cx: &App,
227) -> Stateful<Div> {
228 let size = if role == Role::Root {
229 ROOT_AVATAR
230 } else {
231 REPLY_AVATAR
232 };
233 let author = actions
234 .bot
235 .as_ref()
236 .map_or_else(|| row.author.clone(), |bot| bot.name.clone());
237 let avatar = match (&actions.bot, row.sender_id.as_deref()) {
238 (Some(bot), _) => bot_avatar(bot, size),
239 (None, Some(sender_id)) if role == Role::Root => with_presence(
240 person_avatar(directory, Some(sender_id), &row.author, size),
241 directory.presence_of(sender_id),
242 size,
243 theme::row_hover(),
244 )
245 .into_any_element(),
246 (None, sender_id) => person_avatar(directory, sender_id, &row.author, size),
247 };
248 if let (Role::Reply, Some(call)) = (role, &row.call)
249 && let Some(notice) = &call.notice
250 {
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, notice, 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(8.)).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| {
355 block
356 .pl(px(CARD_PADDING + ROOT_AVATAR + AVATAR_GAP))
357 .bg(theme::reply_surface())
358 })
359 .when(highlighted, |block| block.bg(theme::accent().opacity(0.2)))
360 .when_some(hovered, |block, hovered| {
361 block.on_hover(move |is_hovered, _, cx| hovered(*is_hovered, cx))
362 })
363 .when_some(reply, |block, reply| {
364 block.on_mouse_down(MouseButton::Right, move |_, _, cx| reply(cx))
365 })
366 .child(layout)
367 .when_some(menu, |block, menu| {
368 block.child(placed_toolbar(
369 message_toolbar(menu, cx),
370 timeline.clone(),
371 TOOLBAR_RIGHT_INSET,
372 ))
373 })
374 .when_some(scheduled_menu, |block, menu| {
375 block.child(placed_toolbar(
376 scheduled_toolbar(menu),
377 timeline.clone(),
378 TOOLBAR_RIGHT_INSET,
379 ))
380 })
381}
382
383fn replies_link(label: String, post_index: usize, open: Option<PostAction>) -> Stateful<Div> {
384 div()
385 .id(ElementId::Name(format!("post-replies-{post_index}").into()))
386 .pl(px(CARD_PADDING + ROOT_AVATAR + AVATAR_GAP))
387 .pr(px(CARD_PADDING))
388 .pb(px(8.))
389 .text_size(px(13.))
390 .font_weight(FontWeight::SEMIBOLD)
391 .text_color(theme::accent_text())
392 .when_some(open, |link, open| {
393 link.cursor_pointer()
394 .hover(|link| link.text_color(theme::accent_soft()))
395 .on_click(move |_, window, cx| open(window, cx))
396 })
397 .child(label)
398}
399
400fn reply_footer(
401 post_index: usize,
402 open: Option<PostAction>,
403 editor: Option<AnyElement>,
404 directory: &Directory,
405) -> Option<Div> {
406 let footer = v_flex()
407 .w_full()
408 .px(px(CARD_PADDING - FOOTER_BUTTON_PADDING))
409 .rounded_b(px(CARD_RADIUS))
410 .bg(theme::reply_surface());
411 if let Some(editor) = editor {
412 return Some(footer.px(px(CARD_PADDING)).py(px(8.)).child(editor));
413 }
414 let open = open?;
415 let own_avatar = directory.me.as_ref().map(|me| {
416 div()
417 .debug_selector(|| POST_FOOTER_AVATAR_TAG.into())
418 .flex_none()
419 .child(person_avatar(
420 directory,
421 Some(&me.user_id),
422 &me.display_name,
423 FOOTER_AVATAR,
424 ))
425 });
426 Some(
427 footer.py(px(CARD_PADDING - FOOTER_BUTTON_VERTICAL)).child(
428 h_flex().child(
429 h_flex()
430 .id(ElementId::Name(format!("post-reply-{post_index}").into()))
431 .gap(px(8.))
432 .px(px(FOOTER_BUTTON_PADDING))
433 .py(px(FOOTER_BUTTON_VERTICAL))
434 .items_center()
435 .rounded(px(6.))
436 .cursor_pointer()
437 .text_size(px(13.))
438 .font_weight(FontWeight::SEMIBOLD)
439 .text_color(theme::text_soft())
440 .hover(|button| {
441 button
442 .bg(theme::row_hover())
443 .text_color(theme::text_strong())
444 })
445 .on_click(move |_, window, cx| open(window, cx))
446 .children(own_avatar)
447 .child(REPLY_IN_THREAD),
448 ),
449 ),
450 )
451}
452
453pub fn post_in_channel_card(directory: &Directory, open: PostAction) -> AnyElement {
454 let own_avatar = directory.me.as_ref().map(|me| {
455 div().flex_none().child(person_avatar(
456 directory,
457 Some(&me.user_id),
458 &me.display_name,
459 ROOT_AVATAR,
460 ))
461 });
462 v_flex()
463 .id(POST_IN_CHANNEL_TAG)
464 .debug_selector(|| POST_IN_CHANNEL_TAG.into())
465 .w_full()
466 .p(px(CARD_PADDING))
467 .gap(px(CARD_GAP))
468 .rounded(px(CARD_RADIUS))
469 .bg(theme::row_hover())
470 .cursor_pointer()
471 .on_click(move |_, window, cx| open(window, cx))
472 .child(
473 h_flex()
474 .gap(px(AVATAR_GAP))
475 .items_center()
476 .children(own_avatar)
477 .child(
478 div()
479 .text_size(px(13.))
480 .font_weight(FontWeight::SEMIBOLD)
481 .text_color(theme::text_strong())
482 .child(POST_IN_CHANNEL),
483 ),
484 )
485 .child(
486 h_flex().child(
487 h_flex()
488 .h(px(32.))
489 .px(px(12.))
490 .gap(px(6.))
491 .items_center()
492 .rounded(px(6.))
493 .bg(theme::reply_surface())
494 .text_size(px(13.))
495 .font_weight(FontWeight::SEMIBOLD)
496 .text_color(theme::accent_text())
497 .child(icon(IconName::MessageSquare, 14., theme::accent_text()))
498 .child(POST_LABEL),
499 ),
500 )
501 .into_any_element()
502}
503
504fn new_post_badge() -> Div {
505 div()
506 .absolute()
507 .top(px(CARD_PADDING + (ROOT_AVATAR - NEW_BADGE_HEIGHT) / 2.))
508 .right(px(CARD_PADDING))
509 .h(px(NEW_BADGE_HEIGHT))
510 .px(px(8.))
511 .flex()
512 .items_center()
513 .rounded_full()
514 .bg(theme::mention_background(false))
515 .text_size(px(11.))
516 .font_weight(FontWeight::SEMIBOLD)
517 .text_color(theme::accent_text())
518 .child(NEW_LABEL)
519}
520
521fn new_post_stripe() -> Div {
522 div()
523 .absolute()
524 .top_0()
525 .bottom_0()
526 .left_0()
527 .w(px(NEW_STRIPE_WIDTH))
528 .rounded_l(px(CARD_RADIUS))
529 .bg(theme::accent())
530}
531
532pub fn render_post(
533 post: &PostRow,
534 index: usize,
535 actions: PostActions,
536 directory: &Directory,
537 cx: &App,
538) -> AnyElement {
539 let PostActions {
540 root,
541 replies,
542 open_thread,
543 open_reply,
544 reply_editor,
545 } = actions;
546 let mut card = v_flex()
547 .debug_selector(|| POST_CARD_TAG.into())
548 .relative()
549 .w_full()
550 .rounded(px(CARD_RADIUS))
551 .bg(theme::row_hover())
552 .child(message_block(
553 &post.root,
554 message_index(index, 0),
555 Role::Root,
556 root,
557 directory,
558 cx,
559 ));
560 if let Some(label) = post.hidden_replies_label() {
561 card = card.child(replies_link(label, index, open_thread));
562 }
563 let has_footer = open_reply.is_some() || reply_editor.is_some();
564 let reply_count = post.replies.len();
565 for (position, (reply, actions)) in post.replies.iter().zip(replies).enumerate() {
566 let closes_card = !has_footer && position + 1 == reply_count;
567 card = card.child(
568 message_block(
569 reply,
570 message_index(index, position + 1),
571 Role::Reply,
572 actions,
573 directory,
574 cx,
575 )
576 .when(closes_card, |block| block.rounded_b(px(CARD_RADIUS))),
577 );
578 }
579 card = card.children(reply_footer(index, open_reply, reply_editor, directory));
580 if post.is_new {
581 card = card.child(new_post_stripe()).child(new_post_badge());
582 }
583 div()
584 .w_full()
585 .px(px(20.))
586 .pt(px(CARD_GAP))
587 .child(card)
588 .into_any_element()
589}