BlueGit

ict rusty-teams

Files

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

post_card.rs

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