diff --git a/Cargo.toml b/Cargo.toml index db234b5..586b77d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 } diff --git a/src/anchor.rs b/src/anchor.rs index 8fcecfc..c495e6f 100644 --- a/src/anchor.rs +++ b/src/anchor.rs @@ -15,10 +15,10 @@ pub enum Anchor { impl Anchor { #[inline] - pub(crate) fn anim_side(&self) -> f32 { + 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., } } } @@ -26,10 +26,10 @@ impl Anchor { 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 @@ -37,19 +37,19 @@ impl Anchor { 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; } diff --git a/src/lib.rs b/src/lib.rs index 7537ec0..2dc812f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -49,6 +49,7 @@ pub struct Toasts { impl Toasts { /// Creates new [`Toasts`] instance. + #[must_use] pub const fn new() -> Self { Self { anchor: Anchor::TopRight, @@ -69,11 +70,10 @@ impl Toasts { 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 @@ -92,7 +92,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(); } } @@ -195,13 +195,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()) { @@ -265,11 +265,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 { @@ -287,11 +286,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.); @@ -307,8 +305,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(); diff --git a/src/toast.rs b/src/toast.rs index 5935cb4..c0c555a 100644 --- a/src/toast.rs +++ b/src/toast.rs @@ -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) } } @@ -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, @@ -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); @@ -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 {