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 1 commit
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
10 changes: 10 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,13 @@ 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]
nursery = { level = "deny", priority = 0 }
pedantic = { level = "deny", priority = 1 }
bircni marked this conversation as resolved.
Show resolved Hide resolved
enum_glob_use = { level = "deny", priority = 2 }
module_name_repetitions = { level = "allow", priority = 3 }
cast_precision_loss = { level = "allow", priority = 4 }
28 changes: 14 additions & 14 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 {
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) {
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
59 changes: 39 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,18 @@ 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
#[must_use]
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;
return self.toasts.get_mut(l).unwrap();
bircni marked this conversation as resolved.
Show resolved Hide resolved
}

/// Dismisses the oldest toast
Expand All @@ -92,37 +96,43 @@ 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();
}
}

/// Shortcut for adding a toast with info `success`.
#[must_use]
bircni marked this conversation as resolved.
Show resolved Hide resolved
pub fn success(&mut self, caption: impl Into<String>) -> &mut Toast {
self.add(Toast::success(caption))
}

/// Shortcut for adding a toast with info `level`.
#[must_use]
pub fn info(&mut self, caption: impl Into<String>) -> &mut Toast {
self.add(Toast::info(caption))
}

/// Shortcut for adding a toast with warning `level`.
#[must_use]
pub fn warning(&mut self, caption: impl Into<String>) -> &mut Toast {
self.add(Toast::warning(caption))
}

/// Shortcut for adding a toast with error `level`.
#[must_use]
pub fn error(&mut self, caption: impl Into<String>) -> &mut Toast {
self.add(Toast::error(caption))
}

/// Shortcut for adding a toast with no level.
#[must_use]
pub fn basic(&mut self, caption: impl Into<String>) -> &mut Toast {
self.add(Toast::basic(caption))
}

/// Shortcut for adding a toast with custom `level`.
#[must_use]
pub fn custom(
&mut self,
caption: impl Into<String>,
Expand All @@ -136,43 +146,50 @@ impl Toasts {
}

/// Should toasts be added in reverse order?
#[must_use]
pub const fn reverse(mut self, reverse: bool) -> Self {
self.reverse = reverse;
self
}

/// Where toasts should appear.
#[must_use]
pub const fn with_anchor(mut self, anchor: Anchor) -> Self {
self.anchor = anchor;
self
}

/// Sets spacing between adjacent toasts.
#[must_use]
pub const fn with_spacing(mut self, spacing: f32) -> Self {
self.spacing = spacing;
self
}

/// Margin or distance from screen to toasts' bounding boxes
#[must_use]
pub const fn with_margin(mut self, margin: Vec2) -> Self {
self.margin = margin;
self
}

/// Padding or distance from toasts' bounding boxes to inner contents.
#[must_use]
pub const fn with_padding(mut self, padding: Vec2) -> Self {
self.padding = padding;
self
}

/// Changes the default font used for all toasts.
#[must_use]
pub fn with_default_font(mut self, font: FontId) -> Self {
self.font = Some(font);
self
}
}

impl Toasts {
#[allow(clippy::cognitive_complexity, clippy::too_many_lines)]
bircni marked this conversation as resolved.
Show resolved Hide resolved
/// Displays toast queue
pub fn show(&mut self, ctx: &Context) {
let Self {
Expand All @@ -195,13 +212,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 +282,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 +303,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 +322,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
42 changes: 28 additions & 14 deletions src/toast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,40 @@ 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
#[must_use]
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
#[must_use]
pub const fn disappearing(&self) -> bool {
matches!(self, Self::Disappear)
}
pub fn disappeared(&self) -> bool {

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

/// Returns `true` if the toast is idling
#[must_use]
pub const fn idling(&self) -> bool {
matches!(self, Self::Idle)
}
}
Expand Down Expand Up @@ -84,12 +100,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 +175,7 @@ impl Toast {
)
}

/// Set the options with a ToastOptions
/// Set the options with a `ToastOptions`
bircni marked this conversation as resolved.
Show resolved Hide resolved
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 +233,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