Browse Source

make rebuild lazy: only rebuild when view is active (both startup and file change)

Nicolas Cannasse 5 years ago
parent
commit
8a23f915ea
3 changed files with 38 additions and 7 deletions
  1. 11 1
      hide/Ide.hx
  2. 23 3
      hide/ui/View.hx
  3. 4 3
      hide/view/FileView.hx

+ 11 - 1
hide/Ide.hx

@@ -311,11 +311,18 @@ class Ide {
 
 		layout = new golden.Layout(config);
 
+		var initViews = [];
+		function initView(view:hide.ui.View<Dynamic>) {
+			if( isDebugger ) view.rebuild() else try view.rebuild() catch( e : Dynamic ) error(view+":"+e);
+		}
 		for( vcl in hide.ui.View.viewClasses )
 			layout.registerComponent(vcl.name,function(cont,state) {
 				var view = Type.createInstance(vcl.cl,[state]);
 				view.setContainer(cont);
-				if( isDebugger ) view.rebuild() else try view.rebuild() catch( e : Dynamic ) error(vcl.name+":"+e);
+				if( initializing )
+					initViews.push(view);
+				else
+					initView(view);
 			});
 
 		layout.init();
@@ -348,6 +355,9 @@ class Ide {
 				}
 			}
 			initializing = false;
+			for( v in initViews )
+				initView(v);
+			initViews = null;
 			if( subView == null && views.length == 0 ) {
 				if( isCDB )
 					open("hide.view.CdbTable",{}, function(v) v.fullScreen = true);

+ 23 - 3
hide/ui/View.hx

@@ -24,6 +24,7 @@ class View<T> extends hide.comp.Component {
 
 	var contentWidth(get,never) : Int;
 	var contentHeight(get,never) : Int;
+	var needRebuild : Bool;
 
 	public function new(state:T) {
 		super(null,null);
@@ -146,8 +147,23 @@ class View<T> extends hide.comp.Component {
 		element = cont.getElement();
 	}
 
-	public function rebuild() {
-		if( container == null ) return;
+	public final function rebuild() {
+		function checkRebuild() {
+			if( container == null || !needRebuild ) return;
+			if( !isActive() ) {
+				haxe.Timer.delay(checkRebuild,200);
+				return;
+			}
+			needRebuild = false;
+			onRebuild();
+		}
+		if( !needRebuild ) {
+			needRebuild = true;
+			checkRebuild();
+		}
+	}
+
+	function onRebuild() {
 		for( w in watches.copy() )
 			if( !w.keep ) {
 				ide.fileWatcher.unregister(w.path, w.callb);
@@ -169,6 +185,10 @@ class View<T> extends hide.comp.Component {
 	public function onActivate() {
 	}
 
+	public function isActive() {
+		return container != null && !container.isHidden;
+	}
+
 	public function onDragDrop(items : Array<String>, isDrop : Bool) {
 		return false;
 	}
@@ -188,7 +208,7 @@ class View<T> extends hide.comp.Component {
 		}
 	}
 
-	public function saveState() {
+	function saveState() {
 		container.setState(state);
 	}
 

+ 4 - 3
hide/view/FileView.hx

@@ -21,7 +21,7 @@ class FileView extends hide.ui.View<{ path : String }> {
 			}, { checkDelete : true, keepOnRebuild : true });
 	}
 
-	override function rebuild() {
+	override function onRebuild() {
 		var path = getPath();
 		if( path != null ) {
 			saveDisplayKey = Type.getClassName(Type.getClass(this)) + ":" + path.split("\\").join("/");
@@ -30,11 +30,11 @@ class FileView extends hide.ui.View<{ path : String }> {
 				return;
 			}
 		}
-		super.rebuild();
+		super.onRebuild();
 	}
 
 	function onFileChanged( wasDeleted : Bool, rebuildView = true ) {
-		if( !wasDeleted ) {
+		if( !wasDeleted && currentSign != null ) {
 			// double check if content has changed
 			var content = sys.io.File.getContent(getPath());
 			var sign = haxe.crypto.Md5.encode(content);
@@ -46,6 +46,7 @@ class FileView extends hide.ui.View<{ path : String }> {
 			element.html('${state.path} no longer exists');
 			return;
 		}
+
 		if( modified && !ide.confirm('${state.path} has been modified, reload and ignore local changes?') )
 			return;
 		modified = false;