BlueGit

ict rusty-teams

Files

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

toast.rs

9c0a9281 History
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_ROW;
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 PROGRESS_HEIGHT: f32 = 3.;
26const TOAST_BORDER_MENTION: f32 = 2.;
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, 4)
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 pill_button(label: &'static str, symbol_name: &'static str, accent: bool) -> Stateful<Div> {
120 let color = if accent {
121 theme::accent_text()
122 } else {
123 theme::text_soft()
124 };
125 h_flex()
126 .id(label)
127 .h(px(ACTION_ROW))
128 .px(px(14.))
129 .gap(px(6.))
130 .items_center()
131 .justify_center()
132 .rounded(px(8.))
133 .border_1()
134 .border_color(theme::border_strong())
135 .cursor_pointer()
136 .text_size(px(13.))
137 .font_weight(FontWeight::SEMIBOLD)
138 .text_color(color)
139 .hover(|button| button.bg(theme::row_hover()))
140 .child(symbol(symbol_name, 18., color))
141 .child(label)
142}
143
144fn highlighted_preview(text: &str, mention: bool) -> AnyElement {
145 let mut styled = StyledText::new(text.to_owned());
146 if mention {
147 let style = HighlightStyle {
148 color: Some(theme::accent_soft()),
149 font_weight: Some(FontWeight::SEMIBOLD),
150 ..Default::default()
151 };
152 let highlights: Vec<_> = mention_ranges(text)
153 .into_iter()
154 .map(|range| (range, style))
155 .collect();
156 styled = styled.with_highlights(highlights);
157 }
158 div()
159 .text_size(px(13.))
160 .line_height(px(18.))
161 .text_color(theme::text())
162 .line_clamp(2)
163 .child(styled)
164 .into_any_element()
165}
166
167fn side_chip(label: String, mention: bool) -> Div {
168 div()
169 .h(px(20.))
170 .px(px(8.))
171 .flex()
172 .items_center()
173 .rounded(px(10.))
174 .bg(if mention {
175 theme::bubble_own()
176 } else {
177 theme::border_strong()
178 })
179 .text_color(if mention {
180 theme::accent_soft()
181 } else {
182 theme::text()
183 })
184 .text_size(px(11.))
185 .line_height(px(20.))
186 .font_weight(FontWeight::SEMIBOLD)
187 .child(label)
188}
189
190impl ToastView {
191 fn avatar(&self, model: &ToastModel, fill: Hsla, cx: &App) -> AnyElement {
192 let center = self.center.read(cx);
193 let directory = center.directory(cx);
194 let sender = Face {
195 user_id: model.sender_id.clone(),
196 name: model.sender_name.clone(),
197 };
198 match &model.kind {
199 ChatKind::Group { .. } => {
200 let chat = Face {
201 user_id: None,
202 name: model.chat_title.clone(),
203 };
204 spec_avatar(directory, &AvatarSpec::Pair(sender, chat), AVATAR_SIZE, fill)
205 }
206 _ => {
207 let presence = model
208 .sender_id
209 .as_deref()
210 .map(|user_id| directory.presence_of(user_id))
211 .unwrap_or(crate::data::PresenceKind::Unknown);
212 let face = spec_avatar(directory, &AvatarSpec::Single(sender), AVATAR_SIZE, fill);
213 with_presence(face, presence, AVATAR_SIZE, fill).into_any_element()
214 }
215 }
216 }
217
218 fn side_slot(&self, model: &ToastModel, hovered: bool) -> AnyElement {
219 let id = self.id;
220 if hovered {
221 let center = self.center.clone();
222 return div()
223 .id("toast-close")
224 .size(px(CLOSE_SIZE))
225 .flex_none()
226 .flex()
227 .items_center()
228 .justify_center()
229 .rounded_full()
230 .bg(theme::border_strong())
231 .cursor_pointer()
232 .hover(|button| button.bg(theme::text_faint()))
233 .child(symbol("close", 16., theme::text()))
234 .on_click(move |_, _, cx| {
235 cx.stop_propagation();
236 center.update(cx, |center, cx| center.dismiss(id, cx));
237 })
238 .into_any_element();
239 }
240 if model.mentions_me {
241 return side_chip("Mention".to_owned(), true).into_any_element();
242 }
243 if model.count > 1 {
244 return side_chip(format!("{} new", model.count), false).into_any_element();
245 }
246 div()
247 .text_size(px(12.))
248 .text_color(theme::text_muted())
249 .child(model.time.clone())
250 .into_any_element()
251 }
252
253 fn body(&self, model: &ToastModel, text: &ToastText, hovered: bool) -> Div {
254 let subtitle_icon = match &model.kind {
255 ChatKind::Group { .. } => Some("group"),
256 ChatKind::Channel { .. } => Some("tag"),
257 ChatKind::Direct => None,
258 };
259 let preview = if text.image_only {
260 h_flex()
261 .gap(px(6.))
262 .items_center()
263 .child(symbol("image", 16., theme::text_muted()))
264 .child(
265 div()
266 .text_size(px(13.))
267 .text_color(theme::text())
268 .child(text.preview.clone()),
269 )
270 .into_any_element()
271 } else {
272 highlighted_preview(&text.preview, model.mentions_me)
273 };
274 v_flex()
275 .flex_1()
276 .min_w_0()
277 .gap(px(2.))
278 .child(
279 h_flex()
280 .w_full()
281 .gap(px(8.))
282 .items_center()
283 .justify_between()
284 .child(
285 div()
286 .flex_1()
287 .min_w_0()
288 .truncate()
289 .text_size(px(14.))
290 .line_height(px(20.))
291 .font_weight(FontWeight::SEMIBOLD)
292 .text_color(theme::text())
293 .child(text.title.clone()),
294 )
295 .child(div().flex_none().child(self.side_slot(model, hovered))),
296 )
297 .child(
298 h_flex()
299 .gap(px(4.))
300 .items_center()
301 .h(px(16.))
302 .text_size(px(12.))
303 .text_color(theme::text_muted())
304 .children(subtitle_icon.map(|name| symbol(name, 14., theme::text_muted())))
305 .child(div().truncate().child(text.subtitle.clone())),
306 )
307 .child(preview)
308 }
309
310 fn action_row(&self, cx: &mut Context<Self>) -> Div {
311 let id = self.id;
312 let center = self.center.clone();
313 h_flex()
314 .w_full()
315 .mt(px(4.))
316 .justify_between()
317 .child(pill_button("Mark as read", "done", false).on_click(move |_, _, cx| {
318 cx.stop_propagation();
319 center.update(cx, |center, cx| center.mark_read(id, cx));
320 }))
321 .child(
322 pill_button("Reply", "reply", true).on_click(cx.listener(
323 |this, _, window, cx| {
324 cx.stop_propagation();
325 this.open_reply(window, cx);
326 },
327 )),
328 )
329 }
330
331 fn reply_field(&self, window: &Window, cx: &mut Context<Self>) -> Div {
332 let focused = self.input.focus_handle(cx).contains_focused(window, cx);
333 let empty = self.input.read(cx).value().trim().is_empty();
334 let send = div()
335 .id("toast-send")
336 .size(px(ACTION_ROW))
337 .flex_none()
338 .flex()
339 .items_center()
340 .justify_center()
341 .rounded(px(8.))
342 .bg(theme::accent())
343 .child(symbol("arrow_upward", 20., theme::on_accent()))
344 .when(empty, |button| button.opacity(0.4))
345 .when(!empty, |button| {
346 button
347 .cursor_pointer()
348 .hover(|button| button.bg(theme::accent_text()))
349 .on_click(cx.listener(|this, _, window, cx| {
350 cx.stop_propagation();
351 this.submit(window, cx);
352 }))
353 });
354 let hint = |keys: &'static str, action: &'static str| {
355 h_flex()
356 .gap(px(4.))
357 .child(div().text_color(theme::text_soft()).child(keys))
358 .child(action)
359 };
360 v_flex()
361 .w_full()
362 .mt(px(8.))
363 .gap(px(4.))
364 .child(
365 h_flex()
366 .w_full()
367 .h(px(ACTION_ROW))
368 .gap(px(6.))
369 .items_center()
370 .child(
371 div()
372 .flex_1()
373 .min_w_0()
374 .h(px(ACTION_ROW))
375 .px(px(10.))
376 .flex()
377 .items_center()
378 .rounded(px(8.))
379 .bg(theme::surface())
380 .border_1()
381 .border_color(if focused {
382 theme::accent()
383 } else {
384 theme::border_strong()
385 })
386 .child(
387 Textarea::new(&self.input)
388 .appearance(false)
389 .bordered(false),
390 ),
391 )
392 .child(send),
393 )
394 .child(
395 h_flex()
396 .h(px(16.))
397 .gap(px(12.))
398 .text_size(px(11.))
399 .text_color(theme::text_muted())
400 .child(hint("Enter", "senden"))
401 .child(hint("Shift+Enter", "new line"))
402 .child(hint("Esc", "close")),
403 )
404 }
405
406 fn confirmation(&self, model: &ToastModel, cx: &mut Context<Self>) -> Div {
407 let sent = model.reply == ReplyState::Sent;
408 let label = if sent {
409 format!("Sent to {}", target_name(model))
410 } else {
411 "Couldn't send".to_owned()
412 };
413 let (icon_name, tone) = if sent {
414 ("check_circle", theme::green())
415 } else {
416 ("error", theme::red_soft())
417 };
418 h_flex()
419 .size_full()
420 .gap(px(10.))
421 .items_center()
422 .child(symbol(icon_name, 22., tone))
423 .child(
424 div()
425 .flex_1()
426 .min_w_0()
427 .truncate()
428 .text_size(px(14.))
429 .font_weight(FontWeight::SEMIBOLD)
430 .text_color(theme::text())
431 .child(label),
432 )
433 .when(sent, |row| {
434 row.child(
435 div()
436 .text_size(px(12.))
437 .text_color(theme::text_muted())
438 .child(model.time.clone()),
439 )
440 })
441 .when(!sent, |row| {
442 row.child(pill_button("Retry", "reply", true).on_click(cx.listener(
443 |this, _, _, cx| {
444 cx.stop_propagation();
445 this.retry(cx);
446 },
447 )))
448 })
449 }
450}
451
452fn target_name(model: &ToastModel) -> String {
453 match &model.kind {
454 ChatKind::Group { .. } => model.chat_title.clone(),
455 _ => model.sender_name.clone(),
456 }
457}
458
459impl Render for ToastView {
460 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
461 let id = self.id;
462 let (model, preview_on) = {
463 let center = self.center.read(cx);
464 (center.model(id).cloned(), center.settings().preview)
465 };
466 let Some(model) = model else {
467 return div()
468 .size_full()
469 .bg(theme::surface_raised())
470 .into_any_element();
471 };
472 let text = describe(&model, preview_on);
473 let narration = text.narration();
474 if narration != self.last_title {
475 window.set_window_title(&narration);
476 self.last_title = narration;
477 }
478 let mention = model.mentions_me;
479 let fill = if mention {
480 theme::toast_mention_fill()
481 } else {
482 theme::surface_raised()
483 };
484 let border = if mention {
485 theme::accent()
486 } else {
487 theme::border_strong()
488 };
489 let border_width = if mention { TOAST_BORDER_MENTION } else { 1. };
490 let fraction = model.timer.fraction_left(Instant::now());
491 let bar_color = if model.timer.is_paused() {
492 theme::text_faint()
493 } else {
494 theme::accent()
495 };
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 .when(hovered, |column| column.child(self.action_row(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 .bg(fill)
530 .border(px(border_width))
531 .border_color(border)
532 .overflow_hidden()
533 .cursor_pointer()
534 .on_hover(move |is_hovered, _, cx| {
535 hover_center.update(cx, |center, cx| center.set_hover(id, *is_hovered, cx));
536 })
537 .on_click(move |_, _, cx| {
538 center.update(cx, |center, cx| center.activate(id, cx));
539 })
540 .capture_action(cx.listener(|this, _: &gpui_kit::component::input::Escape, window, cx| {
541 this.close_reply(window, cx);
542 cx.stop_propagation();
543 }))
544 .on_key_down(cx.listener(|this, event: &KeyDownEvent, _, cx| {
545 if event.keystroke.key == "escape" {
546 let id = this.id;
547 this.center.update(cx, |center, cx| center.dismiss(id, cx));
548 }
549 }))
550 .child(div().size_full().p(px(12.)).child(content))
551 .child(
552 div()
553 .absolute()
554 .left_0()
555 .bottom_0()
556 .h(px(PROGRESS_HEIGHT))
557 .w(relative(fraction))
558 .bg(bar_color),
559 )
560 .into_any_element()
561 }
562}
563
564pub struct PillView {
565 center: Entity<NotificationCenter>,
566 _subscription: Subscription,
567}
568
569impl PillView {
570 pub fn new(center: Entity<NotificationCenter>, cx: &mut Context<Self>) -> Self {
571 let subscription = cx.observe(&center, |_, _, cx| cx.notify());
572 PillView {
573 center,
574 _subscription: subscription,
575 }
576 }
577}
578
579impl Render for PillView {
580 fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
581 let hidden = self.center.read(cx).hidden_count();
582 let center = self.center.clone();
583 div()
584 .id("toast-pill")
585 .size_full()
586 .flex()
587 .items_center()
588 .justify_center()
589 .bg(theme::surface_raised())
590 .border_1()
591 .border_color(theme::border_strong())
592 .cursor_pointer()
593 .text_size(px(13.))
594 .font_weight(FontWeight::SEMIBOLD)
595 .text_color(theme::text())
596 .child(format!("+{hidden} more"))
597 .on_click(move |_, _, cx| {
598 center.update(cx, |center, cx| center.open_chat_list(cx));
599 })
600 }
601}