BlueGit

ict rusty-teams

Files

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

post_card.rs

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