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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
use crate::{
    makepad_draw::*,
    file_tree::*,
    view::View,
    widget::*,
};

live_design!{
    import makepad_widgets::base::*
    import makepad_widgets::theme_desktop_dark::*
    import makepad_draw::shader::std::*
    
    Designer = {{Designer}} {
        has_view: true,
        flow: Right
        container: <RoundedView> {
            draw_bg: {color: #3}
            width: Fill, height: 400
            flow: Down, spacing: 10, padding:10
            <RoundedView>{
                width: Fill, height: Fit
                padding:5
                draw_bg:{color:#5}
                label = <Label> {text: "HI", draw_text:{color:#f}}
            }
            inner = <HookWidget> {}
        }
        <Splitter> {
            align: FromStart(300),
            a: <View> {
                outline = <FileTree> {
                }
            },
            b: <CachedScrollXY> {
                dpi_factor: 1.5
                draw_bg: {color: #4}
                width: Fill, height: Fill
                flow: Down
                design = <HookWidget> {}
            },
        }
    }
}

#[allow(dead_code)]
enum OutlineNode {
    Global {
        uid: FileNodeId,
        name: LiveId,
        ptr: LivePtr
    },
    Component {
        uid: FileNodeId,
        name: LiveId,
        class: LiveId,
        prop_type: LivePropType,
        ptr: LivePtr,
        children: Vec<OutlineNode>
    }
}

#[derive(Live)]
pub struct Designer {
    #[live] container: Option<LivePtr>,
    #[rust] outline_nodes: Vec<OutlineNode>,
    #[rust] components: ComponentMap<LivePtr, (WidgetRef, WidgetRef)>,
    #[deref] ui: View,
}

impl LiveHook for Designer {
    fn before_live_design(cx: &mut Cx) {
        register_widget!(cx, Designer)
    }
    
    fn after_new_from_doc(&mut self, cx: &mut Cx) {
        // lets take the doc we need (app_mobile for instance)
        let live_registry_rc = cx.live_registry.clone();
        let live_registry = &*live_registry_rc.borrow();
        let file_id = live_registry.file_name_to_file_id("examples/ironfish/src/app_desktop.rs").unwrap();
        // now we fetch the unexpanded nodes
        // and build a list
        let file = live_registry.file_id_to_file(file_id);
        let nodes = &file.expanded.nodes;
        // lets run over the file
        fn recur_walk(live_registry: &LiveRegistry, base_ptr: LivePtr, mut index: usize, nodes: &[LiveNode], out: &mut Vec<OutlineNode>) -> usize {
            while index < nodes.len() - 1 {
                if let LiveValue::Class {class_parent, ..} = &nodes[index].value {
                    // lets emit a class at our level
                    let mut children = Vec::new();
                    let name = nodes[index].id;
                    let class = live_registry.ptr_to_node(class_parent.unwrap()).id;
                    let ptr = base_ptr.with_index(index);
                    index = recur_walk(live_registry, base_ptr, index + 1, nodes, &mut children);
                    out.insert(0, OutlineNode::Component {
                        uid: LiveId::unique().into(),
                        name,
                        prop_type: nodes[index].origin.prop_type(),
                        class,
                        ptr,
                        children
                    });
                }
                else if nodes[index].value.is_close() {
                    return index + 1;
                }
                else {
                    index = nodes.skip_node(index);
                }
            }
            index
        }
        let base_ptr = live_registry.file_id_index_to_live_ptr(file_id, 0);
        recur_walk(live_registry, base_ptr, 1, nodes, &mut self.outline_nodes);
    }
    // ok now we can iterate our top level components
    // and instance them
}

impl Designer {
    
    fn draw_design(&mut self, cx: &mut Cx2d) {
        // alrigh so. lets draw the designs
        let mut count = 0;
        for node in &self.outline_nodes {
            if let OutlineNode::Component {ptr, name, class, ..} = node {
                count += 1;
                if count > 5{
                    break;
                }
                let container_ptr = self.container;
                let (widget, container) = self.components.get_or_insert(cx, *ptr, | cx | {
                    (
                        WidgetRef::new_from_ptr(cx, Some(*ptr)),
                        WidgetRef::new_from_ptr(cx, container_ptr),
                    )
                });
                container.widget(id!(label)).set_text(&format!("{}=<{}>", name, class));
                // lets draw this thing in a neat little container box with a title bar
                while let Some(_) = container.draw_widget(cx).hook_widget() {
                    widget.draw_widget_all(cx);
                }
            }
        }
        
    }
    
    fn draw_outline(&mut self, cx: &mut Cx2d, outline: &mut FileTree) {
        fn recur_walk(cx: &mut Cx2d, outline: &mut FileTree, children: &[OutlineNode]) {
            for child in children {
                match child {
                    OutlineNode::Global {..} => {}
                    OutlineNode::Component {name, children, uid, class, prop_type, ..} => {
                        if outline.begin_folder(cx, *uid, &if !name.is_unique(){
                            if let LivePropType::Field = prop_type {
                                format!("{}: <{}>", name, class)
                            }
                            else {
                                format!("{}=<{}>", name, class)
                            }
                        }else {
                            format!("<{}>", class)
                        }).is_ok() {
                            recur_walk(cx, outline, children);
                            outline.end_folder();
                        }
                    }
                }
            }
        }
        recur_walk(cx, outline, &self.outline_nodes);
    }
    
}

impl Widget for Designer {
    fn handle_widget_event_with(&mut self, cx: &mut Cx, event: &Event, _dispatch_action: &mut dyn FnMut(&mut Cx, WidgetActionItem)) {
        let _actions = self.ui.handle_widget_event(cx, event);
        for (component, container) in self.components.values_mut() {
            component.handle_widget_event(cx, event);
            container.handle_widget_event(cx, event);
        }
    }
    
    fn redraw(&mut self, cx: &mut Cx) {
        self.ui.redraw(cx)
    }
    
    fn draw_walk_widget(&mut self, cx: &mut Cx2d, _walk: Walk) -> WidgetDraw {
        let outline = self.ui.file_tree(id!(outline));
        while let Some(next) = self.ui.draw_widget(cx).hook_widget() {
            if let Some(mut outline) = outline.has_widget(&next).borrow_mut() {
                self.draw_outline(cx, &mut *outline);
            }
            else if next == self.ui.widget(id!(design)) {
                self.draw_design(cx);
            }
        }
        WidgetDraw::done()
    }
}