makepad-widgets 1.0.0

Makepad widgets
Documentation
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
use crate::{
    makepad_draw::*,
    widget::*,
    makepad_platform::studio::DesignerComponentPosition,
    //makepad_live_compiler::LiveTokenId,
};
use std::collections::HashMap;
use std::fmt::Write;

pub enum OutlineNode{
    Virtual{
        name: String,
        children: SmallVec<[LiveId;4]>
    },
    File{
        file_id: LiveFileId,
        name: String,
        children: SmallVec<[LiveId;4]>
    },
    Folder{
        name: String,        
        children: SmallVec<[LiveId;4]>
    },
    Component{
        name: String,        
        id: LiveId,
        class: LiveId,
        prop_type: LivePropType,
        //token_id: LiveTokenId,
        ptr: LivePtr,
        children: SmallVec<[LiveId;4]>
    }
}

impl OutlineNode{
    fn children(&self)->&[LiveId]{
        match self{
            Self::Virtual{children,..}=>children,
            Self::File{children,..}=>children,
            Self::Folder{children,..}=>children,
            Self::Component{children,..}=>children,
        }
    }
    
    fn name(&self)->&str{
        match self{
            Self::Virtual{name,..}=>name,
            Self::File{name,..}=>name,
            Self::Folder{name,..}=>name,
            Self::Component{name,..}=>name,
        }
    }
}


#[derive(Default)]
pub struct DesignerDataToWidget{
    pub positions: Vec<DesignerComponentPosition>,
    pub live_ptr_to_widget: HashMap<LivePtr, WidgetRef>, 
}

impl DesignerDataToWidget{
    pub fn find_ptr_by_widget_ref(&self, widget:&WidgetRef)->Option<LivePtr>{
        for (ptr, wref) in &self.live_ptr_to_widget{
            if wref == widget{
                return Some(*ptr)
            }
        }
        None
    }
}

#[derive(Default)]
pub struct DesignerData{
    pub pending_revision: bool,
    pub root: LiveId,
    pub node_map: HashMap<LiveId, OutlineNode>,
    pub selected: Option<LiveId>,
    pub to_widget: DesignerDataToWidget,
}

