notification: Fix when multiple notifications pop up at the same time, autohide not work (#255)

This commit is contained in:
Floyd Wang 2024-09-19 17:33:49 +08:00 committed by GitHub
parent 20926b7077
commit f20f573dbc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -135,7 +135,7 @@ impl Notification {
/// Set the title of the notification, default is None. /// Set the title of the notification, default is None.
/// ///
/// If tilte is None, the notification will not have a title. /// If title is None, the notification will not have a title.
pub fn title(mut self, title: impl Into<SharedString>) -> Self { pub fn title(mut self, title: impl Into<SharedString>) -> Self {
self.title = Some(title.into()); self.title = Some(title.into());
self self
@ -305,23 +305,15 @@ impl NotificationList {
}) })
.detach(); .detach();
self.notifications.push_back(notification); self.notifications.push_back(notification.clone());
if autohide { if autohide {
// Sleep for 5 seconds to autohide the notification // Sleep for 5 seconds to autohide the notification
cx.spawn(|view, mut cx| async move { cx.spawn(|_, mut cx| async move {
Timer::after(Duration::from_secs(5)).await; Timer::after(Duration::from_secs(5)).await;
let _ = view.update(&mut cx, |view, cx| {
if let Some(ix) = view notification
.notifications .update(&mut cx, |note, cx| note.dismiss(&ClickEvent::default(), cx))
.iter() .expect("failed to auto hide notification");
.position(|note| note.read(cx).autohide)
{
if let Some(note) = view.notifications.get(ix) {
note.update(cx, |note, cx| note.dismiss(&ClickEvent::default(), cx));
}
}
cx.notify()
});
}) })
.detach(); .detach();
} }