BlueGit

ict rusty-teams

Files

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

toast.rs

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