1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use crate::TextStyle;
use elvis::{Image, Text, Tree};
use std::convert::Into;
use std::ops::{Deref, DerefMut};
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
#[derive(Clone, Debug, Default)]
pub struct Widget(Tree);
impl Widget {
pub fn new<W>(tree: W) -> Widget
where
W: Into<Tree>,
{
Widget(tree.into())
}
}
#[wasm_bindgen]
impl Widget {
#[wasm_bindgen(constructor)]
pub fn constructor() -> Widget {
Widget::default()
}
}
impl std::convert::Into<Tree> for Widget {
fn into(self) -> Tree {
self.0
}
}
deref!(Widget, Tree);
#[wasm_bindgen(js_name = "Text")]
pub fn text(text: Option<String>, style: Option<TextStyle>) -> Widget {
Widget::new(Text::new(
text.unwrap_or_default(),
style.unwrap_or_default().into(),
))
}
#[wasm_bindgen(js_name = "Image")]
pub fn img(src: Option<String>, child: Option<Widget>) -> Widget {
Widget::new(Image::new(
src.unwrap_or("".into()),
child.unwrap_or_default().into(),
))
}