use peniko::Color;
#[macro_export]
macro_rules! debug_panic {
($msg:expr$(,)?) => {
if cfg!(debug_assertions) {
panic!($msg);
} else {
tracing::error!($msg);
}
};
($fmt:expr, $($arg:tt)+) => {
if cfg!(debug_assertions) {
panic!($fmt, $($arg)*);
} else {
tracing::error!($fmt, $($arg)*);
}
};
}
use crate::core::{Widget, WidgetId};
pub use crate::debug_panic;
pub trait Sanitize {
#[track_caller]
fn sanitize(self, name: &str) -> Self;
}
impl Sanitize for f64 {
#[track_caller]
fn sanitize(self, name: &str) -> Self {
if !self.is_finite() {
debug_panic!("{name} must be finite. Received: {self}");
0.
} else if self < 0. {
debug_panic!("{name} must be non-negative. Received: {self}");
0.
} else {
self
}
}
}
impl Sanitize for Option<f64> {
#[track_caller]
fn sanitize(self, name: &str) -> Self {
match self {
Some(val) => {
if !val.is_finite() {
debug_panic!("{name} must be finite. Received: {val}");
None
} else if val < 0. {
debug_panic!("{name} must be non-negative. Received: {val}");
None
} else {
Some(val)
}
}
None => None,
}
}
}
pub(crate) type AnyMap = anymap3::Map<dyn anymap3::CloneAny + Send + Sync>;
pub fn bounding_box_to_rect(bb: parley::BoundingBox) -> kurbo::Rect {
kurbo::Rect {
x0: bb.x0,
y0: bb.y0,
x1: bb.x1,
y1: bb.y1,
}
}
#[cfg(not(target_arch = "wasm32"))]
pub use std::time::{Duration, Instant};
#[cfg(target_arch = "wasm32")]
pub use web_time::{Duration, Instant};
static DEBUG_COLOR: &[Color] = &[
Color::from_rgb8(230, 25, 75),
Color::from_rgb8(60, 180, 75),
Color::from_rgb8(255, 225, 25),
Color::from_rgb8(0, 130, 200),
Color::from_rgb8(245, 130, 48),
Color::from_rgb8(70, 240, 240),
Color::from_rgb8(240, 50, 230),
Color::from_rgb8(250, 190, 190),
Color::from_rgb8(0, 128, 128),
Color::from_rgb8(230, 190, 255),
Color::from_rgb8(170, 110, 40),
Color::from_rgb8(255, 250, 200),
Color::from_rgb8(128, 0, 0),
Color::from_rgb8(170, 255, 195),
Color::from_rgb8(0, 0, 128),
Color::from_rgb8(128, 128, 128),
Color::from_rgb8(255, 255, 255),
Color::from_rgb8(0, 0, 0),
];
pub fn get_debug_color(id: u64) -> Color {
let color_num = id as usize % DEBUG_COLOR.len();
DEBUG_COLOR[color_num]
}
pub(crate) struct ParentLinkedList<'a> {
pub(crate) widget: &'a dyn Widget,
pub(crate) id: WidgetId,
pub(crate) parent: Option<&'a Self>,
}