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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
package com.vaadin.terminal.gwt.client;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import com.google.gwt.core.client.JsArrayString;
import com.google.gwt.dom.client.Element;
import com.google.gwt.user.client.ui.RequiresResize;
import com.google.gwt.user.client.ui.Widget;
public class MeasureManager {
public static final class MeasuredSize {
private int width = -1;
private int height = -1;
private int[] paddings = new int[4];
private int[] borders = new int[4];
private int[] margins = new int[4];
private final VPaintableWidget paintable;
private boolean isDirty = true;
private final Map<Element, int[]> dependencySizes = new HashMap<Element, int[]>();
public MeasuredSize(VPaintableWidget paintable) {
this.paintable = paintable;
}
public int getOuterHeight() {
return height;
}
public int getOuterWidth() {
return width;
}
private static int sumWidths(int[] sizes) {
return sizes[1] + sizes[3];
}
private static int sumHeights(int[] sizes) {
return sizes[0] + sizes[2];
}
public int getInnerHeight() {
return height - sumHeights(margins) - sumHeights(borders)
- sumHeights(paddings);
}
public int getInnerWidth() {
return width - sumWidths(margins) - sumWidths(borders)
- sumWidths(paddings);
}
public void setOuterHeight(int height) {
if (this.height != height) {
isDirty = true;
this.height = height;
}
}
public void setOuterWidth(int width) {
if (this.width != width) {
isDirty = true;
this.width = width;
}
}
public boolean isDirty() {
return isDirty;
}
public void setDirty(boolean isDirty) {
this.isDirty = isDirty;
}
public void registerDependency(Element element) {
if (!dependencySizes.containsKey(element)) {
dependencySizes.put(element, new int[] { -1, -1 });
isDirty = true;
}
}
public void deRegisterDependency(Element element) {
dependencySizes.remove(element);
}
public int getDependencyOuterWidth(Element e) {
return getDependencySize(e, 0);
}
public int getDependencyOuterHeight(Element e) {
return getDependencySize(e, 1);
}
private int getDependencySize(Element e, int index) {
int[] sizes = dependencySizes.get(e);
if (sizes == null) {
return -1;
} else {
return sizes[index];
}
}
public int getBorderHeight() {
return sumHeights(borders);
}
public int getBorderWidth() {
return sumWidths(borders);
}
public int getPaddingHeight() {
return sumHeights(paddings);
}
public int getPaddingWidth() {
return sumWidths(paddings);
}
public int getMarginHeight() {
return sumHeights(margins);
}
public int getMarginWidth() {
return sumWidths(margins);
}
public int getMarginTop() {
return margins[0];
}
public int getMarginRight() {
return margins[1];
}
public int getMarginBottom() {
return margins[2];
}
public int getMarginLeft() {
return margins[3];
}
public int getBorderTop() {
return margins[0];
}
public int getBorderRight() {
return margins[1];
}
public int getBorderBottom() {
return margins[2];
}
public int getBorderLeft() {
return margins[3];
}
public int getPaddingTop() {
return paddings[0];
}
public int getPaddingRight() {
return paddings[1];
}
public int getPaddingBottom() {
return paddings[2];
}
public int getPaddingLeft() {
return paddings[3];
}
private void measure() {
boolean changed = isDirty;
Widget widget = paintable.getWidgetForPaintable();
ComputedStyle computedStyle = new ComputedStyle(widget.getElement());
int[] paddings = computedStyle.getPadding();
if (!changed && !Arrays.equals(this.paddings, paddings)) {
changed = true;
this.paddings = paddings;
}
int[] margins = computedStyle.getMargin();
if (!changed && !Arrays.equals(this.margins, margins)) {
changed = true;
this.margins = margins;
}
int[] borders = computedStyle.getBorder();
if (!changed && !Arrays.equals(this.borders, borders)) {
changed = true;
this.borders = borders;
}
int offsetHeight = widget.getOffsetHeight();
int marginHeight = sumHeights(margins);
setOuterHeight(offsetHeight + marginHeight);
int offsetWidth = widget.getOffsetWidth();
int marginWidth = sumWidths(margins);
setOuterWidth(offsetWidth + marginWidth);
// int i = 0;
for (Entry<Element, int[]> entry : dependencySizes.entrySet()) {
Element element = entry.getKey();
// int[] elementMargin = new ComputedStyle(element).getMargin();
int[] sizes = entry.getValue();
int elementWidth = element.getOffsetWidth();
// elementWidth += elementMargin[1] + elementMargin[3];
if (elementWidth != sizes[0]) {
// System.out.println(paintable.getId() + " dependency " + i
// + " width changed from " + sizes[0] + " to "
// + elementWidth);
sizes[0] = elementWidth;
changed = true;
}
int elementHeight = element.getOffsetHeight();
// Causes infinite loops as a negative margin based on the
// measured height is currently used for captions
// elementHeight += elementMargin[0] + elementMargin[1];
if (elementHeight != sizes[1]) {
// System.out.println(paintable.getId() + " dependency " + i
// + " height changed from " + sizes[1] + " to "
// + elementHeight);
sizes[1] = elementHeight;
changed = true;
}
// i++;
}
if (changed) {
setDirty(true);
}
}
}
public void doLayout(ApplicationConnection client) {
VPaintableMap paintableMap = client.getPaintableMap();
VPaintableWidget[] paintableWidgets = paintableMap
.getRegisteredPaintableWidgets();
int passes = 0;
long start = System.currentTimeMillis();
while (true) {
long passStart = System.currentTimeMillis();
passes++;
long measureStart = System.currentTimeMillis();
FastStringSet changedSet = findChangedWidgets(paintableWidgets,
paintableMap);
JsArrayString changed = changedSet.dump();
long measureEnd = System.currentTimeMillis();
VConsole.log("Measure in " + (measureEnd - measureStart) + " ms");
if (changed.length() == 0) {
VConsole.log("No more changes in pass " + passes);
break;
}
if (passes > 100) {
VConsole.log("Aborting layout");
break;
}
FastStringSet affectedContainers = FastStringSet.create();
for (int i = 0; i < changed.length(); i++) {
VPaintableWidget paintable = (VPaintableWidget) paintableMap
.getPaintable(changed.get(i));
VPaintableWidget parentPaintable = paintable.getParent();
if (parentPaintable instanceof CalculatingLayout) {
affectedContainers
.add(paintableMap.getPid(parentPaintable));
}
}
long layoutStart = System.currentTimeMillis();
for (int i = 0; i < changed.length(); i++) {
String pid = changed.get(i);
VPaintableWidget paintable = (VPaintableWidget) paintableMap
.getPaintable(pid);
if (!affectedContainers.contains(pid)) {
Widget widget = paintable.getWidgetForPaintable();
if (widget instanceof RequiresResize) {
// TODO Do nothing here if parent instanceof
// ProvidesRepaint?
((RequiresResize) widget).onResize();
} else if (paintable instanceof CalculatingLayout) {
CalculatingLayout calculating = (CalculatingLayout) paintable;
calculating.updateHorizontalSizes();
calculating.updateVerticalSizes();
}
}
}
JsArrayString affectedPids = affectedContainers.dump();
for (int i = 0; i < affectedPids.length(); i++) {
// Find all changed children
String containerPid = affectedPids.get(i);
CalculatingLayout container = (CalculatingLayout) paintableMap
.getPaintable(containerPid);
container.updateHorizontalSizes();
container.updateVerticalSizes();
}
long layoutEnd = System.currentTimeMillis();
VConsole.log(affectedPids.length()
+ " requestLayout invocations in "
+ (layoutEnd - layoutStart) + "ms");
long passEnd = System.currentTimeMillis();
StringBuilder b = new StringBuilder();
b.append(changed.length());
b.append(" changed widgets in pass ");
b.append(passes);
b.append(" in ");
b.append((passEnd - passStart));
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());
}
long end = System.currentTimeMillis();
VConsole.log("Total layout time: " + (end - start) + "ms");
}
private FastStringSet findChangedWidgets(
VPaintableWidget[] paintableWidgets, VPaintableMap paintableMap) {
FastStringSet changed = FastStringSet.create();
for (VPaintableWidget paintableWidget : paintableWidgets) {
MeasureManager.MeasuredSize measuredSize = paintableWidget
.getMeasuredSize();
measuredSize.measure();
if (measuredSize.isDirty()) {
changed.add(paintableMap.getPid(paintableWidget));
measuredSize.setDirty(false);
}
}
return changed;
}
}
|