BlueGit

ict rusty-teams

Files

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

post.rs

post.rs 12.9 KiB Download Raw
1use gpui_kit::component::input::{Input, 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::theme;
11use crate::views::announcement_banner::{
12 BANNER_COLORS, banner_fill, banner_frame, color_label, headline_style,
13};
14use crate::views::avatar::person_avatar;
15use crate::views::widgets::symbol;
16
17pub const POST_CARD_HEADER_TAG: &str = "composer/post/header";
18pub const POST_CARD_SUBJECT_TAG: &str = "composer/post/subject";
19pub const POST_CARD_DIVIDER_TAG: &str = "composer/post/divider";
20pub const POST_CARD_FOOTER_TAG: &str = "composer/post/footer";
21pub const POST_CARD_SEND_TAG: &str = "composer/post/send";
22pub const POST_ANNOUNCEMENT_TAG: &str = "composer/post/announcement";
23pub const POST_ANNOUNCEMENT_TOGGLE_TAG: &str = "composer/post/announcement-toggle";
24const ANNOUNCEMENT_TOOLTIP: &str = "Announcement";
25const HEADLINE_PLACEHOLDER: &str = "Add a headline";
26pub const SUBJECT_PLACEHOLDER: &str = "Add a subject";
27const SUBHEADING_PLACEHOLDER: &str = "Add a subheading";
28const HEADLINE_SIZE: f32 = 24.;
29const SWATCH_SIZE: f32 = 18.;
30const SWATCH_RING: f32 = 2.;
31const SWATCH_GAP: f32 = 6.;
32const SWATCH_INSET: f32 = 8.;
33const POST_LABEL: &str = "Post";
34const CLOSE_TOOLTIP: &str = "Close";
35const AUTHOR_AVATAR: f32 = 24.;
36const CARD_INSET: f32 = 12.;
37const SUBJECT_SIZE: f32 = 18.;
38const INPUT_TEXT_SCALE: f32 = 0.875;
39const SUBJECT_INSET: f32 = 8.;
40const TEXT_INSET: f32 = 16.;
41
42pub(super) struct AnnouncementDraft {
43 headline: Entity<InputState>,
44 color: &'static str,
45}
46
47impl Composer {
48 /// Shows the channel post layout with `subject` above the body, or the chat layout when `None`.
49 pub fn set_post_layout(&mut self, subject: Option<Entity<InputState>>, cx: &mut Context<Self>) {
50 let changed = self.post_subject.as_ref().map(Entity::entity_id)
51 != subject.as_ref().map(Entity::entity_id);
52 self.post_subject = subject;
53 if self.post_subject.is_none() {
54 self.announcement = None;
55 }
56 if changed {
57 cx.notify();
58 }
59 }
60
61 pub(super) fn post_layout(&self) -> bool {
62 self.post_subject.is_some() && self.editing.is_none()
63 }
64
65 pub(super) fn announcement_banner(&self, cx: &App) -> Option<AnnouncementBanner> {
66 let draft = self.announcement.as_ref().filter(|_| self.post_layout())?;
67 let title = draft.headline.read(cx).value().trim().to_owned();
68 (!title.is_empty()).then(|| AnnouncementBanner {
69 title,
70 background: BannerBackground::Color(draft.color.to_owned()),
71 })
72 }
73
74 pub(super) fn restore_announcement(
75 &mut self,
76 banner: Option<&AnnouncementBanner>,
77 window: &mut Window,
78 cx: &mut Context<Self>,
79 ) {
80 match banner {
81 Some(banner) => {
82 let color = match &banner.background {
83 BannerBackground::Color(color) => BANNER_COLORS
84 .into_iter()
85 .find(|known| known == color)
86 .unwrap_or(BANNER_COLORS[0]),
87 BannerBackground::Image(_) => BANNER_COLORS[0],
88 };
89 self.open_announcement(color, window, cx);
90 if let Some(draft) = &self.announcement {
91 let title = banner.title.clone();
92 draft
93 .headline
94 .update(cx, |input, cx| input.set_value(title, window, cx));
95 }
96 }
97 None => self.close_announcement(window, cx),
98 }
99 }
100
101 fn toggle_announcement(&mut self, window: &mut Window, cx: &mut Context<Self>) {
102 if self.announcement.is_some() {
103 self.close_announcement(window, cx);
104 self.focus(window, cx);
105 } else {
106 self.open_announcement(BANNER_COLORS[0], window, cx);
107 if let Some(draft) = &self.announcement {
108 draft
109 .headline
110 .update(cx, |input, cx| input.focus(window, cx));
111 }
112 }
113 }
114
115 fn open_announcement(
116 &mut self,
117 color: &'static str,
118 window: &mut Window,
119 cx: &mut Context<Self>,
120 ) {
121 let headline = cx.new(|cx| InputState::new(window, cx).placeholder(HEADLINE_PLACEHOLDER));
122 self.announcement = Some(AnnouncementDraft { headline, color });
123 self.set_subject_placeholder(SUBHEADING_PLACEHOLDER, window, cx);
124 cx.notify();
125 }
126
127 pub(super) fn close_announcement(&mut self, window: &mut Window, cx: &mut Context<Self>) {
128 if self.announcement.take().is_some() {
129 self.set_subject_placeholder(SUBJECT_PLACEHOLDER, window, cx);
130 cx.notify();
131 }
132 }
133
134 fn set_subject_placeholder(
135 &self,
136 placeholder: &'static str,
137 window: &mut Window,
138 cx: &mut Context<Self>,
139 ) {
140 if let Some(subject) = &self.post_subject {
141 subject.update(cx, |input, cx| {
142 input.set_placeholder(placeholder, window, cx)
143 });
144 }
145 }
146
147 pub(super) fn render_announcement_toggle(
148 &self,
149 cx: &mut Context<Self>,
150 ) -> Option<Stateful<Div>> {
151 if !self.post_layout() {
152 return None;
153 }
154 let active = self.announcement.is_some();
155 Some(
156 div()
157 .id("composer-post-announcement")
158 .debug_selector(|| POST_ANNOUNCEMENT_TOGGLE_TAG.into())
159 .size(px(COMPOSER_BUTTON_SIZE))
160 .flex_none()
161 .flex()
162 .items_center()
163 .justify_center()
164 .rounded(px(6.))
165 .cursor_pointer()
166 .when(active, |button| button.bg(theme::popup_hover()))
167 .hover(|button| button.bg(theme::popup_hover()))
168 .tooltip(|window, cx| Tooltip::new(ANNOUNCEMENT_TOOLTIP).build(window, cx))
169 .child(symbol(
170 "campaign",
171 COMPOSER_ICON_SIZE,
172 if active {
173 theme::accent_soft()
174 } else {
175 theme::text_muted()
176 },
177 ))
178 .on_mouse_down(MouseButton::Left, |_, window, cx| {
179 window.prevent_default();
180 cx.stop_propagation();
181 })
182 .on_click(cx.listener(|this, _, window, cx| this.toggle_announcement(window, cx))),
183 )
184 }
185
186 fn render_announcement(&self, cx: &mut Context<Self>) -> Option<Div> {
187 let draft = self.announcement.as_ref()?;
188 let directory = &self.app.read(cx).directory;
189 let swatches = h_flex()
190 .absolute()
191 .right(px(SWATCH_INSET))
192 .bottom(px(SWATCH_INSET))
193 .gap(px(SWATCH_GAP))
194 .children(BANNER_COLORS.into_iter().map(|color| {
195 let selected = color == draft.color;
196 div()
197 .id(ElementId::Name(format!("composer-banner-{color}").into()))
198 .size(px(SWATCH_SIZE))
199 .rounded_full()
200 .border(px(SWATCH_RING))
201 .border_color(if selected {
202 white()
203 } else {
204 white().opacity(0.4)
205 })
206 .bg(banner_fill(color))
207 .cursor_pointer()
208 .tooltip(move |window, cx| Tooltip::new(color_label(color)).build(window, cx))
209 .on_click(cx.listener(move |this, _, _, cx| {
210 if let Some(draft) = this.announcement.as_mut() {
211 draft.color = color;
212 cx.notify();
213 }
214 }))
215 }));
216 Some(
217 div()
218 .debug_selector(|| POST_ANNOUNCEMENT_TAG.into())
219 .w_full()
220 .px(px(CARD_INSET))
221 .pt(px(CARD_INSET))
222 .child(
223 banner_frame(&BannerBackground::Color(draft.color.to_owned()), directory)
224 .child(
225 headline_style(div().w_full()).child(
226 Input::new(&draft.headline)
227 .appearance(false)
228 .bordered(false)
229 .with_size(Size::Size(px(HEADLINE_SIZE / INPUT_TEXT_SCALE))),
230 ),
231 )
232 .child(swatches),
233 ),
234 )
235 }
236
237 pub(super) fn render_post_header(&self, cx: &mut Context<Self>) -> Option<Div> {
238 let subject = self
239 .post_subject
240 .clone()
241 .filter(|_| self.editing.is_none())?;
242 let directory = &self.app.read(cx).directory;
243 let author = directory.me.as_ref().map(|me| {
244 h_flex()
245 .gap(px(8.))
246 .items_center()
247 .child(person_avatar(
248 directory,
249 Some(&me.user_id),
250 &me.display_name,
251 AUTHOR_AVATAR,
252 ))
253 .child(
254 div()
255 .text_size(px(13.))
256 .font_weight(FontWeight::SEMIBOLD)
257 .text_color(theme::text_strong())
258 .child(me.display_name.clone()),
259 )
260 });
261 let close = div()
262 .id("composer-post-close")
263 .size(px(28.))
264 .flex_none()
265 .flex()
266 .items_center()
267 .justify_center()
268 .rounded(px(6.))
269 .cursor_pointer()
270 .hover(|button| button.bg(theme::popup_hover()))
271 .tooltip(|window, cx| Tooltip::new(CLOSE_TOOLTIP).build(window, cx))
272 .child(symbol("close", 18., theme::text_muted()))
273 .on_click(cx.listener(|_, _, _, cx| cx.emit(ComposerEvent::ClosePost)));
274 Some(
275 v_flex()
276 .w_full()
277 .child(
278 h_flex()
279 .debug_selector(|| POST_CARD_HEADER_TAG.into())
280 .w_full()
281 .pl(px(TEXT_INSET))
282 .pr(px(CARD_INSET))
283 .pt(px(CARD_INSET))
284 .items_center()
285 .children(author)
286 .child(div().flex_1())
287 .child(close),
288 )
289 .children(self.render_announcement(cx))
290 .child(
291 div()
292 .debug_selector(|| POST_CARD_SUBJECT_TAG.into())
293 .w_full()
294 .px(px(SUBJECT_INSET))
295 .py(px(6.))
296 .font_weight(FontWeight::SEMIBOLD)
297 .child(
298 Input::new(&subject)
299 .appearance(false)
300 .bordered(false)
301 .with_size(Size::Size(px(SUBJECT_SIZE / INPUT_TEXT_SCALE))),
302 ),
303 )
304 .child(
305 div()
306 .debug_selector(|| POST_CARD_DIVIDER_TAG.into())
307 .w_full()
308 .h(px(1.))
309 .bg(theme::border()),
310 ),
311 )
312 }
313
314 pub(super) fn render_post_footer(
315 &self,
316 actions: impl IntoElement,
317 can_send: bool,
318 cx: &mut Context<Self>,
319 ) -> Div {
320 h_flex()
321 .debug_selector(|| POST_CARD_FOOTER_TAG.into())
322 .w_full()
323 .px(px(CARD_INSET))
324 .pb(px(CARD_INSET))
325 .items_center()
326 .child(actions)
327 .child(div().flex_1())
328 .child(
329 div()
330 .id("composer-post-send")
331 .debug_selector(|| POST_CARD_SEND_TAG.into())
332 .h(px(COMPOSER_BUTTON_SIZE))
333 .px(px(14.))
334 .flex()
335 .items_center()
336 .rounded(px(6.))
337 .bg(theme::accent())
338 .text_size(px(13.))
339 .font_weight(FontWeight::SEMIBOLD)
340 .text_color(theme::on_accent())
341 .child(POST_LABEL)
342 .when(!can_send, |button| button.opacity(0.4))
343 .when(can_send, |button| {
344 button.cursor_pointer().on_click(
345 cx.listener(|this, _, window, cx| this.submit_current(window, cx)),
346 )
347 }),
348 )
349 }
350}