use std::{collections::HashMap, sync::Arc};
use super::RoleName;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct IconGlyph {
pub family: Arc<str>,
pub glyph: char,
}
impl IconGlyph {
pub fn new(family: impl Into<Arc<str>>, glyph: char) -> Self {
Self {
family: family.into(),
glyph,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum IconTint {
Stroke,
Fill,
None,
}
#[derive(Clone)]
pub struct IconSvg {
source: Arc<str>,
tint: IconTint,
#[cfg(feature = "canvas")]
data: Option<crate::svg::SvgData>,
}
impl IconSvg {
pub fn new(source: impl Into<Arc<str>>, tint: IconTint) -> Self {
let source = source.into();
Self {
#[cfg(feature = "canvas")]
data: crate::svg::SvgData::parse(source.as_bytes()),
source,
tint,
}
}
pub fn stroked(source: impl Into<Arc<str>>) -> Self {
Self::new(source, IconTint::Stroke)
}
pub fn filled(source: impl Into<Arc<str>>) -> Self {
Self::new(source, IconTint::Fill)
}
pub fn colored(source: impl Into<Arc<str>>) -> Self {
Self::new(source, IconTint::None)
}
pub fn source(&self) -> &str {
&self.source
}
pub fn tint(&self) -> IconTint {
self.tint
}
#[cfg(feature = "canvas")]
pub fn data(&self) -> Option<crate::svg::SvgData> {
let data = self.data.clone()?;
Some(match self.tint {
IconTint::Stroke => data.stroke_current_color(),
IconTint::Fill => data.fill_current_color(),
IconTint::None => data,
})
}
}
impl std::fmt::Debug for IconSvg {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter
.debug_struct("IconSvg")
.field("tint", &self.tint)
.field("bytes", &self.source.len())
.finish()
}
}
impl PartialEq for IconSvg {
fn eq(&self, other: &Self) -> bool {
self.tint == other.tint && self.source == other.source
}
}
#[derive(Clone, Debug, PartialEq)]
enum Drawing {
Glyph(IconGlyph),
Svg(IconSvg),
}
#[derive(Clone, Debug, PartialEq)]
pub struct ThemeIcons {
glyphs: HashMap<RoleName, Drawing>,
}
impl ThemeIcons {
pub fn lurq() -> Self {
let mut icons = Self::empty();
icons.set_font(crate::ui::icons::FAMILY, crate::ui::icons::ALL.iter().copied());
icons
}
pub fn empty() -> Self {
Self { glyphs: HashMap::new() }
}
pub fn get(&self, name: impl Into<RoleName>) -> Option<&IconGlyph> {
match self.glyphs.get(&name.into())? {
Drawing::Glyph(glyph) => Some(glyph),
Drawing::Svg(_) => None,
}
}
pub fn svg(&self, name: impl Into<RoleName>) -> Option<&IconSvg> {
match self.glyphs.get(&name.into())? {
Drawing::Svg(svg) => Some(svg),
Drawing::Glyph(_) => None,
}
}
pub fn set(&mut self, name: impl Into<RoleName>, glyph: IconGlyph) {
self.glyphs.insert(name.into(), Drawing::Glyph(glyph));
}
pub fn set_svg(&mut self, name: impl Into<RoleName>, svg: IconSvg) {
self.glyphs.insert(name.into(), Drawing::Svg(svg));
}
pub fn set_font(
&mut self,
family: impl Into<Arc<str>>,
glyphs: impl IntoIterator<Item = (impl Into<RoleName>, char)>,
) {
let family = family.into();
for (name, glyph) in glyphs {
self.set(name, IconGlyph::new(family.clone(), glyph));
}
}
pub fn remove(&mut self, name: impl Into<RoleName>) -> Option<IconGlyph> {
match self.glyphs.remove(&name.into())? {
Drawing::Glyph(glyph) => Some(glyph),
Drawing::Svg(_) => None,
}
}
pub fn names(&self) -> Vec<RoleName> {
let mut names: Vec<RoleName> = self.glyphs.keys().copied().collect();
names.sort_by_key(|name| name.as_str());
names
}
pub fn len(&self) -> usize {
self.glyphs.len()
}
pub fn is_empty(&self) -> bool {
self.glyphs.is_empty()
}
}
impl Default for ThemeIcons {
fn default() -> Self {
Self::lurq()
}
}