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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use super::control::*;
use super::attributes::*;
use super::super::json::*;

use serde_json::*;

impl ToJsonValue for ControlAttribute {
    fn to_json(&self) -> Value {
        use ControlAttribute::*;
        use Appearance::*;
        use State::*;

        match self {
            &BoundingBox(ref bounds)                => json!({ "BoundingBox": bounds }),
            &Text(ref property)                     => json!({ "Text": property }),
            &ZIndex(zindex)                         => json!({ "ZIndex": zindex }),
            &Padding((left, top), (right, bottom))  => json!({ "Padding": { "left": left, "top": top, "right": right, "bottom": bottom } }),
            &FontAttr(attr)                         => json!({ "Font": attr }),
            &StateAttr(Selected(ref property))      => json!({ "Selected": property }),
            &StateAttr(Badged(ref property))        => json!({ "Badged": property }),
            &StateAttr(Value(ref property))         => json!({ "Value": property }),
            &StateAttr(Range((ref min, ref max)))   => json!({ "Range": [min, max] }),
            &PopupAttr(ref popup)                   => json!({ "Popup": popup }),
            &ScrollAttr(ref scroll)                 => json!({ "Scroll": scroll }),
            &Id(ref id)                             => json!({ "Id": id }),
            &Controller(ref name)                   => json!({ "Controller": name }),
            &Action(ref trigger, ref action)        => json!({ "Action": (trigger, action) }),

            &SubComponents(ref components)          => {
                let json_components: Vec<_> = components.iter()
                    .map(|component| component.to_json())
                    .collect();

                json!({ "SubComponents": json_components })
            },

            &AppearanceAttr(Image(ref image_resource))  => {
                // For images, we only store the ID: callers need to look it up from the resource manager in the controller that made this control
                json!({ 
                    "Image": {
                        "id":   image_resource.id(),
                        "name": image_resource.name()
                    }
                })
            },

            &AppearanceAttr(Background(ref color))  => json!({ "Background": color.to_rgba_components() }),
            &AppearanceAttr(Foreground(ref color))  => json!({ "Foreground": color.to_rgba_components() }),

            &Canvas(ref canvas_resource)            => {
                json!({
                    "Image": {
                        "id":   canvas_resource.id(),
                        "name": canvas_resource.name()
                    }
                })
            }
        }
    }
}

impl ToJsonValue for Control {
    fn to_json(&self) -> Value {
        let attributes: Vec<_> = self.attributes()
            .map(|attribute| attribute.to_json())
            .collect();

        json!({
            "attributes":   attributes,
            "control_type": self.control_type()
        })
    }
}