BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / notify / toast.rs

toast.rs

toast.rs 19.9 KiB Download Raw
1use std::time::Instant;
2
3use gpui_kit::component::{
4 h_flex,
5 input::{InputEvent, Textarea, TextareaState},
6 v_flex,
7};
8use gpui_kit::prelude::FluentBuilder as _;
9use gpui_kit::*;
10
11use super::center::NotificationCenter;
12use super::layout::{ACTION_BUTTON, SEND_BUTTON};
13use super::platform;
14use super::rules::ChatKind;
15use super::stack::{ReplyState, ToastModel};
16use super::text::{ToastText, describe, mention_ranges};
17use crate::sidebar_model::{AvatarSpec, Face};
18use crate::views::avatar::{spec_avatar, with_presence};
19use crate::views::composer::submitted_text;
20use crate::views::widgets::symbol;
21use crate::theme;
22
23const AVATAR_SIZE: f32 = 40.;
24const CLOSE_SIZE: f32 = 24.;
25const TOAST_BORDER_MENTION: f32 = 2.;
26const TOAST_RADIUS: f32 = 12.;
27
28pub struct ToastView {
29 center: Entity<NotificationCenter>,
30 id: u64,
31 input: Entity<TextareaState>,
32 focus_handle: FocusHandle,
33 last_title: String,
34 _subscriptions: Vec<Subscription>,
35}
36
37impl ToastView {
38 pub fn new(
39 center: Entity<NotificationCenter>,
40 id: u64,
41 window: &mut Window,
42 cx: &mut Context<Self>,
43 ) -> Self {
44 let input = cx.new(|cx| {
45 TextareaState::new(window, cx)
46 .auto_grow(1, 1)
47 .submit_on_enter(true)
48 .placeholder("Reply ...")
49 });
50 let subscriptions = vec![
51 cx.subscribe_in(&input, window, |this, input, event: &InputEvent, window, cx| {
52 let value = input.read(cx).value();
53 if submitted_text(event, &value).is_some() {
54 this.submit(window, cx);
55 }
56 }),
57 cx.observe(&center, |_, _, cx| cx.notify()),
58 ];
59 ToastView {
60 center,
61 id,
62 input,
63 focus_handle: cx.focus_handle(),
64 last_title: String::new(),
65 _subscriptions: subscriptions,
66 }
67 }
68
69 fn submit(&mut self, window: &mut Window, cx: &mut Context<Self>) {
70 let text = self.input.read(cx).value().trim().to_owned();
71 if text.is_empty() {
72 return;
73 }
74 self.input
75 .update(cx, |state, cx| state.set_value("", window, cx));
76 let id = self.id;
77 self.center
78 .update(cx, |center, cx| center.submit_reply(id, text, cx));
79 }
80
81 fn open_reply(&mut self, window: &mut Window, cx: &mut Context<Self>) {
82 let id = self.id;
83 let draft = self
84 .center
85 .update(cx, |center, cx| center.open_reply(id, cx));
86 if let Some(native) = platform::native_handle(window) {
87 platform::set_activatable(native, true);
88 }
89 let input = self.input.clone();
90 cx.defer_in(window, move |_, window, cx| {
91 input.update(cx, |state, cx| {
92 if let Some(draft) = draft.filter(|draft| !draft.is_empty()) {
93 state.set_value(&draft, window, cx);
94 }
95 state.focus(window, cx);
96 });
97 });
98 }
99
100 fn close_reply(&mut self, window: &mut Window, cx: &mut Context<Self>) {
101 self.input
102 .update(cx, |state, cx| state.set_value("", window, cx));
103 let id = self.id;
104 self.center
105 .update(cx, |center, cx| center.close_reply(id, cx));
106 window.focus(&self.focus_handle, cx);
107 }
108
109 fn retry(&mut self, cx: &mut Context<Self>) {
110 let id = self.id;
111 self.center.update(cx, |center, cx| {
112 if let Some(text) = center.reply_text(id) {
113 center.submit_reply(id, text, cx);
114 }
115 });
116 }
117}
118
119fn flat_button(
120 label: &'static str,
121 symbol_name: &'static str,
122 accent: bool,
123 mention: bool,
124) -> Stateful<Div> {
125 let color = if accent {
126 theme::accent_text()
127 } else {
128 theme::text_soft()
129 };
130 let hover_fill = if mention {
131 theme::bubble_own()
132 } else {
133 theme::border_strong()
134 };
135 h_flex()
136 .id(label)
137 .h(px(ACTION_BUTTON))
138 .px(px(10.))
139 .gap(px(6.))
140 .items_center()
141 .rounded(px(6.))
142 .cursor_pointer()
143 .text_size(px(12.5))
144 .font_weight(FontWeight::SEMIBOLD)
145 .text_color(color)
146 .hover(move |button| button.bg(hover_fill))
147 .child(symbol(symbol_name, 15., color))
148 .child(label)
149}
150
151fn highlighted_preview(text: &str, mention: bool) -> AnyElement {
152 let mut styled = StyledText::new(text.to_owned());
153 if mention {
154 let style = HighlightStyle {
155 color: Some(theme::accent_soft()),
156 font_weight: Some(FontWeight::SEMIBOLD),
157 ..Default::default()
158 };
159 let highlights: Vec<_> = mention_ranges(text)
160 .into_iter()
161 .map(|range| (range, style))
162 .collect();
163 styled = styled.with_highlights(highlights);
164 }
165 div()
166 .text_size(px(13.))
167 .line_height(px(18.))
168 .text_color(theme::text())
169 .line_clamp(2)
170 .child(styled)
171 .into_any_element()
172}
173
174fn side_chip(label: String, mention: bool) -> Div {
175 div()
176 .h(px(20.))
177 .px(px(8.))
178 .flex()
179 .items_center()
180 .rounded(px(10.))
181 .bg(if mention {
182 theme::bubble_own()
183 } else {
184 theme::border_strong()
185 })
186 .text_color(if mention {
187 theme::accent_soft()
188 } else {
189 theme::text()
190 })
191 .text_size(px(11.))
192 .line_height(px(20.))
193 .font_weight(FontWeight::SEMIBOLD)
194 .child(label)
195}
196
197impl ToastView {
198 fn avatar(&self, model: &ToastModel, fill: Hsla, cx: &App) -> AnyElement {
199 let center = self.center.read(cx);
200 let directory = center.directory(cx);
201 let sender = Face {
202 user_id: model.sender_id.clone(),
203 name: model.sender_name.clone(),
204 };
205 match &model.kind {
206 ChatKind::Group { .. } => {
207 let chat = Face {
208 user_id: None,
209 name: model.chat_title.clone(),
210 };
211 spec_avatar(directory, &AvatarSpec::Pair(sender, chat), AVATAR_SIZE, fill)
212 }
213 _ => {
214 let presence = model
215 .sender_id
216 .as_deref()
217 .map(|user_id| directory.presence_of(user_id))
218 .unwrap_or(crate::data::Presence::Live(crate::data::PresenceKind::Unknown));
219 let face = spec_avatar(directory, &AvatarSpec::Single(sender), AVATAR_SIZE, fill);
220 with_presence(face, presence, AVATAR_SIZE, fill).into_any_element()
221 }
222 }
223 }
224
225 fn side_slot(&self, model: &ToastModel, hovered: bool) -> AnyElement {
226 let id = self.id;
227 if hovered {
228 let center = self.center.clone();
229 return div()
230 .id("toast-close")
231 .size(px(CLOSE_SIZE))
232 .flex_none()
233 .flex()
234 .items_center()
235 .justify_center()
236 .rounded_full()
237 .bg(theme::border_strong())
238 .cursor_pointer()
239 .hover(|button| button.bg(theme::text_faint()))
240 .child(symbol("close", 16., theme::text()))
241 .on_click(move |_, _, cx| {
242 cx.stop_propagation();
243 center.update(cx, |center, cx| center.dismiss(id, cx));
244 })
245 .into_any_element();
246 }
247 if model.mentions_me {
248 return side_chip("Mention".to_owned(), true).into_any_element();
249 }
250 if model.count > 1 {
251 return side_chip(format!("{} new", model.count), false).into_any_element();
252 }
253 div()
254 .text_size(px(12.))
255 .text_color(theme::text_muted())
256 .child(model.time.clone())
257 .into_any_element()
258 }
259
260 fn body(&self, model: &ToastModel, text: &ToastText, hovered: bool) -> Div {
261 let subtitle_icon = match &model.kind {
262 ChatKind::Group { .. } => Some("group"),
263 ChatKind::Channel { .. } => Some("tag"),
264 ChatKind::Direct => None,
265 };
266 let preview = if text.image_only {
267 h_flex()
268 .gap(px(6.))
269 .items_center()
270 .child(symbol("image", 16., theme::text_muted()))
271 .child(
272 div()
273 .text_size(px(13.))
274 .text_color(theme::text())
275 .child(text.preview.clone()),
276 )
277 .into_any_element()
278 } else {
279 highlighted_preview(&text.preview, model.mentions_me)
280 };
281 v_flex()
282 .flex_1()
283 .min_w_0()
284 .gap(px(2.))
285 .child(
286 h_flex()
287 .w_full()
288 .h(px(20.))
289 .gap(px(8.))
290 .items_center()
291 .justify_between()
292 .child(
293 div()
294 .flex_1()
295 .min_w_0()
296 .truncate()
297 .text_size(px(14.))
298 .line_height(px(20.))
299 .font_weight(FontWeight::SEMIBOLD)
300 .text_color(theme::text())
301 .child(text.title.clone()),
302 )
303 .child(div().flex_none().child(self.side_slot(model, hovered))),
304 )
305 .child(
306 h_flex()
307 .gap(px(4.))
308 .items_center()
309 .h(px(16.))
310 .text_size(px(12.))
311 .text_color(theme::text_muted())
312 .children(subtitle_icon.map(|name| symbol(name, 14., theme::text_muted())))
313 .child(div().truncate().child(text.subtitle.clone())),
314 )
315 .child(preview)
316 }
317
318 fn action_row(&self, mention: bool, cx: &mut Context<Self>) -> Div {
319 let id = self.id;
320 let center = self.center.clone();
321 h_flex()
322 .w_full()
323 .mt(px(4.))
324 .gap(px(4.))
325 .justify_end()
326 .child(
327 flat_button("Mark as read", "done", false, mention).on_click(move |_, _, cx| {
328 cx.stop_propagation();
329 center.update(cx, |center, cx| center.mark_read(id, cx));
330 }),
331 )
332 .child(
333 flat_button("Reply", "reply", true, mention).on_click(cx.listener(
334 |this, _, window, cx| {
335 cx.stop_propagation();
336 this.open_reply(window, cx);
337 },
338 )),
339 )
340 }
341
342 fn reply_field(&self, window: &Window, cx: &mut Context<Self>) -> Div {
343 let focused = self.input.focus_handle(cx).contains_focused(window, cx);
344 let empty = self.input.read(cx).value().trim().is_empty();
345 let send = div()
346 .id("toast-send")
347 .size(px(SEND_BUTTON))
348 .flex_none()
349 .flex()
350 .items_center()
351 .justify_center()
352 .rounded(px(8.))
353 .child(symbol("keyboard_return", 20., white()))
354 .when(empty, |button| button.opacity(0.4))
355 .when(!empty, |button| {
356 button
357 .cursor_pointer()
358 .hover(|button| button.bg(white().opacity(0.1)))
359 .on_click(cx.listener(|this, _, window, cx| {
360 cx.stop_propagation();
361 this.submit(window, cx);
362 }))
363 });
364 let hint = |keys: &'static str, action: &'static str| {
365 h_flex()
366 .gap(px(4.))
367 .child(div().text_color(theme::text_soft()).child(keys))
368 .child(action)
369 };
370 v_flex()
371 .w_full()
372 .mt(px(8.))
373 .gap(px(4.))
374 .child(
375 h_flex()
376 .w_full()
377 .items_end()
378 .gap(px(6.))
379 .py(px(6.))
380 .pl(px(12.))
381 .pr(px(6.))
382 .rounded(px(10.))
383 .bg(theme::surface())
384 .border_1()
385 .border_color(if focused {
386 theme::accent()
387 } else {
388 theme::border_strong()
389 })
390 .child(
391 div().flex_1().min_w_0().child(
392 Textarea::new(&self.input)
393 .appearance(false)
394 .bordered(false),
395 ),
396 )
397 .child(send),
398 )
399 .child(
400 h_flex()
401 .h(px(16.))
402 .gap(px(12.))
403 .justify_end()
404 .text_size(px(11.))
405 .text_color(theme::text_muted())
406 .child(hint("Enter", "send"))
407 .child(hint("Shift+Enter", "new line"))
408 .child(hint("Esc", "close")),
409 )
410 }
411
412 fn confirmation(&self, model: &ToastModel, cx: &mut Context<Self>) -> Div {
413 let sent = model.reply == ReplyState::Sent;
414 let label = if sent {
415 format!("Sent to {}", target_name(model))
416 } else {
417 "Couldn't send".to_owned()
418 };
419 let (icon_name, tone) = if sent {
420 ("check_circle", theme::green())
421 } else {
422 ("error", theme::red_soft())
423 };
424 h_flex()
425 .size_full()
426 .gap(px(10.))
427 .items_center()
428 .child(symbol(icon_name, 22., tone))
429 .child(
430 div()
431 .flex_1()
432 .min_w_0()
433 .truncate()
434 .text_size(px(14.))
435 .font_weight(FontWeight::SEMIBOLD)
436 .text_color(theme::text())
437 .child(label),
438 )
439 .when(sent, |row| {
440 row.child(
441 div()
442 .text_size(px(12.))
443 .text_color(theme::text_muted())
444 .child(model.time.clone()),
445 )
446 })
447 .when(!sent, |row| {
448 row.child(flat_button("Retry", "reply", true, false).on_click(cx.listener(
449 |this, _, _, cx| {
450 cx.stop_propagation();
451 this.retry(cx);
452 },
453 )))
454 })
455 }
456}
457
458fn target_name(model: &ToastModel) -> String {
459 match &model.kind {
460 ChatKind::Group { .. } => model.chat_title.clone(),
461 _ => model.sender_name.clone(),
462 }
463}
464
465impl Render for ToastView {
466 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
467 let id = self.id;
468 let (model, preview_on) = {
469 let center = self.center.read(cx);
470 (center.model(id).cloned(), center.settings().preview)
471 };
472 let Some(model) = model else {
473 return div()
474 .size_full()
475 .bg(theme::surface_raised())
476 .into_any_element();
477 };
478 let text = describe(&model, preview_on);
479 let narration = text.narration();
480 if narration != self.last_title {
481 window.set_window_title(&narration);
482 self.last_title = narration;
483 }
484 let mention = model.mentions_me;
485 let fill = if mention {
486 theme::toast_mention_fill()
487 } else {
488 theme::surface_raised()
489 };
490 let border = if mention {
491 theme::accent()
492 } else {
493 theme::border_strong()
494 };
495 let border_width = if mention { TOAST_BORDER_MENTION } else { 1. };
496 let hovered = model.hovered;
497 let content = match model.reply {
498 ReplyState::Sent | ReplyState::Failed => self.confirmation(&model, cx),
499 ReplyState::Open => v_flex()
500 .w_full()
501 .child(
502 h_flex()
503 .w_full()
504 .gap(px(12.))
505 .items_start()
506 .child(self.avatar(&model, fill, cx))
507 .child(self.body(&model, &text, true)),
508 )
509 .child(self.reply_field(window, cx)),
510 ReplyState::Closed => v_flex()
511 .w_full()
512 .child(
513 h_flex()
514 .w_full()
515 .gap(px(12.))
516 .items_start()
517 .child(self.avatar(&model, fill, cx))
518 .child(self.body(&model, &text, hovered)),
519 )
520 .child(self.action_row(mention, cx)),
521 };
522 let center = self.center.clone();
523 let hover_center = self.center.clone();
524 div()
525 .id(("toast", id))
526 .track_focus(&self.focus_handle)
527 .relative()
528 .size_full()
529 .opacity(model.opacity(Instant::now()))
530 .rounded(px(TOAST_RADIUS))
531 .bg(fill)
532 .border(px(border_width))
533 .border_color(border)
534 .overflow_hidden()
535 .cursor_pointer()
536 .on_hover(move |is_hovered, _, cx| {
537 hover_center.update(cx, |center, cx| center.set_hover(id, *is_hovered, cx));
538 })
539 .on_click(move |_, _, cx| {
540 center.update(cx, |center, cx| center.activate(id, cx));
541 })
542 .capture_action(cx.listener(|this, _: &gpui_kit::component::input::Escape, window, cx| {
543 this.close_reply(window, cx);
544 cx.stop_propagation();
545 }))
546 .on_key_down(cx.listener(|this, event: &KeyDownEvent, _, cx| {
547 if event.keystroke.key == "escape" {
548 let id = this.id;
549 this.center.update(cx, |center, cx| center.dismiss(id, cx));
550 }
551 }))
552 .child(div().size_full().p(px(12.)).child(content))
553 .into_any_element()
554 }
555}
556
557pub struct PillView {
558 center: Entity<NotificationCenter>,
559 _subscription: Subscription,
560}
561
562impl PillView {
563 pub fn new(center: Entity<NotificationCenter>, cx: &mut Context<Self>) -> Self {
564 let subscription = cx.observe(&center, |_, _, cx| cx.notify());
565 PillView {
566 center,
567 _subscription: subscription,
568 }
569 }
570}
571
572impl Render for PillView {
573 fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
574 let (queued, opacity) = {
575 let center = self.center.read(cx);
576 (center.queued_count(), center.stack_opacity())
577 };
578 let center = self.center.clone();
579 h_flex()
580 .id("toast-hide-all")
581 .size_full()
582 .gap(px(6.))
583 .items_center()
584 .justify_center()
585 .opacity(opacity)
586 .rounded(px(TOAST_RADIUS))
587 .bg(theme::surface_raised())
588 .border_1()
589 .border_color(theme::border_strong())
590 .cursor_pointer()
591 .hover(|bar| bar.bg(theme::border_strong()))
592 .text_size(px(12.5))
593 .font_weight(FontWeight::SEMIBOLD)
594 .text_color(theme::text_soft())
595 .child("Hide all")
596 .when(queued > 0, |bar| {
597 bar.child(
598 div()
599 .text_color(theme::text_muted())
600 .child(format!("{queued} more waiting")),
601 )
602 })
603 .on_click(move |_, _, cx| {
604 center.update(cx, |center, cx| center.hide_all(cx));
605 })
606 }
607}