BlueGit

ict rusty-teams

Files

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

post_card.rs

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