BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / views / composer / post.rs

post.rs

post.rs 16.8 KiB Download Raw
1use gpui_kit::component::input::{Input, InputEvent, InputState};
2use gpui_kit::component::tooltip::Tooltip;
3use gpui_kit::component::{Sizable as _, Size, h_flex, v_flex};
4use gpui_kit::prelude::FluentBuilder as _;
5use gpui_kit::*;
6
7use teams_core::{AnnouncementBanner, BannerBackground};
8
9use super::{COMPOSER_BUTTON_SIZE, COMPOSER_ICON_SIZE, Composer, ComposerEvent};
10use crate::runtime;
11use crate::theme;
12use crate::views::announcement_banner::{
13 BANNER_COLORS, banner_fill, banner_frame, color_label, headline_style,
14};
15use crate::views::avatar::person_avatar;
16use crate::views::widgets::symbol;
17
18pub const POST_CARD_HEADER_TAG: &str = "composer/post/header";
19pub const POST_CARD_SUBJECT_TAG: &str = "composer/post/subject";
20pub const POST_CARD_DIVIDER_TAG: &str = "composer/post/divider";
21pub const POST_CARD_FOOTER_TAG: &str = "composer/post/footer";
22pub const POST_CARD_SEND_TAG: &str = "composer/post/send";
23pub const POST_ANNOUNCEMENT_TAG: &str = "composer/post/announcement";
24pub const POST_ANNOUNCEMENT_TOGGLE_TAG: &str = "composer/post/announcement-toggle";
25pub const POST_AUDIENCE_TAG: &str = "composer/post/audience";
26const DEMO_AUDIENCE: usize = 24;
27const AUDIENCE_HEIGHT: f32 = 32.;
28const ANNOUNCEMENT_TOOLTIP: &str = "Announcement";
29const HEADLINE_PLACEHOLDER: &str = "Type a headline";
30pub const SUBJECT_PLACEHOLDER: &str = "Add a subject";
31const SUBHEADING_PLACEHOLDER: &str = "Add a subheading";
32const HEADLINE_SIZE: f32 = 32.;
33const COMPOSER_BANNER_HEIGHT: f32 = 240.;
34const HEADLINE_LINE: f32 = 1.25;
35const SWATCH_SIZE: f32 = 18.;
36const SWATCH_RING: f32 = 2.;
37const SWATCH_GAP: f32 = 6.;
38const SWATCH_INSET: f32 = 8.;
39const POST_LABEL: &str = "Post";
40const CLOSE_TOOLTIP: &str = "Close";
41const AUTHOR_AVATAR: f32 = 24.;
42const CARD_INSET: f32 = 12.;
43const SUBJECT_SIZE: f32 = 18.;
44const INPUT_TEXT_SCALE: f32 = 0.875;
45const SUBJECT_INSET: f32 = 8.;
46const TEXT_INSET: f32 = 16.;
47
48pub(super) struct AnnouncementDraft {
49 pub(super) headline: Entity<InputState>,
50 color: &'static str,
51 _changes: Subscription,
52}
53
54pub fn audience_hint(count: usize) -> String {
55 match count {
56 1 => "1 person will see your message.".to_owned(),
57 count => format!("{count} people will see your message."),
58 }
59}
60
61impl Composer {
62 /// Shows the channel post layout with `subject` above the body, or the chat layout when `None`.
63 pub fn set_post_layout(&mut self, subject: Option<Entity<InputState>>, cx: &mut Context<Self>) {
64 let changed = self.post_subject.as_ref().map(Entity::entity_id)
65 != subject.as_ref().map(Entity::entity_id);
66 self.post_subject = subject;
67 if self.post_subject.is_none() {
68 self.announcement = None;
69 }
70 if changed {
71 self.schedule_draft_save(cx);
72 cx.notify();
73 }
74 }
75
76 pub(super) fn post_layout(&self) -> bool {
77 self.post_subject.is_some() && self.editing.is_none()
78 }
79
80 pub(super) fn post_subject_text(&self, cx: &App) -> Option<String> {
81 let subject = self.post_subject.as_ref().filter(|_| self.post_layout())?;
82 let text = subject.read(cx).value().trim().to_owned();
83 (!text.is_empty()).then_some(text)
84 }
85
86 /// Saves the draft with its post subject and banner before the post card closes.
87 pub fn leave_conversation(&mut self, next_conversation_id: &str, cx: &mut Context<Self>) {
88 if self.conversation_id.as_deref() != Some(next_conversation_id) {
89 self.save_draft_now(cx);
90 self.conversation_id = None;
91 }
92 }
93
94 pub(super) fn announcement_banner(&self, cx: &App) -> Option<AnnouncementBanner> {
95 let draft = self.announcement.as_ref().filter(|_| self.post_layout())?;
96 let title = draft.headline.read(cx).value().trim().to_owned();
97 (!title.is_empty()).then(|| AnnouncementBanner {
98 title,
99 background: BannerBackground::Color(draft.color.to_owned()),
100 })
101 }
102
103 pub(super) fn restore_announcement(
104 &mut self,
105 banner: Option<&AnnouncementBanner>,
106 window: &mut Window,
107 cx: &mut Context<Self>,
108 ) {
109 match banner {
110 Some(banner) => {
111 let color = match &banner.background {
112 BannerBackground::Color(color) => BANNER_COLORS
113 .into_iter()
114 .find(|known| known == color)
115 .unwrap_or(BANNER_COLORS[0]),
116 BannerBackground::Image(_) => BANNER_COLORS[0],
117 };
118 self.open_announcement(color, window, cx);
119 if let Some(draft) = &self.announcement {
120 let title = banner.title.clone();
121 draft
122 .headline
123 .update(cx, |input, cx| input.set_value(title, window, cx));
124 }
125 }
126 None => self.close_announcement(window, cx),
127 }
128 }
129
130 pub(super) fn toggle_announcement(&mut self, window: &mut Window, cx: &mut Context<Self>) {
131 if self.announcement.is_some() {
132 self.close_announcement(window, cx);
133 self.focus(window, cx);
134 } else {
135 self.open_announcement(BANNER_COLORS[0], window, cx);
136 if let Some(draft) = &self.announcement {
137 draft
138 .headline
139 .update(cx, |input, cx| input.focus(window, cx));
140 }
141 }
142 }
143
144 fn open_announcement(
145 &mut self,
146 color: &'static str,
147 window: &mut Window,
148 cx: &mut Context<Self>,
149 ) {
150 let headline = cx.new(|cx| InputState::new(window, cx).placeholder(HEADLINE_PLACEHOLDER));
151 let changes = cx.subscribe(&headline, |this, _, event: &InputEvent, cx| {
152 if matches!(event, InputEvent::Change) {
153 this.schedule_draft_save(cx);
154 }
155 });
156 self.announcement = Some(AnnouncementDraft {
157 headline,
158 color,
159 _changes: changes,
160 });
161 self.set_subject_placeholder(SUBHEADING_PLACEHOLDER, window, cx);
162 self.load_audience(cx);
163 self.schedule_draft_save(cx);
164 cx.notify();
165 }
166
167 pub(super) fn close_announcement(&mut self, window: &mut Window, cx: &mut Context<Self>) {
168 if self.announcement.take().is_some() {
169 self.set_subject_placeholder(SUBJECT_PLACEHOLDER, window, cx);
170 self.schedule_draft_save(cx);
171 cx.notify();
172 }
173 }
174
175 fn load_audience(&mut self, cx: &mut Context<Self>) {
176 let Some(channel_id) = self.conversation_id.clone() else {
177 return;
178 };
179 if self
180 .audience
181 .as_ref()
182 .is_some_and(|(id, _)| *id == channel_id)
183 {
184 return;
185 }
186 let state = self.app.read(cx);
187 if state.mode.demo {
188 self.audience = Some((channel_id, DEMO_AUDIENCE));
189 return;
190 }
191 let Some(engine) = state.engine.clone() else {
192 return;
193 };
194 let lookup = channel_id.clone();
195 let receiver = runtime::spawn(async move { engine.channel_member_count(&lookup).await });
196 cx.spawn(async move |this, cx| {
197 let Ok(Ok(count)) = receiver.await else {
198 return;
199 };
200 this.update(cx, |this, cx| {
201 if this.conversation_id.as_deref() == Some(channel_id.as_str()) {
202 this.audience = Some((channel_id, count));
203 cx.notify();
204 }
205 })
206 .ok();
207 })
208 .detach();
209 }
210
211 pub(super) fn render_audience_hint(&self) -> Option<Div> {
212 self.announcement.as_ref().filter(|_| self.post_layout())?;
213 let (channel_id, count) = self.audience.as_ref()?;
214 (self.conversation_id.as_ref() == Some(channel_id)).then(|| {
215 h_flex()
216 .debug_selector(|| POST_AUDIENCE_TAG.into())
217 .w_full()
218 .h(px(AUDIENCE_HEIGHT))
219 .mb(px(8.))
220 .px(px(CARD_INSET))
221 .items_center()
222 .rounded(px(10.))
223 .bg(theme::surface_raised())
224 .text_size(px(12.))
225 .text_color(theme::text_muted())
226 .child(audience_hint(*count))
227 })
228 }
229
230 pub fn sync_subject_placeholder(&self, window: &mut Window, cx: &mut Context<Self>) {
231 let placeholder = if self.announcement.is_some() {
232 SUBHEADING_PLACEHOLDER
233 } else {
234 SUBJECT_PLACEHOLDER
235 };
236 self.set_subject_placeholder(placeholder, window, cx);
237 }
238
239 fn set_subject_placeholder(
240 &self,
241 placeholder: &'static str,
242 window: &mut Window,
243 cx: &mut Context<Self>,
244 ) {
245 if let Some(subject) = &self.post_subject {
246 subject.update(cx, |input, cx| {
247 input.set_placeholder(placeholder, window, cx)
248 });
249 }
250 }
251
252 pub(super) fn render_announcement_toggle(
253 &self,
254 cx: &mut Context<Self>,
255 ) -> Option<Stateful<Div>> {
256 if !self.post_layout() {
257 return None;
258 }
259 let active = self.announcement.is_some();
260 Some(
261 div()
262 .id("composer-post-announcement")
263 .debug_selector(|| POST_ANNOUNCEMENT_TOGGLE_TAG.into())
264 .size(px(COMPOSER_BUTTON_SIZE))
265 .flex_none()
266 .flex()
267 .items_center()
268 .justify_center()
269 .rounded(px(6.))
270 .cursor_pointer()
271 .when(active, |button| button.bg(theme::popup_hover()))
272 .hover(|button| button.bg(theme::popup_hover()))
273 .tooltip(|window, cx| Tooltip::new(ANNOUNCEMENT_TOOLTIP).build(window, cx))
274 .child(symbol(
275 "campaign",
276 COMPOSER_ICON_SIZE,
277 if active {
278 theme::accent_soft()
279 } else {
280 theme::text_muted()
281 },
282 ))
283 .on_mouse_down(MouseButton::Left, |_, window, cx| {
284 window.prevent_default();
285 cx.stop_propagation();
286 })
287 .on_click(cx.listener(|this, _, window, cx| this.toggle_announcement(window, cx))),
288 )
289 }
290
291 fn render_announcement(&self, cx: &mut Context<Self>) -> Option<Div> {
292 let draft = self.announcement.as_ref()?;
293 let directory = &self.app.read(cx).directory;
294 let swatches = h_flex()
295 .absolute()
296 .right(px(SWATCH_INSET))
297 .bottom(px(SWATCH_INSET))
298 .gap(px(SWATCH_GAP))
299 .children(BANNER_COLORS.into_iter().map(|color| {
300 let selected = color == draft.color;
301 div()
302 .id(ElementId::Name(format!("composer-banner-{color}").into()))
303 .size(px(SWATCH_SIZE))
304 .rounded_full()
305 .border(px(SWATCH_RING))
306 .border_color(if selected {
307 white()
308 } else {
309 white().opacity(0.4)
310 })
311 .bg(banner_fill(color))
312 .cursor_pointer()
313 .tooltip(move |window, cx| Tooltip::new(color_label(color)).build(window, cx))
314 .on_click(cx.listener(move |this, _, _, cx| {
315 if let Some(draft) = this.announcement.as_mut() {
316 draft.color = color;
317 this.schedule_draft_save(cx);
318 cx.notify();
319 }
320 }))
321 }));
322 Some(
323 div()
324 .debug_selector(|| POST_ANNOUNCEMENT_TAG.into())
325 .w_full()
326 .px(px(CARD_INSET))
327 .pt(px(CARD_INSET))
328 .child(
329 banner_frame(&BannerBackground::Color(draft.color.to_owned()), directory)
330 .h(px(COMPOSER_BANNER_HEIGHT))
331 .child(
332 headline_style(div().w_full()).child(
333 Input::new(&draft.headline)
334 .appearance(false)
335 .bordered(false)
336 .text_center()
337 .h(px(HEADLINE_SIZE * HEADLINE_LINE))
338 .line_height(px(HEADLINE_SIZE * HEADLINE_LINE))
339 .with_size(Size::Size(px(HEADLINE_SIZE / INPUT_TEXT_SCALE))),
340 ),
341 )
342 .child(swatches),
343 ),
344 )
345 }
346
347 pub(super) fn render_post_header(&self, cx: &mut Context<Self>) -> Option<Div> {
348 let subject = self
349 .post_subject
350 .clone()
351 .filter(|_| self.editing.is_none())?;
352 let directory = &self.app.read(cx).directory;
353 let author = directory.me.as_ref().map(|me| {
354 h_flex()
355 .gap(px(8.))
356 .items_center()
357 .child(person_avatar(
358 directory,
359 Some(&me.user_id),
360 &me.display_name,
361 AUTHOR_AVATAR,
362 ))
363 .child(
364 div()
365 .text_size(px(13.))
366 .font_weight(FontWeight::SEMIBOLD)
367 .text_color(theme::text_strong())
368 .child(me.display_name.clone()),
369 )
370 });
371 let close = div()
372 .id("composer-post-close")
373 .size(px(28.))
374 .flex_none()
375 .flex()
376 .items_center()
377 .justify_center()
378 .rounded(px(6.))
379 .cursor_pointer()
380 .hover(|button| button.bg(theme::popup_hover()))
381 .tooltip(|window, cx| Tooltip::new(CLOSE_TOOLTIP).build(window, cx))
382 .child(symbol("close", 18., theme::text_muted()))
383 .on_click(cx.listener(|_, _, _, cx| cx.emit(ComposerEvent::ClosePost)));
384 Some(
385 v_flex()
386 .w_full()
387 .child(
388 h_flex()
389 .debug_selector(|| POST_CARD_HEADER_TAG.into())
390 .w_full()
391 .pl(px(TEXT_INSET))
392 .pr(px(CARD_INSET))
393 .pt(px(CARD_INSET))
394 .items_center()
395 .children(author)
396 .child(div().flex_1())
397 .child(close),
398 )
399 .children(self.render_announcement(cx))
400 .child(
401 div()
402 .debug_selector(|| POST_CARD_SUBJECT_TAG.into())
403 .w_full()
404 .px(px(SUBJECT_INSET))
405 .py(px(6.))
406 .font_weight(FontWeight::SEMIBOLD)
407 .child(
408 Input::new(&subject)
409 .appearance(false)
410 .bordered(false)
411 .with_size(Size::Size(px(SUBJECT_SIZE / INPUT_TEXT_SCALE))),
412 ),
413 )
414 .child(
415 div()
416 .debug_selector(|| POST_CARD_DIVIDER_TAG.into())
417 .w_full()
418 .h(px(1.))
419 .bg(theme::border()),
420 ),
421 )
422 }
423
424 pub(super) fn render_post_footer(
425 &self,
426 actions: impl IntoElement,
427 can_send: bool,
428 cx: &mut Context<Self>,
429 ) -> Div {
430 h_flex()
431 .debug_selector(|| POST_CARD_FOOTER_TAG.into())
432 .w_full()
433 .px(px(CARD_INSET))
434 .pb(px(CARD_INSET))
435 .items_center()
436 .child(actions)
437 .child(div().flex_1())
438 .child(
439 div()
440 .id("composer-post-send")
441 .debug_selector(|| POST_CARD_SEND_TAG.into())
442 .h(px(COMPOSER_BUTTON_SIZE))
443 .px(px(14.))
444 .flex()
445 .items_center()
446 .rounded(px(6.))
447 .bg(theme::accent())
448 .text_size(px(13.))
449 .font_weight(FontWeight::SEMIBOLD)
450 .text_color(theme::on_accent())
451 .child(POST_LABEL)
452 .when(!can_send, |button| button.opacity(0.4))
453 .when(can_send, |button| {
454 button.cursor_pointer().on_click(
455 cx.listener(|this, _, window, cx| this.submit_current(window, cx)),
456 )
457 }),
458 )
459 }
460}