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