use std::sync::Arc;
use super::{
icon::Icon,
names,
recipe::{Case, Name, Recipe, ResolvedPart, ResolvedRecipe},
theme::{Themed, current_breakpoint, current_theme},
};
use crate::{
app::theme::{BorderSize, PaletteColor, RadiusSize, Theme, TypographyStyle},
components::{Column, Row},
layout::{Alignment, layout_kind::Justify},
node::{Element, border::Border, color::Color, dimension::Dimension},
ui::{Length, PartStyle},
};
pub(crate) mod look {
pub const LINE: f32 = 17.0;
pub const ITEM_GAP: f32 = 6.0;
pub const BULLET: f32 = 5.0;
pub const BULLET_GAP: f32 = 10.0;
pub const NUMBER_WIDTH: f32 = 18.0;
pub const NUMBER_GAP: f32 = 8.0;
}
const ICON: f32 = 14.0;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum BulletedListPart {
Root,
Item,
Mark,
Bullet,
Label,
Body,
}
names!(BulletedListPart {
Root => "root",
Item => "item",
Mark => "mark",
Bullet => "bullet",
Label => "label",
Body => "body",
});
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum BulletedListState {
Numbered,
Icon,
Nested,
}
names!(BulletedListState {
Numbered => "numbered",
Icon => "icon",
Nested => "nested",
});
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub enum BulletedListMarker {
#[default]
Bullet,
Number,
Icon(Name),
None,
}
impl From<&str> for BulletedListMarker {
fn from(icon: &str) -> Self {
Self::Icon(Name::from(icon))
}
}
impl From<Name> for BulletedListMarker {
fn from(icon: Name) -> Self {
Self::Icon(icon)
}
}
pub type BulletedListRenderer = Arc<dyn Fn(BulletedListView) -> Element + Send + Sync>;
pub struct BulletedListItem {
text: Arc<str>,
body: Vec<Element>,
list: Option<BulletedList>,
icon: Option<Name>,
media: Option<Element>,
id: Option<Arc<str>>,
key: Option<Arc<str>>,
}
impl BulletedListItem {
pub fn new(text: &str) -> Self {
Self {
text: Arc::from(text),
body: Vec::new(),
list: None,
icon: None,
media: None,
id: None,
key: None,
}
}
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 list(mut self, list: BulletedList) -> Self {
self.list = Some(list);
self
}
pub fn icon(mut self, icon: impl Into<Name>) -> 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 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
}
}
impl From<&str> for BulletedListItem {
fn from(text: &str) -> Self {
Self::new(text)
}
}
impl From<String> for BulletedListItem {
fn from(text: String) -> Self {
Self::new(&text)
}
}
impl From<&String> for BulletedListItem {
fn from(text: &String) -> Self {
Self::new(text)
}
}
pub struct BulletedList {
items: Vec<BulletedListItem>,
marker: BulletedListMarker,
start: u64,
number_text: Arc<dyn Fn(u64) -> String + Send + Sync>,
depth: usize,
label: Option<Arc<str>>,
states: Vec<Name>,
rules: Recipe,
id: Option<Arc<str>>,
key: Option<Arc<str>>,
}
impl BulletedList {
pub fn new(items: impl IntoIterator<Item = impl Into<BulletedListItem>>) -> Self {
Self {
items: items.into_iter().map(Into::into).collect(),
marker: BulletedListMarker::Bullet,
start: 1,
number_text: Arc::new(|number| format!("{number}.")),
depth: 0,
label: None,
states: Vec::new(),
rules: Recipe::new(),
id: None,
key: None,
}
}
pub fn item(mut self, item: impl Into<BulletedListItem>) -> Self {
self.items.push(item.into());
self
}
pub fn items(mut self, items: impl IntoIterator<Item = impl Into<BulletedListItem>>) -> Self {
self.items.extend(items.into_iter().map(Into::into));
self
}
pub fn marker(mut self, marker: impl Into<BulletedListMarker>) -> Self {
self.marker = marker.into();
self
}
pub fn start(mut self, start: u64) -> Self {
self.start = start;
self
}
pub fn number_text(mut self, text: impl Fn(u64) -> String + Send + Sync + 'static) -> Self {
self.number_text = Arc::new(text);
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 mut case = Case::new()
.breakpoint(current_breakpoint())
.state(BulletedListState::Numbered, self.marker == BulletedListMarker::Number)
.state(
BulletedListState::Icon,
self.marker != BulletedListMarker::Number
&& (matches!(self.marker, BulletedListMarker::Icon(_))
|| self.items.iter().any(|item| item.icon.is_some() || item.media.is_some())),
)
.state(BulletedListState::Nested, self.depth > 0);
for state in &self.states {
case = case.state(state.clone(), true);
}
let view = BulletedListView {
items: self.items,
marker: self.marker,
start: self.start,
number_text: self.number_text,
depth: self.depth,
label: self.label,
recipe: theme
.resolve::<BulletedList>(&case, Some(&self.rules))
.owned_by(self.id.as_ref()),
rules: self.rules,
id: self.id,
key: self.key,
};
match theme.renderer::<BulletedList>() {
Some(renderer) => renderer(view),
None => view.render(),
}
}
}
impl From<BulletedList> for Element {
fn from(list: BulletedList) -> Self {
list.build(¤t_theme())
}
}
pub struct BulletedListView {
pub items: Vec<BulletedListItem>,
pub marker: BulletedListMarker,
pub start: u64,
number_text: Arc<dyn Fn(u64) -> String + Send + Sync>,
pub depth: usize,
pub label: Option<Arc<str>>,
pub recipe: ResolvedRecipe,
rules: Recipe,
id: Option<Arc<str>>,
key: Option<Arc<str>>,
}
impl BulletedListView {
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::Stretch)
.overflow_visible()
.part(&self.part(BulletedListPart::Root))
.describe("a11y_role", "list");
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());
}
root
}
pub fn line_height(&self) -> f32 {
let style = self.part(BulletedListPart::Label).rest().text.resolve(self.theme());
style.font_size * style.line_height
}
pub fn number(&self, index: usize) -> String {
(self.number_text)(self.start + index as u64)
}
pub fn mark(&self, index: usize, icon: Option<Name>, media: Option<Element>) -> Option<Element> {
self.mark_in(&self.recipe, index, icon, media)
}
fn mark_in(
&self,
recipe: &ResolvedRecipe,
index: usize,
icon: Option<Name>,
media: Option<Element>,
) -> Option<Element> {
let part = recipe.part(BulletedListPart::Mark);
let held = Row::new()
.height(self.line_height())
.align_items(Alignment::Center)
.justify(Justify::Center)
.flex_shrink(0.0)
.part(&part);
if let Some(media) = media {
let least = part.rest().width.and_then(|width| width.resolve(self.theme()));
let held = held.width(Dimension::Auto).overflow_visible();
return Some(
match least {
Some(least) => held.min_width(least),
None => held,
}
.child(media)
.into(),
);
}
let icon = icon.or(match &self.marker {
BulletedListMarker::Icon(icon) => Some(*icon),
_ => None,
});
if let Some(icon) = icon {
let side = part.rest().width.unwrap_or(Length::Px(ICON));
return Some(held.child(Icon::new(icon).size(side).build(self.theme())).into());
}
match self.marker {
BulletedListMarker::Number => Some(
held
.justify(Justify::End)
.child(part.text(&self.number(index)).nowrap())
.into(),
),
BulletedListMarker::None => None,
BulletedListMarker::Bullet | BulletedListMarker::Icon(_) => Some(
held
.child(Row::new().part(&recipe.part(BulletedListPart::Bullet)))
.into(),
),
}
}
pub fn label(&self, text: &str) -> Option<Element> {
Self::label_in(&self.recipe, text)
}
fn label_in(recipe: &ResolvedRecipe, text: &str) -> Option<Element> {
if text.is_empty() {
return None;
}
let part = recipe.part(BulletedListPart::Label);
Some(part.text(text).part(&part).width(Dimension::Pct(100.0)).into())
}
pub fn item(&self, index: usize, item: BulletedListItem) -> Element {
let recipe = self.recipe.item(item.id.as_ref(), BulletedListPart::Item);
let mark = self.mark_in(&recipe, index, item.icon, item.media);
let mut body = Column::new()
.flex(1.0)
.min_width(0.0)
.align_items(Alignment::Stretch)
.overflow_visible()
.part(&recipe.part(BulletedListPart::Body));
if let Some(label) = Self::label_in(&recipe, &item.text) {
body = body.child(label);
}
body = body.with_children(item.body);
if let Some(mut list) = item.list {
list.depth = self.depth + 1;
list.rules = self
.rules
.clone()
.extend(&recipe.lend(<BulletedList as Themed>::NAME))
.extend(&list.rules);
body = body.child(list.build(self.theme()));
}
let mut row = Row::new()
.align_items(Alignment::Start)
.overflow_visible()
.part(&recipe.part(BulletedListPart::Item))
.describe("a11y_role", "listitem");
if let Some(id) = item.id {
row = row.id(id);
}
if let Some(key) = item.key {
row = row.key(key);
}
row.with_children(mark).child(body).into()
}
pub fn render(mut self) -> Element {
let items = std::mem::take(&mut self.items);
let rows: Vec<Element> = items
.into_iter()
.enumerate()
.map(|(index, item)| self.item(index, item))
.collect();
self.root().with_children(rows).into()
}
}
impl crate::ui::Styled for BulletedList {
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 BulletedList {
const NAME: &'static str = "bulleted_list";
type Renderer = BulletedListRenderer;
fn parts() -> crate::ui::Declared {
BulletedListPart::declared()
}
fn states() -> crate::ui::Declared {
BulletedListState::declared()
}
fn recipe() -> Recipe {
use BulletedListPart::{Body, Bullet, Item, Label, Mark, Root};
use BulletedListState::{Icon, Nested, Numbered};
use PaletteColor::{Foreground, MutedForeground};
Recipe::new()
.base(Root, |s| {
s.width(Length::Fill).gap(look::ITEM_GAP).foreground(Foreground)
})
.base(Item, |s| s.gap(look::BULLET_GAP))
.base(Mark, |s| {
s.width(look::BULLET)
.typography(TypographyStyle::Body)
.line_height(look::LINE / 14.0)
.foreground(MutedForeground)
})
.base(Bullet, |s| {
s.square(look::BULLET)
.radius(RadiusSize::Full)
.background(MutedForeground)
})
.base(Label, |s| {
s.typography(TypographyStyle::Body)
.line_height(look::LINE / 14.0)
.foreground(Foreground)
})
.base(Body, |s| s.gap(look::ITEM_GAP))
.state(Numbered, Item, |s| s.gap(look::NUMBER_GAP))
.state(Numbered, Mark, |s| s.width(look::NUMBER_WIDTH))
.state(Icon, Mark, |s| s.width(ICON))
.state(Nested, Bullet, |s| {
s.background(Color::new(0, 0, 0, 0))
.border(Border::inside(BorderSize::Sm, MutedForeground))
})
}
}