src/Builder4/WindowState.vala
[app.Builder.js] / src / Builder4 / WindowState.vala
1 /**
2  * as state management is a bit too complicated inside the builder
3  * it's better to seperate this into this class
4  * 
5  * This class has references to all the Class instances that make up the window..
6  * 
7  */
8 public class WindowState : Object 
9 {
10     public Xcls_MainWindow win;
11
12     public enum State {
13         PREVIEW,
14         OBJECT,
15         PROP,
16         LISTENER,
17         CODE,
18         FILES,
19         PROJECT // project settings..
20     }
21
22     public State state;
23
24     public bool children_loaded = false;
25
26     
27     public Xcls_WindowLeftTree  left_tree;
28     public Xcls_WindowAddProp   add_props;
29     public Xcls_LeftProps       left_props;
30     public Xcls_ProjectSettings projectsettings;
31     public ValaProjectSettings  vala_projectsettings;
32     public Xcls_RightPalete     rightpalete;
33     public Editor               code_editor;    
34     public Xcls_WindowRooView   window_rooview;
35     public Xcls_GtkView         window_gladeview;
36     public Xcls_DialogNewComponent new_file_dialog;
37     public Xcls_ClutterFiles     clutterfiles;
38
39     public Xcls_WindowLeftProjects left_projects; // can not see where this is initialized.. 
40     // ctor 
41     public WindowState(Xcls_MainWindow win)
42     {
43         this.win = win;
44         // initialize
45
46         // left elements..
47         this.leftTreeInit();
48         this.propsListInit();
49
50         // on clutter space...
51         this.projectEditInit();
52         this.codeEditInit();
53         this.projectListInit();
54         this.fileViewInit();
55         
56         // adding stuff
57         this.objectAddInit();
58         this.propsAddInit();
59      
60         
61         // previews...
62         this.gtkViewInit();
63         this.webkitViewInit();
64
65         // dialogs
66
67         this.fileNewInit();
68
69         this.children_loaded = true;
70     }
71
72
73     // left tree
74
75     public void leftTreeInit()
76     {
77      
78         this.left_tree = new Xcls_WindowLeftTree();
79         this.left_tree.ref();
80         this.left_tree.main_window = this.win;
81     
82         this.win.tree.el.pack_start(this.left_tree.el,true, true,0);
83         this.left_tree.el.show_all();
84            
85         this.left_tree.before_node_change.connect(() => {
86             return this.leftTreeBeforeChange();
87
88         });
89
90         this.left_tree.node_selected.connect((sel) => {
91             this.leftTreeNodeSelected(sel);
92         });
93      
94         this.left_tree.changed.connect(() => {
95             this.window_rooview.requestRedraw();
96             this.left_tree.model.file.save();
97         });
98          
99     }
100
101     public bool leftTreeBeforeChange()
102     {
103         if (this.state != State.CODE) {
104             this.left_props.finish_editing();
105             return true;
106         }
107         if (!this.code_editor.saveContents()) {
108             return false;
109         }
110         return false;
111     }
112     
113     public void leftTreeNodeSelected(JsRender.Node? sel)
114     {
115
116         print("node_selected called %s\n", (sel == null) ? "NULL" : "a value");
117
118         if (sel == null) {
119             this.left_props.el.hide();
120         } 
121         this.left_props.el.show();
122         this.left_props.load(this.left_tree.getActiveFile(), sel);
123         switch (this.state) {
124             
125             case State.OBJECT: 
126                   
127                  if (sel == null) {
128                     this.rightpalete.clear();
129                     break;
130                 }
131                 this.rightpalete.load(this.left_tree.getActiveFile().palete(), sel.fqn());
132                 break;
133                  
134         
135            case State.PROP:
136                 if (sel == null) {
137                     this.add_props.clear();
138                     break;
139                 }
140                 this.add_props.show(this.left_tree.getActiveFile().palete(), "props", sel.fqn());
141                 break;
142
143             case State.LISTENER:
144                
145                 if (sel == null) {
146                     this.add_props.clear();
147                     break;
148                 }
149                 this.add_props.show(this.left_tree.getActiveFile().palete(), "signals", sel.fqn());
150                 break;
151                 
152             case State.CODE:
153                  this.switchState(State.PREVIEW);
154              
155                 break;
156                
157                             
158         }
159          
160
161     }
162
163
164
165
166     public void propsListInit()
167     {
168     
169         this.left_props =new Xcls_LeftProps();
170         this.left_props.ref();
171         this.left_props.main_window = this.win;
172         this.win.props.el.pack_start(this.left_props.el,true, true,0);
173         this.left_props.el.show_all();
174     
175         this.left_props.show_editor.connect( (file, node, type,  key) => {
176             this.switchState(State.CODE);
177             this.code_editor.show(
178                 file,
179                 node,
180                 type,
181                 key
182             );
183             
184             
185         });
186
187    
188         this.left_props.stop_editor.connect( () => {
189             if (this.state != State.CODE) {
190                 return true;
191             }
192     
193             var ret =  this.code_editor.saveContents();
194             if (!ret) {
195                 return false;
196             }
197             this.switchState(State.PREVIEW);
198             return ret;
199         });
200     
201         this.left_props.changed.connect(() => {
202               if (this.left_tree.getActiveFile().xtype == "Roo" ) {
203                    this.win.window_rooview.requestRedraw();
204                    
205                } else {
206                   this.win.window_gladeview.loadFile(this.left_tree.getActiveFile());
207               }
208               this.left_tree.model.updateSelected();
209               this.left_tree.model.file.save();
210         });
211     
212
213
214     }
215
216     //-------------  projects edit
217
218     public void projectEditInit()
219     {
220         this.projectsettings  =new Xcls_ProjectSettings();
221         this.projectsettings.ref();  /// really?
222     
223         this.vala_projectsettings  =new ValaProjectSettings();
224         this.vala_projectsettings.ref();
225         this.vala_projectsettings.window = this.win;
226     
227         ((Gtk.Container)(this.win.projecteditview.el.get_widget())).add(this.projectsettings.el);
228         //this.projectsettings.el.show_all();
229
230         var stage = this.win.projecteditview.el.get_stage();
231         stage.set_background_color(  Clutter.Color.from_string("#000"));
232     
233         this.projectsettings.buttonPressed.connect((btn) => {
234              if (this.left_tree.getActiveFile().xtype == "Roo" ) {
235                 if (btn == "save") {
236                     this.window_rooview.view.renderJS(true);
237                 }
238                 if (btn == "apply") {
239                     this.window_rooview.view.renderJS(true);
240                     return;
241                 }
242             } else {
243                 // do nothing for gtk..
244             }
245             if (btn == "save" || btn == "apply") {
246                 this.win.project.save();
247          
248             }
249             this.switchState (State.PREVIEW); 
250              
251          });
252
253     }
254     // ----------- object adding
255     public void objectAddInit()
256     {
257
258         this.rightpalete  = new Xcls_RightPalete();
259         this.rightpalete.ref();  /// really?
260         ((Gtk.Container)(this.win.objectview.el.get_widget())).add(this.rightpalete.el);
261         //this.projectsettings.el.show_all();
262
263         stage = _this.win.objectview.el.get_stage();
264         stage.set_background_color(  Clutter.Color.from_string("#000"));
265            
266     }
267     
268     // -----------  properties adding list...
269     // listener uses the properties 
270     public void propsAddInit()
271     {
272     // Add properties
273         this.add_props  = new Xcls_WindowAddProp();
274         this.add_props.ref();  /// really?
275         ((Gtk.Container)(this.win.addpropsview.el.get_widget())).add(this.add_props.el);
276         //this.projectsettings.el.show_all();
277
278         var  stage = _this.win.addpropsview.el.get_stage();
279         stage.set_background_color(  Clutter.Color.from_string("#000"));
280
281
282         this.add_props.select.connect( (key,type,skel, etype) => {
283             this.left_props.addProp(etype, key, skel, type);
284         });
285
286     }
287     public void propsAddShow()
288     {
289
290     }
291     public void propsAddHide()
292     {
293     
294     }
295
296
297
298     
299     // ----------- Add / Edit listener
300     // listener uses the properties 
301     //public void listenerInit()     { }
302     public void listenerShow()
303     {
304
305     }
306     public void listenerHide()
307     {
308     
309     }
310
311     // -------------- codeEditor
312
313     public void codeEditInit()
314     {
315         this.code_editor  = new  Editor();
316         this.code_editor.ref();  /// really?
317         ((Gtk.Container)(this.win.codeeditview.el.get_widget())).add(this.code_editor.el);
318         //this.projectsettings.el.show_all();
319
320         var stage = _this.win.codeeditview.el.get_stage();
321         stage.set_background_color(  Clutter.Color.from_string("#000"));
322         // editor.save...
323
324         this.code_editor.save.connect( () => {
325              this.left_tree.model.file.save();
326              this.left_tree.model.updateSelected();
327         });
328         
329     }
330
331     // ----------- list of projects on left
332     public void  projectListInit() 
333     {
334
335         this.left_projects = new Xcls_WindowLeftProjects();
336          this.left_projects.ref();
337          this.win.leftpane.el.pack_start(this.left_projects.el,true, true,0);
338          this.left_projects.el.show_all();
339          this.left_projects.project_selected.connect((proj) => {
340             proj.scanDirs();
341             this.clutterfiles.loadProject(proj);
342         
343          });
344
345     }
346     // ----------- file view
347
348     public void fileViewInit()
349     {
350         var stage = this.win.rooview.el.get_stage(); // seems odd... 
351         this.clutterfiles = new Xcls_ClutterFiles();
352         this.clutterfiles.ref();
353         stage.add_child(this.clutterfiles.el);
354         this.clutterfiles.el.show_all();
355
356
357         this.clutterfiles.open.connect((file) => { 
358             this.fileViewOpen(file);
359         });
360
361     }
362     public void fileNewInit()
363     {
364         this.new_file_dialog = new Xcls_DialogNewComponent();
365         // force it modal to the main window..
366         this.new_file_dialog.el.set_transient_for(this.el);
367         this.new_file_dialog.el.set_modal(true);
368     
369         this.new_file_dialog.success.connect((project,file) =>
370         {
371             this.fileViewOpen(file);
372         });
373
374     }
375
376     
377     public void fileViewOpen(JsRender.JsRender file)
378     {
379         this.win.project = file.project;
380         this.previewShow();
381             this.left_tree.model.loadFile(file);
382     
383         var ctr= ((Gtk.Container)(this.win.rooview.el.get_widget()));
384         var ctr_p= ((Gtk.Container)(this.win.projecteditview.el.get_widget()));
385     
386         if (file.xtype == "Roo" ) { 
387             ctr.foreach( (w) => { ctr.remove(w); });
388             ctr_p.foreach( (w) => { ctr_p.remove(w); });
389             ctr.add(this.window_rooview.el);
390             ctr_p.add(this.projectsettings.el);            
391             this.window_rooview.loadFile(file);
392             this.window_rooview.el.show_all();
393             this.projectsettings.el.show_all();            
394
395         } else {
396             ctr.foreach( (w) => { ctr.remove(w); });
397             ctr_p.foreach( (w) => { ctr_p.remove(w); });            
398             ctr.add(this.window_gladeview.el);
399             ctr_p.add(this.vala_projectsettings.el);
400             this.window_gladeview.loadFile(file);
401             this.window_gladeview.el.show_all();
402             this.vala_projectsettings.el.show_all();
403         }
404         print("OPEN : " + file.name);
405         this.editpane.el.set_position(_this.editpane.el.max_position);
406         this.win.setTitle(file.project.name + " : " +file.name);
407              
408
409         }
410
411     
412     // ---------  webkit view
413     public void webkitViewInit()
414     {
415         this.window_rooview  =new Xcls_WindowRooView();
416         this.window_rooview.ref();
417         ((Gtk.Container)(this.win.rooview.el.get_widget())).add(this.window_rooview.el);
418         this.window_rooview.el.show_all();
419
420         stage = this.win.rooview.el.get_stage();
421         stage.set_background_color(  Clutter.Color.from_string("#000"));
422     }
423
424     // ------ Gtk  - view
425
426     public void gtkViewInit()
427     {
428         this.window_gladeview  =new Xcls_GtkView();
429         this.window_gladeview.ref();
430     }
431
432     public void switchState(State new_state)
433     {
434         
435
436         
437         switch (this.state) {
438
439             case State.PREVIEW:
440                 if (this.left_tree.getActiveFile() != null) {
441                      if (this.left_tree.getActiveFile().xtype == "Roo" ) {
442                          this.window_rooview.createThumb();
443                      } else {
444                           this.window_gladeview.createThumb();
445                       }
446                 }
447                 // normally we are going from preview to another state.
448                 // and different windows hide the preview in differnt ways..
449                 
450                 break;
451             
452            case State.LISTENER:
453            case State.PROP:
454                 this.win.addpropsview.el.save_easing_state();
455                 this.win.addpropsview.el.set_scale(0.0f,0.0f);
456                 this.win.addpropsview.el.restore_easing_state();   
457                 break;
458                 
459             case State.CODE:
460
461
462                 this.code_editor.saveContents();
463                 this.win.codeeditview.el.save_easing_state();
464                 this.win.codeeditview.el.set_scale(0.0f,0.0f);
465                 this.win.codeeditview.el.restore_easing_state();    
466                 break;
467
468
469              case State.OBJECT:
470                 this.win.objectview.el.save_easing_state();
471                 this.win.objectview.el.set_scale(0.0f,0.0f);
472                 this.win.objectview.el.restore_easing_state();    
473                 break;
474
475            case State.PROJECT:
476                 this.win.projecteditview.el.save_easing_state();
477                 this.win.projecteditview.el.set_scale(0.0f,0.0f);
478                 this.win.projecteditview.el.restore_easing_state();    
479                 break;
480
481           case State.FILES:       
482                 this.win.rooview.el.save_easing_state();
483                 this.win.rooview.el.set_rotation_angle(Clutter.RotateAxis.Y_AXIS, 0.0f);
484                 this.win.rooview.el.set_scale(1.0f,1.0f);
485                 this.win.rooview.el.restore_easing_state();  
486
487                  this.win.clutterfiles.el.hide();
488                 break;
489
490                 
491         }
492         
493         var oldstate  =this.state;
494         this.state = new_state;
495
496                 
497         this.buttonsShowHide();
498         
499         switch (this.state) {
500             
501             case State.PREVIEW:  // this is the default state when working...
502                  this.win.rooview.el.save_easing_state();
503                  this.win.rooview.el.set_scale(1.0f,1.0f);
504                  this.win.rooview.el.restore_easing_state();
505                
506                 break;
507
508
509             case State.LISTENER:
510             case State.PROP:
511                 var ae =      this.left_tree.getActiveElement();
512                 if (ae == null) {
513                     this.state = oldstate;
514                     this.buttonsShowHide();
515                     return;
516                 }
517                 this.add_props.el.show_all();
518                 this.add_props.show(
519                     Palete.factory(this.win.project.xtype), 
520                     this.state == State.LISTENER ? "signals" : "props",
521                     ae.fqn()
522                 );
523  
524                     
525  
526                 this.win.rooview.el.save_easing_state();
527                 // -- FIXME? this needs to be State aware?
528                 this.resizeCanvasElementsA();
529                 this.win.rooview.el.restore_easing_state();
530                 
531               
532                 this.win.addpropsview.el.save_easing_state();
533                 this.win.addpropsview.el.set_scale(1.0f,1.0f);
534                 this.win.addpropsview.el.restore_easing_state();
535                  
536                 
537             case State.CODE:
538
539                 this.code_editor.el.show_all();
540                 
541                 // caller needs to call editor - show....
542                 this.win.rooview.el.save_easing_state();
543                 this.resizeCanvasElementsA();
544                 this.win.rooview.el.restore_easing_state();
545                 
546  
547                 this.win.codeeditview.el.save_easing_state();
548                 this.win.codeeditview.el.set_scale(1.0f,1.0f);
549                 this.win.codeeditview.el.restore_easing_state();    
550                 break;
551
552
553              case State.OBJECT:
554                  var n = this.left_tree.getActiveElement();
555
556                 if (this.left_tree.model.file == null) {
557                     this.state =oldstate;
558                     this.buttonsShowHide();
559                     return;
560                 }
561                 
562                 if (n == null && this.left_tree.model.file.tree != null) {
563                     this.state = oldstate;
564                     this.buttonsShowHide();
565                     return;
566                 }
567
568                 this.rightpalete.el.show_all();
569                 this.rightpalete.load(this.left_tree.getActiveFile().palete(), n == null ? "*top" : n.fqn());
570
571                 this.win.rooview.el.save_easing_state();
572                 this.resizeCanvasElementsA();
573                 this.win.rooview.el.restore_easing_state();
574
575                 
576                 this.win.objectview.el.save_easing_state();
577                 this.win.objectview.el.set_scale(1.0f,1.0f);
578                 this.win.objectview.el.restore_easing_state();    
579                 break;
580
581            case State.PROJECT:
582
583              if (this.win.project.xtype == "Roo") {
584                     this.projectsettings.el.show_all();
585                     this.projectsettings.show(this.win.project);
586                 } else {
587                     this.vala_projectsettings.el.show_all();
588                     this.vala_projectsettings.show((Project.Gtk)this.win.project);
589                 }
590
591                  this.win.rooview.el.save_easing_state();
592                 this.resizeCanvasElementsA();
593                 this.win.rooview.el.restore_easing_state();
594
595                 this.win.projecteditview.el.save_easing_state();
596                 this.win.projecteditview.el.set_scale(0.0f,0.0f);
597                 this.win.projecteditview.el.restore_easing_state();    
598                 break;
599                 
600            case State.FILES:  // can only get here from PREVIEW state.. in theory..
601                 
602    
603                 this.win.editpane.el.hide(); // holder for tree and properties..
604              
605                 this.left_projects.el.show(); 
606             
607                 var el = this.win.rooview.el;
608                 el.save_easing_state();
609                 el.set_easing_duration(1000);
610
611                 el.set_rotation_angle(Clutter.RotateAxis.Y_AXIS, 360.0f);
612                 el.set_scale(0.0f,0.0f);
613
614  
615                 if (this.win.project != null) {
616                     this.left_projects.selectProject(this.win.project);
617                 }
618              
619                 el.restore_easing_state();
620                 
621                 break;
622
623
624         }
625
626             
627     }
628     
629     // -- buttons show hide.....
630
631     public void buttonsShowHide()
632     {
633         // basically hide everything, then show the relivant..
634
635          this.win.backbutton.el.hide();
636     
637         this.win.projectbutton.el.hide(); // show file nav...
638         this.win.editfilebutton.el.hide();
639         this.win.projecteditbutton.el.hide();
640          
641         
642         this.win.objectshowbutton.el.hide(); // add objects
643         this.win.addpropbutton.el.hide();  
644         this.win.addlistenerbutton.el.hide(); 
645
646     
647     
648         this.win.addprojectbutton.el.hide();
649         this.win.addfilebutton.el.hide();
650         this.win.delprojectbutton.el.hide();
651         this.win.new_window.el.hide();
652
653         
654         switch (this.state) {
655             
656             case State.PREVIEW:  // this is the default state when working...
657                
658                 this.win.projectbutton.el.show(); // show file nav...
659                 this.win.editfilebutton.el.show();
660                 this.win.projecteditbutton.el.show();
661                  
662                 
663                 this.win.objectshowbutton.el.show(); // add objects
664                 this.win.addpropbutton.el.show();  
665                 this.win.addlistenerbutton.el.show(); 
666                 break;
667             
668            
669             case State.CODE: 
670             case State.PROP:
671             case State.LISTENER:
672             case State.OBJECT:
673                 
674                 this.win.backbutton.el.show();
675                 this.win.objectshowbutton.el.show(); // add objects
676                 this.win.addpropbutton.el.show();  
677                 this.win.addlistenerbutton.el.show(); 
678                 break;
679                 
680             case State.FILES:
681                 this.win.backbutton.el.show();
682                      
683                 this.win.addprojectbutton.el.show();
684                 this.win.addfilebutton.el.show();
685                 this.win.delprojectbutton.el.show();
686                 this.win.new_window.el.show();
687                 break;
688         }
689
690     }
691     public void resizeCanvasElementsA()
692     {
693         Gtk.Allocation alloc;
694         this.win.clutterembed.el.get_allocation(out alloc);
695         this.resizeCanvasElements(alloc);
696     }
697     public void resizeCanvasElements(Gtk.Allocation alloc)
698     {
699         if (!this.children_loaded) { 
700             return; 
701         }
702      
703         this.win.clutterfiles.set_size(alloc.width-50, alloc.height);
704         
705         // ------- project view appears at top...
706         this.win.projecteditview.el.set_size(alloc.width-50, alloc.height / 2.0f);
707                
708         // ------- add property/object left - max 300px, min 50... (or disapear..)     
709         
710         var avail = alloc.width < 50.0f ? 0 :  alloc.width - 50.0f;
711         var palsize = avail < 300.0f ? avail : 300.0f;
712         //print("set palsize size %f\n", palsize);
713         // palate / props : fixed 300 pix
714                 
715         this.win.objectview.el.set_size(palsize, alloc.height);    
716         this.win.addpropsview.el.set_size(palsize, alloc.height);
717         
718          
719         
720         // -------- code edit min 600
721         
722         var codesize = avail < 800.0f ? avail : 800.0f;
723         //print("set code size %f\n", codesize);
724
725         this.win.codeeditview.el.set_size(codesize, alloc.height);
726         this.win.rooview.el.set_size(alloc.width-50, alloc.height);    
727
728
729         
730         switch ( this.state) {
731             case State.CODE: 
732                 var scale = avail > 0.0f ? (avail - codesize -10 ) / avail : 0.0f;
733                     this.win.rooview.el.set_scale(scale,scale);
734                 break;
735                 
736             case State.PROP:
737             case State.LISTENER:        
738             case State.OBJECT:   
739                     var scale = avail > 0.0f ? (avail - palsize -10 ) / avail : 0.0f;
740                 this.win.rooview.el.set_scale(scale,scale);
741                 break;
742         }
743     }
744
745     
746 }
747
748