use super::PropMap;
use crate::View;
use crate::derive_support::PlushieType;
use crate::types::*;
pub struct ButtonBuilder {
id: String,
props: PropMap,
}
pub fn button(id: &str, label: &str) -> ButtonBuilder {
let mut props = PropMap::new();
super::set_prop(&mut props, "label", label);
ButtonBuilder {
id: id.to_string(),
props,
}
}
impl ButtonBuilder {
pub fn style(mut self, s: impl Into<Style>) -> Self {
let s = s.into();
super::set_prop(&mut self.props, "style", super::style_to_value(&s));
self
}
pub fn disabled(mut self, v: bool) -> Self {
super::set_prop(&mut self.props, "disabled", v);
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 padding(mut self, p: impl Into<Padding>) -> Self {
super::set_prop(
&mut self.props,
"padding",
super::padding_to_value(p.into()),
);
self
}
pub fn clip(mut self, v: bool) -> Self {
super::set_prop(&mut self.props, "clip", v);
self
}
pub fn mnemonic(mut self, mnemonic: char) -> Self {
super::set_prop(&mut self.props, "mnemonic", mnemonic.to_string());
self
}
pub fn access_key(self, access_key: char) -> Self {
self.mnemonic(access_key)
}
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<ButtonBuilder> for View {
fn from(b: ButtonBuilder) -> Self {
super::view_leaf(b.id, "button", b.props)
}
}
pub struct PointerAreaBuilder {
id: String,
props: PropMap,
child: Option<View>,
}
pub fn pointer_area(id: &str) -> PointerAreaBuilder {
PointerAreaBuilder {
id: id.to_string(),
props: PropMap::new(),
child: None,
}
}
impl PointerAreaBuilder {
pub fn on_press(mut self, tag: &str) -> Self {
super::set_prop(&mut self.props, "on_press", tag);
self
}
pub fn on_release(mut self, tag: &str) -> Self {
super::set_prop(&mut self.props, "on_release", tag);
self
}
pub fn on_enter(mut self, v: bool) -> Self {
super::set_prop(&mut self.props, "on_enter", v);
self
}
pub fn on_exit(mut self, v: bool) -> Self {
super::set_prop(&mut self.props, "on_exit", v);
self
}
pub fn on_move(mut self, v: bool) -> Self {
super::set_prop(&mut self.props, "on_move", v);
self
}
pub fn on_scroll(mut self, v: bool) -> Self {
super::set_prop(&mut self.props, "on_scroll", v);
self
}
pub fn on_middle_press(mut self, v: bool) -> Self {
super::set_prop(&mut self.props, "on_middle_press", v);
self
}
pub fn on_right_press(mut self, v: bool) -> Self {
super::set_prop(&mut self.props, "on_right_press", v);
self
}
pub fn on_right_release(mut self, v: bool) -> Self {
super::set_prop(&mut self.props, "on_right_release", v);
self
}
pub fn on_middle_release(mut self, v: bool) -> Self {
super::set_prop(&mut self.props, "on_middle_release", v);
self
}
pub fn on_double_click(mut self, v: bool) -> Self {
super::set_prop(&mut self.props, "on_double_click", v);
self
}
pub fn cursor(mut self, cursor: CursorStyle) -> Self {
super::set_prop(&mut self.props, "cursor", cursor.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
}
pub fn child(mut self, child: impl Into<View>) -> Self {
self.child = Some(child.into());
self
}
}
impl From<PointerAreaBuilder> for View {
fn from(b: PointerAreaBuilder) -> Self {
let children = b.child.into_iter().collect();
super::view_node(b.id, "pointer_area", b.props, children)
}
}
pub struct SensorBuilder {
id: String,
props: PropMap,
child: Option<View>,
}
pub fn sensor(id: &str) -> SensorBuilder {
SensorBuilder {
id: id.to_string(),
props: PropMap::new(),
child: None,
}
}
impl SensorBuilder {
pub fn delay(mut self, ms: u32) -> Self {
super::set_prop(&mut self.props, "delay", ms);
self
}
pub fn anticipate(mut self, pixels: f32) -> Self {
super::set_prop(&mut self.props, "anticipate", pixels);
self
}
pub fn on_resize(mut self, tag: &str) -> Self {
super::set_prop(&mut self.props, "on_resize", tag);
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
}
pub fn child(mut self, child: impl Into<View>) -> Self {
self.child = Some(child.into());
self
}
}
impl From<SensorBuilder> for View {
fn from(b: SensorBuilder) -> Self {
let children = b.child.into_iter().collect();
super::view_node(b.id, "sensor", b.props, children)
}
}
pub struct TooltipBuilder {
id: String,
props: PropMap,
child: Option<View>,
}
pub fn tooltip(id: &str, tip: &str) -> TooltipBuilder {
let mut props = PropMap::new();
super::set_prop(&mut props, "tip", tip);
TooltipBuilder {
id: id.to_string(),
props,
child: None,
}
}
impl TooltipBuilder {
pub fn position(mut self, pos: Position) -> Self {
super::set_prop(&mut self.props, "position", pos.wire_encode());
self
}
pub fn gap(mut self, v: impl Into<Animatable<f32>>) -> Self {
super::set_prop(&mut self.props, "gap", v.into().wire_encode());
self
}
pub fn padding(mut self, v: impl Into<Animatable<f32>>) -> Self {
super::set_prop(&mut self.props, "padding", v.into().wire_encode());
self
}
pub fn snap_within_viewport(mut self, v: bool) -> Self {
super::set_prop(&mut self.props, "snap_within_viewport", v);
self
}
pub fn delay(mut self, ms: u32) -> Self {
super::set_prop(&mut self.props, "delay", ms);
self
}
pub fn style(mut self, s: impl Into<Style>) -> Self {
let s = s.into();
super::set_prop(&mut self.props, "style", super::style_to_value(&s));
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
}
pub fn child(mut self, child: impl Into<View>) -> Self {
self.child = Some(child.into());
self
}
}
impl From<TooltipBuilder> for View {
fn from(b: TooltipBuilder) -> Self {
let children = b.child.into_iter().collect();
super::view_node(b.id, "tooltip", b.props, children)
}
}
pub struct ThemerBuilder {
id: String,
props: PropMap,
child: Option<View>,
}
pub fn themer(id: &str) -> ThemerBuilder {
ThemerBuilder {
id: id.to_string(),
props: PropMap::new(),
child: None,
}
}
impl ThemerBuilder {
pub fn theme(mut self, theme: impl Into<Theme>) -> Self {
let theme: Theme = theme.into();
super::set_prop(&mut self.props, "theme", theme.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
}
pub fn child(mut self, child: impl Into<View>) -> Self {
self.child = Some(child.into());
self
}
}
impl From<ThemerBuilder> for View {
fn from(b: ThemerBuilder) -> Self {
let children = b.child.into_iter().collect();
super::view_node(b.id, "themer", b.props, children)
}
}
pub struct OverlayBuilder {
id: String,
props: PropMap,
children: Vec<View>,
}
pub fn overlay(id: &str) -> OverlayBuilder {
OverlayBuilder {
id: id.to_string(),
props: PropMap::new(),
children: vec![],
}
}
impl OverlayBuilder {
pub fn position(mut self, pos: Position) -> Self {
super::set_prop(&mut self.props, "position", pos.wire_encode());
self
}
pub fn align(mut self, a: Align) -> Self {
super::set_prop(&mut self.props, "align", super::cross_align_to_value(a));
self
}
pub fn flip(mut self, v: bool) -> Self {
super::set_prop(&mut self.props, "flip", v);
self
}
pub fn gap(mut self, v: impl Into<Animatable<f32>>) -> Self {
super::set_prop(&mut self.props, "gap", v.into().wire_encode());
self
}
pub fn offset_x(mut self, v: impl Into<Animatable<f32>>) -> Self {
super::set_prop(&mut self.props, "offset_x", v.into().wire_encode());
self
}
pub fn offset_y(mut self, v: impl Into<Animatable<f32>>) -> Self {
super::set_prop(&mut self.props, "offset_y", v.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 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
}
pub fn child(mut self, child: impl Into<View>) -> Self {
self.children.push(child.into());
self
}
pub fn children<I, V>(mut self, items: I) -> Self
where
I: IntoIterator<Item = V>,
V: Into<View>,
{
self.children.extend(items.into_iter().map(Into::into));
self
}
}
impl From<OverlayBuilder> for View {
fn from(b: OverlayBuilder) -> Self {
super::view_node(b.id, "overlay", b.props, b.children)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn button_mnemonic_builder_sets_wire_prop() {
let view: View = button("save", "Save").mnemonic('S').into();
assert_eq!(view.type_name(), "button");
assert_eq!(view.props().get_str("mnemonic"), Some("S"));
}
#[test]
fn button_access_key_builder_uses_mnemonic_wire_prop() {
let view: View = button("open", "Open").access_key('O').into();
assert_eq!(view.props().get_str("mnemonic"), Some("O"));
}
}