FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Fix a crash when nesting a ProxyViewContainer in FlexboxLayout. · NativeScript/NativeScript@a03ce20 · GitHub

Commit a03ce20

Browse files
committed
Fix a crash when nesting a ProxyViewContainer in FlexboxLayout.
Conflicts: tns-core-modules/ui/layouts/flexbox-layout/flexbox-layout.ios.ts
1 parent ce72b60 commit a03ce20

2 files changed

Lines changed: 47 additions & 9 deletions

File tree

‎tests/app/ui/layouts/flexbox-layout-tests.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1911,6 +1911,20 @@ export const testWrap_childMargin_vertical = test(
19111911
}
19121912
);
19131913

1914+
let activity_flexbox_with_proxy_view_container = () => getViews(
1915+
`<FlexboxLayout id="flexbox">
1916+
<ProxyViewContainer></ProxyViewContainer>
1917+
</FlexboxLayout>`
1918+
);
1919+
1920+
export const testFlexboxLayout_does_not_crash_with_proxy_view_container = test(
1921+
activity_flexbox_with_proxy_view_container,
1922+
noop,
1923+
({root, flexbox}) => {
1924+
TKUnit.assert(flexbox.id === "flexbox", "FlexboxLayout actually there");
1925+
}
1926+
);
1927+
19141928
// Omit testEmptyChildren
19151929
// Omit testDivider_directionRow_verticalBeginning
19161930

‎tns-core-modules/ui/layouts/flexbox-layout/flexbox-layout.ios.ts‎

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,28 @@ import makeMeasureSpec = layout.makeMeasureSpec;
9494
import getMeasureSpecMode = layout.getMeasureSpecMode;
9595
import getMeasureSpecSize = layout.getMeasureSpecSize;
9696

97+
// `eachLayoutChild` iterates over children, and we need more - indexed access.
98+
// This class tries to accomodate that by collecting all children in an
99+
// array no more than once per measure.
100+
class MeasureContext {
101+
private children: View[];
102+
103+
constructor(private owner: FlexboxLayout) {
104+
this.children = [];
105+
this.owner.eachLayoutChild((child) => {
106+
this.children.push(child);
107+
});
108+
}
109+
110+
public get childrenCount(): number {
111+
return this.children.length;
112+
}
113+
114+
public childAt(index: number): View {
115+
return this.children[index];
116+
}
117+
}
118+
97119
class FlexLine {
98120

99121
_left: number = Number.MAX_VALUE;
@@ -144,15 +166,17 @@ export class FlexboxLayout extends FlexboxLayoutBase {
144166
private _orderCache: number[];
145167
private _flexLines: FlexLine[] = [];
146168
private _childrenFrozen: boolean[];
169+
private measureContext: MeasureContext;
147170

148171
public onMeasure(widthMeasureSpec: number, heightMeasureSpec: number): void {
172+
this.measureContext = new MeasureContext(this);
149173
// Omit: super.onMeasure(widthMeasureSpec, heightMeasureSpec);
150174

151175
if (this._isOrderChangedFromLastMeasurement) {
152176
this._reorderedIndices = this._createReorderedIndices();
153177
}
154-
if (!this._childrenFrozen || this._childrenFrozen.length < this.getChildrenCount()) {
155-
this._childrenFrozen = new Array(this.getChildrenCount());
178+
if (!this._childrenFrozen || this._childrenFrozen.length < this.measureContext.childrenCount) {
179+
this._childrenFrozen = new Array(this.measureContext.childrenCount);
156180
}
157181

158182
switch (this.flexDirection) {
@@ -177,13 +201,13 @@ export class FlexboxLayout extends FlexboxLayoutBase {
177201
child = null;
178202
} else {
179203
let reorderedIndex = this._reorderedIndices[index];
180-
child = this.getChildAt(reorderedIndex);
204+
child = this.measureContext.childAt(reorderedIndex);
181205
}
182206
return child;
183207
}
184208

185209
private _createReorderedIndices(): number[] {
186-
let childCount = this.getChildrenCount();
210+
let childCount = this.measureContext.childrenCount;
187211
let orders = this._createOrders(childCount);
188212
return this._sortOrdersIntoReorderedIndices(childCount, orders);
189213
}
@@ -206,7 +230,7 @@ export class FlexboxLayout extends FlexboxLayoutBase {
206230
private _createOrders(childCount: number): Order[] {
207231
let orders: Order[] = [];
208232
for (let i = 0; i < childCount; i++) {
209-
let child = this.getChildAt(i);
233+
let child = this.measureContext.childAt(i);
210234
let order = new Order();
211235
order.order = FlexboxLayout.getOrder(child);
212236
order.index = i;
@@ -216,15 +240,15 @@ export class FlexboxLayout extends FlexboxLayoutBase {
216240
}
217241

218242
private get _isOrderChangedFromLastMeasurement(): boolean {
219-
let childCount = this.getChildrenCount();
243+
let childCount = this.measureContext.childrenCount;
220244
if (!this._orderCache) {
221245
this._orderCache = [];
222246
}
223247
if (this._orderCache.length !== childCount) {
224248
return true;
225249
}
226250
for (let i = 0; i < childCount; i++) {
227-
let view = this.getChildAt(i);
251+
let view = this.measureContext.childAt(i);
228252
if (view === null) {
229253
continue;
230254
}
@@ -244,7 +268,7 @@ export class FlexboxLayout extends FlexboxLayoutBase {
244268
this._flexLines.length = 0;
245269

246270
(() => {
247-
let childCount = this.getChildrenCount();
271+
let childCount = this.measureContext.childrenCount;
248272
let paddingStart = FlexboxLayout.getPaddingStart(this);
249273
let paddingEnd = FlexboxLayout.getPaddingEnd(this);
250274
let largestHeightInRow = Number.MIN_VALUE;
@@ -359,7 +383,7 @@ export class FlexboxLayout extends FlexboxLayoutBase {
359383

360384
this._flexLines.length = 0;
361385

362-
let childCount = this.getChildrenCount();
386+
let childCount = this.measureContext.childrenCount;
363387
let paddingTop = this.effectivePaddingTop;
364388
let paddingBottom = this.effectivePaddingBottom;
365389
let largestWidthInColumn = Number.MIN_VALUE;

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL