Skip to content

Commit

Permalink
Removed ToastCaption enum, and replaced usages with `egui::widget_t…
Browse files Browse the repository at this point in the history
…ext::WidgetText`
  • Loading branch information
jorgsaa committed Oct 11, 2024
1 parent 3378493 commit b59f817
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 76 deletions.
38 changes: 19 additions & 19 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ pub use toast::*;
mod anchor;
pub use anchor::*;

use crate::ToastCaption::{Simple, WidgetText};
#[doc(hidden)]
pub use egui::__run_test_ctx;
use egui::text::TextWrapping;
use egui::{
vec2, Align, Color32, Context, FontId, FontSelection, Id, LayerId, Order, Rect, RichText,
Rounding, Shadow, Stroke, TextWrapMode, Vec2,
vec2, Align, Color32, Context, FontId, FontSelection, Id, LayerId, Order, Rect, Rounding,
Shadow, Stroke, TextWrapMode, Vec2, WidgetText,
};

pub(crate) const TOAST_WIDTH: f32 = 180.;
Expand Down Expand Up @@ -105,34 +104,34 @@ impl Toasts {
}

/// Shortcut for adding a toast with info `success`.
pub fn success(&mut self, caption: impl Into<ToastCaption>) -> &mut Toast {
pub fn success(&mut self, caption: impl Into<WidgetText>) -> &mut Toast {
self.add(Toast::success(caption))
}

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

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

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

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

/// Shortcut for adding a toast with custom `level`.
pub fn custom(
&mut self,
caption: impl Into<ToastCaption>,
caption: impl Into<WidgetText>,
level_string: String,
level_color: egui::Color32,
) -> &mut Toast {
Expand Down Expand Up @@ -240,14 +239,7 @@ impl Toasts {
}
}

let widget_text = match toast.caption {
Simple(ref string) => egui::widget_text::WidgetText::from(
RichText::new(string).color(visuals.fg_stroke.color),
),
WidgetText(ref widget_text) => widget_text.to_owned(),
};

let caption_galley = widget_text.into_galley_impl(
let caption_galley = toast.caption.clone().into_galley_impl(
ctx,
ctx.style().as_ref(),
TextWrapping::from_wrap_mode_and_width(TextWrapMode::Extend, f32::INFINITY),
Expand Down Expand Up @@ -348,7 +340,11 @@ impl Toasts {
{
let oy = toast.height / 2. - action_height / 2.;
let ox = padding.x + icon_x_padding.0;
p.galley(rect.min + vec2(ox, oy), icon_galley, Color32::BLACK);
p.galley(
rect.min + vec2(ox, oy),
icon_galley,
visuals.fg_stroke.color,
);
}

// Paint caption
Expand All @@ -364,7 +360,11 @@ impl Toasts {
cross_width + cross_x_padding.0
};
let ox = (toast.width / 2. - caption_width / 2.) + o_from_icon / 2. - o_from_cross / 2.;
p.galley(rect.min + vec2(ox, oy), caption_galley, Color32::BLACK);
p.galley(
rect.min + vec2(ox, oy),
caption_galley,
visuals.fg_stroke.color,
);

// Paint cross
if let Some(cross_galley) = cross_galley {
Expand Down
66 changes: 9 additions & 57 deletions src/toast.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{Anchor, TOAST_HEIGHT, TOAST_WIDTH};
use egui::{pos2, vec2, Color32, Pos2, Rect, RichText, WidgetText};
use egui::{pos2, vec2, Color32, Pos2, Rect, WidgetText};
use std::{fmt::Debug, time::Duration};

/// Level of importance
Expand Down Expand Up @@ -58,28 +58,10 @@ pub struct ToastOptions {
show_progress_bar: bool,
}

/// Text and text options for a toast
pub enum ToastCaption {
/// Text with default options
Simple(String),
/// Text with custom options
WidgetText(WidgetText),
}

impl ToastCaption {
/// Text within the caption
pub fn text(&self) -> &str {
match self {
Self::Simple(s) => s,
Self::WidgetText(wt) => wt.text(),
}
}
}

/// Single notification or *toast*
pub struct Toast {
pub(crate) level: ToastLevel,
pub(crate) caption: ToastCaption,
pub(crate) caption: WidgetText,
// (initial, current)
pub(crate) duration: Option<(f32, f32)>,
pub(crate) height: f32,
Expand Down Expand Up @@ -107,7 +89,7 @@ fn duration_to_seconds_f32(duration: Duration) -> f32 {
}

impl Toast {
fn new(caption: impl Into<ToastCaption>, options: ToastOptions) -> Self {
fn new(caption: impl Into<WidgetText>, options: ToastOptions) -> Self {
Self {
caption: caption.into(),
height: TOAST_HEIGHT,
Expand All @@ -125,12 +107,12 @@ impl Toast {
}

/// Creates new basic toast, can be closed by default.
pub fn basic(caption: impl Into<ToastCaption>) -> Self {
pub fn basic(caption: impl Into<WidgetText>) -> Self {
Self::new(caption, ToastOptions::default())
}

/// Creates new success toast, can be closed by default.
pub fn success(caption: impl Into<ToastCaption>) -> Self {
pub fn success(caption: impl Into<WidgetText>) -> Self {
Self::new(
caption,
ToastOptions {
Expand All @@ -141,7 +123,7 @@ impl Toast {
}

/// Creates new info toast, can be closed by default.
pub fn info(caption: impl Into<ToastCaption>) -> Self {
pub fn info(caption: impl Into<WidgetText>) -> Self {
Self::new(
caption,
ToastOptions {
Expand All @@ -152,7 +134,7 @@ impl Toast {
}

/// Creates new warning toast, can be closed by default.
pub fn warning(caption: impl Into<ToastCaption>) -> Self {
pub fn warning(caption: impl Into<WidgetText>) -> Self {
Self::new(
caption,
ToastOptions {
Expand All @@ -163,7 +145,7 @@ impl Toast {
}

/// Creates new error toast, can not be closed by default.
pub fn error(caption: impl Into<ToastCaption>) -> Self {
pub fn error(caption: impl Into<WidgetText>) -> Self {
Self::new(
caption,
ToastOptions {
Expand All @@ -175,7 +157,7 @@ impl Toast {
}

/// Creates new custom toast, can be closed by default.
pub fn custom(caption: impl Into<ToastCaption>, level: ToastLevel) -> Self {
pub fn custom(caption: impl Into<WidgetText>, level: ToastLevel) -> Self {
Self::new(
caption,
ToastOptions {
Expand Down Expand Up @@ -267,33 +249,3 @@ impl Toast {
}
}
}

impl From<String> for ToastCaption {
fn from(s: String) -> Self {
Self::Simple(s)
}
}

impl From<&String> for ToastCaption {
fn from(s: &String) -> Self {
Self::Simple(s.clone())
}
}

impl From<&str> for ToastCaption {
fn from(s: &str) -> Self {
Self::Simple(s.to_owned())
}
}

impl From<WidgetText> for ToastCaption {
fn from(wt: WidgetText) -> Self {
Self::WidgetText(wt)
}
}

impl From<RichText> for ToastCaption {
fn from(text: RichText) -> Self {
Self::WidgetText(text.into())
}
}

0 comments on commit b59f817

Please sign in to comment.