BlueGit

ict rusty-teams

Files

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

post_card.rs

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