]> source.dussan.org Git - vaadin-framework.git/commitdiff
Removed PreLayoutListener again
authorJouni Koivuviita <jouni@jounikoivuviita.com>
Wed, 25 Apr 2012 08:18:20 +0000 (11:18 +0300)
committerJouni Koivuviita <jouni@jounikoivuviita.com>
Wed, 25 Apr 2012 08:18:20 +0000 (11:18 +0300)
src/com/vaadin/terminal/gwt/client/ComputedStyle.java
src/com/vaadin/terminal/gwt/client/LayoutManager.java
src/com/vaadin/terminal/gwt/client/ui/AbstractBoxLayoutConnector.java
tests/testbench/com/vaadin/tests/components/orderedlayout/LayoutRenderTimeTest.java [new file with mode: 0644]

index bedb217c63c375f6378508c33226a206f1560987..7dc937cee7a4fa42ac700eebcb2cf08fe1a702ef 100644 (file)
@@ -83,19 +83,19 @@ public class ComputedStyle {
 
             // If we're not dealing with a regular pixel number
             // but a number that has a weird ending, we need to convert it to pixels
-            if ( !/^\d+(px)?$/i.test( ret ) && /^\d/.test( ret ) ) {
-                // Remember the original values
-                var left = style.left, rsLeft = elem.runtimeStyle.left;
-
-                // Put in the new values to get a computed value out
-                elem.runtimeStyle.left = cs.left;
-                style.left = ret || 0;
-                ret = style.pixelLeft + "px";
-
-                // Revert the changed values
-                style.left = left;
-                elem.runtimeStyle.left = rsLeft;
-            }
+                if ( !/^\d+(px)?$/i.test( ret ) && /^\d/.test( ret ) ) {
+                    // Remember the original values
+                    var left = style.left, rsLeft = elem.runtimeStyle.left;
+    
+                    // Put in the new values to get a computed value out
+                    elem.runtimeStyle.left = cs.left;
+                    style.left = ret || 0;
+                    ret = style.pixelLeft + "px";
+    
+                    // Revert the changed values
+                    style.left = left;
+                    elem.runtimeStyle.left = rsLeft;
+                }
             
         }
         
index 628a247d391a60b89ef524c5aa49ad7abd0df84f..55d3fbe27294c2bc898e1c471c873c7e3d0e6faf 100644 (file)
@@ -18,7 +18,6 @@ import com.google.gwt.user.client.Timer;
 import com.vaadin.terminal.gwt.client.MeasuredSize.MeasureResult;
 import com.vaadin.terminal.gwt.client.ui.ManagedLayout;
 import com.vaadin.terminal.gwt.client.ui.PostLayoutListener;
-import com.vaadin.terminal.gwt.client.ui.PreLayoutListener;
 import com.vaadin.terminal.gwt.client.ui.SimpleManagedLayout;
 import com.vaadin.terminal.gwt.client.ui.layout.ElementResizeEvent;
 import com.vaadin.terminal.gwt.client.ui.layout.ElementResizeListener;
