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