summaryrefslogtreecommitdiffstats
path: root/src/com/vaadin/terminal/gwt/client/LayoutManager.java
blob: f2117dc51339bfaa11b74da3b8b8e1ac8cdddbc9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/* 
@VaadinApache2LicenseForJavaFiles@
 */
package com.vaadin.terminal.gwt.client;

import java.util.HashSet;
import java.util.Set;

import com.google.gwt.core.client.Duration;
import com.google.gwt.core.client.JsArrayString;
import com.google.gwt.dom.client.Element;
import com.vaadin.terminal.gwt.client.ui.ManagedLayout;
import com.vaadin.terminal.gwt.client.ui.PostLayoutListener;
import com.vaadin.terminal.gwt.client.ui.SimpleManagedLayout;

public class LayoutManager {
    private final ApplicationConnection connection;
    private final Set<Element> nonPaintableElements = new HashSet<Element>();
    private final MeasuredSize nullSize = new MeasuredSize();

    public LayoutManager(ApplicationConnection connection) {
        this.connection = connection;
    }

    public static LayoutManager get(ApplicationConnection connection) {
        return connection.getLayoutManager();
    }

    public void registerDependency(ManagedLayout owner, Element element) {
        MeasuredSize measuredSize = ensureMeasured(element);

        MeasuredSize ownerSize = getMeasuredSize(owner);
        if (measuredSize.isHeightNeedsUpdate()) {
            ownerSize.setHeightNeedsUpdate();
        }
        if (measuredSize.isWidthNeedsUpdate()) {
            ownerSize.setWidthNeedsUpdate();
        }
        measuredSize.addDependent(owner.getId());
    }

    private MeasuredSize ensureMeasured(Element element) {
        MeasuredSize measuredSize = getMeasuredSize(element, null);
        if (measuredSize == null) {
            measuredSize = new MeasuredSize();

            if (VPaintableMap.get(connection).getPaintable(element) == null) {
                nonPaintableElements.add(element);
            }
            setMeasuredSize(element, measuredSize);
        }
        return measuredSize;
    }

    private boolean needsMeasure(Element e) {
        if (connection.getPaintableMap().getPid(e) != null) {
            return true;
        } else if (getMeasuredSize(e, nullSize).hasDependents()) {
            return true;
        } else {
            return false;
        }
    }

    private static native void setMeasuredSize(Element element,
            MeasuredSize measuredSize)
    /*-{
        if (measuredSize) {
            element.vMeasuredSize = measuredSize;
        } else {
            delete element.vMeasuredSize;
        }
    }-*/;

    private static native final MeasuredSize getMeasuredSize(Element element,
            MeasuredSize defaultSize)
    /*-{
        return element.vMeasuredSize || defaultSize;
    }-*/;

    private static final MeasuredSize getMeasuredSize(VPaintableWidget paintable) {
        Element element = paintable.getWidgetForPaintable().getElement();
        MeasuredSize measuredSize = getMeasuredSize(element, null);
        if (measuredSize == null) {
            measuredSize = new MeasuredSize();
            setMeasuredSize(element, measuredSize);
        }
        return measuredSize;
    }

    public void unregisterDependency(ManagedLayout owner, Element element) {
        MeasuredSize measuredSize = getMeasuredSize(element, null);
        if (measuredSize == null) {
            return;
        }
        measuredSize.removeDependent(owner.getId());
        if (!needsMeasure(element)) {
            nonPaintableElements.remove(element);
            setMeasuredSize(element, null);
        }
    }

    public void doLayout() {
        VPaintableMap paintableMap = connection.getPaintableMap();
        VPaintableWidget[] paintableWidgets = paintableMap
                .getRegisteredPaintableWidgets();

        int passes = 0;
        Duration totalDuration = new Duration();

        while (true) {
            Duration passDuration = new Duration();
            passes++;
            measureElements(paintableWidgets);

            FastStringSet needsHeightUpdate = FastStringSet.create();
            FastStringSet needsWidthUpdate = FastStringSet.create();

            for (VPaintableWidget paintable : paintableWidgets) {
                MeasuredSize measuredSize = getMeasuredSize(paintable);
                boolean managed = isManagedLayout(paintable);

                VPaintableWidgetContainer parent = paintable.getParent();
                boolean managedParent = parent != null
                        && isManagedLayout(parent);

                if (measuredSize.isHeightNeedsUpdate()) {
                    if (managed) {
                        needsHeightUpdate.add(paintable.getId());
                    }
                    if (!paintable.isRelativeHeight() && managedParent) {
                        needsHeightUpdate.add(parent.getId());
                    }
                }
                if (measuredSize.isWidthNeedsUpdate()) {
                    if (managed) {
                        needsWidthUpdate.add(paintable.getId());
                    }
                    if (!paintable.isRelativeWidth() && managedParent) {
                        needsWidthUpdate.add(parent.getId());
                    }
                }
                measuredSize.clearDirtyState();
            }

            int measureTime = passDuration.elapsedMillis();
            VConsole.log("Measure in " + measureTime + " ms");

            FastStringSet updatedSet = FastStringSet.create();

            JsArrayString needsWidthUpdateArray = needsWidthUpdate.dump();

            for (int i = 0; i < needsWidthUpdateArray.length(); i++) {
                String pid = needsWidthUpdateArray.get(i);

                VPaintable paintable = paintableMap.getPaintable(pid);
                if (paintable instanceof DirectionalManagedLayout) {
                    DirectionalManagedLayout cl = (DirectionalManagedLayout) paintable;
                    cl.layoutHorizontally();
                } else if (paintable instanceof SimpleManagedLayout) {
                    SimpleManagedLayout rr = (SimpleManagedLayout) paintable;
                    rr.layout();
                    needsHeightUpdate.remove(pid);
                }
                updatedSet.add(pid);
            }

            JsArrayString needsHeightUpdateArray = needsHeightUpdate.dump();
            for (int i = 0; i < needsHeightUpdateArray.length(); i++) {
                String pid = needsHeightUpdateArray.get(i);

                VPaintableWidget paintable = (VPaintableWidget) paintableMap
                        .getPaintable(pid);
                if (paintable instanceof DirectionalManagedLayout) {
                    DirectionalManagedLayout cl = (DirectionalManagedLayout) paintable;
                    cl.layoutVertically();
                } else if (paintable instanceof SimpleManagedLayout) {
                    SimpleManagedLayout rr = (SimpleManagedLayout) paintable;
                    rr.layout();
                }
                updatedSet.add(pid);
            }

            JsArrayString changed = updatedSet.dump();
            VConsole.log(changed.length() + " requestLayout invocations in "
                    + (passDuration.elapsedMillis() - measureTime) + "ms");

            StringBuilder b = new StringBuilder();
            b.append(changed.length());
            b.append(" changed widgets in pass ");
            b.append(passes);
            b.append(" in ");
            b.append(passDuration.elapsedMillis());
            b.append(" ms: ");
            if (changed.length() < 10) {
                for (int i = 0; i < changed.length(); i++) {
                    if (i != 0) {
                        b.append(", ");
                    }
                    b.append(changed.get(i));
                }
            }
            VConsole.log(b.toString());

            if (changed.length() == 0) {
                VConsole.log("No more changes in pass " + passes);
                break;
            }

            if (passes > 100) {
                VConsole.log("Aborting layout");
                break;
            }
        }

        for (VPaintableWidget vPaintableWidget : paintableWidgets) {
            if (vPaintableWidget instanceof PostLayoutListener) {
                ((PostLayoutListener) vPaintableWidget).postLayout();
            }
        }

        VConsole.log("Total layout time: " + totalDuration.elapsedMillis()
                + "ms");
    }

