use std::sync::Arc;
use super::{
empty_state::centred,
icons, names,
part_style::{Length, PartStyle},
recipe::{Case, Name, Recipe, ResolvedPart, ResolvedRecipe},
shared::{IconChoice, glyph, shown_icon},
theme::{Themed, current_theme},
};
use crate::{
app::theme::{BorderSize, PaletteColor, RadiusSize, Theme, TypographyStyle},
components::{Column, Row},
layout::{Alignment, layout_kind::Justify, text_style::FontWeight},
node::{Element, border::Border as Line, dimension::Dimension},
};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum OutcomeVariant {
Info,
#[default]
Success,
Warning,
Destructive,
NotFound,
}
names!(OutcomeVariant {
Info => "info",
Success => "success",
Warning => "warning",
Destructive => "destructive",
NotFound => "not_found",
});
impl OutcomeVariant {
pub fn icon(self) -> &'static str {
match self {
Self::Info => icons::INFO,
Self::Success => icons::CHECK,
Self::Warning => icons::WARNING,
Self::Destructive => icons::CLOSE,
Self::NotFound => icons::SEARCH_CLOSE,
}
}
fn named(name: Name) -> Option<Self> {
Self::all().iter().copied().find(|variant| Name::from(*variant) == name)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum OutcomePart {
Root,
Mark,
Icon,
Text,
Title,
Description,
Body,
Actions,
}
names!(OutcomePart {
Root => "root",
Mark => "mark",
Icon => "icon",
Text => "text",
Title => "title",
Description => "description",
Body => "body",
Actions => "actions",
});
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum OutcomeState {
Bordered,
}
names!(OutcomeState {
Bordered => "bordered",
});
pub type OutcomeRenderer = Arc<dyn Fn(OutcomeView) -> Element + Send + Sync>;
pub struct Outcome {
variant: Name,
title: Arc<str>,
label: Option<Arc<str>>,
description: Option<Arc<str>>,
icon: Option<IconChoice>,
media: Option<Element>,
body: Vec<Element>,
actions: Vec<Element>,
bordered: bool,
states: Vec<Name>,
rules: Recipe,
id: Option<Arc<str>>,
key: Option<Arc<str>>,
}
impl Outcome {
pub fn new(title: &str) -> Self {
Self {
variant: OutcomeVariant::default().into(),
title: Arc::from(title),
label: None,
description: None,
icon: None,
media: None,
body: Vec::new(),
actions: Vec::new(),
bordered: true,
states: Vec::new(),
rules: Recipe::new(),
id: None,
key: None,
}
}
pub fn variant(mut self, variant: impl Into<Name>) -> Self {
self.variant = variant.into();
self
}
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, icon: impl Into<IconChoice>) -> Self {
self.icon = Some(icon.into());
self
}
pub fn media(mut self, media: impl Into<Element>) -> Self {
self.media = Some(media.into());
self
}
pub fn child(mut self, child: impl Into<Element>) -> Self {
self.body.push(child.into());
self
}
pub fn children(mut self, children: impl IntoIterator<Item = impl Into<Element>>) -> Self {
self.body.extend(children.into_iter().map(Into::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 bordered(mut self, bordered: bool) -> Self {
self.bordered = bordered;
self
}
pub fn width(mut self, width: impl Into<Length>) -> Self {
let width = width.into();
self.rules = self.rules.base(OutcomePart::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(OutcomePart::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 case = Case {
variant: Some(self.variant),
size: None,
density: None,
breakpoint: super::current_breakpoint(),
states: self.states.clone(),
}
.state(OutcomeState::Bordered, self.bordered);
let default_icon = OutcomeVariant::named(self.variant).map_or(icons::INFO, OutcomeVariant::icon);
let view = OutcomeView {
variant: self.variant,
title: self.title,
label: self.label,
description: self.description,
icon: shown_icon(self.icon, default_icon),
media: self.media,
body: self.body,
actions: self.actions,
bordered: self.bordered,
recipe: theme
.resolve::<Outcome>(&case, Some(&self.rules))
.owned_by(self.id.as_ref()),
id: self.id,
key: self.key,
};
match theme.renderer::<Outcome>() {
Some(renderer) => renderer(view),
None => view.render(),
}
}
}
impl From<Outcome> for Element {
fn from(outcome: Outcome) -> Self {
outcome.build(¤t_theme())
}
}
pub struct OutcomeView {
pub variant: Name,
pub title: Arc<str>,
pub label: Option<Arc<str>>,
pub description: Option<Arc<str>>,
pub icon: Option<Name>,
pub media: Option<Element>,
pub body: Vec<Element>,
pub actions: Vec<Element>,
pub bordered: bool,
pub recipe: ResolvedRecipe,
id: Option<Arc<str>>,
key: Option<Arc<str>>,
}
impl OutcomeView {
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(OutcomePart::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 icon = self.icon?;
Some(
Row::new()
.align_items(Alignment::Center)
.justify(Justify::Center)
.part(&self.part(OutcomePart::Mark))
.child(glyph(&self.part(OutcomePart::Icon), icon, false))
.into(),
)
}
pub fn text(&self) -> Column {
let mut text = Column::new()
.width(Dimension::Pct(100.0))
.align_items(Alignment::Center)
.part(&self.part(OutcomePart::Text))
.child(centred(&self.part(OutcomePart::Title), &self.title));
if let Some(description) = &self.description {
text = text.child(centred(&self.part(OutcomePart::Description), description));
}
text
}
pub fn body(&mut self) -> Option<Column> {
if self.body.is_empty() {
return None;
}
Some(
Column::new()
.width(Dimension::Pct(100.0))
.part(&self.part(OutcomePart::Body))
.with_children(std::mem::take(&mut self.body)),
)
}
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(OutcomePart::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(body) = self.body() {
root = root.child(body);
}
if let Some(actions) = self.actions() {
root = root.child(actions);
}
root.into()
}
}
impl crate::ui::Styled for Outcome {
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 Outcome {
const NAME: &'static str = "outcome";
type Renderer = OutcomeRenderer;
fn parts() -> crate::ui::Declared {
OutcomePart::declared()
}
fn states() -> crate::ui::Declared {
OutcomeState::declared()
}
fn recipe() -> Recipe {
use OutcomePart::{Actions, Body, Description, Icon, Mark, Root, Text, Title};
use OutcomeVariant::{Destructive as Failed, Info, NotFound, Success, Warning};
use PaletteColor::*;
let mut recipe = Recipe::new()
.base(Root, |s| s.width(300.0).max_width(Length::Fill).gap(16.0))
.base(Mark, |s| s.square(48.0).radius(RadiusSize::Full).background(Muted))
.base(Icon, |s| s.square(24.0).foreground(Foreground))
.base(Text, |s| s.gap(4.0))
.base(Title, |s| {
s.typography(TypographyStyle::Body)
.font_size(16.0)
.weight(FontWeight::SemiBold)
.foreground(Foreground)
})
.base(Description, |s| {
s.typography(TypographyStyle::Description)
.font_size(13.0)
.foreground(MutedForeground)
})
.base(Body, |s| s.gap(8.0))
.base(Actions, |s| s.gap(8.0))
.state(OutcomeState::Bordered, Root, |s| {
s.padding_x(24.0)
.padding_y(24.0)
.radius(RadiusSize::Lg)
.background(Background)
.border(Line::inside(BorderSize::Sm, PaletteColor::Border))
});
for (variant, strong, muted) in [
(Success, PaletteColor::Success, SuccessMuted),
(Failed, Destructive, DestructiveMuted),
(Warning, PaletteColor::Warning, WarningMuted),
(Info, PaletteColor::Info, InfoMuted),
(NotFound, Foreground, Muted),
] {
recipe = recipe
.variant(variant, Mark, |s| s.background(muted))
.variant(variant, Icon, |s| s.foreground(strong));
}
recipe
}
}