BlueGit

ict rusty-teams

Files

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

post_card.rs

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