use std::borrow::Cow;
use iced::widget::{container, text};
use iced::{Background, Border, Color, Element};
#[derive(Debug, Clone, Copy, Default)]
pub enum BadgeVariant {
#[default]
Default,
Primary,
Success,
Warning,
Error,
}
impl BadgeVariant {
fn colors(self) -> (Color, Color) {
match self {
Self::Default => (Color::from_rgb(0.9, 0.9, 0.9), Color::from_rgb(0.3, 0.3, 0.3)),
Self::Primary => (Color::from_rgb(0.22, 0.47, 0.87), Color::WHITE),
Self::Success => (Color::from_rgb(0.13, 0.70, 0.40), Color::WHITE),
Self::Warning => (Color::from_rgb(0.95, 0.65, 0.15), Color::BLACK),
Self::Error => (Color::from_rgb(0.87, 0.24, 0.24), Color::WHITE),
}
}
}
pub struct Badge<'a> {
content: Cow<'a, str>,
variant: BadgeVariant,
}
impl<'a> Badge<'a> {
#[must_use]
pub fn new(content: impl Into<Cow<'a, str>>) -> Self {
Self {
content: content.into(),
variant: BadgeVariant::default(),
}
}
#[must_use]
pub fn count(count: usize) -> Self {
Self::new(count.to_string())
}
#[must_use]
pub fn variant(mut self, variant: BadgeVariant) -> Self {
self.variant = variant;
self
}
#[must_use]
pub fn primary(self) -> Self {
self.variant(BadgeVariant::Primary)
}
#[must_use]
pub fn success(self) -> Self {
self.variant(BadgeVariant::Success)
}
#[must_use]
pub fn warning(self) -> Self {
self.variant(BadgeVariant::Warning)
}
#[must_use]
pub fn error(self) -> Self {
self.variant(BadgeVariant::Error)
}
}
impl<'a, Message: 'a> From<Badge<'a>> for Element<'a, Message, iced::Theme> {
fn from(badge: Badge<'a>) -> Self {
let (bg_color, text_color) = badge.variant.colors();
let content: String = badge.content.into_owned();
container(text(content).size(12).color(text_color))
.padding([2, 8])
.style(move |_theme: &iced::Theme| container::Style {
background: Some(Background::Color(bg_color)),
border: Border {
radius: 12.0.into(),
..Default::default()
},
..Default::default()
})
.into()
}
}