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
|
package com.vaadin.terminal.gwt.client;
import java.util.ArrayList;
import java.util.Collection;
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.HasWidgets;
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 boolean isDirty = true;
private final Map<Element, int[]> dependencySizes = new HashMap<Element, int[]>();
public int getHeight() {
return height;
}
public int getWidth() {
return width;
}
public void setHeight(int height) {
if (this.height != height) {
isDirty = true;
}
this.height = height;
}
public void setWidth(int width) {
if (width != this.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 getDependencyWidth(Element e) {
return getDependencySize(e, 0);
}
public int getDependencyHeight(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];
}
}
}
private static MeasureManager instance = new MeasureManager();
public static Collection<VPaintableWidget> getChildren(
VPaintableWidgetContainer paintable, ApplicationConnection client) {
Widget widget = paintable.getWidgetForPaintable();
Collection<VPaintableWidget> children = new ArrayList<VPaintableWidget>();
addDescendantPaintables(widget, children, client);
return children;
}
private static void addDescendantPaintables(Widget widget,
Collection<VPaintableWidget> paintables,
ApplicationConnection client) {
if (widget instanceof HasWidgets) {
VPaintableMap paintableMap = client.getPaintableMap();
for (Widget child : (HasWidgets) widget) {
VPaintableWidget paintable = paintableMap.getPaintable(child);
if (paintable != null) {
paintables.add(paintable);
} else {
addDescendantPaintables(child, paintables, client);
}
}
}
}
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) {
Widget widget = paintableWidget.getWidgetForPaintable();
MeasureManager.MeasuredSize measuredSize = paintableMap
.getMeasuredSize(paintableWidget);
measuredSize.setWidth(widget.getOffsetWidth());
measuredSize.setHeight(widget.getOffsetHeight());
boolean dirtyDependency = false;
for (Entry<Element, int[]> entry : measuredSize.dependencySizes
.entrySet()) {
Element element = entry.getKey();
int[] sizes = entry.getValue();
int offsetWidth = element.getOffsetWidth();
if (offsetWidth != sizes[0]) {
sizes[0] = offsetWidth;
dirtyDependency = true;
}
int offsetHeight = element.getOffsetHeight();
if (offsetHeight != sizes[1]) {
sizes[1] = offsetHeight;
dirtyDependency = true;
}
}
if (dirtyDependency) {
measuredSize.setDirty(true);
}
if (measuredSize.isDirty()) {
changed.add(paintableMap.getPid(paintableWidget));
measuredSize.setDirty(false);
}
}
return changed;
}
private MeasureManager() {
// Singleton constructor
}
public static MeasureManager get() {
return instance;
}
}
|