@@ -30,8 +29,6 @@ public class LayoutManager {
 
     private static final boolean debugLogging = false;
 
-    private static int totalLayoutTime = 0;
-
     private ApplicationConnection connection;
     private final Set<Element> measuredNonConnectorElements = new HashSet<Element>();
     private final MeasuredSize nullSize = new MeasuredSize();
@@ -181,16 +178,6 @@ public class LayoutManager {
 
         Duration totalDuration = new Duration();
 
-        int preLayoutStart = totalDuration.elapsedMillis();
-        for (ComponentConnector connector : connection.getConnectorMap()
-                .getComponentConnectors()) {
-            if (connector instanceof PreLayoutListener) {
-                ((PreLayoutListener) connector).preLayout();
-            }
-        }
-        VConsole.log("Invoke pre layout listeners in "
-                + (totalDuration.elapsedMillis() - preLayoutStart) + " ms");
-
         Map<ManagedLayout, Integer> layoutCounts = new HashMap<ManagedLayout, Integer>();
 
         int passes = 0;
@@ -205,6 +192,9 @@ public class LayoutManager {
         needsVerticalLayout.clear();
 
         for (ComponentConnector connector : needsMeasure) {
+            // Ensure the MeasuredSize is attached to the DOM element before
+            // doing measurements to avoid reflows in "some" browsers
+            getMeasuredSize(connector);
             currentDependencyTree.setNeedsMeasure(connector, true);
         }
         needsMeasure.clear();
@@ -229,6 +219,9 @@ public class LayoutManager {
             VConsole.log("  Measured " + measuredConnectorCount
                     + " elements in " + measureTime + " ms");
 
+            VConsole.log("  Total of " + measureCount
+                    + " measurement operations");
+
             if (!listenersToFire.isEmpty()) {
                 for (Element element : listenersToFire) {
                     Collection<ElementResizeListener> listeners = elementResizeListeners
@@ -365,14 +358,9 @@ public class LayoutManager {
         VConsole.log("Invoke post layout listeners in "
                 + (totalDuration.elapsedMillis() - postLayoutStart) + " ms");
 
-        VConsole.log("Total layout phase time: "
-                + totalDuration.elapsedMillis() + "ms");
+        VConsole.log("<b>Total layout phase time: "
+                + totalDuration.elapsedMillis() + "ms</b>");
 
-        totalLayoutTime += totalDuration.elapsedMillis();
-        VConsole.log("");
-        VConsole.log("### Total combined layout phase: " + totalLayoutTime
-                + "ms");
-        VConsole.log("");
     }
 
     private void logConnectorStatus(int connectorId) {
@@ -457,7 +445,7 @@ public class LayoutManager {
             ComponentConnector[] connectors = ConnectorMap.get(connection)
                     .getComponentConnectors();
             for (ComponentConnector connector : connectors) {
-                measueConnector(connector);
+                measureConnector(connector);
             }
             for (ComponentConnector connector : connectors) {
                 layoutDependencyTree.setNeedsMeasure(connector, false);
@@ -469,7 +457,7 @@ public class LayoutManager {
             Collection<ComponentConnector> measureTargets = layoutDependencyTree
                     .getMeasureTargets();
             for (ComponentConnector connector : measureTargets) {
-                measueConnector(connector);
+                measureConnector(connector);
                 measureCount++;
             }
             for (ComponentConnector connector : measureTargets) {
@@ -479,9 +467,16 @@ public class LayoutManager {
         return measureCount;
     }
 
-    private void measueConnector(ComponentConnector connector) {
-        Element element = connector.getWidget().getElement();
+    private void measureConnector(ComponentConnector connector) {
         MeasuredSize measuredSize = getMeasuredSize(connector);
+        if (!isManagedLayout(connector)
+                && !isManagedLayout(connector.getParent())
+                && elementResizeListeners.get(connector.getWidget()
+                        .getElement()) == null && !measuredSize.hasDependents()) {
+            return;
+        }
+
+        Element element = connector.getWidget().getElement();
         MeasureResult measureResult = measuredAndUpdate(element, measuredSize);
 
         if (measureResult.isChanged()) {
@@ -521,8 +516,11 @@ public class LayoutManager {
                 + " non connector elements");
     }
 
+    int measureCount = 0;
+
     private MeasureResult measuredAndUpdate(Element element,
             MeasuredSize measuredSize) {
+        measureCount++;
         MeasureResult measureResult = measuredSize.measure(element);
         if (measureResult.isChanged()) {
             notifyListenersAndDepdendents(element,
index b3857cdae9eb1229816ec402e85ff3950d6af14a..ab0168b39b6bcb951f69ca70cba37bb30bc0b040 100644 (file)
@@ -143,7 +143,7 @@ PostLayoutListener {
             // extra space equally if no expand ratios are specified inside a
             // layout with specified size
             if (expandRatios.getKeySet().size() == 0
-                    && ((getWidget().vertical && !isUndefinedHeight()) || !isUndefinedWidth())) {
+                    && ((getWidget().vertical && !isUndefinedHeight()) || (!getWidget().vertical && !isUndefinedWidth()))) {
                 expandRatio = 1;
                 hasExpandRatio.add(child);
             } else if (expandRatios.containsKey(pid)
@@ -153,6 +153,35 @@ PostLayoutListener {
             } else {
                 expandRatio = -1;
                 hasExpandRatio.remove(child);
+                if (expandRatios.getKeySet().size() > 0
+                        || hasExpandRatio.size() > 0) {
+                    // Only add resize listeners if there are expand ratios in
+                    // use
+                    getLayoutManager().addElementResizeListener(
+                            child.getWidget().getElement(),
+                            childComponentResizeListener);
+                    if (slot.hasCaption()) {
+                        getLayoutManager().addElementResizeListener(
+                                slot.getCaptionElement(),
+                                slotCaptionResizeListener);
+                    }
+                }
+            }
+            slot.setExpandRatio(expandRatio);
+
+            if (slot.getSpacingElement() != null && needsExpand()) {
+                getLayoutManager().addElementResizeListener(
+                        slot.getSpacingElement(), spacingResizeListener);
+            } else if (slot.getSpacingElement() != null) {
+                getLayoutManager().removeElementResizeListener(
+                        slot.getSpacingElement(), spacingResizeListener);
+            }
+
+        }
+
+        if (needsFixedHeight()) {
+            for (ComponentConnector child : getChildren()) {
+                Slot slot = layout.getSlot(child);
                 getLayoutManager().addElementResizeListener(
                         child.getWidget().getElement(),
                         childComponentResizeListener);
@@ -162,13 +191,6 @@ PostLayoutListener {
                                     slotCaptionResizeListener);
                 }
             }
-            slot.setExpandRatio(expandRatio);
-
-            if (slot.getSpacingElement() != null) {
-                getLayoutManager().addElementResizeListener(
-                        slot.getSpacingElement(), spacingResizeListener);
-            }
-
         }
 
         if (needsExpand()) {
@@ -353,7 +375,7 @@ PostLayoutListener {
                 getLayoutManager().addElementResizeListener(
                         slot.getSpacingElement(), spacingResizeListener);
             } else if (slot.getSpacingElement() != null) {
-                getLayoutManager().addElementResizeListener(
+                getLayoutManager().removeElementResizeListener(
                         slot.getSpacingElement(), spacingResizeListener);
             }
 
@@ -420,8 +442,10 @@ PostLayoutListener {
                             .getOuterHeight(captionElement));
                 }
             }
-            // System.out.println("  ###  SlotCaption sizes: "
-            // + childCaptionElementHeight.values().toString());
+            System.out.println("  ###  Child sizes: "
+                    + childElementHeight.values().toString());
+            System.out.println("  ###  Caption sizes: "
+                    + childCaptionElementHeight.values().toString());
 
             // If no height has been set, use the natural height for the
             // component (this is mostly just a precaution so that something
diff --git a/tests/testbench/com/vaadin/tests/components/orderedlayout/LayoutRenderTimeTest.java b/tests/testbench/com/vaadin/tests/components/orderedlayout/LayoutRenderTimeTest.java
new file mode 100644 (file)
index 0000000..871a467
--- /dev/null
@@ -0,0 +1,58 @@
+package com.vaadin.tests.components.orderedlayout;
+
+import com.vaadin.Application.LegacyApplication;
+import com.vaadin.terminal.ThemeResource;
+import com.vaadin.ui.Button;
+import com.vaadin.ui.Embedded;
+import com.vaadin.ui.HorizontalLayout;
+import com.vaadin.ui.Label;
+import com.vaadin.ui.Root;
+import com.vaadin.ui.VerticalLayout;
+import com.vaadin.ui.themes.Reindeer;
+
+public class LayoutRenderTimeTest extends LegacyApplication {
+
+    @Override
+    public void init() {
+        Root.LegacyWindow main = new Root.LegacyWindow();
+        setMainWindow(main);
+
+        VerticalLayout root = new VerticalLayout();
+        root.setWidth("100%");
+        main.setContent(root);
+
+        for (int i = 1; i <= 100; i++) {
+            root.addComponent(getRow(i));
+        }
+    }
+
+    private HorizontalLayout getRow(int i) {
+        HorizontalLayout row = new HorizontalLayout();
+        // row.setWidth("100%");
+        // row.setSpacing(true);
+
+        Embedded icon = new Embedded(null, new ThemeResource(
+                "../runo/icons/32/document.png"));
+        // row.addComponent(icon);
+        // row.setComponentAlignment(icon, Alignment.MIDDLE_LEFT);
+
+        Label text = new Label(
+                "Row content #"
+                        + i
+                        + ". In pellentesque faucibus vestibulum. Nulla at nulla justo, eget luctus tortor. Nulla facilisi. Duis aliquet.");
+        // row.addComponent(text);
+        // row.setExpandRatio(text, 1);
+
+        Button button = new Button("Edit");
+        button.addStyleName(Reindeer.BUTTON_SMALL);
+        row.addComponent(button);
+        // row.setComponentAlignment(button, Alignment.MIDDLE_LEFT);
+
+        button = new Button("Delete");
+        button.addStyleName(Reindeer.BUTTON_SMALL);
+        row.addComponent(button);
+        // row.setComponentAlignment(button, Alignment.MIDDLE_LEFT);
+
+        return row;
+    }
+}