BlueGit

ict rusty-teams

Files

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

post_card.rs

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