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

Add lints #27

Merged
merged 3 commits into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,10 @@ eframe = { version = "0.28.0", default-features = false, features = [
"glow",
] }
egui-phosphor = { git = "https://github.com/ItsEthra/egui-phosphor", branch = "main" }

[lints.rust]
unsafe_code = "forbid"

[lints.clippy]
all = { level = "deny", priority = 0 }
enum_glob_use = { level = "deny", priority = 2 }
24 changes: 12 additions & 12 deletions src/anchor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,41 +15,41 @@ pub enum Anchor {

impl Anchor {
#[inline]
pub(crate) fn anim_side(&self) -> f32 {
bircni marked this conversation as resolved.
Show resolved Hide resolved
pub(crate) const fn anim_side(&self) -> f32 {
match self {
Anchor::TopRight | Anchor::BottomRight => 1.,
Anchor::TopLeft | Anchor::BottomLeft => -1.,
Self::TopRight | Self::BottomRight => 1.,
Self::TopLeft | Self::BottomLeft => -1.,
}
}
}

impl Anchor {
pub(crate) fn screen_corner(&self, sc: Pos2, margin: Vec2) -> Pos2 {
let mut out = match self {
Anchor::TopRight => pos2(sc.x, 0.),
Anchor::TopLeft => pos2(0., 0.),
Anchor::BottomRight => sc,
Anchor::BottomLeft => pos2(0., sc.y),
Self::TopRight => pos2(sc.x, 0.),
Self::TopLeft => pos2(0., 0.),
Self::BottomRight => sc,
Self::BottomLeft => pos2(0., sc.y),
};
self.apply_margin(&mut out, margin);
out
}

pub(crate) fn apply_margin(&self, pos: &mut Pos2, margin: Vec2) {
match self {
Anchor::TopRight => {
Self::TopRight => {
pos.x -= margin.x;
pos.y += margin.y;
}
Anchor::TopLeft => {
Self::TopLeft => {
pos.x += margin.x;
pos.y += margin.y
pos.y += margin.y;
}
Anchor::BottomRight => {
Self::BottomRight => {
pos.x -= margin.x;
pos.y -= margin.y;
}
Anchor::BottomLeft => {
Self::BottomLeft => {
pos.x += margin.x;
pos.y -= margin.y;
}
Expand Down
45 changes: 25 additions & 20 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub struct Toasts {

impl Toasts {
/// Creates new [`Toasts`] instance.
#[must_use]
pub const fn new() -> Self {
Self {
anchor: Anchor::TopRight,
Expand All @@ -65,15 +66,17 @@ impl Toasts {

/// Adds new toast to the collection.
/// By default adds toast at the end of the list, can be changed with `self.reverse`.
/// # Panics
///
/// Will panic if after adding a toast the list is empty.
bircni marked this conversation as resolved.
Show resolved Hide resolved
pub fn add(&mut self, toast: Toast) -> &mut Toast {
if self.reverse {
self.toasts.insert(0, toast);
return self.toasts.get_mut(0).unwrap();
} else {
self.toasts.push(toast);
let l = self.toasts.len() - 1;
return self.toasts.get_mut(l).unwrap();
}
self.toasts.push(toast);
let l = self.toasts.len() - 1;
self.toasts.get_mut(l).unwrap()
}

/// Dismisses the oldest toast
Expand All @@ -92,7 +95,7 @@ impl Toasts {

/// Dismisses all toasts
pub fn dismiss_all_toasts(&mut self) {
for toast in self.toasts.iter_mut() {
for toast in &mut self.toasts {
toast.dismiss();
}
}
Expand Down Expand Up @@ -195,13 +198,13 @@ impl Toasts {
toasts.retain(|t| !t.state.disappeared());

// Start disappearing expired toasts
toasts.iter_mut().for_each(|t| {
for t in toasts.iter_mut() {
if let Some((_initial_d, current_d)) = t.duration {
if current_d <= 0. {
t.state = ToastState::Disapper
t.state = ToastState::Disappear;
}
}
});
}

// `held` used to prevent sticky removal
if ctx.input(|i| i.pointer.primary_released()) {
Expand Down Expand Up @@ -265,11 +268,10 @@ impl Toasts {
ToastLevel::None => None,
};

let (action_width, action_height) = if let Some(icon_galley) = icon_galley.as_ref() {
(icon_galley.rect.width(), icon_galley.rect.height())
} else {
(0., 0.)
};
let (action_width, action_height) =
icon_galley.as_ref().map_or((0., 0.), |icon_galley| {
(icon_galley.rect.width(), icon_galley.rect.height())
});

// Create closing cross
let cross_galley = if toast.closable {
Expand All @@ -287,11 +289,10 @@ impl Toasts {
None
};

let (cross_width, cross_height) = if let Some(cross_galley) = cross_galley.as_ref() {
(cross_galley.rect.width(), cross_galley.rect.height())
} else {
(0., 0.)
};
let (cross_width, cross_height) =
cross_galley.as_ref().map_or((0., 0.), |cross_galley| {
(cross_galley.rect.width(), cross_galley.rect.height())
});

let icon_x_padding = (0., padding.x);
let cross_x_padding = (padding.x, 0.);
Expand All @@ -307,8 +308,12 @@ impl Toasts {
cross_width + cross_x_padding.0 + cross_x_padding.1
};

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.;
toast.width = padding
.x
.mul_add(2., icon_width_padded + caption_width + cross_width_padded);
toast.height = padding
.y
.mul_add(2., action_height.max(caption_height).max(cross_height));

let anim_offset = toast.width * (1. - ease_in_cubic(toast.value));
pos.x += anim_offset * anchor.anim_side();
Expand Down
38 changes: 24 additions & 14 deletions src/toast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,36 @@ pub enum ToastLevel {
}

#[derive(Debug)]
pub(crate) enum ToastState {
/// State of the toast
pub enum ToastState {
/// Toast is appearing
Appear,
Disapper,
/// Toast is disappearing
Disappear,
/// Toast has disappeared
Disappeared,
/// Toast is idling
Idle,
}

impl ToastState {
pub fn appearing(&self) -> bool {
/// Returns `true` if the toast is appearing
pub const fn appearing(&self) -> bool {
matches!(self, Self::Appear)
}
pub fn disappearing(&self) -> bool {
matches!(self, Self::Disapper)

/// Returns `true` if the toast is disappearing
pub const fn disappearing(&self) -> bool {
matches!(self, Self::Disappear)
}
pub fn disappeared(&self) -> bool {

/// Returns `true` if the toast has disappeared
pub const fn disappeared(&self) -> bool {
matches!(self, Self::Disappeared)
}
pub fn idling(&self) -> bool {

/// Returns `true` if the toast is idling
pub const fn idling(&self) -> bool {
matches!(self, Self::Idle)
}
}
Expand Down Expand Up @@ -84,12 +96,10 @@ impl Toast {
caption: caption.into(),
height: TOAST_HEIGHT,
width: TOAST_WIDTH,
duration: if let Some(dur) = options.duration {
duration: options.duration.map(|dur| {
let max_dur = duration_to_seconds_f32(dur);
Some((max_dur, max_dur))
} else {
None
},
(max_dur, max_dur)
}),
closable: options.closable,
show_progress_bar: options.show_progress_bar,
level: options.level,
Expand Down Expand Up @@ -161,7 +171,7 @@ impl Toast {
)
}

/// Set the options with a ToastOptions
/// Set the options with a [`ToastOptions`]
pub fn set_options(&mut self, options: ToastOptions) -> &mut Self {
self.set_closable(options.closable);
self.set_duration(options.duration);
Expand Down Expand Up @@ -219,7 +229,7 @@ impl Toast {

/// Dismiss this toast
pub fn dismiss(&mut self) {
self.state = ToastState::Disapper;
self.state = ToastState::Disappear;
}

pub(crate) fn calc_anchored_rect(&self, pos: Pos2, anchor: Anchor) -> Rect {
Expand Down
Loading