diff --git a/README.md b/README.md index 37f28a2..cc70573 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,13 @@ # egui-notify + Simple notifications library for [`egui`](https://github.com/emilk/egui) ![example_image](media/toasts_type.png) ![example_video](media/toasts_example_video.gif) -# Usage + +## Usage + ```rust use egui_notify::Toasts; use std::time::Duration; @@ -12,6 +15,7 @@ use std::time::Duration; // initialize once let mut toasts = Toasts::default(); ``` + ```rust // somewhere within [egui::App::update]... toasts.info("Hello world!").set_duration(Duration::from_secs(5)); @@ -19,17 +23,24 @@ toasts.info("Hello world!").set_duration(Duration::from_secs(5)); toasts.show(ctx); ``` -# Installation +## Installation + +```sh +cargo add egui-notify +``` + ```toml [dependencies] egui-notify = "0.15.0" ``` -# Difference to [`egui-toast`](https://github.com/urholaukkarinen/egui-toast) -### `egui-notify` has - - Animations for appearing/disappearing toasts - - Duration meter for expiring toasts - - Toast positioning not influenced by which `Context` you pass to it (like if for example, you passed in a `Context` already altered for an `egui::Window`) - - Differing methodology (create `Toasts` instance once, save save somewhere in application state) - - Threadsafe `Toasts` instance, implements `Send`, `Sync`. - - No support for custom toasts +## Difference to [`egui-toast`](https://github.com/urholaukkarinen/egui-toast) + +### `egui-notify` has + +- Animations for appearing/disappearing toasts +- Duration meter for expiring toasts +- Toast positioning not influenced by which `Context` you pass to it (like if for example, you passed in a `Context` already altered for an `egui::Window`) +- Differing methodology (create `Toasts` instance once, save save somewhere in application state) +- Threadsafe `Toasts` instance, implements `Send`, `Sync`. +- No support for custom toasts diff --git a/src/lib.rs b/src/lib.rs index 7537ec0..11e574f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -212,9 +212,16 @@ impl Toasts { let mut update = false; for (i, toast) in toasts.iter_mut().enumerate() { - // Decrease duration if idling + 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); + if let Some((_, d)) = toast.duration.as_mut() { - if toast.state.idling() { + // Check if we hover over the toast and if true don't decrease the duration + let hover_pos = ctx.input(|i| i.pointer.hover_pos()); + let is_outside_rect = hover_pos.map_or(true, |pos| !rect.contains(pos)); + + if is_outside_rect && toast.state.idling() { *d -= ctx.input(|i| i.stable_dt); update = true; } @@ -310,10 +317,6 @@ impl Toasts { toast.width = icon_width_padded + caption_width + cross_width_padded + (padding.x * 2.); toast.height = action_height.max(caption_height).max(cross_height) + padding.y * 2.; - 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); - // Required due to positioning of the next toast pos.x -= anim_offset * anchor.anim_side();