impl DesignerData{
    pub fn get_node_by_path(&self, root:LiveId, path:&str)->Option<LiveId>{
        let mut current = root;
        let mut split = path.split("/");
        'outer: while let Some(node) = self.node_map.get(&current){
            if let Some(next_name) = split.next(){
                for child in node.children(){
                    let node = self.node_map.get(&child).unwrap();
                    if node.name().starts_with(next_name){
                        current = *child;
                        continue 'outer;
                    }
                }
                return None
            }
            else{
                return Some(current)
            }
        }
        None
    }
    
    pub fn update_from_live_registry(&mut self, cx:&mut Cx){
        self.node_map.clear();
        self.to_widget.live_ptr_to_widget.clear();
        let root_uid = live_id!(designer_root).into();
        self.root = root_uid;
        self.node_map.insert(root_uid, OutlineNode::Virtual{
            name: "root".into(),
            children: SmallVec::new()
        });
                
        // 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 main_module_lti = live_registry.main_module.as_ref().unwrap().clone();
        let mut path_hash = String::new();
        for (file_path, file_id) in live_registry.file_ids(){
            // 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_components(
                main_module_lti:&LiveTypeInfo, 
                live_registry: &LiveRegistry, 
                hash_id:LiveId, 
                base_ptr: LivePtr, 
                mut index: usize, 
                nodes: &[LiveNode],
                map: &mut HashMap<LiveId, OutlineNode>,
                parent_children: &mut SmallVec<[LiveId;4]>) -> usize {
                                        
                while index < nodes.len().saturating_sub(1) { 
                    if let LiveValue::Class {live_type, class_parent, ..} = &nodes[index].value {
                        // lets check if its a widget
                        let wr = live_registry.components.get::<WidgetRegistry>();
                        if main_module_lti.live_type == *live_type || wr.map.get(live_type).is_some(){
                                                            
                            // lets emit a class at our level
                            let id = nodes[index].id;
                            let class = live_registry.ptr_to_node(*class_parent).id;
                            
                            // skip the fluff
                            if class == live_id!(Designer) || class == live_id!(PerformanceView) || 
                            id == live_id!(caption_bar) && class == live_id!(SolidView) ||
                            id == live_id!(window_menu) && class == live_id!(WindowMenu){
                                index = nodes.skip_node(index);
                                continue;
                            }
                            
                            let ptr = base_ptr.with_index(index);
                            let prop_type =  nodes[index].origin.prop_type();
                            //let token_id = nodes[index].origin.token_id();
                            let uid = hash_id.bytes_append(&id.0.to_be_bytes());
                            let mut children = SmallVec::new();
                                                            
                            index = recur_walk_components(
                                main_module_lti, 
                                live_registry, 
                                uid, 
                                base_ptr, 
                                index + 1, 
                                nodes, 
                                map, 
                                &mut children
                            );
                                                            
                            let uid = uid.into();
                            parent_children.push(uid);
                            
                            let mut name = String::new();
                            
                            if !id.is_unique(){
                                if let LivePropType::Field = prop_type {
                                    write!(name, "{}: <{}>", id, class).unwrap();
                                }
                                else {
                                    write!(name, "{}=<{}>", id, class).unwrap();
                                }
                            }
                            else {
                                write!(name, "<{}>", class).unwrap();
                            }
                                
                            map.insert(uid, OutlineNode::Component {
                                id,
                                name,
                                prop_type,
                                class,
                                ptr,
                                children
                            });
                        
                            // find all the components that start with app_ and make sure the folder is visible
                            
                        }
                        else{
                            //   log!("NOT A WIDGET {}", nodes[index].id);
                            index = nodes.skip_node(index);
                        }
                    }
                    else if nodes[index].value.is_close() {
                        return index + 1;
                    }
                    else {
                        index = nodes.skip_node(index);
                    }
                }
                index
            }
            // alright lets iterate over the files
                            
            let base_ptr = live_registry.file_id_index_to_live_ptr(*file_id, 0);
                            
            path_hash.clear();
                            

            let base_id = LiveId(0).bytes_append(&file_id.0.to_be_bytes());
                            
            let mut children_out = SmallVec::new();
            recur_walk_components(
                &main_module_lti, 
                live_registry, 
                base_id, 
                base_ptr, 
                1, 
                nodes,
                &mut self.node_map, 
                &mut children_out
            );
            if children_out.len() == 0{
                continue
            }
            let parent_id = path_split(self.root, &mut path_hash, file_path, &mut self.node_map, *file_id);
            if let Some(OutlineNode::File{children,..}) = self.node_map.get_mut(&parent_id){
                *children = children_out;
            }
                            
            fn path_split<'a>(parent_id: LiveId, path_hash:&mut String, name:&str, map:&mut HashMap<LiveId, OutlineNode>, file_id:LiveFileId)->LiveId{
                if let Some((folder,rest)) = name.split_once("/"){
                                            
                    if folder == "src"{ // flatten this
                        return path_split(parent_id, path_hash, rest, map, file_id)
                    }
                    
                    path_hash.push_str(folder);
                    path_hash.push_str("/");
                                                                                    
                    let what_uid =  LiveId::from_str(&path_hash).into();
                                            
                    // add node to pareht
                    if let Some(OutlineNode::Folder{children, ..}) | Some(OutlineNode::Virtual{children, ..})= map.get_mut(&parent_id){
                        if !children.contains(&what_uid){
                            children.push(what_uid);
                        }
                    }
                                            
                    if map.get_mut(&what_uid).is_some(){
                        return path_split(what_uid, path_hash, rest, map, file_id)
                    }
                    
                    map.insert(what_uid, OutlineNode::Folder{
                        name: folder.to_string(),
                        children: SmallVec::new()
                    });
                                            
                    return path_split(what_uid, path_hash, rest, map, file_id)
                }
                else{ // we're a file
                    path_hash.push_str(name);
                    path_hash.push_str("/");
                    let what_uid =  LiveId::from_str(&path_hash).into();
                    if let Some(OutlineNode::Folder{children, ..}) | Some(OutlineNode::Virtual{children, ..})= map.get_mut(&parent_id){
                        if !children.contains(&what_uid){
                            children.push(what_uid);
                        }
                    }
                    map.insert(what_uid, OutlineNode::File{
                        file_id,
                        name: name.to_string(),
                        children: SmallVec::new()
                    });
                                            
                    return what_uid
                }
            }
        }
    }
    
    pub fn swap_child_refs(&mut self,parent:&WidgetRef, index:usize, index2:usize){
        if let Some(id) = self.find_component_by_widget_ref(parent){
            if let Some(OutlineNode::Component{children,..}) = self.node_map.get_mut(&id){
                children.swap(index, index2);
            }
        }
    }
        
    pub fn find_component_by_ptr(&mut self, find_ptr:LivePtr)->Option<LiveId>{
        for (node_id, node) in &self.node_map{
            if let OutlineNode::Component{ptr,..} = node{
                if *ptr == find_ptr{
                    return Some(*node_id)
                }
            }
        }
        None
    }
    
        
    pub fn find_component_by_widget_ref(&mut self, wref:&WidgetRef)->Option<LiveId>{
        if let Some(ptr) = self.to_widget.find_ptr_by_widget_ref(wref){
            return self.find_component_by_ptr(ptr);
        }
        None
    }
    
    pub fn construct_path_ids(&self, find_node:LiveId)->Vec<LiveId>{
        let mut result = Vec::new();
        result.push(find_node);
        let mut iter = find_node;
        while let Some(parent) = self.find_parent(iter){
            result.insert(0, parent);
            iter = parent;
        }
        result
    }
    
    pub fn path_ids_to_string(&self, path:&[LiveId])->String{
        let mut path_str= String::new();
        for node_id in path{
            if let Some(node) = self.node_map.get(&node_id){
                match node{
                    OutlineNode::Folder{name,..} | OutlineNode::File{name,..}=>{
                        path_str.push_str(name);
                        path_str.push_str("/");
                    }
                    _=>()
                }
            }
        }
        path_str
    }
    
    pub fn path_str_to_path_ids(path:&str)->Vec<LiveId>{
        let mut path_id = Vec::new();
        for (idx,_) in path.match_indices('/'){
            let slice = &path[0..idx+1];
            let hash =  LiveId::from_str(slice).into();
            path_id.push(hash);
        }
        path_id
    }

    pub fn find_parent(&self, find_node:LiveId)->Option<LiveId>{
        for (node_id, node) in &self.node_map{
            match node{
                OutlineNode::Component{children,..} | OutlineNode::Virtual{children,..} | OutlineNode::File{children,..} | OutlineNode::Folder{children, ..} =>{
                    if children.iter().position(|v| *v == find_node).is_some(){
                        return Some(*node_id)
                    }
                }
            }
        }
        None
    }
    
    pub fn find_file_parent(&mut self, find_node:LiveId)->Option<LiveId>{
        let mut iter = find_node;
        while let Some(parent) = self.find_parent(iter){
            if let Some(OutlineNode::File{..}) = self.node_map.get(&parent){
                return Some(parent);
            }
            iter = parent;
        }
        None
    }
    
    pub fn _remove_child(&mut self, find_node:LiveId){
        for node in &mut self.node_map.values_mut(){
            match node{
                OutlineNode::Component{children,..} | OutlineNode::Virtual{children,..} | OutlineNode::File{children,..} | OutlineNode::Folder{children, ..} =>{
                    if let Some(i) = children.iter().position(|v| *v == find_node){
                        children.remove(i);
                        break;
                    }
                }
            }
        }
    }
    
    pub fn _find_component_by_path(&self, path:&[LiveId])->Option<LiveId>{
        fn get_node(node:LiveId, path:&[LiveId],  map:&HashMap<LiveId, OutlineNode>)->Option<LiveId>{
            match map.get(&node).as_ref(){
                Some(OutlineNode::Virtual{children,..}) |
                Some(OutlineNode::Folder{children,..}) |
                Some(OutlineNode::File{children,..}) =>{
                    for child in children{
                        if let Some(v) = get_node(*child, path, map){
                            return Some(v)
                        }
                    }
                }
                Some(OutlineNode::Component{children, id, ..}) => {
                    if *id == path[0]{
                        if path.len()>1{
                            for child in children{
                                 if let Some(v) = get_node(*child, &path[1..], map){
                                     return Some(v)
                                 }
                            }
                        }
                        else{
                            return Some(node)
                        }
                    }
                    for child in children{
                        if let Some(v) = get_node(*child, path, map){
                            return Some(v)
                        }
                    }
                }
                _=>()
            }
            None
        }
        get_node(self.root, path, &self.node_map)
    }
}