use super::{PropMap, PropValue};
use crate::View;
use crate::derive_support::PlushieType;
use crate::types::*;
pub struct TextBuilder {
id: String,
props: PropMap,
}
#[track_caller]
pub fn text(content: &str) -> TextBuilder {
let mut props = PropMap::new();
super::set_prop(&mut props, "content", content);
TextBuilder {
id: super::auto_id("text"),
props,
}
}
impl TextBuilder {
pub fn id(mut self, id: &str) -> Self {
self.id = id.to_string();
self
}
pub fn size(mut self, s: impl Into<Animatable<f32>>) -> Self {
super::set_prop(&mut self.props, "size", s.into().wire_encode());
self
}
pub fn color(mut self, c: impl Into<Animatable<Color>>) -> Self {
super::set_prop(&mut self.props, "color", c.into().wire_encode());
self
}
pub fn font(mut self, f: Font) -> Self {
super::set_prop(&mut self.props, "font", f.wire_encode());
self
}
pub fn width(mut self, w: impl Into<Length>) -> Self {
super::set_prop(&mut self.props, "width", super::length_to_value(w.into()));
self
}
pub fn height(mut self, h: impl Into<Length>) -> Self {
super::set_prop(&mut self.props, "height", super::length_to_value(h.into()));
self
}
pub fn align_x(mut self, a: impl Into<TextAlignment>) -> Self {
super::set_prop(&mut self.props, "align_x", a.into().wire_encode());
self
}
pub fn text_direction(mut self, d: TextDirection) -> Self {
super::set_prop(&mut self.props, "text_direction", d.wire_encode());
self
}
pub fn align_y(mut self, a: Align) -> Self {
super::set_prop(&mut self.props, "align_y", super::valign_to_value(a));
self
}
pub fn wrapping(mut self, w: Wrapping) -> Self {
super::set_prop(&mut self.props, "wrapping", w.wire_encode());
self
}
pub fn shaping(mut self, s: Shaping) -> Self {
super::set_prop(&mut self.props, "shaping", s.wire_encode());
self
}
pub fn line_height(mut self, lh: impl Into<Animatable<LineHeight>>) -> Self {
super::set_prop(&mut self.props, "line_height", lh.into().wire_encode());
self
}
pub fn ellipsis(mut self, e: Ellipsis) -> Self {
super::set_prop(&mut self.props, "ellipsis", e.wire_encode());
self
}
pub fn style(mut self, s: impl Into<Style>) -> Self {
super::set_prop(&mut self.props, "style", super::style_to_value(&s.into()));
self
}
pub fn event_rate(mut self, rate: u32) -> Self {
super::set_prop(&mut self.props, "event_rate", rate);
self
}
pub fn a11y(mut self, a11y: &A11y) -> Self {
super::set_prop(&mut self.props, "a11y", a11y.wire_encode());
self
}
}
impl From<TextBuilder> for View {
fn from(b: TextBuilder) -> View {
super::view_leaf(b.id, "text", b.props)
}
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct SpanHighlight {
pub background: Option<Color>,
pub border: Option<Border>,
}
impl SpanHighlight {
pub fn new() -> Self {
Self::default()
}
pub fn background(mut self, color: impl Into<Color>) -> Self {
self.background = Some(color.into());
self
}
pub fn border(mut self, border: Border) -> Self {
self.border = Some(border);
self
}
fn wire_encode(&self) -> PropValue {
let mut map = PropMap::new();
if let Some(ref bg) = self.background {
map.insert("background", bg.wire_encode());
}
if let Some(ref b) = self.border {
map.insert("border", b.wire_encode());
}
PropValue::Object(map)
}
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Span {
pub text: String,
pub size: Option<f32>,
pub font: Option<Font>,
pub color: Option<Color>,
pub line_height: Option<LineHeight>,
pub link: Option<String>,
pub underline: Option<bool>,
pub strikethrough: Option<bool>,
pub padding: Option<Padding>,
pub highlight: Option<SpanHighlight>,
}
impl Span {
pub fn new(text: impl Into<String>) -> Self {
Self {
text: text.into(),
..Self::default()
}
}
pub fn size(mut self, size: f32) -> Self {
self.size = Some(size);
self
}
pub fn font(mut self, font: Font) -> Self {
self.font = Some(font);
self
}
pub fn color(mut self, color: impl Into<Color>) -> Self {
self.color = Some(color.into());
self
}
pub fn line_height(mut self, line_height: impl Into<LineHeight>) -> Self {
self.line_height = Some(line_height.into());
self
}
pub fn link(mut self, url: impl Into<String>) -> Self {
self.link = Some(url.into());
self
}
pub fn underline(mut self, on: bool) -> Self {
self.underline = Some(on);
self
}
pub fn strikethrough(mut self, on: bool) -> Self {
self.strikethrough = Some(on);
self
}
pub fn padding(mut self, padding: impl Into<Padding>) -> Self {
self.padding = Some(padding.into());
self
}
pub fn highlight(mut self, highlight: SpanHighlight) -> Self {
self.highlight = Some(highlight);
self
}
pub fn wire_encode(&self) -> PropValue {
let mut map = PropMap::new();
map.insert("text", PropValue::Str(self.text.clone()));
if let Some(size) = self.size {
map.insert("size", PropValue::F64(size as f64));
}
if let Some(ref font) = self.font {
map.insert("font", font.wire_encode());
}
if let Some(ref color) = self.color {
map.insert("color", color.wire_encode());
}
if let Some(lh) = self.line_height {
map.insert("line_height", lh.wire_encode());
}
if let Some(ref link) = self.link {
map.insert("link", PropValue::Str(link.clone()));
}
if let Some(u) = self.underline {
map.insert("underline", PropValue::Bool(u));
}
if let Some(s) = self.strikethrough {
map.insert("strikethrough", PropValue::Bool(s));
}
if let Some(ref p) = self.padding {
map.insert("padding", p.wire_encode());
}
if let Some(ref h) = self.highlight {
map.insert("highlight", h.wire_encode());
}
PropValue::Object(map)
}
}
pub struct RichTextBuilder {
id: String,
props: PropMap,
}
#[track_caller]
pub fn rich_text() -> RichTextBuilder {
RichTextBuilder {
id: super::auto_id("rich_text"),
props: PropMap::new(),
}
}
pub fn rich_text_id(id: &str) -> RichTextBuilder {
RichTextBuilder {
id: id.to_string(),
props: PropMap::new(),
}
}
impl RichTextBuilder {
pub fn id(mut self, id: &str) -> Self {
self.id = id.to_string();
self
}
pub fn spans(mut self, spans: Vec<Span>) -> Self {
let encoded: Vec<PropValue> = spans.iter().map(Span::wire_encode).collect();
super::set_prop(&mut self.props, "spans", PropValue::Array(encoded));
self
}
pub fn size(mut self, s: impl Into<Animatable<f32>>) -> Self {
super::set_prop(&mut self.props, "size", s.into().wire_encode());
self
}
pub fn font(mut self, f: Font) -> Self {
super::set_prop(&mut self.props, "font", f.wire_encode());
self
}
pub fn color(mut self, c: impl Into<Animatable<Color>>) -> Self {
super::set_prop(&mut self.props, "color", c.into().wire_encode());
self
}
pub fn width(mut self, w: impl Into<Length>) -> Self {
super::set_prop(&mut self.props, "width", super::length_to_value(w.into()));
self
}
pub fn height(mut self, h: impl Into<Length>) -> Self {
super::set_prop(&mut self.props, "height", super::length_to_value(h.into()));
self
}
pub fn line_height(mut self, lh: impl Into<Animatable<LineHeight>>) -> Self {
super::set_prop(&mut self.props, "line_height", lh.into().wire_encode());
self
}
pub fn wrapping(mut self, w: Wrapping) -> Self {
super::set_prop(&mut self.props, "wrapping", w.wire_encode());
self
}
pub fn ellipsis(mut self, e: Ellipsis) -> Self {
super::set_prop(&mut self.props, "ellipsis", e.wire_encode());
self
}
pub fn event_rate(mut self, rate: u32) -> Self {
super::set_prop(&mut self.props, "event_rate", rate);
self
}
pub fn a11y(mut self, a11y: &A11y) -> Self {
super::set_prop(&mut self.props, "a11y", a11y.wire_encode());
self
}
}
impl From<RichTextBuilder> for View {
fn from(b: RichTextBuilder) -> View {
super::view_leaf(b.id, "rich_text", b.props)
}
}
pub struct SpaceBuilder {
id: String,
props: PropMap,
}
#[track_caller]
pub fn space() -> SpaceBuilder {
SpaceBuilder {
id: super::auto_id("space"),
props: PropMap::new(),
}
}
impl SpaceBuilder {
pub fn id(mut self, id: &str) -> Self {
self.id = id.to_string();
self
}
pub fn width(mut self, w: impl Into<Length>) -> Self {
super::set_prop(&mut self.props, "width", super::length_to_value(w.into()));
self
}
pub fn height(mut self, h: impl Into<Length>) -> Self {
super::set_prop(&mut self.props, "height", super::length_to_value(h.into()));
self
}
pub fn event_rate(mut self, rate: u32) -> Self {
super::set_prop(&mut self.props, "event_rate", rate);
self
}
pub fn a11y(mut self, a11y: &A11y) -> Self {
super::set_prop(&mut self.props, "a11y", a11y.wire_encode());
self
}
}
impl From<SpaceBuilder> for View {
fn from(b: SpaceBuilder) -> View {
super::view_leaf(b.id, "space", b.props)
}
}
pub struct RuleBuilder {
id: String,
props: PropMap,
}
#[track_caller]
pub fn rule() -> RuleBuilder {
RuleBuilder {
id: super::auto_id("rule"),
props: PropMap::new(),
}
}
impl RuleBuilder {
pub fn id(mut self, id: &str) -> Self {
self.id = id.to_string();
self
}
pub fn width(mut self, w: impl Into<Animatable<f32>>) -> Self {
super::set_prop(&mut self.props, "width", w.into().wire_encode());
self
}
pub fn height(mut self, h: impl Into<Animatable<f32>>) -> Self {
super::set_prop(&mut self.props, "height", h.into().wire_encode());
self
}
pub fn direction(mut self, d: Direction) -> Self {
super::set_prop(&mut self.props, "direction", d.wire_encode());
self
}
pub fn thickness(mut self, t: f32) -> Self {
super::set_prop(&mut self.props, "thickness", t);
self
}
pub fn style(mut self, s: impl Into<Style>) -> Self {
super::set_prop(&mut self.props, "style", super::style_to_value(&s.into()));
self
}
pub fn event_rate(mut self, rate: u32) -> Self {
super::set_prop(&mut self.props, "event_rate", rate);
self
}
pub fn a11y(mut self, a11y: &A11y) -> Self {
super::set_prop(&mut self.props, "a11y", a11y.wire_encode());
self
}
}
impl From<RuleBuilder> for View {
fn from(b: RuleBuilder) -> View {
super::view_leaf(b.id, "rule", b.props)
}
}
pub struct ProgressBarBuilder {
id: String,
props: PropMap,
}
#[track_caller]
pub fn progress_bar(range: (f32, f32), value: f32) -> ProgressBarBuilder {
let mut props = PropMap::new();
super::set_prop(
&mut props,
"range",
PropValue::Array(vec![
PropValue::F64(range.0 as f64),
PropValue::F64(range.1 as f64),
]),
);
super::set_prop(&mut props, "value", value);
ProgressBarBuilder {
id: super::auto_id("progress_bar"),
props,
}
}
impl ProgressBarBuilder {
pub fn id(mut self, id: &str) -> Self {
self.id = id.to_string();
self
}
pub fn width(mut self, w: impl Into<Length>) -> Self {
super::set_prop(&mut self.props, "width", super::length_to_value(w.into()));
self
}
pub fn height(mut self, h: impl Into<Length>) -> Self {
super::set_prop(&mut self.props, "height", super::length_to_value(h.into()));
self
}
pub fn vertical(mut self, v: bool) -> Self {
super::set_prop(&mut self.props, "vertical", v);
self
}
pub fn label(mut self, l: &str) -> Self {
super::set_prop(&mut self.props, "label", l);
self
}
pub fn style(mut self, s: impl Into<Style>) -> Self {
super::set_prop(&mut self.props, "style", super::style_to_value(&s.into()));
self
}
pub fn event_rate(mut self, rate: u32) -> Self {
super::set_prop(&mut self.props, "event_rate", rate);
self
}
pub fn a11y(mut self, a11y: &A11y) -> Self {
super::set_prop(&mut self.props, "a11y", a11y.wire_encode());
self
}
}
impl From<ProgressBarBuilder> for View {
fn from(b: ProgressBarBuilder) -> View {
super::view_leaf(b.id, "progress_bar", b.props)
}
}
pub struct ImageBuilder {
id: String,
props: PropMap,
}
#[track_caller]
pub fn image(source: &str) -> ImageBuilder {
let mut props = PropMap::new();
super::set_prop(&mut props, "source", source);
ImageBuilder {
id: super::auto_id("image"),
props,
}
}
impl ImageBuilder {
pub fn id(mut self, id: &str) -> Self {
self.id = id.to_string();
self
}
pub fn width(mut self, w: impl Into<Length>) -> Self {
super::set_prop(&mut self.props, "width", super::length_to_value(w.into()));
self
}
pub fn height(mut self, h: impl Into<Length>) -> Self {
super::set_prop(&mut self.props, "height", super::length_to_value(h.into()));
self
}
pub fn content_fit(mut self, fit: ContentFit) -> Self {
super::set_prop(&mut self.props, "content_fit", fit.wire_encode());
self
}
pub fn filter_method(mut self, method: FilterMethod) -> Self {
super::set_prop(&mut self.props, "filter_method", method.wire_encode());
self
}
pub fn rotation(mut self, angle: impl Into<Animatable<Angle>>) -> Self {
super::set_prop(&mut self.props, "rotation", angle.into().wire_encode());
self
}
pub fn opacity(mut self, o: impl Into<Animatable<f32>>) -> Self {
super::set_prop(&mut self.props, "opacity", o.into().wire_encode());
self
}
pub fn border_radius(mut self, r: impl Into<Animatable<f32>>) -> Self {
super::set_prop(&mut self.props, "border_radius", r.into().wire_encode());
self
}
pub fn expand(mut self, v: bool) -> Self {
super::set_prop(&mut self.props, "expand", v);
self
}
pub fn scale(mut self, s: impl Into<Animatable<f32>>) -> Self {
super::set_prop(&mut self.props, "scale", s.into().wire_encode());
self
}
pub fn crop(mut self, x: f32, y: f32, width: f32, height: f32) -> Self {
let mut crop = PropMap::new();
crop.insert("x", PropValue::F64(x as f64));
crop.insert("y", PropValue::F64(y as f64));
crop.insert("width", PropValue::F64(width as f64));
crop.insert("height", PropValue::F64(height as f64));
super::set_prop(&mut self.props, "crop", PropValue::Object(crop));
self
}
pub fn alt(mut self, alt: &str) -> Self {
super::set_prop(&mut self.props, "alt", alt);
self
}
pub fn description(mut self, desc: &str) -> Self {
super::set_prop(&mut self.props, "description", desc);
self
}
pub fn decorative(mut self, v: bool) -> Self {
super::set_prop(&mut self.props, "decorative", v);
self
}
pub fn event_rate(mut self, rate: u32) -> Self {
super::set_prop(&mut self.props, "event_rate", rate);
self
}
pub fn a11y(mut self, a11y: &A11y) -> Self {
super::set_prop(&mut self.props, "a11y", a11y.wire_encode());
self
}
}
impl From<ImageBuilder> for View {
fn from(b: ImageBuilder) -> View {
super::view_leaf(b.id, "image", b.props)
}
}
pub struct SvgBuilder {
id: String,
props: PropMap,
}
#[track_caller]
pub fn svg(source: &str) -> SvgBuilder {
let mut props = PropMap::new();
super::set_prop(&mut props, "source", source);
SvgBuilder {
id: super::auto_id("svg"),
props,
}
}
impl SvgBuilder {
pub fn id(mut self, id: &str) -> Self {
self.id = id.to_string();
self
}
pub fn width(mut self, w: impl Into<Length>) -> Self {
super::set_prop(&mut self.props, "width", super::length_to_value(w.into()));
self
}
pub fn height(mut self, h: impl Into<Length>) -> Self {
super::set_prop(&mut self.props, "height", super::length_to_value(h.into()));
self
}
pub fn color(mut self, c: impl Into<Animatable<Color>>) -> Self {
super::set_prop(&mut self.props, "color", c.into().wire_encode());
self
}
pub fn content_fit(mut self, fit: ContentFit) -> Self {
super::set_prop(&mut self.props, "content_fit", fit.wire_encode());
self
}
pub fn rotation(mut self, angle: impl Into<Animatable<Angle>>) -> Self {
super::set_prop(&mut self.props, "rotation", angle.into().wire_encode());
self
}
pub fn opacity(mut self, o: impl Into<Animatable<f32>>) -> Self {
super::set_prop(&mut self.props, "opacity", o.into().wire_encode());
self
}
pub fn alt(mut self, alt: &str) -> Self {
super::set_prop(&mut self.props, "alt", alt);
self
}
pub fn description(mut self, desc: &str) -> Self {
super::set_prop(&mut self.props, "description", desc);
self
}
pub fn decorative(mut self, v: bool) -> Self {
super::set_prop(&mut self.props, "decorative", v);
self
}
pub fn event_rate(mut self, rate: u32) -> Self {
super::set_prop(&mut self.props, "event_rate", rate);
self
}
pub fn a11y(mut self, a11y: &A11y) -> Self {
super::set_prop(&mut self.props, "a11y", a11y.wire_encode());
self
}
}
impl From<SvgBuilder> for View {
fn from(b: SvgBuilder) -> View {
super::view_leaf(b.id, "svg", b.props)
}
}
pub struct MarkdownBuilder {
id: String,
props: PropMap,
}
#[track_caller]
pub fn markdown(content: &str) -> MarkdownBuilder {
let mut props = PropMap::new();
super::set_prop(&mut props, "content", content);
MarkdownBuilder {
id: super::auto_id("markdown"),
props,
}
}
impl MarkdownBuilder {
pub fn id(mut self, id: &str) -> Self {
self.id = id.to_string();
self
}
pub fn width(mut self, w: impl Into<Length>) -> Self {
super::set_prop(&mut self.props, "width", super::length_to_value(w.into()));
self
}
pub fn text_size(mut self, s: impl Into<Animatable<f32>>) -> Self {
super::set_prop(&mut self.props, "text_size", s.into().wire_encode());
self
}
pub fn h1_size(mut self, s: impl Into<Animatable<f32>>) -> Self {
super::set_prop(&mut self.props, "h1_size", s.into().wire_encode());
self
}
pub fn h2_size(mut self, s: impl Into<Animatable<f32>>) -> Self {
super::set_prop(&mut self.props, "h2_size", s.into().wire_encode());
self
}
pub fn h3_size(mut self, s: impl Into<Animatable<f32>>) -> Self {
super::set_prop(&mut self.props, "h3_size", s.into().wire_encode());
self
}
pub fn code_size(mut self, s: impl Into<Animatable<f32>>) -> Self {
super::set_prop(&mut self.props, "code_size", s.into().wire_encode());
self
}
pub fn spacing(mut self, s: impl Into<Animatable<f32>>) -> Self {
super::set_prop(&mut self.props, "spacing", s.into().wire_encode());
self
}
pub fn link_color(mut self, c: impl Into<Animatable<Color>>) -> Self {
super::set_prop(&mut self.props, "link_color", c.into().wire_encode());
self
}
pub fn code_theme(mut self, theme: &str) -> Self {
super::set_prop(&mut self.props, "code_theme", theme);
self
}
pub fn event_rate(mut self, rate: u32) -> Self {
super::set_prop(&mut self.props, "event_rate", rate);
self
}
pub fn a11y(mut self, a11y: &A11y) -> Self {
super::set_prop(&mut self.props, "a11y", a11y.wire_encode());
self
}
}
impl From<MarkdownBuilder> for View {
fn from(b: MarkdownBuilder) -> View {
super::view_leaf(b.id, "markdown", b.props)
}
}
pub struct QrCodeBuilder {
id: String,
props: PropMap,
}
#[track_caller]
pub fn qr_code(data: &str) -> QrCodeBuilder {
let mut props = PropMap::new();
super::set_prop(&mut props, "data", data);
QrCodeBuilder {
id: super::auto_id("qr_code"),
props,
}
}
impl QrCodeBuilder {
pub fn id(mut self, id: &str) -> Self {
self.id = id.to_string();
self
}
pub fn cell_size(mut self, s: impl Into<Animatable<f32>>) -> Self {
super::set_prop(&mut self.props, "cell_size", s.into().wire_encode());
self
}
pub fn total_size(mut self, s: f32) -> Self {
super::set_prop(&mut self.props, "total_size", s);
self
}
pub fn error_correction(mut self, level: ErrorCorrection) -> Self {
super::set_prop(&mut self.props, "error_correction", level.wire_encode());
self
}
pub fn cell_color(mut self, c: impl Into<Animatable<Color>>) -> Self {
super::set_prop(&mut self.props, "cell_color", c.into().wire_encode());
self
}
pub fn background(mut self, c: impl Into<Animatable<Color>>) -> Self {
super::set_prop(&mut self.props, "background", c.into().wire_encode());
self
}
pub fn alt(mut self, alt: &str) -> Self {
super::set_prop(&mut self.props, "alt", alt);
self
}
pub fn description(mut self, desc: &str) -> Self {
super::set_prop(&mut self.props, "description", desc);
self
}
pub fn event_rate(mut self, rate: u32) -> Self {
super::set_prop(&mut self.props, "event_rate", rate);
self
}
pub fn a11y(mut self, a11y: &A11y) -> Self {
super::set_prop(&mut self.props, "a11y", a11y.wire_encode());
self
}
}
impl From<QrCodeBuilder> for View {
fn from(b: QrCodeBuilder) -> View {
super::view_leaf(b.id, "qr_code", b.props)
}
}