use std::sync::Arc;
use super::{
Orientation, names,
part_style::PartStyle,
recipe::{Case, Name, Recipe, ResolvedPart, ResolvedRecipe},
theme::{Themed, current_theme},
};
use crate::{
app::theme::{BorderSize, PaletteColor, Theme, TypographyStyle},
components::{Column, Grid, Row},
layout::{Alignment, grid::GridTrack},
node::{Element, border::Border, dimension::Dimension},
};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum DataListPart {
Root,
Item,
Term,
Value,
}
names!(DataListPart {
Root => "root",
Item => "item",
Term => "term",
Value => "value",
});
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum DataListState {
Horizontal,
Vertical,
Separator,
}
names!(DataListState {
Horizontal => "horizontal",
Vertical => "vertical",
Separator => "separator",
});
pub type DataListOrientation = Orientation;
pub type DataListRenderer = Arc<dyn Fn(DataListView) -> Element + Send + Sync>;
enum Shown {
Text(Arc<str>),
Element(Element),
}
pub struct DataList {
items: Vec<(Arc<str>, Shown)>,
orientation: Orientation,
label: Option<Arc<str>>,
states: Vec<Name>,
rules: Recipe,
id: Option<Arc<str>>,
key: Option<Arc<str>>,
}
impl Default for DataList {
fn default() -> Self {
Self::new()
}
}
impl DataList {
pub fn new() -> Self {
Self {
items: Vec::new(),
orientation: Orientation::Horizontal,
label: None,
states: Vec::new(),
rules: Recipe::new(),
id: None,
key: None,
}
}
pub fn item(mut self, term: &str, value: impl Into<Element>) -> Self {
self.items.push((Arc::from(term), Shown::Element(value.into())));
self
}
pub fn items<'a, V: Into<Element>>(mut self, items: impl IntoIterator<Item = (&'a str, V)>) -> Self {
for (term, value) in items {
self = self.item(term, value);
}
self
}
pub fn text(mut self, term: &str, value: &str) -> Self {
self.items.push((Arc::from(term), Shown::Text(Arc::from(value))));
self
}
pub fn texts<'a>(mut self, items: impl IntoIterator<Item = (&'a str, &'a str)>) -> Self {
for (term, value) in items {
self = self.text(term, value);
}
self
}
pub fn orientation(mut self, orientation: Orientation) -> Self {
self.orientation = orientation;
self
}
pub fn label(mut self, label: &str) -> Self {
self.label = Some(Arc::from(label));
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: None,
size: None,
density: None,
breakpoint: super::current_breakpoint(),
states: self.states.clone(),
}
.state(DataListState::Horizontal, !self.orientation.is_vertical())
.state(DataListState::Vertical, self.orientation.is_vertical());
let recipe = theme
.resolve::<DataList>(&case, Some(&self.rules))
.owned_by(self.id.as_ref());
let separator = theme
.resolve::<DataList>(&case.state(DataListState::Separator, true), Some(&self.rules))
.owned_like(&recipe);
let view = DataListView {
items: self
.items
.into_iter()
.map(|(term, shown)| DataListItem {
term,
value: match &shown {
Shown::Text(text) => Some(text.clone()),
Shown::Element(_) => None,
},
element: match shown {
Shown::Element(element) => Some(element),
Shown::Text(_) => None,
},
})
.collect(),
orientation: self.orientation,
label: self.label,
recipe,
separator,
id: self.id,
key: self.key,
};
match theme.renderer::<DataList>() {
Some(renderer) => renderer(view),
None => view.render(),
}
}
}
impl From<DataList> for Element {
fn from(list: DataList) -> Self {
list.build(¤t_theme())
}
}
pub struct DataListItem {
pub term: Arc<str>,
pub value: Option<Arc<str>>,
pub element: Option<Element>,
}
pub struct DataListView {
pub items: Vec<DataListItem>,
pub orientation: Orientation,
pub label: Option<Arc<str>>,
pub recipe: ResolvedRecipe,
pub separator: ResolvedRecipe,
id: Option<Arc<str>>,
key: Option<Arc<str>>,
}
impl DataListView {
pub fn part(&self, part: impl Into<Name>) -> ResolvedPart {
self.recipe.part(part)
}
pub fn theme(&self) -> &Theme {
self.recipe.theme()
}
pub fn term(&self, item: &DataListItem, lined: bool) -> Row {
let part = self.of(lined).part(DataListPart::Term);
Row::new()
.align_items(Alignment::Center)
.describe("a11y_role", "term")
.part(&part)
.child(part.text(&item.term))
}
pub fn value(&self, item: &mut DataListItem, lined: bool) -> Row {
let part = self.of(lined).part(DataListPart::Value);
let fill = Dimension::Pct(100.0);
let mut value = Row::new()
.align_items(Alignment::Center)
.min_width(Dimension::Px(0.0))
.describe("a11y_role", "definition")
.part(&part);
if let Some(text) = &item.value {
value = value.child(
Column::new()
.width(fill)
.min_width(Dimension::Px(0.0))
.child(part.text(text).width(fill)),
);
}
if let Some(element) = item.element.take() {
value = value.child(element);
}
value
}
fn of(&self, lined: bool) -> &ResolvedRecipe {
if lined { &self.separator } else { &self.recipe }
}
pub fn render(mut self) -> Element {
let items = std::mem::take(&mut self.items);
let count = items.len();
let root_part = self.part(DataListPart::Root);
if self.orientation.is_vertical() {
let mut root = Column::new().align_items(Alignment::Stretch).part(&root_part);
for (index, mut item) in items.into_iter().enumerate() {
let lined = index + 1 < count;
let term = self.term(&item, false);
let value = self.value(&mut item, false);
root = root.child(
Column::new()
.align_items(Alignment::Stretch)
.part(&self.of(lined).part(DataListPart::Item))
.child(term)
.child(value),
);
}
if let Some(label) = &self.label {
root = root.describe("a11y_name", label.clone());
}
if let Some(id) = &self.id {
root = root.id(id.clone());
}
if let Some(key) = &self.key {
root = root.key(key.clone());
}
return root.into();
}
let mut grid = Grid::new()
.columns([GridTrack::Auto, GridTrack::Fr(1.0)])
.gap(0.0)
.align_items(Alignment::Stretch)
.part(&root_part);
for (index, mut item) in items.into_iter().enumerate() {
let lined = index + 1 < count;
grid = grid.child(self.term(&item, lined)).child(self.value(&mut item, lined));
}
if let Some(label) = &self.label {
grid = grid.describe("a11y_name", label.clone());
}
if let Some(id) = &self.id {
grid = grid.id(id.clone());
}
if let Some(key) = &self.key {
grid = grid.key(key.clone());
}
grid.into()
}
}
impl crate::ui::Styled for DataList {
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 DataList {
const NAME: &'static str = "data_list";
type Renderer = DataListRenderer;
fn parts() -> crate::ui::Declared {
DataListPart::declared()
}
fn states() -> crate::ui::Declared {
DataListState::declared()
}
fn recipe() -> Recipe {
use DataListPart::{Item, Root, Term, Value};
use DataListState::{Separator, Vertical};
use PaletteColor::{Foreground, MutedForeground};
let line = || Border::inside(BorderSize::Sm, PaletteColor::Border);
Recipe::new()
.base(Root, |s| s.foreground(Foreground))
.base(Term, |s| {
s.padding_y(10.0)
.padding_end(12.0)
.typography(TypographyStyle::Body)
.font_size(13.0)
.foreground(MutedForeground)
})
.base(Value, |s| {
s.padding_y(10.0)
.gap(8.0)
.typography(TypographyStyle::Body)
.font_size(13.0)
.foreground(Foreground)
})
.state(Separator, Term, |s| s.underline(line()))
.state(Separator, Value, |s| s.underline(line()))
.state(Vertical, Item, |s| s.padding_y(10.0).gap(2.0))
.state(Separator, Item, |s| s.underline(line()))
.state(Vertical, Term, |s| s.padding_y(0.0).padding_end(0.0).font_size(12.0))
.state(Vertical, Value, |s| s.padding_y(0.0))
}
}