BlueGit

ict rusty-teams

feat(app): toast queue, hide-all bar and fade-out like Telegram

- Max 3 toasts on screen. More wait in a queue and start their timer only when shown.
- A full-width Hide all bar sits above the stack from 2 toasts on. It names the waiting count.
- A toast fades out over 4 s (ease-in-circ). Hovering any toast pauses the whole stack and brings fading ones back.
- X, Hide all, Mark as read and opening the chat fade out in 150 ms.
- Toast windows are transparent and rounded by GPUI, so the fade covers the border too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

metis Daniel Däschle · pushed by metis

Authored 2026-10-07 · 7 weeks ago

Hash
ad89b0b541f615b178628af6d87fbdd7fe279115
Parent
9ae4b0b2
7 files changed +362 −88
crates/app/src/notify/center.rs +92 −47
@@ -15,7 +15,9 @@ use super::rules::{
15 15 };
16 16 use super::settings;
17 17 use super::settings_view::SettingsView;
18 use super::stack::{ReplyState, SENT_DURATION, ToastModel, ToastStack};
18 use super::stack::{
19 FADE_IN, FAST_FADE, ReplyState, SENT_DURATION, SLOW_FADE, ToastModel, ToastStack,
20 };
19 21 use super::toast::{PillView, ToastView};
20 22 use crate::app_state::{AppEvent, AppState, Selection};
21 23 use crate::data::{self, Directory, PresenceKind};
@@ -25,14 +27,11 @@ const TICK: Duration = Duration::from_millis(33);
25 27 const SLIDE_DURATION: Duration = Duration::from_millis(180);
26 28 const SLIDE_DISTANCE: f32 = 48.;
27 29 const DEMO_SPEC_DELAY: Duration = Duration::from_secs(3);
28 const FALLBACK_BORDER: u32 = 0x3f3f42;
29 const MENTION_BORDER: u32 = 0xce6a3b;
30 30
31 31 struct OpenWindow {
32 32 handle: AnyWindowHandle,
33 33 native: Option<NativeHandle>,
34 34 opened: Instant,
35 mention: bool,
36 35 }
37 36
38 37 impl OpenWindow {
@@ -57,6 +56,7 @@ pub struct NotificationCenter {
57 56 opening: HashSet<u64>,
58 57 pill: Option<OpenWindow>,
59 58 pill_opening: bool,
59 hover_hold: bool,
60 60 settings_window: Option<AnyWindowHandle>,
61 61 settings_opening: bool,
62 62 unread_mentions: HashSet<String>,
@@ -88,6 +88,7 @@ impl NotificationCenter {
88 88 opening: HashSet::new(),
89 89 pill: None,
90 90 pill_opening: false,
91 hover_hold: false,
91 92 settings_window: None,
92 93 settings_opening: false,
93 94 unread_mentions: HashSet::new(),
@@ -125,8 +126,21 @@ impl NotificationCenter {
125 126 self.stack.get(id)
126 127 }
127 128
128 pub fn hidden_count(&self) -> usize {
129 self.stack.hidden_count()
129 pub fn queued_count(&self) -> usize {
130 self.stack.queued_count()
131 }
132
133 pub fn stack_opacity(&self) -> f32 {
134 let now = Instant::now();
135 self.stack
136 .visible()
137 .iter()
138 .map(|toast| toast.opacity(now))
139 .fold(0., f32::max)
140 }
141
142 fn animated(&self, duration: Duration) -> Duration {
143 if self.animations { duration } else { Duration::ZERO }
130 144 }
131 145
132 146 pub fn directory<'a>(&self, cx: &'a App) -> &'a Directory {
@@ -296,12 +310,12 @@ impl NotificationCenter {
296 310 .iter()
297 311 .map(|toast| layout::toast_height(toast, self.settings.preview))
298 312 .collect();
299 let hidden = self.stack.hidden_count();
300 let slots = layout::stack_slots(area, self.settings.corner, &heights, hidden > 0);
313 let with_hide_all = visible.len() > 1 || self.stack.queued_count() > 0;
314 let slots = layout::stack_slots(area, self.settings.corner, &heights, with_hide_all);
301 315 for (id, slot) in visible.iter().zip(slots.toasts.iter()) {
302 316 self.place_toast(*id, *slot, area, cx);
303 317 }
304 self.sync_pill(slots.pill, area, hidden, cx);
318 self.sync_pill(slots.pill, area, cx);
305 319 }
306 320
307 321 fn slide_offset(&self, opened: Instant, area: WorkArea) -> i32 {
@@ -319,7 +333,6 @@ impl NotificationCenter {
319 333 }
320 334
321 335 fn place_toast(&mut self, id: u64, slot: Slot, area: WorkArea, cx: &mut Context<Self>) {
322 let mention = self.stack.get(id).is_some_and(|toast| toast.mentions_me);
323 336 if !self.windows.contains_key(&id) {
324 337 self.open_toast_window(id, slot, area, cx);
325 338 return;
@@ -329,14 +342,10 @@ impl NotificationCenter {
329 342 .get(&id)
330 343 .map(|window| self.slide_offset(window.opened, area))
331 344 .unwrap_or(0);
332 if let Some(window) = self.windows.get_mut(&id)
345 if let Some(window) = self.windows.get(&id)
333 346 && let Some(native) = window.native
334 347 {
335 348 window.place(native, slot, slot.x + offset, cx);
336 if window.mention != mention {
337 window.mention = mention;
338 platform::set_border_color(native, border_color(mention));
339 }
340 349 }
341 350 }
342 351
@@ -344,20 +353,15 @@ impl NotificationCenter {
344 353 if !self.opening.insert(id) {
345 354 return;
346 355 }
347 let mention = self.stack.get(id).is_some_and(|toast| toast.mentions_me);
348 356 open_popup(
349 357 popup_options(slot, area),
350 358 cx,
351 359 move |center, window, cx| cx.new(|cx| ToastView::new(center, id, window, cx)),
352 360 move |this, window| {
353 361 this.opening.remove(&id);
354 let Some(window) = window else {
355 return;
356 };
357 if let Some(native) = window.native {
358 platform::set_border_color(native, border_color(mention));
362 if let Some(window) = window {
363 this.windows.insert(id, window);
359 364 }
360 this.windows.insert(id, OpenWindow { mention, ..window });
361 365 },
362 366 );
363 367 }
@@ -366,10 +370,9 @@ impl NotificationCenter {
366 370 &mut self,
367 371 slot: Option<Slot>,
368 372 area: WorkArea,
369 hidden: usize,
370 373 cx: &mut Context<Self>,
371 374 ) {
372 let Some(slot) = slot.filter(|_| hidden > 0) else {
375 let Some(slot) = slot else {
373 376 if let Some(pill) = self.pill.take() {
374 377 pill.handle
375 378 .update(cx, |_, window, _| window.remove_window())
@@ -385,9 +388,6 @@ impl NotificationCenter {
385 388 |center, _, cx| cx.new(|cx| PillView::new(center, cx)),
386 389 |this, window| {
387 390 this.pill_opening = false;
388 if let Some(native) = window.as_ref().and_then(|window| window.native) {
389 platform::set_border_color(native, border_color(false));
390 }
391 391 this.pill = window;
392 392 },
393 393 );
@@ -412,15 +412,55 @@ impl NotificationCenter {
412 412 }
413 413
414 414 fn drop_toast(&mut self, id: u64, cx: &mut Context<Self>) {
415 self.stack.remove(id);
415 let fast = self.animated(FAST_FADE);
416 self.stack.dismiss(id, Instant::now(), fast);
416 417 cx.notify();
417 418 }
418 419
420 pub fn hide_all(&mut self, cx: &mut Context<Self>) {
421 for id in self.stack.ids() {
422 self.drop_toast(id, cx);
423 }
424 }
425
426 fn sync_hover_hold(&mut self, now: Instant) {
427 let any_hovered = self.stack.any_hovered();
428 if !any_hovered && !self.hover_hold {
429 return;
430 }
431 if any_hovered {
432 let fast = self.animated(FAST_FADE);
433 self.stack.restore(now, fast);
434 }
435 for id in self.stack.ids() {
436 if let Some(toast) = self.stack.get_mut(id)
437 && !toast.queued
438 {
439 if any_hovered {
440 toast.timer.pause(now);
441 } else if !toast.holds_open() {
442 toast.timer.resume(now);
443 }
444 }
445 }
446 self.hover_hold = any_hovered;
447 }
448
449 fn is_dismissed(&self, id: u64) -> bool {
450 self.stack.get(id).is_none_or(|toast| toast.dismissed)
451 }
452
419 453 fn tick(&mut self, cx: &mut Context<Self>) {
420 454 let now = Instant::now();
455 let (fade_in, slow) = (self.animated(FADE_IN), self.animated(SLOW_FADE));
421 456 for id in self.stack.expired(now) {
457 self.stack.fade_out(id, now, slow);
458 }
459 for id in self.stack.gone(now) {
422 460 self.stack.remove(id);
423 461 }
462 self.stack.promote(now, fade_in);
463 self.sync_hover_hold(now);
424 464 if !self.stack.is_empty() || !self.windows.is_empty() || self.pill.is_some() {
425 465 self.sync_windows(cx);
426 466 cx.notify();
@@ -499,11 +539,7 @@ impl NotificationCenter {
499 539 return;
500 540 }
501 541 toast.hovered = hovered;
502 if hovered {
503 toast.timer.pause(now);
504 } else if !toast.holds_open() {
505 toast.timer.resume(now);
506 }
542 self.sync_hover_hold(now);
507 543 cx.notify();
508 544 }
509 545
@@ -599,6 +635,9 @@ impl NotificationCenter {
599 635 }
600 636
601 637 pub fn mark_read(&mut self, id: u64, cx: &mut Context<Self>) {
638 if self.is_dismissed(id) {
639 return;
640 }
602 641 let Some(conversation_id) = self
603 642 .stack
604 643 .get(id)
@@ -611,6 +650,9 @@ impl NotificationCenter {
611 650 }
612 651
613 652 pub fn activate(&mut self, id: u64, cx: &mut Context<Self>) {
653 if self.is_dismissed(id) {
654 return;
655 }
614 656 let Some((conversation_id, message_id)) = self
615 657 .stack
616 658 .get(id)
@@ -628,10 +670,6 @@ impl NotificationCenter {
628 670 }
629 671 }
630 672
631 pub fn open_chat_list(&mut self, cx: &mut Context<Self>) {
632 self.raise_main_window(cx);
633 }
634
635 673 pub fn open_settings(&mut self, cx: &mut Context<Self>) {
636 674 if let Some(handle) = self.settings_window {
637 675 if handle
@@ -705,14 +743,6 @@ impl NotificationCenter {
705 743 }
706 744 }
707 745
708 fn border_color(mention: bool) -> u32 {
709 if mention {
710 MENTION_BORDER
711 } else {
712 FALLBACK_BORDER
713 }
714 }
715
716 746 /// Opens a window outside the current update: `open_window` draws right away, and every
717 747 /// notification view reads the center while rendering.
718 748 fn open_popup<V: Render>(
@@ -741,7 +771,6 @@ fn open_popup<V: Render>(
741 771 handle,
742 772 native,
743 773 opened: Instant::now(),
744 mention: false,
745 774 };
746 775 center.update(cx, |this, _| opened(this, Some(window)));
747 776 });
@@ -765,7 +794,7 @@ fn popup_options(slot: Slot, area: WorkArea) -> WindowOptions {
765 794 is_movable: false,
766 795 is_resizable: false,
767 796 is_minimizable: false,
768 window_background: WindowBackgroundAppearance::Opaque,
797 window_background: WindowBackgroundAppearance::Transparent,
769 798 ..Default::default()
770 799 }
771 800 }
@@ -841,6 +870,22 @@ fn demo_incoming() -> Vec<Incoming> {
841 870 "@Jonas can you approve the merge? The pipeline is waiting.",
842 871 true,
843 872 ),
873 item(
874 "demo-toast-lea",
875 ChatKind::Direct,
876 "Lea Schneider",
877 "Lea Schneider",
878 "Lunch at 12?",
879 false,
880 ),
881 item(
882 "demo-toast-jonas",
883 ChatKind::Direct,
884 "Jonas Ortega",
885 "Jonas Ortega",
886 "Deploy window moved to Thursday 6 pm.",
887 false,
888 ),
844 889 ]
845 890 }
846 891
@@ -15,7 +15,9 @@ use super::rules::{
15};
15};
16use super::settings;
16use super::settings;
17use super::settings_view::SettingsView;
17use super::settings_view::SettingsView;
18use super::stack::{ReplyState, SENT_DURATION, ToastModel, ToastStack};
18use super::stack::{
19 FADE_IN, FAST_FADE, ReplyState, SENT_DURATION, SLOW_FADE, ToastModel, ToastStack,
20};
19use super::toast::{PillView, ToastView};
21use super::toast::{PillView, ToastView};
20use crate::app_state::{AppEvent, AppState, Selection};
22use crate::app_state::{AppEvent, AppState, Selection};
21use crate::data::{self, Directory, PresenceKind};
23use crate::data::{self, Directory, PresenceKind};
@@ -25,14 +27,11 @@ const TICK: Duration = Duration::from_millis(33);
25const SLIDE_DURATION: Duration = Duration::from_millis(180);
27const SLIDE_DURATION: Duration = Duration::from_millis(180);
26const SLIDE_DISTANCE: f32 = 48.;
28const SLIDE_DISTANCE: f32 = 48.;
27const DEMO_SPEC_DELAY: Duration = Duration::from_secs(3);
29const DEMO_SPEC_DELAY: Duration = Duration::from_secs(3);
28const FALLBACK_BORDER: u32 = 0x3f3f42;
29const MENTION_BORDER: u32 = 0xce6a3b;
30
30
31struct OpenWindow {
31struct OpenWindow {
32 handle: AnyWindowHandle,
32 handle: AnyWindowHandle,
33 native: Option<NativeHandle>,
33 native: Option<NativeHandle>,
34 opened: Instant,
34 opened: Instant,
35 mention: bool,
36}
35}
37
36
38impl OpenWindow {
37impl OpenWindow {
@@ -57,6 +56,7 @@ pub struct NotificationCenter {
57 opening: HashSet<u64>,
56 opening: HashSet<u64>,
58 pill: Option<OpenWindow>,
57 pill: Option<OpenWindow>,
59 pill_opening: bool,
58 pill_opening: bool,
59 hover_hold: bool,
60 settings_window: Option<AnyWindowHandle>,
60 settings_window: Option<AnyWindowHandle>,
61 settings_opening: bool,
61 settings_opening: bool,
62 unread_mentions: HashSet<String>,
62 unread_mentions: HashSet<String>,
@@ -88,6 +88,7 @@ impl NotificationCenter {
88 opening: HashSet::new(),
88 opening: HashSet::new(),
89 pill: None,
89 pill: None,
90 pill_opening: false,
90 pill_opening: false,
91 hover_hold: false,
91 settings_window: None,
92 settings_window: None,
92 settings_opening: false,
93 settings_opening: false,
93 unread_mentions: HashSet::new(),
94 unread_mentions: HashSet::new(),
@@ -125,8 +126,21 @@ impl NotificationCenter {
125 self.stack.get(id)
126 self.stack.get(id)
126 }
127 }
127
128
128 pub fn hidden_count(&self) -> usize {
129 pub fn queued_count(&self) -> usize {
129 self.stack.hidden_count()
130 self.stack.queued_count()
131 }
132
133 pub fn stack_opacity(&self) -> f32 {
134 let now = Instant::now();
135 self.stack
136 .visible()
137 .iter()
138 .map(|toast| toast.opacity(now))
139 .fold(0., f32::max)
140 }
141
142 fn animated(&self, duration: Duration) -> Duration {
143 if self.animations { duration } else { Duration::ZERO }
130 }
144 }
131
145
132 pub fn directory<'a>(&self, cx: &'a App) -> &'a Directory {
146 pub fn directory<'a>(&self, cx: &'a App) -> &'a Directory {
@@ -296,12 +310,12 @@ impl NotificationCenter {
296 .iter()
310 .iter()
297 .map(|toast| layout::toast_height(toast, self.settings.preview))
311 .map(|toast| layout::toast_height(toast, self.settings.preview))
298 .collect();
312 .collect();
299 let hidden = self.stack.hidden_count();
313 let with_hide_all = visible.len() > 1 || self.stack.queued_count() > 0;
300 let slots = layout::stack_slots(area, self.settings.corner, &heights, hidden > 0);
314 let slots = layout::stack_slots(area, self.settings.corner, &heights, with_hide_all);
301 for (id, slot) in visible.iter().zip(slots.toasts.iter()) {
315 for (id, slot) in visible.iter().zip(slots.toasts.iter()) {
302 self.place_toast(*id, *slot, area, cx);
316 self.place_toast(*id, *slot, area, cx);
303 }
317 }
304 self.sync_pill(slots.pill, area, hidden, cx);
318 self.sync_pill(slots.pill, area, cx);
305 }
319 }
306
320
307 fn slide_offset(&self, opened: Instant, area: WorkArea) -> i32 {
321 fn slide_offset(&self, opened: Instant, area: WorkArea) -> i32 {
@@ -319,7 +333,6 @@ impl NotificationCenter {
319 }
333 }
320
334
321 fn place_toast(&mut self, id: u64, slot: Slot, area: WorkArea, cx: &mut Context<Self>) {
335 fn place_toast(&mut self, id: u64, slot: Slot, area: WorkArea, cx: &mut Context<Self>) {
322 let mention = self.stack.get(id).is_some_and(|toast| toast.mentions_me);
323 if !self.windows.contains_key(&id) {
336 if !self.windows.contains_key(&id) {
324 self.open_toast_window(id, slot, area, cx);
337 self.open_toast_window(id, slot, area, cx);
325 return;
338 return;
@@ -329,14 +342,10 @@ impl NotificationCenter {
329 .get(&id)
342 .get(&id)
330 .map(|window| self.slide_offset(window.opened, area))
343 .map(|window| self.slide_offset(window.opened, area))
331 .unwrap_or(0);
344 .unwrap_or(0);
332 if let Some(window) = self.windows.get_mut(&id)
345 if let Some(window) = self.windows.get(&id)
333 && let Some(native) = window.native
346 && let Some(native) = window.native
334 {
347 {
335 window.place(native, slot, slot.x + offset, cx);
348 window.place(native, slot, slot.x + offset, cx);
336 if window.mention != mention {
337 window.mention = mention;
338 platform::set_border_color(native, border_color(mention));
339 }
340 }
349 }
341 }
350 }
342
351
@@ -344,20 +353,15 @@ impl NotificationCenter {
344 if !self.opening.insert(id) {
353 if !self.opening.insert(id) {
345 return;
354 return;
346 }
355 }
347 let mention = self.stack.get(id).is_some_and(|toast| toast.mentions_me);
348 open_popup(
356 open_popup(
349 popup_options(slot, area),
357 popup_options(slot, area),
350 cx,
358 cx,
351 move |center, window, cx| cx.new(|cx| ToastView::new(center, id, window, cx)),
359 move |center, window, cx| cx.new(|cx| ToastView::new(center, id, window, cx)),
352 move |this, window| {
360 move |this, window| {
353 this.opening.remove(&id);
361 this.opening.remove(&id);
354 let Some(window) = window else {
362 if let Some(window) = window {
355 return;
363 this.windows.insert(id, window);
356 };
357 if let Some(native) = window.native {
358 platform::set_border_color(native, border_color(mention));
359 }
364 }
360 this.windows.insert(id, OpenWindow { mention, ..window });
361 },
365 },
362 );
366 );
363 }
367 }
@@ -366,10 +370,9 @@ impl NotificationCenter {
366 &mut self,
370 &mut self,
367 slot: Option<Slot>,
371 slot: Option<Slot>,
368 area: WorkArea,
372 area: WorkArea,
369 hidden: usize,
370 cx: &mut Context<Self>,
373 cx: &mut Context<Self>,
371 ) {
374 ) {
372 let Some(slot) = slot.filter(|_| hidden > 0) else {
375 let Some(slot) = slot else {
373 if let Some(pill) = self.pill.take() {
376 if let Some(pill) = self.pill.take() {
374 pill.handle
377 pill.handle
375 .update(cx, |_, window, _| window.remove_window())
378 .update(cx, |_, window, _| window.remove_window())
@@ -385,9 +388,6 @@ impl NotificationCenter {
385 |center, _, cx| cx.new(|cx| PillView::new(center, cx)),
388 |center, _, cx| cx.new(|cx| PillView::new(center, cx)),
386 |this, window| {
389 |this, window| {
387 this.pill_opening = false;
390 this.pill_opening = false;
388 if let Some(native) = window.as_ref().and_then(|window| window.native) {
389 platform::set_border_color(native, border_color(false));
390 }
391 this.pill = window;
391 this.pill = window;
392 },
392 },
393 );
393 );
@@ -412,15 +412,55 @@ impl NotificationCenter {
412 }
412 }
413
413
414 fn drop_toast(&mut self, id: u64, cx: &mut Context<Self>) {
414 fn drop_toast(&mut self, id: u64, cx: &mut Context<Self>) {
415 self.stack.remove(id);
415 let fast = self.animated(FAST_FADE);
416 self.stack.dismiss(id, Instant::now(), fast);
416 cx.notify();
417 cx.notify();
417 }
418 }
418
419
420 pub fn hide_all(&mut self, cx: &mut Context<Self>) {
421 for id in self.stack.ids() {
422 self.drop_toast(id, cx);
423 }
424 }
425
426 fn sync_hover_hold(&mut self, now: Instant) {
427 let any_hovered = self.stack.any_hovered();
428 if !any_hovered && !self.hover_hold {
429 return;
430 }
431 if any_hovered {
432 let fast = self.animated(FAST_FADE);
433 self.stack.restore(now, fast);
434 }
435 for id in self.stack.ids() {
436 if let Some(toast) = self.stack.get_mut(id)
437 && !toast.queued
438 {
439 if any_hovered {
440 toast.timer.pause(now);
441 } else if !toast.holds_open() {
442 toast.timer.resume(now);
443 }
444 }
445 }
446 self.hover_hold = any_hovered;
447 }
448
449 fn is_dismissed(&self, id: u64) -> bool {
450 self.stack.get(id).is_none_or(|toast| toast.dismissed)
451 }
452
419 fn tick(&mut self, cx: &mut Context<Self>) {
453 fn tick(&mut self, cx: &mut Context<Self>) {
420 let now = Instant::now();
454 let now = Instant::now();
455 let (fade_in, slow) = (self.animated(FADE_IN), self.animated(SLOW_FADE));
421 for id in self.stack.expired(now) {
456 for id in self.stack.expired(now) {
457 self.stack.fade_out(id, now, slow);
458 }
459 for id in self.stack.gone(now) {
422 self.stack.remove(id);
460 self.stack.remove(id);
423 }
461 }
462 self.stack.promote(now, fade_in);
463 self.sync_hover_hold(now);
424 if !self.stack.is_empty() || !self.windows.is_empty() || self.pill.is_some() {
464 if !self.stack.is_empty() || !self.windows.is_empty() || self.pill.is_some() {
425 self.sync_windows(cx);
465 self.sync_windows(cx);
426 cx.notify();
466 cx.notify();
@@ -499,11 +539,7 @@ impl NotificationCenter {
499 return;
539 return;
500 }
540 }
501 toast.hovered = hovered;
541 toast.hovered = hovered;
502 if hovered {
542 self.sync_hover_hold(now);
503 toast.timer.pause(now);
504 } else if !toast.holds_open() {
505 toast.timer.resume(now);
506 }
507 cx.notify();
543 cx.notify();
508 }
544 }
509
545
@@ -599,6 +635,9 @@ impl NotificationCenter {
599 }
635 }
600
636
601 pub fn mark_read(&mut self, id: u64, cx: &mut Context<Self>) {
637 pub fn mark_read(&mut self, id: u64, cx: &mut Context<Self>) {
638 if self.is_dismissed(id) {
639 return;
640 }
602 let Some(conversation_id) = self
641 let Some(conversation_id) = self
603 .stack
642 .stack
604 .get(id)
643 .get(id)
@@ -611,6 +650,9 @@ impl NotificationCenter {
611 }
650 }
612
651
613 pub fn activate(&mut self, id: u64, cx: &mut Context<Self>) {
652 pub fn activate(&mut self, id: u64, cx: &mut Context<Self>) {
653 if self.is_dismissed(id) {
654 return;
655 }
614 let Some((conversation_id, message_id)) = self
656 let Some((conversation_id, message_id)) = self
615 .stack
657 .stack
616 .get(id)
658 .get(id)
@@ -628,10 +670,6 @@ impl NotificationCenter {
628 }
670 }
629 }
671 }
630
672
631 pub fn open_chat_list(&mut self, cx: &mut Context<Self>) {
632 self.raise_main_window(cx);
633 }
634
635 pub fn open_settings(&mut self, cx: &mut Context<Self>) {
673 pub fn open_settings(&mut self, cx: &mut Context<Self>) {
636 if let Some(handle) = self.settings_window {
674 if let Some(handle) = self.settings_window {
637 if handle
675 if handle
@@ -705,14 +743,6 @@ impl NotificationCenter {
705 }
743 }
706}
744}
707
745
708fn border_color(mention: bool) -> u32 {
709 if mention {
710 MENTION_BORDER
711 } else {
712 FALLBACK_BORDER
713 }
714}
715
716/// Opens a window outside the current update: `open_window` draws right away, and every
746/// Opens a window outside the current update: `open_window` draws right away, and every
717/// notification view reads the center while rendering.
747/// notification view reads the center while rendering.
718fn open_popup<V: Render>(
748fn open_popup<V: Render>(
@@ -741,7 +771,6 @@ fn open_popup<V: Render>(
741 handle,
771 handle,
742 native,
772 native,
743 opened: Instant::now(),
773 opened: Instant::now(),
744 mention: false,
745 };
774 };
746 center.update(cx, |this, _| opened(this, Some(window)));
775 center.update(cx, |this, _| opened(this, Some(window)));
747 });
776 });
@@ -765,7 +794,7 @@ fn popup_options(slot: Slot, area: WorkArea) -> WindowOptions {
765 is_movable: false,
794 is_movable: false,
766 is_resizable: false,
795 is_resizable: false,
767 is_minimizable: false,
796 is_minimizable: false,
768 window_background: WindowBackgroundAppearance::Opaque,
797 window_background: WindowBackgroundAppearance::Transparent,
769 ..Default::default()
798 ..Default::default()
770 }
799 }
771}
800}
@@ -841,6 +870,22 @@ fn demo_incoming() -> Vec<Incoming> {
841 "@Jonas can you approve the merge? The pipeline is waiting.",
870 "@Jonas can you approve the merge? The pipeline is waiting.",
842 true,
871 true,
843 ),
872 ),
873 item(
874 "demo-toast-lea",
875 ChatKind::Direct,
876 "Lea Schneider",
877 "Lea Schneider",
878 "Lunch at 12?",
879 false,
880 ),
881 item(
882 "demo-toast-jonas",
883 ChatKind::Direct,
884 "Jonas Ortega",
885 "Jonas Ortega",
886 "Deploy window moved to Thursday 6 pm.",
887 false,
888 ),
844 ]
889 ]
845}
890}
846
891
crates/app/src/notify/layout.rs +7 −5
@@ -3,8 +3,7 @@ use super::rules::{Corner, Preview};
3 3 use super::stack::{ReplyState, ToastModel};
4 4
5 5 pub const TOAST_WIDTH: f32 = 360.;
6 pub const PILL_WIDTH: f32 = 132.;
7 pub const PILL_HEIGHT: f32 = 32.;
6 pub const HIDE_ALL_HEIGHT: f32 = 36.;
8 7 pub const SIDE_MARGIN: f32 = 16.;
9 8 pub const EDGE_MARGIN: f32 = 12.;
10 9 pub const STACK_GAP: f32 = 8.;
@@ -67,7 +66,7 @@ pub fn stack_slots(
67 66 let gap = physical(STACK_GAP);
68 67 let mut rows: Vec<(i32, i32)> = Vec::new();
69 68 if with_pill {
70 rows.push((physical(PILL_WIDTH), physical(PILL_HEIGHT)));
69 rows.push((width, physical(HIDE_ALL_HEIGHT)));
71 70 }
72 71 rows.extend(heights.iter().map(|height| (width, physical(*height))));
73 72 let total: i32 = rows.iter().map(|(_, height)| height).sum::<i32>()
@@ -132,9 +131,9 @@ mod tests {
132 131 fn pill_sits_above_oldest_toast() {
133 132 let slots = stack_slots(area(1.), Corner::BottomRight, &[80., 80., 80.], true);
134 133 let pill = slots.pill.unwrap();
135 assert_eq!(pill.height, 32);
134 assert_eq!(pill.height, 36);
136 135 assert_eq!(pill.y + pill.height + 8, slots.toasts[0].y);
137 assert_eq!(pill.x + pill.width, slots.toasts[0].x + slots.toasts[0].width);
136 assert_eq!((pill.x, pill.width), (slots.toasts[0].x, slots.toasts[0].width));
138 137 }
139 138
140 139 #[test]
@@ -172,6 +171,9 @@ mod tests {
172 171 reply_text: String::new(),
173 172 hovered: false,
174 173 time: String::new(),
174 queued: false,
175 dismissed: false,
176 fade: super::super::stack::Fade::shown(std::time::Instant::now()),
175 177 };
176 178 let short = toast_height(&model("Hi"), true);
177 179 let long = toast_height(&model(&"x".repeat(200)), true);
@@ -3,8 +3,7 @@ use super::rules::{Corner, Preview};
3use super::stack::{ReplyState, ToastModel};
3use super::stack::{ReplyState, ToastModel};
4
4
5pub const TOAST_WIDTH: f32 = 360.;
5pub const TOAST_WIDTH: f32 = 360.;
6pub const PILL_WIDTH: f32 = 132.;
6pub const HIDE_ALL_HEIGHT: f32 = 36.;
7pub const PILL_HEIGHT: f32 = 32.;
8pub const SIDE_MARGIN: f32 = 16.;
7pub const SIDE_MARGIN: f32 = 16.;
9pub const EDGE_MARGIN: f32 = 12.;
8pub const EDGE_MARGIN: f32 = 12.;
10pub const STACK_GAP: f32 = 8.;
9pub const STACK_GAP: f32 = 8.;
@@ -67,7 +66,7 @@ pub fn stack_slots(
67 let gap = physical(STACK_GAP);
66 let gap = physical(STACK_GAP);
68 let mut rows: Vec<(i32, i32)> = Vec::new();
67 let mut rows: Vec<(i32, i32)> = Vec::new();
69 if with_pill {
68 if with_pill {
70 rows.push((physical(PILL_WIDTH), physical(PILL_HEIGHT)));
69 rows.push((width, physical(HIDE_ALL_HEIGHT)));
71 }
70 }
72 rows.extend(heights.iter().map(|height| (width, physical(*height))));
71 rows.extend(heights.iter().map(|height| (width, physical(*height))));
73 let total: i32 = rows.iter().map(|(_, height)| height).sum::<i32>()
72 let total: i32 = rows.iter().map(|(_, height)| height).sum::<i32>()
@@ -132,9 +131,9 @@ mod tests {
132 fn pill_sits_above_oldest_toast() {
131 fn pill_sits_above_oldest_toast() {
133 let slots = stack_slots(area(1.), Corner::BottomRight, &[80., 80., 80.], true);
132 let slots = stack_slots(area(1.), Corner::BottomRight, &[80., 80., 80.], true);
134 let pill = slots.pill.unwrap();
133 let pill = slots.pill.unwrap();
135 assert_eq!(pill.height, 32);
134 assert_eq!(pill.height, 36);
136 assert_eq!(pill.y + pill.height + 8, slots.toasts[0].y);
135 assert_eq!(pill.y + pill.height + 8, slots.toasts[0].y);
137 assert_eq!(pill.x + pill.width, slots.toasts[0].x + slots.toasts[0].width);
136 assert_eq!((pill.x, pill.width), (slots.toasts[0].x, slots.toasts[0].width));
138 }
137 }
139
138
140 #[test]
139 #[test]
@@ -172,6 +171,9 @@ mod tests {
172 reply_text: String::new(),
171 reply_text: String::new(),
173 hovered: false,
172 hovered: false,
174 time: String::new(),
173 time: String::new(),
174 queued: false,
175 dismissed: false,
176 fade: super::super::stack::Fade::shown(std::time::Instant::now()),
175 };
177 };
176 let short = toast_height(&model("Hi"), true);
178 let short = toast_height(&model("Hi"), true);
177 let long = toast_height(&model(&"x".repeat(200)), true);
179 let long = toast_height(&model(&"x".repeat(200)), true);
crates/app/src/notify/platform/stub.rs +0 −1
@@ -45,7 +45,6 @@ impl Tray {
45 45 pub fn sync(&self, _sound_on: bool, _do_not_disturb: bool, _mention: bool) {}
46 46 }
47 47
48 pub fn set_border_color(_handle: NativeHandle, _rgb: u32) {}
49 48
50 49 pub fn animations_enabled() -> bool {
51 50 true
@@ -45,7 +45,6 @@ impl Tray {
45 pub fn sync(&self, _sound_on: bool, _do_not_disturb: bool, _mention: bool) {}
45 pub fn sync(&self, _sound_on: bool, _do_not_disturb: bool, _mention: bool) {}
46}
46}
47
47
48pub fn set_border_color(_handle: NativeHandle, _rgb: u32) {}
49
48
50pub fn animations_enabled() -> bool {
49pub fn animations_enabled() -> bool {
51 true
50 true
crates/app/src/notify/platform/windows_impl.rs +10 −14
@@ -7,7 +7,8 @@ use tray_icon::menu::{CheckMenuItem, Menu, MenuEvent, MenuItem, PredefinedMenuIt
7 7 use tray_icon::{Icon, MouseButton, MouseButtonState, TrayIcon, TrayIconBuilder, TrayIconEvent};
8 8 use windows::Win32::Foundation::{HWND, RECT};
9 9 use windows::Win32::Graphics::Dwm::{
10 DWMWA_BORDER_COLOR, DWMWA_WINDOW_CORNER_PREFERENCE, DWMWCP_ROUND, DwmSetWindowAttribute,
10 DWMWA_BORDER_COLOR, DWMWA_COLOR_NONE, DWMWA_WINDOW_CORNER_PREFERENCE, DWMWCP_DONOTROUND,
11 DwmSetWindowAttribute,
11 12 };
12 13 use windows::Win32::Graphics::Gdi::{
13 14 CreateBitmap, DeleteObject, GetMonitorInfoW, HGDIOBJ, MONITOR_DEFAULTTONEAREST, MONITORINFO,
@@ -75,7 +76,14 @@ pub fn prepare_toast_window(handle: NativeHandle) {
75 76 GWL_EXSTYLE,
76 77 style | WS_EX_NOACTIVATE.0 as isize,
77 78 );
78 let preference = DWMWCP_ROUND;
79 let no_border = DWMWA_COLOR_NONE;
80 let _ = DwmSetWindowAttribute(
81 hwnd(handle),
82 DWMWA_BORDER_COLOR,
83 &no_border as *const _ as *const c_void,
84 size_of::<u32>() as u32,
85 );
86 let preference = DWMWCP_DONOTROUND;
79 87 let _ = DwmSetWindowAttribute(
80 88 hwnd(handle),
81 89 DWMWA_WINDOW_CORNER_PREFERENCE,
@@ -352,18 +360,6 @@ fn tray_icons() -> Option<(Icon, Icon)> {
352 360 ))
353 361 }
354 362
355 pub fn set_border_color(handle: NativeHandle, rgb: u32) {
356 let colorref: u32 = ((rgb & 0xff) << 16) | (rgb & 0xff00) | ((rgb >> 16) & 0xff);
357 unsafe {
358 let _ = DwmSetWindowAttribute(
359 hwnd(handle),
360 DWMWA_BORDER_COLOR,
361 &colorref as *const _ as *const c_void,
362 size_of::<u32>() as u32,
363 );
364 }
365 }
366
367 363 pub fn animations_enabled() -> bool {
368 364 let mut enabled = BOOL(1);
369 365 let read = unsafe {
@@ -7,7 +7,8 @@ use tray_icon::menu::{CheckMenuItem, Menu, MenuEvent, MenuItem, PredefinedMenuIt
7use tray_icon::{Icon, MouseButton, MouseButtonState, TrayIcon, TrayIconBuilder, TrayIconEvent};
7use tray_icon::{Icon, MouseButton, MouseButtonState, TrayIcon, TrayIconBuilder, TrayIconEvent};
8use windows::Win32::Foundation::{HWND, RECT};
8use windows::Win32::Foundation::{HWND, RECT};
9use windows::Win32::Graphics::Dwm::{
9use windows::Win32::Graphics::Dwm::{
10 DWMWA_BORDER_COLOR, DWMWA_WINDOW_CORNER_PREFERENCE, DWMWCP_ROUND, DwmSetWindowAttribute,
10 DWMWA_BORDER_COLOR, DWMWA_COLOR_NONE, DWMWA_WINDOW_CORNER_PREFERENCE, DWMWCP_DONOTROUND,
11 DwmSetWindowAttribute,
11};
12};
12use windows::Win32::Graphics::Gdi::{
13use windows::Win32::Graphics::Gdi::{
13 CreateBitmap, DeleteObject, GetMonitorInfoW, HGDIOBJ, MONITOR_DEFAULTTONEAREST, MONITORINFO,
14 CreateBitmap, DeleteObject, GetMonitorInfoW, HGDIOBJ, MONITOR_DEFAULTTONEAREST, MONITORINFO,
@@ -75,7 +76,14 @@ pub fn prepare_toast_window(handle: NativeHandle) {
75 GWL_EXSTYLE,
76 GWL_EXSTYLE,
76 style | WS_EX_NOACTIVATE.0 as isize,
77 style | WS_EX_NOACTIVATE.0 as isize,
77 );
78 );
78 let preference = DWMWCP_ROUND;
79 let no_border = DWMWA_COLOR_NONE;
80 let _ = DwmSetWindowAttribute(
81 hwnd(handle),
82 DWMWA_BORDER_COLOR,
83 &no_border as *const _ as *const c_void,
84 size_of::<u32>() as u32,
85 );
86 let preference = DWMWCP_DONOTROUND;
79 let _ = DwmSetWindowAttribute(
87 let _ = DwmSetWindowAttribute(
80 hwnd(handle),
88 hwnd(handle),
81 DWMWA_WINDOW_CORNER_PREFERENCE,
89 DWMWA_WINDOW_CORNER_PREFERENCE,
@@ -352,18 +360,6 @@ fn tray_icons() -> Option<(Icon, Icon)> {
352 ))
360 ))
353}
361}
354
362
355pub fn set_border_color(handle: NativeHandle, rgb: u32) {
356 let colorref: u32 = ((rgb & 0xff) << 16) | (rgb & 0xff00) | ((rgb >> 16) & 0xff);
357 unsafe {
358 let _ = DwmSetWindowAttribute(
359 hwnd(handle),
360 DWMWA_BORDER_COLOR,
361 &colorref as *const _ as *const c_void,
362 size_of::<u32>() as u32,
363 );
364 }
365}
366
367pub fn animations_enabled() -> bool {
363pub fn animations_enabled() -> bool {
368 let mut enabled = BOOL(1);
364 let mut enabled = BOOL(1);
369 let read = unsafe {
365 let read = unsafe {
crates/app/src/notify/stack.rs +223 −12
@@ -8,6 +8,63 @@ pub const MENTION_DURATION: Duration = Duration::from_secs(10);
8 8 pub const MAX_TOTAL: Duration = Duration::from_secs(20);
9 9 pub const RESUME_REMAINING: Duration = Duration::from_secs(2);
10 10 pub const SENT_DURATION: Duration = Duration::from_millis(1500);
11 pub const FADE_IN: Duration = Duration::from_millis(150);
12 pub const FAST_FADE: Duration = Duration::from_millis(150);
13 pub const SLOW_FADE: Duration = Duration::from_secs(4);
14
15 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
16 pub enum Curve {
17 Linear,
18 EaseInCirc,
19 }
20
21 #[derive(Debug, Clone, Copy, PartialEq)]
22 pub struct Fade {
23 from: f32,
24 to: f32,
25 start: Instant,
26 duration: Duration,
27 curve: Curve,
28 }
29
30 impl Fade {
31 pub fn new(from: f32, to: f32, start: Instant, duration: Duration, curve: Curve) -> Self {
32 Fade {
33 from,
34 to,
35 start,
36 duration,
37 curve,
38 }
39 }
40
41 #[cfg(test)]
42 pub fn shown(now: Instant) -> Self {
43 Fade::new(1., 1., now, Duration::ZERO, Curve::Linear)
44 }
45
46 pub fn value(&self, now: Instant) -> f32 {
47 if self.duration.is_zero() {
48 return self.to;
49 }
50 let progress = (now.saturating_duration_since(self.start).as_secs_f32()
51 / self.duration.as_secs_f32())
52 .clamp(0., 1.);
53 let eased = match self.curve {
54 Curve::Linear => progress,
55 Curve::EaseInCirc => 1. - (1. - progress * progress).sqrt(),
56 };
57 self.from + (self.to - self.from) * eased
58 }
59
60 pub fn is_leaving(&self) -> bool {
61 self.to == 0.
62 }
63
64 fn is_gone(&self, now: Instant) -> bool {
65 self.is_leaving() && now.saturating_duration_since(self.start) >= self.duration
66 }
67 }
11 68
12 69 #[derive(Debug, Clone, Copy)]
13 70 pub struct ToastTimer {
@@ -100,12 +157,23 @@ pub struct ToastModel {
100 157 pub reply_text: String,
101 158 pub hovered: bool,
102 159 pub time: String,
160 pub queued: bool,
161 pub dismissed: bool,
162 pub fade: Fade,
103 163 }
104 164
105 165 impl ToastModel {
106 166 pub fn holds_open(&self) -> bool {
107 167 self.reply != ReplyState::Closed
108 168 }
169
170 pub fn opacity(&self, now: Instant) -> f32 {
171 self.fade.value(now)
172 }
173
174 pub fn is_leaving(&self) -> bool {
175 self.fade.is_leaving()
176 }
109 177 }
110 178
111 179 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
@@ -150,6 +218,7 @@ impl ToastStack {
150 218 };
151 219 }
152 220 self.next_id += 1;
221 let queued = self.toasts.len() >= MAX_VISIBLE;
153 222 self.toasts.push(ToastModel {
154 223 id: self.next_id,
155 224 conversation_id: incoming.conversation_id.clone(),
@@ -166,6 +235,9 @@ impl ToastStack {
166 235 reply_text: String::new(),
167 236 hovered: false,
168 237 time: String::new(),
238 queued,
239 dismissed: false,
240 fade: Fade::new(0., 1., now, FADE_IN, Curve::Linear),
169 241 });
170 242 PushOutcome {
171 243 id: self.next_id,
@@ -200,12 +272,76 @@ impl ToastStack {
200 272 }
201 273
202 274 pub fn visible(&self) -> &[ToastModel] {
203 let start = self.toasts.len().saturating_sub(MAX_VISIBLE);
204 &self.toasts[start..]
275 let shown = self
276 .toasts
277 .iter()
278 .position(|toast| toast.queued)
279 .unwrap_or(self.toasts.len());
280 &self.toasts[..shown]
281 }
282
283 pub fn queued_count(&self) -> usize {
284 self.toasts.len() - self.visible().len()
285 }
286
287 pub fn promote(&mut self, now: Instant, fade_in: Duration) {
288 for toast in self.toasts.iter_mut().take(MAX_VISIBLE) {
289 if toast.queued {
290 toast.queued = false;
291 toast.timer = ToastTimer::new(now, toast.mentions_me);
292 toast.fade = Fade::new(0., 1., now, fade_in, Curve::Linear);
293 }
294 }
295 }
296
297 pub fn fade_out(&mut self, id: u64, now: Instant, duration: Duration) {
298 if let Some(toast) = self.get_mut(id)
299 && !toast.is_leaving()
300 {
301 toast.fade = Fade::new(toast.opacity(now), 0., now, duration, Curve::EaseInCirc);
302 }
303 }
304
305 pub fn dismiss(&mut self, id: u64, now: Instant, duration: Duration) {
306 let Some(index) = self.toasts.iter().position(|toast| toast.id == id) else {
307 return;
308 };
309 if self.toasts[index].queued {
310 self.toasts.remove(index);
311 return;
312 }
313 let toast = &mut self.toasts[index];
314 if !toast.dismissed {
315 toast.dismissed = true;
316 toast.hovered = false;
317 toast.fade = Fade::new(toast.opacity(now), 0., now, duration, Curve::Linear);
318 }
319 }
320
321 pub fn restore(&mut self, now: Instant, duration: Duration) {
322 for toast in self
323 .toasts
324 .iter_mut()
325 .filter(|toast| toast.is_leaving() && !toast.dismissed)
326 {
327 toast.fade = Fade::new(toast.opacity(now), 1., now, duration, Curve::Linear);
328 }
329 }
330
331 pub fn any_hovered(&self) -> bool {
332 self.toasts.iter().any(|toast| toast.hovered)
333 }
334
335 pub fn ids(&self) -> Vec<u64> {
336 self.toasts.iter().map(|toast| toast.id).collect()
205 337 }
206 338
207 pub fn hidden_count(&self) -> usize {
208 self.toasts.len().saturating_sub(MAX_VISIBLE)
339 pub fn gone(&self, now: Instant) -> Vec<u64> {
340 self.toasts
341 .iter()
342 .filter(|toast| toast.fade.is_gone(now))
343 .map(|toast| toast.id)
344 .collect()
209 345 }
210 346
211 347 pub fn is_empty(&self) -> bool {
@@ -215,7 +351,12 @@ impl ToastStack {
215 351 pub fn expired(&self, now: Instant) -> Vec<u64> {
216 352 self.toasts
217 353 .iter()
218 .filter(|toast| !toast.holds_open() && toast.timer.expired(now))
354 .filter(|toast| {
355 !toast.queued
356 && !toast.is_leaving()
357 && !toast.holds_open()
358 && toast.timer.expired(now)
359 })
219 360 .map(|toast| toast.id)
220 361 .collect()
221 362 }
@@ -284,20 +425,90 @@ mod tests {
284 425 assert!(!stack.push(&incoming("b", false), now).plays_sound(false));
285 426 }
286 427
428 fn visible_chats(stack: &ToastStack) -> Vec<&str> {
429 stack
430 .visible()
431 .iter()
432 .map(|toast| toast.conversation_id.as_str())
433 .collect()
434 }
435
287 436 #[test]
288 fn shows_three_newest_and_counts_the_rest() {
437 fn shows_first_three_and_queues_the_rest() {
289 438 let mut stack = ToastStack::default();
290 439 let now = Instant::now();
291 440 for chat in ["a", "b", "c", "d", "e"] {
292 441 stack.push(&incoming(chat, false), now);
293 442 }
294 let visible: Vec<&str> = stack
295 .visible()
296 .iter()
297 .map(|toast| toast.conversation_id.as_str())
443 assert_eq!(visible_chats(&stack), ["a", "b", "c"]);
444 assert_eq!(stack.queued_count(), 2);
445 }
446
447 #[test]
448 fn queued_toast_starts_its_timer_when_promoted() {
449 let mut stack = ToastStack::default();
450 let start = Instant::now();
451 let ids: Vec<u64> = ["a", "b", "c", "d"]
452 .into_iter()
453 .map(|chat| stack.push(&incoming(chat, false), start).id)
298 454 .collect();
299 assert_eq!(visible, ["c", "d", "e"]);
300 assert_eq!(stack.hidden_count(), 2);
455 let later = start + Duration::from_secs(30);
456 assert!(!stack.expired(later).contains(&ids[3]));
457 stack.remove(ids[0]);
458 stack.promote(later, FADE_IN);
459 assert_eq!(visible_chats(&stack), ["b", "c", "d"]);
460 assert!(!stack.expired(later + Duration::from_secs(5)).contains(&ids[3]));
461 assert!(stack.expired(later + Duration::from_secs(6)).contains(&ids[3]));
462 }
463
464 #[test]
465 fn slow_fade_stays_visible_then_drops_and_is_gone() {
466 let mut stack = ToastStack::default();
467 let now = Instant::now();
468 let id = stack.push(&incoming("a", false), now).id;
469 let hide_at = now + Duration::from_secs(6);
470 stack.fade_out(id, hide_at, SLOW_FADE);
471 let toast = stack.get(id).unwrap();
472 assert!(toast.opacity(hide_at + Duration::from_secs(2)) > 0.8);
473 assert!(stack.gone(hide_at + Duration::from_secs(3)).is_empty());
474 assert_eq!(stack.gone(hide_at + SLOW_FADE), vec![id]);
475 }
476
477 #[test]
478 fn restore_brings_a_fading_toast_back() {
479 let mut stack = ToastStack::default();
480 let now = Instant::now();
481 let id = stack.push(&incoming("a", false), now).id;
482 stack.fade_out(id, now, SLOW_FADE);
483 stack.restore(now + Duration::from_secs(3), FAST_FADE);
484 let toast = stack.get(id).unwrap();
485 assert!(!toast.is_leaving());
486 assert_eq!(toast.opacity(now + Duration::from_secs(4)), 1.);
487 assert!(stack.gone(now + Duration::from_secs(10)).is_empty());
488 }
489
490 #[test]
491 fn hiding_a_queued_toast_drops_it_at_once() {
492 let mut stack = ToastStack::default();
493 let now = Instant::now();
494 let ids: Vec<u64> = ["a", "b", "c", "d"]
495 .into_iter()
496 .map(|chat| stack.push(&incoming(chat, false), now).id)
497 .collect();
498 stack.dismiss(ids[3], now, FAST_FADE);
499 assert!(stack.get(ids[3]).is_none());
500 assert_eq!(stack.queued_count(), 0);
501 }
502
503 #[test]
504 fn dismiss_cuts_a_slow_fade_short_and_hover_does_not_undo_it() {
505 let mut stack = ToastStack::default();
506 let now = Instant::now();
507 let id = stack.push(&incoming("a", false), now).id;
508 stack.fade_out(id, now, SLOW_FADE);
509 stack.dismiss(id, now + Duration::from_secs(1), FAST_FADE);
510 stack.restore(now + Duration::from_millis(1050), FAST_FADE);
511 assert_eq!(stack.gone(now + Duration::from_millis(1150)), vec![id]);
301 512 }
302 513
303 514 #[test]
@@ -8,6 +8,63 @@ pub const MENTION_DURATION: Duration = Duration::from_secs(10);
8pub const MAX_TOTAL: Duration = Duration::from_secs(20);
8pub const MAX_TOTAL: Duration = Duration::from_secs(20);
9pub const RESUME_REMAINING: Duration = Duration::from_secs(2);
9pub const RESUME_REMAINING: Duration = Duration::from_secs(2);
10pub const SENT_DURATION: Duration = Duration::from_millis(1500);
10pub const SENT_DURATION: Duration = Duration::from_millis(1500);
11pub const FADE_IN: Duration = Duration::from_millis(150);
12pub const FAST_FADE: Duration = Duration::from_millis(150);
13pub const SLOW_FADE: Duration = Duration::from_secs(4);
14
15#[derive(Debug, Clone, Copy, PartialEq, Eq)]
16pub enum Curve {
17 Linear,
18 EaseInCirc,
19}
20
21#[derive(Debug, Clone, Copy, PartialEq)]
22pub struct Fade {
23 from: f32,
24 to: f32,
25 start: Instant,
26 duration: Duration,
27 curve: Curve,
28}
29
30impl Fade {
31 pub fn new(from: f32, to: f32, start: Instant, duration: Duration, curve: Curve) -> Self {
32 Fade {
33 from,
34 to,
35 start,
36 duration,
37 curve,
38 }
39 }
40
41 #[cfg(test)]
42 pub fn shown(now: Instant) -> Self {
43 Fade::new(1., 1., now, Duration::ZERO, Curve::Linear)
44 }
45
46 pub fn value(&self, now: Instant) -> f32 {
47 if self.duration.is_zero() {
48 return self.to;
49 }
50 let progress = (now.saturating_duration_since(self.start).as_secs_f32()
51 / self.duration.as_secs_f32())
52 .clamp(0., 1.);
53 let eased = match self.curve {
54 Curve::Linear => progress,
55 Curve::EaseInCirc => 1. - (1. - progress * progress).sqrt(),
56 };
57 self.from + (self.to - self.from) * eased
58 }
59
60 pub fn is_leaving(&self) -> bool {
61 self.to == 0.
62 }
63
64 fn is_gone(&self, now: Instant) -> bool {
65 self.is_leaving() && now.saturating_duration_since(self.start) >= self.duration
66 }
67}
11
68
12#[derive(Debug, Clone, Copy)]
69#[derive(Debug, Clone, Copy)]
13pub struct ToastTimer {
70pub struct ToastTimer {
@@ -100,12 +157,23 @@ pub struct ToastModel {
100 pub reply_text: String,
157 pub reply_text: String,
101 pub hovered: bool,
158 pub hovered: bool,
102 pub time: String,
159 pub time: String,
160 pub queued: bool,
161 pub dismissed: bool,
162 pub fade: Fade,
103}
163}
104
164
105impl ToastModel {
165impl ToastModel {
106 pub fn holds_open(&self) -> bool {
166 pub fn holds_open(&self) -> bool {
107 self.reply != ReplyState::Closed
167 self.reply != ReplyState::Closed
108 }
168 }
169
170 pub fn opacity(&self, now: Instant) -> f32 {
171 self.fade.value(now)
172 }
173
174 pub fn is_leaving(&self) -> bool {
175 self.fade.is_leaving()
176 }
109}
177}
110
178
111#[derive(Debug, Clone, Copy, PartialEq, Eq)]
179#[derive(Debug, Clone, Copy, PartialEq, Eq)]
@@ -150,6 +218,7 @@ impl ToastStack {
150 };
218 };
151 }
219 }
152 self.next_id += 1;
220 self.next_id += 1;
221 let queued = self.toasts.len() >= MAX_VISIBLE;
153 self.toasts.push(ToastModel {
222 self.toasts.push(ToastModel {
154 id: self.next_id,
223 id: self.next_id,
155 conversation_id: incoming.conversation_id.clone(),
224 conversation_id: incoming.conversation_id.clone(),
@@ -166,6 +235,9 @@ impl ToastStack {
166 reply_text: String::new(),
235 reply_text: String::new(),
167 hovered: false,
236 hovered: false,
168 time: String::new(),
237 time: String::new(),
238 queued,
239 dismissed: false,
240 fade: Fade::new(0., 1., now, FADE_IN, Curve::Linear),
169 });
241 });
170 PushOutcome {
242 PushOutcome {
171 id: self.next_id,
243 id: self.next_id,
@@ -200,12 +272,76 @@ impl ToastStack {
200 }
272 }
201
273
202 pub fn visible(&self) -> &[ToastModel] {
274 pub fn visible(&self) -> &[ToastModel] {
203 let start = self.toasts.len().saturating_sub(MAX_VISIBLE);
275 let shown = self
204 &self.toasts[start..]
276 .toasts
277 .iter()
278 .position(|toast| toast.queued)
279 .unwrap_or(self.toasts.len());
280 &self.toasts[..shown]
281 }
282
283 pub fn queued_count(&self) -> usize {
284 self.toasts.len() - self.visible().len()
285 }
286
287 pub fn promote(&mut self, now: Instant, fade_in: Duration) {
288 for toast in self.toasts.iter_mut().take(MAX_VISIBLE) {
289 if toast.queued {
290 toast.queued = false;
291 toast.timer = ToastTimer::new(now, toast.mentions_me);
292 toast.fade = Fade::new(0., 1., now, fade_in, Curve::Linear);
293 }
294 }
295 }
296
297 pub fn fade_out(&mut self, id: u64, now: Instant, duration: Duration) {
298 if let Some(toast) = self.get_mut(id)
299 && !toast.is_leaving()
300 {
301 toast.fade = Fade::new(toast.opacity(now), 0., now, duration, Curve::EaseInCirc);
302 }
303 }
304
305 pub fn dismiss(&mut self, id: u64, now: Instant, duration: Duration) {
306 let Some(index) = self.toasts.iter().position(|toast| toast.id == id) else {
307 return;
308 };
309 if self.toasts[index].queued {
310 self.toasts.remove(index);
311 return;
312 }
313 let toast = &mut self.toasts[index];
314 if !toast.dismissed {
315 toast.dismissed = true;
316 toast.hovered = false;
317 toast.fade = Fade::new(toast.opacity(now), 0., now, duration, Curve::Linear);
318 }
319 }
320
321 pub fn restore(&mut self, now: Instant, duration: Duration) {
322 for toast in self
323 .toasts
324 .iter_mut()
325 .filter(|toast| toast.is_leaving() && !toast.dismissed)
326 {
327 toast.fade = Fade::new(toast.opacity(now), 1., now, duration, Curve::Linear);
328 }
329 }
330
331 pub fn any_hovered(&self) -> bool {
332 self.toasts.iter().any(|toast| toast.hovered)
333 }
334
335 pub fn ids(&self) -> Vec<u64> {
336 self.toasts.iter().map(|toast| toast.id).collect()
205 }
337 }
206
338
207 pub fn hidden_count(&self) -> usize {
339 pub fn gone(&self, now: Instant) -> Vec<u64> {
208 self.toasts.len().saturating_sub(MAX_VISIBLE)
340 self.toasts
341 .iter()
342 .filter(|toast| toast.fade.is_gone(now))
343 .map(|toast| toast.id)
344 .collect()
209 }
345 }
210
346
211 pub fn is_empty(&self) -> bool {
347 pub fn is_empty(&self) -> bool {
@@ -215,7 +351,12 @@ impl ToastStack {
215 pub fn expired(&self, now: Instant) -> Vec<u64> {
351 pub fn expired(&self, now: Instant) -> Vec<u64> {
216 self.toasts
352 self.toasts
217 .iter()
353 .iter()
218 .filter(|toast| !toast.holds_open() && toast.timer.expired(now))
354 .filter(|toast| {
355 !toast.queued
356 && !toast.is_leaving()
357 && !toast.holds_open()
358 && toast.timer.expired(now)
359 })
219 .map(|toast| toast.id)
360 .map(|toast| toast.id)
220 .collect()
361 .collect()
221 }
362 }
@@ -284,20 +425,90 @@ mod tests {
284 assert!(!stack.push(&incoming("b", false), now).plays_sound(false));
425 assert!(!stack.push(&incoming("b", false), now).plays_sound(false));
285 }
426 }
286
427
428 fn visible_chats(stack: &ToastStack) -> Vec<&str> {
429 stack
430 .visible()
431 .iter()
432 .map(|toast| toast.conversation_id.as_str())
433 .collect()
434 }
435
287 #[test]
436 #[test]
288 fn shows_three_newest_and_counts_the_rest() {
437 fn shows_first_three_and_queues_the_rest() {
289 let mut stack = ToastStack::default();
438 let mut stack = ToastStack::default();
290 let now = Instant::now();
439 let now = Instant::now();
291 for chat in ["a", "b", "c", "d", "e"] {
440 for chat in ["a", "b", "c", "d", "e"] {
292 stack.push(&incoming(chat, false), now);
441 stack.push(&incoming(chat, false), now);
293 }
442 }
294 let visible: Vec<&str> = stack
443 assert_eq!(visible_chats(&stack), ["a", "b", "c"]);
295 .visible()
444 assert_eq!(stack.queued_count(), 2);
296 .iter()
445 }
297 .map(|toast| toast.conversation_id.as_str())
446
447 #[test]
448 fn queued_toast_starts_its_timer_when_promoted() {
449 let mut stack = ToastStack::default();
450 let start = Instant::now();
451 let ids: Vec<u64> = ["a", "b", "c", "d"]
452 .into_iter()
453 .map(|chat| stack.push(&incoming(chat, false), start).id)
298 .collect();
454 .collect();
299 assert_eq!(visible, ["c", "d", "e"]);
455 let later = start + Duration::from_secs(30);
300 assert_eq!(stack.hidden_count(), 2);
456 assert!(!stack.expired(later).contains(&ids[3]));
457 stack.remove(ids[0]);
458 stack.promote(later, FADE_IN);
459 assert_eq!(visible_chats(&stack), ["b", "c", "d"]);
460 assert!(!stack.expired(later + Duration::from_secs(5)).contains(&ids[3]));
461 assert!(stack.expired(later + Duration::from_secs(6)).contains(&ids[3]));
462 }
463
464 #[test]
465 fn slow_fade_stays_visible_then_drops_and_is_gone() {
466 let mut stack = ToastStack::default();
467 let now = Instant::now();
468 let id = stack.push(&incoming("a", false), now).id;
469 let hide_at = now + Duration::from_secs(6);
470 stack.fade_out(id, hide_at, SLOW_FADE);
471 let toast = stack.get(id).unwrap();
472 assert!(toast.opacity(hide_at + Duration::from_secs(2)) > 0.8);
473 assert!(stack.gone(hide_at + Duration::from_secs(3)).is_empty());
474 assert_eq!(stack.gone(hide_at + SLOW_FADE), vec![id]);
475 }
476
477 #[test]
478 fn restore_brings_a_fading_toast_back() {
479 let mut stack = ToastStack::default();
480 let now = Instant::now();
481 let id = stack.push(&incoming("a", false), now).id;
482 stack.fade_out(id, now, SLOW_FADE);
483 stack.restore(now + Duration::from_secs(3), FAST_FADE);
484 let toast = stack.get(id).unwrap();
485 assert!(!toast.is_leaving());
486 assert_eq!(toast.opacity(now + Duration::from_secs(4)), 1.);
487 assert!(stack.gone(now + Duration::from_secs(10)).is_empty());
488 }
489
490 #[test]
491 fn hiding_a_queued_toast_drops_it_at_once() {
492 let mut stack = ToastStack::default();
493 let now = Instant::now();
494 let ids: Vec<u64> = ["a", "b", "c", "d"]
495 .into_iter()
496 .map(|chat| stack.push(&incoming(chat, false), now).id)
497 .collect();
498 stack.dismiss(ids[3], now, FAST_FADE);
499 assert!(stack.get(ids[3]).is_none());
500 assert_eq!(stack.queued_count(), 0);
501 }
502
503 #[test]
504 fn dismiss_cuts_a_slow_fade_short_and_hover_does_not_undo_it() {
505 let mut stack = ToastStack::default();
506 let now = Instant::now();
507 let id = stack.push(&incoming("a", false), now).id;
508 stack.fade_out(id, now, SLOW_FADE);
509 stack.dismiss(id, now + Duration::from_secs(1), FAST_FADE);
510 stack.restore(now + Duration::from_millis(1050), FAST_FADE);
511 assert_eq!(stack.gone(now + Duration::from_millis(1150)), vec![id]);
301 }
512 }
302
513
303 #[test]
514 #[test]
crates/app/src/notify/text.rs +4 −1
@@ -76,7 +76,7 @@ mod tests {
76 76 use std::time::Instant;
77 77
78 78 use super::*;
79 use crate::notify::stack::{ReplyState, ToastTimer};
79 use crate::notify::stack::{Fade, ReplyState, ToastTimer};
80 80
81 81 fn model(kind: ChatKind, preview: Preview) -> ToastModel {
82 82 ToastModel {
@@ -95,6 +95,9 @@ mod tests {
95 95 reply_text: String::new(),
96 96 hovered: false,
97 97 time: "14:30".into(),
98 queued: false,
99 dismissed: false,
100 fade: Fade::shown(Instant::now()),
98 101 }
99 102 }
100 103
@@ -76,7 +76,7 @@ mod tests {
76 use std::time::Instant;
76 use std::time::Instant;
77
77
78 use super::*;
78 use super::*;
79 use crate::notify::stack::{ReplyState, ToastTimer};
79 use crate::notify::stack::{Fade, ReplyState, ToastTimer};
80
80
81 fn model(kind: ChatKind, preview: Preview) -> ToastModel {
81 fn model(kind: ChatKind, preview: Preview) -> ToastModel {
82 ToastModel {
82 ToastModel {
@@ -95,6 +95,9 @@ mod tests {
95 reply_text: String::new(),
95 reply_text: String::new(),
96 hovered: false,
96 hovered: false,
97 time: "14:30".into(),
97 time: "14:30".into(),
98 queued: false,
99 dismissed: false,
100 fade: Fade::shown(Instant::now()),
98 }
101 }
99 }
102 }
100
103
crates/app/src/notify/toast.rs +26 −8
@@ -1,3 +1,5 @@
1 use std::time::Instant;
2
1 3 use gpui_kit::component::{
2 4 h_flex,
3 5 input::{InputEvent, Textarea, TextareaState},
@@ -21,6 +23,7 @@ use crate::theme;
21 23 const AVATAR_SIZE: f32 = 40.;
22 24 const CLOSE_SIZE: f32 = 24.;
23 25 const TOAST_BORDER_MENTION: f32 = 2.;
26 const TOAST_RADIUS: f32 = 12.;
24 27
25 28 pub struct ToastView {
26 29 center: Entity<NotificationCenter>,
@@ -523,6 +526,8 @@ impl Render for ToastView {
523 526 .track_focus(&self.focus_handle)
524 527 .relative()
525 528 .size_full()
529 .opacity(model.opacity(Instant::now()))
530 .rounded(px(TOAST_RADIUS))
526 531 .bg(fill)
527 532 .border(px(border_width))
528 533 .border_color(border)
@@ -566,24 +571,37 @@ impl PillView {
566 571
567 572 impl Render for PillView {
568 573 fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
569 let hidden = self.center.read(cx).hidden_count();
574 let (queued, opacity) = {
575 let center = self.center.read(cx);
576 (center.queued_count(), center.stack_opacity())
577 };
570 578 let center = self.center.clone();
571 div()
572 .id("toast-pill")
579 h_flex()
580 .id("toast-hide-all")
573 581 .size_full()
574 .flex()
582 .gap(px(6.))
575 583 .items_center()
576 584 .justify_center()
585 .opacity(opacity)
586 .rounded(px(TOAST_RADIUS))
577 587 .bg(theme::surface_raised())
578 588 .border_1()
579 589 .border_color(theme::border_strong())
580 590 .cursor_pointer()
581 .text_size(px(13.))
591 .hover(|bar| bar.bg(theme::border_strong()))
592 .text_size(px(12.5))
582 593 .font_weight(FontWeight::SEMIBOLD)
583 .text_color(theme::text())
584 .child(format!("+{hidden} more"))
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 })
585 603 .on_click(move |_, _, cx| {
586 center.update(cx, |center, cx| center.open_chat_list(cx));
604 center.update(cx, |center, cx| center.hide_all(cx));
587 605 })
588 606 }
589 607 }
@@ -1,3 +1,5 @@
1use std::time::Instant;
2
1use gpui_kit::component::{
3use gpui_kit::component::{
2 h_flex,
4 h_flex,
3 input::{InputEvent, Textarea, TextareaState},
5 input::{InputEvent, Textarea, TextareaState},
@@ -21,6 +23,7 @@ use crate::theme;
21const AVATAR_SIZE: f32 = 40.;
23const AVATAR_SIZE: f32 = 40.;
22const CLOSE_SIZE: f32 = 24.;
24const CLOSE_SIZE: f32 = 24.;
23const TOAST_BORDER_MENTION: f32 = 2.;
25const TOAST_BORDER_MENTION: f32 = 2.;
26const TOAST_RADIUS: f32 = 12.;
24
27
25pub struct ToastView {
28pub struct ToastView {
26 center: Entity<NotificationCenter>,
29 center: Entity<NotificationCenter>,
@@ -523,6 +526,8 @@ impl Render for ToastView {
523 .track_focus(&self.focus_handle)
526 .track_focus(&self.focus_handle)
524 .relative()
527 .relative()
525 .size_full()
528 .size_full()
529 .opacity(model.opacity(Instant::now()))
530 .rounded(px(TOAST_RADIUS))
526 .bg(fill)
531 .bg(fill)
527 .border(px(border_width))
532 .border(px(border_width))
528 .border_color(border)
533 .border_color(border)
@@ -566,24 +571,37 @@ impl PillView {
566
571
567impl Render for PillView {
572impl Render for PillView {
568 fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
573 fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
569 let hidden = self.center.read(cx).hidden_count();
574 let (queued, opacity) = {
575 let center = self.center.read(cx);
576 (center.queued_count(), center.stack_opacity())
577 };
570 let center = self.center.clone();
578 let center = self.center.clone();
571 div()
579 h_flex()
572 .id("toast-pill")
580 .id("toast-hide-all")
573 .size_full()
581 .size_full()
574 .flex()
582 .gap(px(6.))
575 .items_center()
583 .items_center()
576 .justify_center()
584 .justify_center()
585 .opacity(opacity)
586 .rounded(px(TOAST_RADIUS))
577 .bg(theme::surface_raised())
587 .bg(theme::surface_raised())
578 .border_1()
588 .border_1()
579 .border_color(theme::border_strong())
589 .border_color(theme::border_strong())
580 .cursor_pointer()
590 .cursor_pointer()
581 .text_size(px(13.))
591 .hover(|bar| bar.bg(theme::border_strong()))
592 .text_size(px(12.5))
582 .font_weight(FontWeight::SEMIBOLD)
593 .font_weight(FontWeight::SEMIBOLD)
583 .text_color(theme::text())
594 .text_color(theme::text_soft())
584 .child(format!("+{hidden} more"))
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 })
585 .on_click(move |_, _, cx| {
603 .on_click(move |_, _, cx| {
586 center.update(cx, |center, cx| center.open_chat_list(cx));
604 center.update(cx, |center, cx| center.hide_all(cx));
587 })
605 })
588 }
606 }
589}
607}