BlueGit

ict rusty-teams

Files

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

post_card.rs

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