You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flex.apache.org by ah...@apache.org on 2014/10/24 22:49:06 UTC

[6/6] git commit: [flex-asjs] [refs/heads/develop] - make layout a bit smarter about how often it runs. If a component as explicitWidth/Height or percentWidth/Height, it now waits until it is sized before doing the first layout pass. But if the compone

make layout a bit smarter about how often it runs.  If a component as explicitWidth/Height or percentWidth/Height, it now waits until it is sized before doing the first layout pass.  But if the component needs to size to content, it does the first layout when children are added.


Project: http://git-wip-us.apache.org/repos/asf/flex-asjs/repo
Commit: http://git-wip-us.apache.org/repos/asf/flex-asjs/commit/1bbf6522
Tree: http://git-wip-us.apache.org/repos/asf/flex-asjs/tree/1bbf6522
Diff: http://git-wip-us.apache.org/repos/asf/flex-asjs/diff/1bbf6522

Branch: refs/heads/develop
Commit: 1bbf6522314ffde45e7ee8d32ab2b5a279b90918
Parents: 84d993f
Author: Alex Harui <ah...@apache.org>
Authored: Fri Oct 24 12:01:53 2014 -0700
Committer: Alex Harui <ah...@apache.org>
Committed: Fri Oct 24 13:07:50 2014 -0700

----------------------------------------------------------------------
 .../html/beads/layouts/NonVirtualBasicLayout.as | 52 ++++++++++++-----
 .../beads/layouts/NonVirtualHorizontalLayout.as | 46 ++++++++++++---
 .../beads/layouts/NonVirtualVerticalLayout.as   | 59 ++++++++++++++------
 3 files changed, 119 insertions(+), 38 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/1bbf6522/frameworks/as/projects/FlexJSUI/src/org/apache/flex/html/beads/layouts/NonVirtualBasicLayout.as
----------------------------------------------------------------------
diff --git a/frameworks/as/projects/FlexJSUI/src/org/apache/flex/html/beads/layouts/NonVirtualBasicLayout.as b/frameworks/as/projects/FlexJSUI/src/org/apache/flex/html/beads/layouts/NonVirtualBasicLayout.as
index eaa58e5..b83b2ff 100644
--- a/frameworks/as/projects/FlexJSUI/src/org/apache/flex/html/beads/layouts/NonVirtualBasicLayout.as
+++ b/frameworks/as/projects/FlexJSUI/src/org/apache/flex/html/beads/layouts/NonVirtualBasicLayout.as
@@ -28,6 +28,7 @@ package org.apache.flex.html.beads.layouts
 	import org.apache.flex.core.ValuesManager;
 	import org.apache.flex.events.Event;
 	import org.apache.flex.events.IEventDispatcher;
+    import org.apache.flex.utils.debug.DOMPathUtil;
 
     /**
      *  The NonVirtualBasicLayout class is a simple layout
@@ -54,8 +55,11 @@ package org.apache.flex.html.beads.layouts
 		{
 		}
 		
-		private var _strand:IStrand;
-		
+        // the strand/host container is also an ILayoutChild because
+        // can have its size dictated by the host's parent which is
+        // important to know for layout optimization
+        private var host:ILayoutChild;
+        		
         /**
          *  @copy org.apache.flex.core.IBead#strand
          *  
@@ -66,19 +70,40 @@ package org.apache.flex.html.beads.layouts
          */
 		public function set strand(value:IStrand):void
 		{
-			_strand = value;
-			IEventDispatcher(value).addEventListener("heightChanged", changeHandler);
-            IEventDispatcher(value).addEventListener("widthChanged", changeHandler);
-			IEventDispatcher(value).addEventListener("childrenAdded", changeHandler);
-            IEventDispatcher(value).addEventListener("layoutNeeded", changeHandler);
-			IEventDispatcher(value).addEventListener("itemsCreated", changeHandler);
-			IEventDispatcher(value).addEventListener("beadsAdded", changeHandler);
+            host = value as ILayoutChild;
+            
+            // if host is going to be sized by its parent, then don't
+            // run layout when the children are added until after the
+            // initial sizing by the parent.
+            if (host.isWidthSizedToContent() && host.isHeightSizedToContent())
+            {
+                host.addEventListener("childrenAdded", changeHandler);
+                host.addEventListener("layoutNeeded", changeHandler);
+                host.addEventListener("itemsCreated", changeHandler);
+            }
+            else
+            {
+                host.addEventListener("heightChanged", changeHandler);
+                host.addEventListener("widthChanged", changeHandler);
+                host.addEventListener("sizeChanged", sizeChangeHandler);
+                if (!isNaN(host.explicitWidth) && !isNaN(host.explicitHeight))
+                    sizeChangeHandler(null);
+            }
 		}
 	
