use std::sync::Arc;
use super::{
names,
recipe::{Case, Name, Recipe, ResolvedPart, ResolvedRecipe},
theme::{Themed, current_breakpoint, current_theme},
};
use crate::{
app::theme::{BorderSize, PaletteColor, Theme, TypographyStyle},
components::Column,
layout::{Alignment, text_style::FontStyle},
node::{Element, border::Border, dimension::Dimension},
ui::{Length, PartStyle},
};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum BlockquotePart {
Root,
Label,
Body,
}
names!(BlockquotePart {
Root => "root",
Label => "label",
Body => "body",
});
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum BlockquoteState {
Body,
}
names!(BlockquoteState {
Body => "body",
});
pub type BlockquoteRenderer = Arc<dyn Fn(BlockquoteView) -> Element + Send + Sync>;
pub struct Blockquote {
text: Arc<str>,
body: Vec<Element>,
label: Option<Arc<str>>,
states: Vec<Name>,
rules: Recipe,
id: Option<Arc<str>>,
key: Option<Arc<str>>,
}
impl Blockquote {
pub fn new(text: &str) -> Self {
Self {
text: Arc::from(text),
body: Vec::new(),
label: None,
states: Vec::new(),
rules: Recipe::new(),
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 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(BlockquoteState::Body, !self.body.is_empty());
for state in &self.states {
case = case.state(state.clone(), true);
}
let view = BlockquoteView {
text: self.text,
body: self.body,
label: self.label,
recipe: theme
.resolve::<Blockquote>(&case, Some(&self.rules))
.owned_by(self.id.as_ref()),
id: self.id,
key: self.key,
};
match theme.renderer::<Blockquote>() {
Some(renderer) => renderer(view),
None => view.render(),
}
}
}
impl From<Blockquote> for Element {
fn from(quote: Blockquote) -> Self {
quote.build(¤t_theme())
}
}
pub struct BlockquoteView {
pub text: Arc<str>,
pub body: Vec<Element>,
pub label: Option<Arc<str>>,
pub recipe: ResolvedRecipe,
id: Option<Arc<str>>,
key: Option<Arc<str>>,
}
impl BlockquoteView {
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)
.part(&self.part(BlockquotePart::Root))
.describe("a11y_role", "blockquote");
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 label(&self) -> Option<Element> {
if self.text.is_empty() {
return None;
}
let part = self.part(BlockquotePart::Label);
Some(part.text(&self.text).part(&part).width(Dimension::Pct(100.0)).into())
}
pub fn body(&mut self) -> Option<Element> {
if self.body.is_empty() {
return None;
}
let body = std::mem::take(&mut self.body);
Some(
Column::new()
.align_items(Alignment::Stretch)
.part(&self.part(BlockquotePart::Body))
.with_children(body)
.into(),
)
}
pub fn render(mut self) -> Element {
let root = self.root();
let label = self.label();
let body = self.body();
root.with_children(label.into_iter().chain(body)).into()
}
}
impl crate::ui::Styled for Blockquote {
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 Blockquote {
const NAME: &'static str = "blockquote";
type Renderer = BlockquoteRenderer;
fn parts() -> crate::ui::Declared {
BlockquotePart::declared()
}
fn states() -> crate::ui::Declared {
BlockquoteState::declared()
}
fn recipe() -> Recipe {
use BlockquotePart::{Body, Label, Root};
Recipe::new()
.base(Root, |s| {
s.width(Length::Fill)
.start_line(Border::inside(BorderSize::Md, PaletteColor::Border))
.padding_start(16.0)
.padding_y(4.0)
.gap(8.0)
.foreground(PaletteColor::MutedForeground)
})
.base(Label, |s| {
s.typography(TypographyStyle::Body)
.font_size(14.0)
.font_style(FontStyle::Italic)
.line_height(1.5)
.foreground(PaletteColor::MutedForeground)
})
.base(Body, |s| s.gap(8.0))
}
}