use std::sync::Arc;
use super::{
names,
part_style::{Length, PartStyle},
recipe::{Case, Name, Recipe, ResolvedPart, ResolvedRecipe},
shared::glyph,
theme::{Themed, current_theme},
};
use crate::{
app::theme::{BorderSize, PaletteColor, RadiusSize, Theme, TypographyStyle},
components::{Column, Row},
layout::{
Alignment,
layout_kind::Justify,
text_style::{FontWeight, TextAlign},
},
node::{Element, border::Border as Line, color::Color, dimension::Dimension},
};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum EmptyStateSize {
Sm,
#[default]
Md,
Lg,
}
names!(EmptyStateSize {
Sm => "sm",
Md => "md",
Lg => "lg",
});
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum EmptyStatePart {
Root,
Mark,
Icon,
Text,
Title,
Description,
Actions,
}
names!(EmptyStatePart {
Root => "root",
Mark => "mark",
Icon => "icon",
Text => "text",
Title => "title",
Description => "description",
Actions => "actions",
});
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum EmptyStateState {
Bordered,
}
names!(EmptyStateState {
Bordered => "bordered",
});
pub type EmptyStateRenderer = Arc<dyn Fn(EmptyStateView) -> Element + Send + Sync>;
pub struct EmptyState {
title: Arc<str>,
label: Option<Arc<str>>,
description: Option<Arc<str>>,
icon: Option<Name>,
media: Option<Element>,
actions: Vec<Element>,
size: Name,
bordered: Option<bool>,
states: Vec<Name>,
rules: Recipe,
id: Option<Arc<str>>,
key: Option<Arc<str>>,
}
impl EmptyState {
pub fn new(title: &str) -> Self {
Self {
title: Arc::from(title),
label: None,
description: None,
icon: None,
media: None,
actions: Vec::new(),
size: EmptyStateSize::default().into(),
bordered: None,
states: Vec::new(),
rules: Recipe::new(),
id: None,
key: None,
}
}
pub fn label(mut self, label: &str) -> Self {
self.label = Some(Arc::from(label));
self
}
pub fn description(mut self, description: &str) -> Self {
self.description = Some(Arc::from(description));
self
}
pub fn icon(mut self, name: impl Into<Name>) -> Self {
self.icon = Some(name.into());
self
}
pub fn media(mut self, media: impl Into<Element>) -> Self {
self.media = Some(media.into());
self
}
pub fn action(mut self, action: impl Into<Element>) -> Self {
self.actions.push(action.into());
self
}
pub fn actions(mut self, actions: impl IntoIterator<Item = impl Into<Element>>) -> Self {
self.actions.extend(actions.into_iter().map(Into::into));
self
}
pub fn size(mut self, size: impl Into<Name>) -> Self {
self.size = size.into();
self
}
pub fn bordered(mut self, bordered: bool) -> Self {
self.bordered = Some(bordered);
self
}
pub fn width(mut self, width: impl Into<Length>) -> Self {
let width = width.into();
self.rules = self.rules.base(EmptyStatePart::Root, |s| s.width(width));
self
}
pub fn height(mut self, height: impl Into<Length>) -> Self {
let height = height.into();
self.rules = self.rules.base(EmptyStatePart::Root, |s| s.height(height));
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(PartStyle) -> 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 bordered = self.bordered.unwrap_or(self.size == Name::from(EmptyStateSize::Md));
let case = Case {
variant: None,
size: Some(self.size),
density: None,
breakpoint: super::current_breakpoint(),
states: self.states.clone(),
}
.state(EmptyStateState::Bordered, bordered);
let view = EmptyStateView {
title: self.title,
label: self.label,
description: self.description,
icon: self.icon,
media: self.media,
actions: self.actions,
size: self.size,
bordered,
recipe: theme
.resolve::<EmptyState>(&case, Some(&self.rules))
.owned_by(self.id.as_ref()),
id: self.id,
key: self.key,
};
match theme.renderer::<EmptyState>() {
Some(renderer) => renderer(view),
None => view.render(),
}
}
}
impl From<EmptyState> for Element {
fn from(empty: EmptyState) -> Self {
empty.build(¤t_theme())
}
}
pub struct EmptyStateView {
pub title: Arc<str>,
pub label: Option<Arc<str>>,
pub description: Option<Arc<str>>,
pub icon: Option<Name>,
pub media: Option<Element>,
pub actions: Vec<Element>,
pub size: Name,
pub bordered: bool,
pub recipe: ResolvedRecipe,
id: Option<Arc<str>>,
key: Option<Arc<str>>,
}
impl EmptyStateView {
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) -> Column {
let mut root = Column::new()
.align_items(Alignment::Center)
.justify(Justify::Center)
.part(&self.part(EmptyStatePart::Root))
.describe("a11y_role", "status")
.describe("a11y_name", self.label.clone().unwrap_or_else(|| self.title.clone()));
if let Some(description) = &self.description {
root = root.describe("a11y_description", description.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 mark(&mut self) -> Option<Element> {
if let Some(media) = self.media.take() {
return Some(media);
}
let name = self.icon?;
Some(
Row::new()
.align_items(Alignment::Center)
.justify(Justify::Center)
.part(&self.part(EmptyStatePart::Mark))
.child(glyph(&self.part(EmptyStatePart::Icon), name, false))
.into(),
)
}
pub fn text(&self) -> Column {
let mut text = Column::new()
.width(Dimension::Pct(100.0))
.align_items(Alignment::Center)
.part(&self.part(EmptyStatePart::Text))
.child(centred(&self.part(EmptyStatePart::Title), &self.title));
if let Some(description) = &self.description {
text = text.child(centred(&self.part(EmptyStatePart::Description), description));
}
text
}
pub fn actions(&mut self) -> Option<Row> {
if self.actions.is_empty() {
return None;
}
Some(
Row::new()
.align_items(Alignment::Center)
.justify(Justify::Center)
.part(&self.part(EmptyStatePart::Actions))
.with_children(std::mem::take(&mut self.actions)),
)
}
pub fn render(mut self) -> Element {
let mut root = self.root();
if let Some(mark) = self.mark() {
root = root.child(mark);
}
root = root.child(self.text());
if let Some(actions) = self.actions() {
root = root.child(actions);
}
root.into()
}
}
pub(crate) fn centred(part: &ResolvedPart, content: &str) -> Column {
let fill = Dimension::Pct(100.0);
Column::new()
.width(fill)
.part(part)
.child(part.text(content).width(fill).text_align(TextAlign::Center))
}
impl crate::ui::Styled for EmptyState {
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 EmptyState {
const NAME: &'static str = "empty_state";
type Renderer = EmptyStateRenderer;
fn parts() -> crate::ui::Declared {
EmptyStatePart::declared()
}
fn states() -> crate::ui::Declared {
EmptyStateState::declared()
}
fn recipe() -> Recipe {
use EmptyStatePart::{Actions, Description, Icon, Mark, Root, Text, Title};
use EmptyStateSize::{Lg, Sm};
use PaletteColor::*;
let clear = Color::new(0, 0, 0, 0);
Recipe::new()
.base(Root, |s| s.width(Length::Fill).padding_x(32.0).padding_y(32.0).gap(16.0))
.base(Mark, |s| s.square(40.0).radius(RadiusSize::Lg).background(Muted))
.base(Icon, |s| s.square(20.0).foreground(Foreground))
.base(Text, |s| s.gap(4.0))
.base(Title, |s| {
s.typography(TypographyStyle::Body)
.font_size(14.0)
.weight(FontWeight::Medium)
.foreground(Foreground)
})
.base(Description, |s| {
s.typography(TypographyStyle::Description)
.font_size(13.0)
.foreground(MutedForeground)
})
.base(Actions, |s| s.gap(8.0))
.state(EmptyStateState::Bordered, Root, |s| {
s.radius(RadiusSize::Xl).border(Line::inside(BorderSize::Sm, PaletteColor::Border))
})
.size(Sm, Root, |s| s.padding_x(16.0).gap(10.0))
.size(Sm, Mark, |s| s.square(22.0).radius(0.0).background(clear))
.size(Sm, Icon, |s| s.square(22.0).foreground(MutedForeground))
.size(Sm, Text, |s| s.gap(10.0))
.size(Lg, Root, |s| s.padding_x(24.0))
.size(Lg, Mark, |s| s.square(64.0).radius(RadiusSize::Full))
.size(Lg, Icon, |s| s.square(28.0))
.size(Lg, Text, |s| s.gap(6.0))
.size(Lg, Title, |s| s.font_size(18.0).weight(FontWeight::SemiBold))
.size(Lg, Description, |s| s.font_size(15.0))
}
}