+        private function sizeChangeHandler(event:Event):void
+        {
+            host.addEventListener("childrenAdded", changeHandler);
+            host.addEventListener("layoutNeeded", changeHandler);
+            host.addEventListener("itemsCreated", changeHandler);
+            changeHandler(event);
+        }
+        
 		private function changeHandler(event:Event):void
 		{
-			var layoutParent:ILayoutParent = _strand.getBeadByType(ILayoutParent) as ILayoutParent;
-			var contentView:IParentIUIBase = layoutParent ? layoutParent.contentView : IParentIUIBase(_strand);
+            //trace(DOMPathUtil.getPath(host), event ? event.type : "fixed size");
+			var layoutParent:ILayoutParent = host.getBeadByType(ILayoutParent) as ILayoutParent;
+			var contentView:IParentIUIBase = layoutParent ? layoutParent.contentView : IParentIUIBase(host);
 			
             var w:Number = contentView.width;
             var h:Number = contentView.height;
@@ -108,7 +133,7 @@ package org.apache.flex.html.beads.layouts
                 {
                     ilc = child as ILayoutChild;
                     if (!isNaN(ilc.percentWidth))
-                        ilc.setWidth((ww - (isNaN(right) ? 0 : right)) * ilc.percentWidth / 100);
+                        ilc.setWidth((ww - (isNaN(right) ? 0 : right)) * ilc.percentWidth / 100, true);
                 }
                 if (!isNaN(right))
                 {
@@ -126,7 +151,7 @@ package org.apache.flex.html.beads.layouts
                 {
                     ilc = child as ILayoutChild;
                     if (!isNaN(ilc.percentHeight))
-                        ilc.setHeight((hh - (isNaN(bottom) ? 0 : bottom)) * ilc.percentHeight / 100);
+                        ilc.setHeight((hh - (isNaN(bottom) ? 0 : bottom)) * ilc.percentHeight / 100, true);
                 }
                 if (!isNaN(bottom))
                 {
@@ -140,6 +165,7 @@ package org.apache.flex.html.beads.layouts
                     else
                         child.y = h - bottom - child.height;
                 }
+                child.dispatchEvent(new Event("sizeChanged"));
 			}
 		}
 	}

http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/1bbf6522/frameworks/as/projects/FlexJSUI/src/org/apache/flex/html/beads/layouts/NonVirtualHorizontalLayout.as
----------------------------------------------------------------------
diff --git a/frameworks/as/projects/FlexJSUI/src/org/apache/flex/html/beads/layouts/NonVirtualHorizontalLayout.as b/frameworks/as/projects/FlexJSUI/src/org/apache/flex/html/beads/layouts/NonVirtualHorizontalLayout.as
index d10aa99..820c410 100644
--- a/frameworks/as/projects/FlexJSUI/src/org/apache/flex/html/beads/layouts/NonVirtualHorizontalLayout.as
+++ b/frameworks/as/projects/FlexJSUI/src/org/apache/flex/html/beads/layouts/NonVirtualHorizontalLayout.as
@@ -27,6 +27,7 @@ package org.apache.flex.html.beads.layouts
 	import org.apache.flex.core.ValuesManager;
 	import org.apache.flex.events.Event;
 	import org.apache.flex.events.IEventDispatcher;
