BlueGit

ict rusty-teams

Files

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

post_card.rs

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