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

bump egui version 0.30 #31

Closed
wants to merge 12 commits into from
Closed
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ keywords = ["egui", "toast", "notification"]
members = ["demo"]

[dependencies]
egui = { version = "0.28", default-features = false }
egui = { version = "0.30", default-features = false }
2 changes: 1 addition & 1 deletion demo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.8.0"
edition = "2021"

[dependencies]
eframe = "0.28"
eframe = "0.30"
egui-toast = { path = ".." }

[target.'cfg(target_arch = "wasm32")'.dependencies]
Expand Down
64 changes: 64 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,70 @@ impl Toasts {

ctx.data_mut(|d| d.insert_temp(id, toasts));
}

pub fn show_inside(&mut self, ui: &mut Ui) {
let Self {
id,
align,
mut offset,
direction,
..
} = *self;

let ctx = ui.ctx();

let dt = ctx.input(|i| i.unstable_dt) as f64;

let mut toasts: Vec<Toast> = ctx.data_mut(|d| d.get_temp(id).unwrap_or_default());
toasts.extend(std::mem::take(&mut self.added_toasts));
toasts.retain(|toast| toast.options.ttl_sec > 0.0);

for (i, toast) in toasts.iter_mut().enumerate() {
let response = Area::new(id.with("toast").with(i))
.anchor(align, offset.to_vec2())
.constrain_to(ui.available_rect_before_wrap())
.order(Order::Foreground)
.interactable(true)
.show(ctx, |ui| {
if let Some(add_contents) = self.custom_toast_contents.get_mut(&toast.kind) {
add_contents(ui, toast)
} else {
default_toast_contents(ui, toast)
};
})
.response;

if !response.hovered() {
toast.options.ttl_sec -= dt;
if toast.options.ttl_sec.is_finite() {
ctx.request_repaint_after(Duration::from_secs_f64(
toast.options.ttl_sec.max(0.0),
));
}
}

if toast.options.show_progress {
ctx.request_repaint();
}

match direction {
Direction::LeftToRight => {
offset.x += response.rect.width() + 10.0;
}
Direction::RightToLeft => {
offset.x -= response.rect.width() + 10.0;
}
Direction::TopDown => {
offset.y += response.rect.height() + 10.0;
}
Direction::BottomUp => {
offset.y -= response.rect.height() + 10.0;
}
}
}

ctx.data_mut(|d| d.insert_temp(id, toasts));
}
}

fn default_toast_contents(ui: &mut Ui, toast: &mut Toast) -> Response {
Expand Down
12 changes: 12 additions & 0 deletions src/toast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,21 @@ impl From<u32> for ToastKind {

#[derive(Clone, Default)]
pub struct Toast {
name: String,
pub kind: ToastKind,
pub text: WidgetText,
pub options: ToastOptions,
pub style: ToastStyle,
}

impl Toast {
pub fn with_name(name: String) -> Self {
Self {
name,
..Default::default()
}
}

pub fn new() -> Self {
Self::default()
}
Expand All @@ -50,6 +58,10 @@ impl Toast {
self
}

pub fn get_name(&self) -> &str {
&self.name
}

/// Close the toast immediately
pub fn close(&mut self) {
self.options.ttl_sec = 0.0;
Expand Down