+    import org.apache.flex.utils.debug.DOMPathUtil;
 
     /**
      *  The NonVirtualHorizontalLayout class is a simple layout
@@ -53,7 +54,10 @@ package org.apache.flex.html.beads.layouts
 		{
 		}
 		
-		private var _strand:IStrand;
+        // the strand/host container is also an ILayoutChild because
+        // can have its size dictated by the host's parent which is
+        // important to know for layout optimization
+		private var host:ILayoutChild;
 		
         /**
          *  @copy org.apache.flex.core.IBead#strand
@@ -65,16 +69,39 @@ package org.apache.flex.html.beads.layouts
          */
 		public function set strand(value:IStrand):void
 		{
-			_strand = value;
-			IEventDispatcher(value).addEventListener("widthChanged", changeHandler);
-			IEventDispatcher(value).addEventListener("childrenAdded", changeHandler);
-            IEventDispatcher(value).addEventListener("layoutNeeded", changeHandler);
-			IEventDispatcher(value).addEventListener("itemsCreated", changeHandler);
+			host = value as ILayoutChild;
+            
+            // if host is going to be sized by its parent, then don't
+            // run layout when the children are added until after the
+            // initial sizing by the parent.
+            if (host.isWidthSizedToContent() && host.isHeightSizedToContent())
+            {
+                host.addEventListener("childrenAdded", changeHandler);
+                host.addEventListener("layoutNeeded", changeHandler);
+                host.addEventListener("itemsCreated", changeHandler);
+            }
+            else
+            {
+        		host.addEventListener("widthChanged", changeHandler);
+                host.addEventListener("heightChanged", changeHandler);
+                host.addEventListener("sizeChanged", sizeChangeHandler);
+                if (!isNaN(host.explicitWidth) && !isNaN(host.explicitHeight))
+                    sizeChangeHandler(null);
+            }
 		}
 	
+        private function sizeChangeHandler(event:Event):void
+        {
+            host.addEventListener("childrenAdded", changeHandler);
+            host.addEventListener("layoutNeeded", changeHandler);
+            host.addEventListener("itemsCreated", changeHandler);
+            changeHandler(event);
+        }
+        
 		private function changeHandler(event:Event):void
 		{
-			var layoutParent:ILayoutParent = _strand.getBeadByType(ILayoutParent) as ILayoutParent;
+            //trace(DOMPathUtil.getPath(host), event ? event.type : "fixed size");
+			var layoutParent:ILayoutParent = host.getBeadByType(ILayoutParent) as ILayoutParent;
 			var contentView:IParentIUIBase = layoutParent.contentView;
 			
 			var n:int = contentView.numElements;
@@ -157,7 +184,7 @@ package org.apache.flex.html.beads.layouts
                 {
                     ilc = child as ILayoutChild;
                     if (!isNaN(ilc.percentWidth))
-                        ilc.setWidth(contentView.width * ilc.percentWidth / 100);
+                        ilc.setWidth(contentView.width * ilc.percentWidth / 100, true);
                 }
 				xx = child.x + child.width;
 				lastmr = mr;
@@ -172,7 +199,7 @@ package org.apache.flex.html.beads.layouts
                 {
                     ilc = child as ILayoutChild;
                     if (!isNaN(ilc.percentHeight))
-                        ilc.setHeight(contentView.height * ilc.percentHeight / 100);
+                        ilc.setHeight(contentView.height * ilc.percentHeight / 100, true);
                 }
 				if (obj.valign == "middle")
 					child.y = (maxHeight - child.height) / 2;
@@ -180,6 +207,7 @@ package org.apache.flex.html.beads.layouts
 					child.y = maxHeight - child.height - obj.marginBottom;
 				else
 					child.y = obj.marginTop;
+                child.dispatchEvent(new Event("sizeChanged"));
 			}
 		}
 	}

http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/1bbf6522/frameworks/as/projects/FlexJSUI/src/org/apache/flex/html/beads/layouts/NonVirtualVerticalLayout.as
----------------------------------------------------------------------
diff --git a/frameworks/as/projects/FlexJSUI/src/org/apache/flex/html/beads/layouts/NonVirtualVerticalLayout.as b/frameworks/as/projects/FlexJSUI/src/org/apache/flex/html/beads/layouts/NonVirtualVerticalLayout.as
index fb7977f..cf0de6a 100644
--- a/frameworks/as/projects/FlexJSUI/src/org/apache/flex/html/beads/layouts/NonVirtualVerticalLayout.as
+++ b/frameworks/as/projects/FlexJSUI/src/org/apache/flex/html/beads/layouts/NonVirtualVerticalLayout.as
@@ -28,6 +28,7 @@ package org.apache.flex.html.beads.layouts
 	import org.apache.flex.core.ValuesManager;
 	import org.apache.flex.events.Event;
 	import org.apache.flex.events.IEventDispatcher;
