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

Hold on hover #26

Merged
merged 3 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
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
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
Loading