Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup toast loop #37

Merged
merged 1 commit into from
Dec 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 13 additions & 21 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,20 +211,6 @@ impl Toasts {
let mut pos = anchor.screen_corner(ctx.input(|i| i.screen_rect.max), *margin);
let p = ctx.layer_painter(LayerId::new(Order::Foreground, Id::new("toasts")));

let mut dismiss = None;

// Remove disappeared toasts
toasts.retain(|t| !t.state.disappeared());

// Start disappearing expired toasts
for t in toasts.iter_mut() {
if let Some((_initial_d, current_d)) = t.duration {
if current_d <= 0. {
t.state = ToastState::Disappear;
}
}
}

// `held` used to prevent sticky removal
if ctx.input(|i| i.pointer.primary_released()) {
*held = false;
Expand All @@ -233,7 +219,14 @@ impl Toasts {
let visuals = ctx.style().visuals.widgets.noninteractive;
let mut update = false;

for (i, toast) in toasts.iter_mut().enumerate() {
toasts.retain_mut(|toast| {
// Start disappearing expired toasts
if let Some((_initial_d, current_d)) = toast.duration {
if current_d <= 0. {
toast.state = ToastState::Disappear;
}
}

let anim_offset = toast.width * (1. - ease_in_cubic(toast.value));
pos.x += anim_offset * anchor.anim_side();
let rect = toast.calc_anchored_rect(pos, *anchor);
Expand Down Expand Up @@ -391,7 +384,7 @@ impl Toasts {

if let Some(pos) = ctx.input(|i| i.pointer.press_origin()) {
if screen_cross.contains(pos) && !*held {
dismiss = Some(i);
toast.dismiss();
*held = true;
}
}
Expand Down Expand Up @@ -431,15 +424,14 @@ impl Toasts {
toast.state = ToastState::Disappeared;
}
}
}

// Remove disappeared toasts
!toast.state.disappeared()
});

if update {
ctx.request_repaint();
}

if let Some(i) = dismiss {
self.toasts[i].dismiss();
}
}
}

Expand Down
Loading