+    import org.apache.flex.utils.debug.DOMPathUtil;
 
     /**
      *  The NonVirtualVerticalLayout class is a simple layout
@@ -54,8 +55,11 @@ package org.apache.flex.html.beads.layouts
 		{
 		}
 		
-		private var _strand:IStrand;
-		
+        // the strand/host container is also an ILayoutChild because
+        // can have its size dictated by the host's parent which is
+        // important to know for layout optimization
+        private var host:ILayoutChild;
+        		
         /**
          *  @copy org.apache.flex.core.IBead#strand
          *  
@@ -66,18 +70,40 @@ package org.apache.flex.html.beads.layouts
          */
 		public function set strand(value:IStrand):void
 		{
-			_strand = value;
-			IEventDispatcher(value).addEventListener("heightChanged", changeHandler);
-			IEventDispatcher(value).addEventListener("childrenAdded", changeHandler);
-			IEventDispatcher(value).addEventListener("itemsCreated", changeHandler);
-            IEventDispatcher(value).addEventListener("layoutNeeded", changeHandler);
-			IEventDispatcher(value).addEventListener("beadsAdded", changeHandler);
+            host = value as ILayoutChild;
+            
+            // if host is going to be sized by its parent, then don't
+            // run layout when the children are added until after the
+            // initial sizing by the parent.
+            if (host.isWidthSizedToContent() && host.isHeightSizedToContent())
+            {
+                host.addEventListener("childrenAdded", changeHandler);
+                host.addEventListener("layoutNeeded", changeHandler);
+                host.addEventListener("itemsCreated", changeHandler);
+            }
+            else
+            {
+                host.addEventListener("widthChanged", changeHandler);
+                host.addEventListener("heightChanged", changeHandler);
+                host.addEventListener("sizeChanged", sizeChangeHandler);
+                if (!isNaN(host.explicitWidth) && !isNaN(host.explicitHeight))
+                    sizeChangeHandler(null);
+            }
 		}
 	
+        private function sizeChangeHandler(event:Event):void
+        {
+            host.addEventListener("childrenAdded", changeHandler);
+            host.addEventListener("layoutNeeded", changeHandler);
+            host.addEventListener("itemsCreated", changeHandler);
+            changeHandler(event);
+        }
+        
 		private function changeHandler(event:Event):void
 		{
-			var layoutParent:ILayoutParent = _strand.getBeadByType(ILayoutParent) as ILayoutParent;
-			var contentView:IParentIUIBase = layoutParent ? layoutParent.contentView : IParentIUIBase(_strand);
+            //trace(DOMPathUtil.getPath(host), event ? event.type : "fixed size");
+			var layoutParent:ILayoutParent = host.getBeadByType(ILayoutParent) as ILayoutParent;
+			var contentView:IParentIUIBase = layoutParent ? layoutParent.contentView : IParentIUIBase(host);
 			
 			var n:int = contentView.numElements;
 			var hasHorizontalFlex:Boolean;
@@ -141,7 +167,7 @@ package org.apache.flex.html.beads.layouts
                 {
                     ilc = child as ILayoutChild;
                     if (!isNaN(ilc.percentHeight))
-                        ilc.setHeight(contentView.height * ilc.percentHeight / 100);
+                        ilc.setHeight(contentView.height * ilc.percentHeight / 100, true);
                 }
 				yy = child.y + child.height;
 				lastmb = mb;
@@ -185,21 +211,22 @@ package org.apache.flex.html.beads.layouts
                 {
                     ilc = child as ILayoutChild;
                     if (!isNaN(ilc.percentWidth))
-                        ilc.setWidth(contentView.width * ilc.percentWidth / 100);
+                        ilc.setWidth(contentView.width * ilc.percentWidth / 100, true);
                 }
 				maxWidth = Math.max(maxWidth, ml + child.width + mr);
 			}
-			if (hasHorizontalFlex)
+			for (i = 0; i < n; i++)
 			{
-				for (i = 0; i < n; i++)
-				{
-					child = contentView.getElementAt(i) as IUIBase;
+                child = contentView.getElementAt(i) as IUIBase;
+                if (hasHorizontalFlex)
+                {
 					var obj:Object = flexibleHorizontalMargins[i];
 					if (obj.marginLeft == "auto" && obj.marginRight == "auto")
 						child.x = maxWidth - child.width / 2;
 					else if (obj.marginLeft == "auto")
 						child.x = maxWidth - child.width - obj.marginRight;
 				}
+                child.dispatchEvent(new Event("sizeChanged"));
 			}
 		}
 	}