use std::sync::Arc;
use super::{
icon::{Icon, SpinMotion},
names,
part_style::Length,
recipe::{Case, Name, Recipe, ResolvedPart, ResolvedRecipe},
shared::one_line,
theme::{Themed, current_theme},
};
use crate::{
app::theme::{IconSize, PaletteColor, Theme, TypographyStyle},
components::Row,
layout::{Alignment, text_style::FontWeight},
node::Element,
};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum StatusLabelPart {
Root,
Icon,
Label,
}
names!(StatusLabelPart {
Root => "root",
Icon => "icon",
Label => "label",
});
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum StatusLabelState {
Spinning,
}
names!(StatusLabelState {
Spinning => "spinning",
});
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum StatusLabelVariant {
#[default]
Neutral,
Muted,
Info,
Success,
Warning,
Destructive,
}
names!(StatusLabelVariant {
Neutral => "neutral",
Muted => "muted",
Info => "info",
Success => "success",
Warning => "warning",
Destructive => "destructive",
});
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum StatusLabelSize {
Sm,
#[default]
Md,
}
names!(StatusLabelSize {
Sm => "sm",
Md => "md",
});
pub type StatusLabelRenderer = Arc<dyn Fn(StatusLabelView) -> Element + Send + Sync>;
pub struct StatusLabel {
label: Arc<str>,
icon: Option<Name>,
spin: bool,
motion: Option<SpinMotion>,
variant: Name,
size: Name,
states: Vec<Name>,
rules: Recipe,
id: Option<Arc<str>>,
key: Option<Arc<str>>,
}
impl StatusLabel {
pub fn new(label: &str) -> Self {
Self {
label: Arc::from(label),
icon: None,
spin: false,
motion: None,
variant: StatusLabelVariant::default().into(),
size: StatusLabelSize::default().into(),
states: Vec::new(),
rules: Recipe::new(),
id: None,
key: None,
}
}
pub fn icon(mut self, name: impl Into<Name>) -> Self {
self.icon = Some(name.into());
self
}
pub fn spin(mut self, spin: bool) -> Self {
self.spin = spin;
self
}
pub fn spin_motion(mut self, motion: SpinMotion) -> Self {
self.motion = Some(motion);
self
}
pub fn variant(mut self, variant: impl Into<Name>) -> Self {
self.variant = variant.into();
self
}
pub fn size(mut self, size: impl Into<Name>) -> Self {
self.size = size.into();
self
}
pub fn state(mut self, state: impl Into<Name>, on: bool) -> Self {
let state = state.into();
self.states.retain(|held| *held != state);
if on {
self.states.push(state);
}
self
}
pub fn style(mut self, part: impl Into<Name>, style: impl FnOnce(super::PartStyle) -> super::PartStyle) -> Self {
self.rules = self.rules.base(part, style);
self
}
pub fn rules(mut self, rules: &Recipe) -> Self {
self.rules = self.rules.extend(rules);
self
}
pub fn id(mut self, id: impl Into<Arc<str>>) -> Self {
self.id = Some(id.into());
self
}
pub fn key(mut self, key: impl Into<Arc<str>>) -> Self {
self.key = Some(key.into());
self
}
pub fn build(self, theme: &Theme) -> Element {
let case = Case {
variant: Some(self.variant),
size: Some(self.size),
density: None,
breakpoint: super::current_breakpoint(),
states: self.states.clone(),
}
.state(StatusLabelState::Spinning, self.spin);
let view = StatusLabelView {
label: self.label,
icon: self.icon,
spin: self.spin,
motion: self.motion.unwrap_or_else(|| theme.spin_motion()),
variant: self.variant,
size: self.size,
recipe: theme
.resolve::<StatusLabel>(&case, Some(&self.rules))
.owned_by(self.id.as_ref()),
id: self.id,
key: self.key,
};
match theme.renderer::<StatusLabel>() {
Some(renderer) => renderer(view),
None => view.render(),
}
}
}
impl From<StatusLabel> for Element {
fn from(label: StatusLabel) -> Self {
label.build(¤t_theme())
}
}
pub struct StatusLabelView {
pub label: Arc<str>,
pub icon: Option<Name>,
pub spin: bool,
pub motion: SpinMotion,
pub variant: Name,
pub size: Name,
pub recipe: ResolvedRecipe,
id: Option<Arc<str>>,
key: Option<Arc<str>>,
}
impl StatusLabelView {
pub fn part(&self, part: impl Into<Name>) -> ResolvedPart {
self.recipe.part(part)
}
pub fn theme(&self) -> &Theme {
self.recipe.theme()
}
pub fn root(&self) -> Row {
let mut root = Row::new()
.align_items(Alignment::Center)
.min_width(0.0)
.part(&self.part(StatusLabelPart::Root))
.describe("a11y_role", "status")
.describe("a11y_name", self.label.clone());
if let Some(id) = &self.id {
root = root.id(id.clone());
}
if let Some(key) = &self.key {
root = root.key(key.clone());
}
root
}
pub fn icon(&self) -> Option<Element> {
let name = self.icon?;
let part = self.part(StatusLabelPart::Icon);
let side = part.rest().width.unwrap_or(Length::Icon(IconSize::Sm));
let icon = Icon::new(name)
.size(side)
.spin(self.spin)
.spin_motion(self.motion)
.build(part.theme());
Some(Row::new().flex_shrink(0.0).part(&part).child(icon).into())
}
pub fn words(&self) -> Element {
one_line(&self.part(StatusLabelPart::Label), &self.label)
}
pub fn render(self) -> Element {
let mut root = self.root();
if let Some(icon) = self.icon() {
root = root.child(icon);
}
root.child(self.words()).into()
}
}
impl crate::ui::Styled for StatusLabel {
type Component = Self;
fn rules(self, rules: &crate::ui::Recipe) -> Self {
Self::rules(self, rules)
}
fn state(self, state: impl Into<crate::ui::Name>, on: bool) -> Self {
Self::state(self, state, on)
}
}
impl Themed for StatusLabel {
const NAME: &'static str = "status_label";
type Renderer = StatusLabelRenderer;
fn parts() -> crate::ui::Declared {
StatusLabelPart::declared()
}
fn states() -> crate::ui::Declared {
StatusLabelState::declared()
}
fn recipe() -> Recipe {
use PaletteColor::{Destructive, Foreground, Info, MutedForeground, Success, Warning};
use StatusLabelPart::{Icon, Label, Root};
let mut recipe = Recipe::new()
.base(Root, |s| s.gap(6.0).foreground(Foreground))
.base(Icon, |s| s.square(IconSize::Sm))
.base(Label, |s| {
s.typography(TypographyStyle::Caption).weight(FontWeight::Medium)
})
.size(StatusLabelSize::Sm, Root, |s| s.gap(4.0))
.size(StatusLabelSize::Sm, Icon, |s| s.square(IconSize::Xs))
.size(StatusLabelSize::Sm, Label, |s| s.font_size(11.0));
for (variant, color) in [
(StatusLabelVariant::Muted, MutedForeground),
(StatusLabelVariant::Info, Info),
(StatusLabelVariant::Success, Success),
(StatusLabelVariant::Warning, Warning),
(StatusLabelVariant::Destructive, Destructive),
] {
recipe = recipe.variant(variant, Root, |s| s.foreground(color));
}
recipe
}
}