Skip to content

Commit

Permalink
Merge pull request #34 from jorgsaa/master
Browse files Browse the repository at this point in the history
Added support for `egui::widget_text::WidgetText`
  • Loading branch information
ItsEthra authored Oct 14, 2024
2 parents 55e0f41 + b59f817 commit b499a5d
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 53 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.17.0
* (breaking) removed `Toast::font(font: FontId)`, this can now be done by using `egui::widget_text::RichText` and `RichText::font`
* Added support for `egui::widget_text::WidgetText` in Toasts, this allows for more customization of the text in the toast.

# 0.16.0

* (breaking) Updated to egui `0.29`.
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "egui-notify"
version = "0.16.0"
version = "0.17.0"
edition = "2021"
license = "MIT"
repository = "https://github.com/ItsEthra/egui-notify"
Expand Down
21 changes: 17 additions & 4 deletions examples/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use eframe::{
egui::{Context, Slider, Window},
App, Frame, NativeOptions,
};
use egui::{Color32, FontId, Shadow, Style, Visuals};
use egui::{Color32, Shadow, Style, Visuals};
use egui_notify::{Toast, Toasts};
use std::time::Duration;

Expand All @@ -17,7 +17,7 @@ struct ExampleApp {
font_size: f32,
dark: bool,
custom_level_string: String,
custom_level_color: egui::Color32,
custom_level_color: Color32,
shadow: bool,
}

Expand Down Expand Up @@ -62,10 +62,10 @@ impl App for ExampleApp {
} else {
None
};

t.closable(self.closable)
.duration(duration)
.show_progress_bar(self.show_progress_bar)
.font(FontId::proportional(self.font_size));
.show_progress_bar(self.show_progress_bar);
};

ui.horizontal(|ui| {
Expand All @@ -89,6 +89,19 @@ impl App for ExampleApp {
customize_toast(self.toasts.basic(self.caption.clone()));
}

if ui.button("Rich text").clicked() {
customize_toast(
self.toasts.success(
egui::RichText::new(self.caption.clone())
.color(Color32::GREEN)
.background_color(Color32::DARK_GRAY)
.size(self.font_size)
.italics()
.underline(),
),
);
}

if ui.button("Custom").clicked() {
customize_toast(self.toasts.custom(
self.caption.clone(),
Expand Down
54 changes: 27 additions & 27 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ pub use anchor::*;

#[doc(hidden)]
pub use egui::__run_test_ctx;
use egui::text::TextWrapping;
use egui::{
vec2, Color32, Context, FontId, Id, LayerId, Order, Rect, Rounding, Shadow, Stroke, 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 @@ -102,34 +104,34 @@ impl Toasts {
}

/// Shortcut for adding a toast with info `success`.
pub fn success(&mut self, caption: impl Into<String>) -> &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<String>) -> &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<String>) -> &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<String>) -> &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<String>) -> &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<String>,
caption: impl Into<WidgetText>,
level_string: String,
level_color: egui::Color32,
) -> &mut Toast {
Expand Down Expand Up @@ -237,28 +239,18 @@ impl Toasts {
}
}

let caption_font = toast
.font
.as_ref()
.or(self.font.as_ref())
.or(ctx.style().override_font_id.as_ref())
.cloned()
.unwrap_or_else(|| FontId::proportional(16.));

// Create toast label
let caption_galley = ctx.fonts(|f| {
f.layout(
toast.caption.clone(),
caption_font,
visuals.fg_stroke.color,
f32::INFINITY,
)
});
let caption_galley = toast.caption.clone().into_galley_impl(
ctx,
ctx.style().as_ref(),
TextWrapping::from_wrap_mode_and_width(TextWrapMode::Extend, f32::INFINITY),
FontSelection::Default,
Align::LEFT,
);

let (caption_width, caption_height) =
(caption_galley.rect.width(), caption_galley.rect.height());

let line_count = toast.caption.chars().filter(|c| *c == '\n').count() + 1;
let line_count = toast.caption.text().chars().filter(|c| *c == '\n').count() + 1;
let icon_width = caption_height / line_count as f32;
let rounding = Rounding::same(4.);

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
31 changes: 10 additions & 21 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, FontId, Pos2, Rect};
use egui::{pos2, vec2, Color32, Pos2, Rect, WidgetText};
use std::{fmt::Debug, time::Duration};

/// Level of importance
Expand All @@ -12,7 +12,7 @@ pub enum ToastLevel {
Error,
Success,
None,
Custom(String, egui::Color32),
Custom(String, Color32),
}

#[derive(Debug)]
Expand Down Expand Up @@ -59,11 +59,9 @@ pub struct ToastOptions {
}

/// Single notification or *toast*
#[derive(Debug)]
pub struct Toast {
pub(crate) level: ToastLevel,
pub(crate) caption: String,
pub(crate) font: Option<FontId>,
pub(crate) caption: WidgetText,
// (initial, current)
pub(crate) duration: Option<(f32, f32)>,
pub(crate) height: f32,
Expand Down Expand Up @@ -91,7 +89,7 @@ fn duration_to_seconds_f32(duration: Duration) -> f32 {
}

impl Toast {
fn new(caption: impl Into<String>, options: ToastOptions) -> Self {
fn new(caption: impl Into<WidgetText>, options: ToastOptions) -> Self {
Self {
caption: caption.into(),
height: TOAST_HEIGHT,
Expand All @@ -103,20 +101,18 @@ impl Toast {
closable: options.closable,
show_progress_bar: options.show_progress_bar,
level: options.level,

value: 0.,
state: ToastState::Appear,
font: None,
}
}

/// Creates new basic toast, can be closed by default.
pub fn basic(caption: impl Into<String>) -> 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<String>) -> Self {
pub fn success(caption: impl Into<WidgetText>) -> Self {
Self::new(
caption,
ToastOptions {
Expand All @@ -127,7 +123,7 @@ impl Toast {
}

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

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

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

/// Creates new custom toast, can be closed by default.
pub fn custom(caption: impl Into<String>, level: ToastLevel) -> Self {
pub fn custom(caption: impl Into<WidgetText>, level: ToastLevel) -> Self {
Self::new(
caption,
ToastOptions {
Expand All @@ -185,13 +181,6 @@ impl Toast {
self
}

/// Changes the font used to draw the caption, it takes precedence over the value from
/// [`Toasts`].
pub fn font(&mut self, font: FontId) -> &mut Self {
self.font = Some(font);
self
}

/// Can the user close the toast?
pub fn closable(&mut self, closable: bool) -> &mut Self {
self.closable = closable;
Expand Down

0 comments on commit b499a5d

Please sign in to comment.