Skip to content
Open
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
14 changes: 13 additions & 1 deletion examples/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ use eframe::{
App, Frame, NativeOptions,
};
use egui::{Color32, Shadow, Style, Visuals};
use egui_notify::{Toast, Toasts};
use egui_notify::{Anchor, Toast, Toasts};
use std::time::Duration;

struct ExampleApp {
toasts: Toasts,
anchor: Anchor,
caption: String,
closable: bool,
show_progress_bar: bool,
Expand Down Expand Up @@ -53,6 +54,16 @@ impl App for ExampleApp {
ui.add(Slider::new(&mut self.font_size, 8.0..=20.0));
});
});
ui.horizontal(|ui| {
ui.label("Toast anchor");
ui.selectable_value(&mut self.anchor, Anchor::TopLeft, "TopLeft");
ui.selectable_value(&mut self.anchor, Anchor::TopMiddle, "TopMiddle");
ui.selectable_value(&mut self.anchor, Anchor::TopRight, "TopRight");
ui.selectable_value(&mut self.anchor, Anchor::BottomLeft, "BottomLeft");
ui.selectable_value(&mut self.anchor, Anchor::BottomMiddle, "BottomMiddle");
ui.selectable_value(&mut self.anchor, Anchor::BottomRight, "BottomRight");
self.toasts.set_anchor(self.anchor);
});
ui.text_edit_singleline(&mut self.custom_level_string);
ui.color_edit_button_srgba(&mut self.custom_level_color);

Expand Down Expand Up @@ -185,6 +196,7 @@ Another one
And another one"#
.into(),
toasts: Toasts::default(),
anchor: Anchor::BottomLeft,
closable: true,
expires: true,
show_progress_bar: true,
Expand Down
16 changes: 14 additions & 2 deletions src/anchor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,22 @@ pub enum Anchor {
TopRight,
/// Top left corner.
TopLeft,
/// Top middle.
TopMiddle,
/// Bottom right corner.
BottomRight,
/// Bottom left corner
BottomLeft,
/// Bottom middle
BottomMiddle,
}

impl Anchor {
#[inline]
pub(crate) const fn anim_side(&self) -> f32 {
match self {
Self::TopRight | Self::BottomRight => 1.,
Self::TopLeft | Self::BottomLeft => -1.,
Self::TopRight | Self::BottomRight | Self::BottomMiddle => 1.,
Self::TopLeft | Self::BottomLeft | Self::TopMiddle => -1.,
}
}
}
Expand All @@ -27,8 +31,10 @@ impl Anchor {
pub(crate) fn screen_corner(&self, sc: Pos2, margin: Vec2) -> Pos2 {
let mut out = match self {
Self::TopRight => pos2(sc.x, 0.),
Self::TopMiddle => pos2(sc.x / 2.0, 0.),
Self::TopLeft => pos2(0., 0.),
Self::BottomRight => sc,
Self::BottomMiddle => pos2(sc.x / 2.0, sc.y),
Self::BottomLeft => pos2(0., sc.y),
};
self.apply_margin(&mut out, margin);
Expand All @@ -41,6 +47,9 @@ impl Anchor {
pos.x -= margin.x;
pos.y += margin.y;
}
Self::TopMiddle => {
pos.y += margin.y;
}
Self::TopLeft => {
pos.x += margin.x;
pos.y += margin.y;
Expand All @@ -49,6 +58,9 @@ impl Anchor {
pos.x -= margin.x;
pos.y -= margin.y;
}
Self::BottomMiddle => {
pos.y -= margin.y;
}
Self::BottomLeft => {
pos.x += margin.x;
pos.y -= margin.y;
Expand Down
28 changes: 23 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ pub use anchor::*;
pub use egui::__run_test_ctx;
use egui::text::TextWrapping;
use egui::{
vec2, Align, Color32, Context, CornerRadius, FontId, FontSelection, Id, LayerId, Order, Rect,
Shadow, Stroke, TextWrapMode, Vec2, WidgetText,
pos2, vec2, Align, Color32, Context, CornerRadius, FontId, FontSelection, Id, LayerId, Order,
Rect, Shadow, Stroke, TextWrapMode, Vec2, WidgetText,
};

pub(crate) const TOAST_WIDTH: f32 = 180.;
Expand Down Expand Up @@ -191,6 +191,11 @@ impl Toasts {
self.font = Some(font);
self
}

/// Changes the position where toasts appear
pub fn set_anchor(&mut self, anchor: Anchor) {
self.anchor = anchor;
}
}

impl Toasts {
Expand Down Expand Up @@ -227,7 +232,12 @@ impl Toasts {
}

let anim_offset = toast.width * (1. - ease_in_cubic(toast.value));
pos.x += anim_offset * anchor.anim_side();
match anchor {
Anchor::TopMiddle | Anchor::BottomMiddle => {
pos.y += anim_offset * anchor.anim_side()
}
_ => pos.x += anim_offset * anchor.anim_side(),
}
let rect = toast.calc_anchored_rect(pos, *anchor);

if let Some((_, d)) = toast.duration.as_mut() {
Expand Down Expand Up @@ -328,7 +338,12 @@ impl Toasts {
.mul_add(2., action_height.max(caption_height).max(cross_height));

// Required due to positioning of the next toast
pos.x -= anim_offset * anchor.anim_side();
match anchor {
Anchor::TopMiddle | Anchor::BottomMiddle => {
pos.y -= anim_offset * anchor.anim_side()
}
_ => pos.x -= anim_offset * anchor.anim_side(),
}

// Draw shadow
if let Some(shadow) = self.shadow {
Expand Down Expand Up @@ -399,7 +414,10 @@ impl Toasts {
p.line_segment(
[
rect.min + vec2(0., toast.height),
rect.max - vec2((1. - (current / initial)) * toast.width, 0.),
pos2(
rect.max.x - (1. - (current / initial)) * toast.width,
rect.min.y + toast.height,
),
],
Stroke::new(4., visuals.fg_stroke.color),
);
Expand Down
16 changes: 14 additions & 2 deletions src/toast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@ impl Toast {
min: pos2(pos.x - self.width, pos.y),
max: pos2(pos.x, pos.y + self.height),
},
Anchor::TopMiddle => Rect {
min: pos2(pos.x - self.width / 2.0, pos.y),
max: pos2(pos.x + self.width / 2.0, pos.y + self.height),
},
Anchor::TopLeft => Rect {
min: pos,
max: pos + vec2(self.width, self.height),
Expand All @@ -235,6 +239,10 @@ impl Toast {
min: pos - vec2(self.width, self.height),
max: pos,
},
Anchor::BottomMiddle => Rect {
min: pos2(pos.x - self.width / 2.0, pos.y - self.height),
max: pos2(pos.x + self.width / 2.0, pos.y),
},
Anchor::BottomLeft => Rect {
min: pos2(pos.x, pos.y - self.height),
max: pos2(pos.x + self.width, pos.y),
Expand All @@ -244,8 +252,12 @@ impl Toast {

pub(crate) fn adjust_next_pos(&self, pos: &mut Pos2, anchor: Anchor, spacing: f32) {
match anchor {
Anchor::TopRight | Anchor::TopLeft => pos.y += self.height + spacing,
Anchor::BottomRight | Anchor::BottomLeft => pos.y -= self.height + spacing,
Anchor::TopRight | Anchor::TopMiddle | Anchor::TopLeft => {
pos.y += self.height + spacing
}
Anchor::BottomRight | Anchor::BottomMiddle | Anchor::BottomLeft => {
pos.y -= self.height + spacing
}
}
}
}