    private void measureElements(VPaintableWidget[] paintableWidgets) {

        for (VPaintableWidget paintableWidget : paintableWidgets) {
            Element element = paintableWidget.getWidgetForPaintable()
                    .getElement();
            MeasuredSize measuredSize = getMeasuredSize(paintableWidget);
            measuredAndUpdate(element, measuredSize);
        }

        for (Element element : nonPaintableElements) {
            MeasuredSize measuredSize = getMeasuredSize(element, null);
            measuredAndUpdate(element, measuredSize);
        }
    }

    private void measuredAndUpdate(Element element, MeasuredSize measuredSize) {
        if (measuredSize.measure(element)) {
            JsArrayString dependents = measuredSize.getDependents();
            for (int i = 0; i < dependents.length(); i++) {
                String pid = dependents.get(i);
                VPaintableWidget dependent = (VPaintableWidget) connection
                        .getPaintableMap().getPaintable(pid);
                if (dependent != null) {
                    MeasuredSize dependentSize = getMeasuredSize(dependent);
                    if (measuredSize.isHeightNeedsUpdate()) {
                        dependentSize.setHeightNeedsUpdate();
                    }
                    if (measuredSize.isWidthNeedsUpdate()) {
                        dependentSize.setWidthNeedsUpdate();
                    }
                }
            }
        }
    }

    private static boolean isManagedLayout(VPaintableWidget paintable) {
        return paintable instanceof ManagedLayout;
    }

    public void foceLayout() {
        VPaintableMap paintableMap = connection.getPaintableMap();
        VPaintableWidget[] paintableWidgets = paintableMap
                .getRegisteredPaintableWidgets();
        for (VPaintableWidget vPaintableWidget : paintableWidgets) {
            MeasuredSize measuredSize = getMeasuredSize(vPaintableWidget);
            measuredSize.setHeightNeedsUpdate();
            measuredSize.setWidthNeedsUpdate();
        }
        doLayout();
    }

    public final void setNeedsUpdate(ManagedLayout layout) {
        setWidthNeedsUpdate(layout);
        setHeightNeedsUpdate(layout);
    }

    public final void setWidthNeedsUpdate(ManagedLayout layout) {
        getMeasuredSize(layout).setWidthNeedsUpdate();
    }

    public final void setHeightNeedsUpdate(ManagedLayout layout) {
        getMeasuredSize(layout).setHeightNeedsUpdate();
    }

    public boolean isMeasured(Element element) {
        return getMeasuredSize(element, nullSize) != nullSize;
    }

    public final int getOuterHeight(Element element) {
        return getMeasuredSize(element, nullSize).getOuterHeight();
    }

    public final int getOuterWidth(Element element) {
        return getMeasuredSize(element, nullSize).getOuterWidth();
    }

    public final int getInnerHeight(Element element) {
        return getMeasuredSize(element, nullSize).getInnerHeight();
    }

    public final int getInnerWidth(Element element) {
        return getMeasuredSize(element, nullSize).getInnerWidth();
    }

    public final int getBorderHeight(Element element) {
        return getMeasuredSize(element, nullSize).getBorderHeight();
    }

    public int getPaddingHeight(Element element) {
        return getMeasuredSize(element, nullSize).getPaddingHeight();
    }

    public int getBorderWidth(Element element) {
        return getMeasuredSize(element, nullSize).getBorderWidth();
    }

    public int getPaddingWidth(Element element) {
        return getMeasuredSize(element, nullSize).getPaddingWidth();
    }

    public int getPaddingTop(Element element) {
        return getMeasuredSize(element, nullSize).getPaddingTop();
    }

    public int getPaddingLeft(Element element) {
        return getMeasuredSize(element, nullSize).getPaddingLeft();
    }
}