Skip to content

Commit

Permalink
Merge pull request #26 from bircni/hold-on-hover
Browse files Browse the repository at this point in the history
Hold on hover
  • Loading branch information
ItsEthra committed Aug 2, 2024
2 parents 2855ed2 + 344b9b0 commit 1d4fae7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
31 changes: 21 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,35 +1,46 @@
# 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;

// initialize once
let mut toasts = Toasts::default();
```

```rust
// somewhere within [egui::App::update]...
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
15 changes: 9 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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();

Expand Down

0 comments on commit 1d4fae7

Please sign in to comment.