use egui::Vec2;
#[derive(Clone, Copy)]
pub struct Icon {
pub name: &'static str,
pub svg_bytes: &'static [u8],
}
impl Icon {
#[inline]
pub const fn new(name: &'static str, svg_bytes: &'static [u8]) -> Self {
Self { name, svg_bytes }
}
#[inline]
pub fn as_image(&self, size: Vec2) -> egui::Image<'static> {
let uri = format!("bytes://{}", self.name);
egui::Image::from_bytes(uri, self.svg_bytes).fit_to_exact_size(size)
}
#[inline]
pub fn as_image_tinted(&self, size: Vec2, tint: egui::Color32) -> egui::Image<'static> {
self.as_image(size).tint(tint)
}
#[inline]
pub fn as_image_square(&self, size: f32) -> egui::Image<'static> {
self.as_image(Vec2::splat(size))
}
#[inline]
pub const fn name(&self) -> &'static str {
self.name
}
#[inline]
pub const fn svg_bytes(&self) -> &'static [u8] {
self.svg_bytes
}
#[inline]
pub const fn byte_size(&self) -> usize {
self.svg_bytes.len()
}
#[inline]
pub fn as_button(&self, size: Vec2) -> egui::Button<'static> {
egui::Button::image(self.as_image(size)).image_tint_follows_text_color(true)
}
#[inline]
pub fn as_button_with_label(
&self,
size: Vec2,
label: impl Into<egui::WidgetText>,
) -> egui::Button<'static> {
egui::Button::image_and_text(self.as_image(size), label).image_tint_follows_text_color(true)
}
#[inline]
pub fn as_button_square(&self, size: f32) -> egui::Button<'static> {
self.as_button(Vec2::splat(size))
}
}
impl std::fmt::Debug for Icon {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Icon")
.field("name", &self.name)
.field("byte_size", &self.svg_bytes.len())
.finish()
}
}
impl PartialEq for Icon {
fn eq(&self, other: &Self) -> bool {
self.name == other.name
}
}
impl Eq for Icon {}
impl std::hash::Hash for Icon {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.name.hash(state);
}
}