use crate::widgets::ListTile;
use elvis_core::{
derive::Setter,
option_to_style,
style::Border,
value::{Color, FontFamily, FontStyle, TextAlign, Unit},
Attribute, Node, Style,
};
#[derive(Default, Setter)]
pub struct Text {
#[skip]
pub text: String,
pub bold: bool,
pub color: Option<Color>,
pub italic: bool,
pub size: Option<Unit>,
pub weight: Option<Unit>,
pub height: Option<Unit>,
pub stretch: Option<Unit>,
pub family: Option<FontFamily>,
pub align: Option<TextAlign>,
}
impl Text {
pub fn with(s: &str) -> Self {
Text::new().text(s)
}
pub fn text(mut self, s: &str) -> Self {
self.text = s.into();
self
}
}
impl Into<Node> for Text {
fn into(mut self) -> Node {
let mut child = Node::default();
child.attr.tag = "plain".into();
child.attr.text = self.text.to_string();
let mut styles: Vec<Style> = vec![];
if self.italic {
styles.push(Style::FontStyle(FontStyle::Normal));
}
if self.bold {
self.weight = Some(Unit::None(700.0));
}
option_to_style! {
styles, [
(Color, self.color),
(FontWeight, self.weight),
(FontSize, self.size),
(FontStretch, self.stretch),
(LineHeight, self.height),
(FontFamily, self.family),
(TextAlign, self.align),
],
}
let mut node = Node::default().children(vec![child]).style(styles);
node.attr.tag = "p".into();
node
}
}
#[derive(Default, Setter)]
pub struct TextField {
pub leading: Node,
pub trailing: Node,
pub text: Text,
}
impl TextField {
pub fn with(t: Text) -> Self {
TextField::new().text(t)
}
}
impl Into<Node> for TextField {
fn into(self) -> Node {
let mut style: Vec<Style> = Border::default().into();
style.append(&mut vec![
Style::Width(Unit::Percent(100.0)),
Style::OutlineWidth(Unit::None(0.0)),
]);
ListTile::new()
.leading(self.leading)
.text(
Into::<Node>::into(self.text)
.attr(Attribute::new().tag("input"))
.append_style(style),
)
.trailing(self.trailing)
.into()
}
}