BlueGit

ict rusty-teams

Files

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

post_card.rs

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