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

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

Commit 18739f8

Browse files
committed
Fix a crash when nesting a ProxyViewContainer in FlexboxLayout.
1 parent e0b0e46 commit 18739f8

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
@@ -1975,6 +1975,20 @@ export const testWrap_childMargin_vertical = test(
19751975
}
19761976
);
19771977

1978+
let activity_flexbox_with_proxy_view_container = () => getViews(
1979+
`<FlexboxLayout id="flexbox">
1980+
<ProxyViewContainer></ProxyViewContainer>
1981+
</FlexboxLayout>`
1982+
);
1983+
1984+
export const testFlexboxLayout_does_not_crash_with_proxy_view_container = test(
1985+
activity_flexbox_with_proxy_view_container,
1986+
noop,
1987+
({root, flexbox}) => {
1988+
TKUnit.assert(flexbox.id === "flexbox", "FlexboxLayout actually there");
1989+
}
1990+
);
1991+
19781992
// Omit testEmptyChildren
19791993
// Omit testDivider_directionRow_verticalBeginning
19801994

‎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
@@ -67,6 +67,28 @@ import makeMeasureSpec = utils.layout.makeMeasureSpec;
6767
import getMeasureSpecMode = utils.layout.getMeasureSpecMode;
6868
import getMeasureSpecSize = utils.layout.getMeasureSpecSize;
6969

70+
// `eachLayoutChild` iterates over children, and we need more - indexed access.
71+
// This class tries to accomodate that by collecting all children in an
72+
// array no more than once per measure.
73+
class MeasureContext {
74+
private children: View[];
75+
76+
constructor(private owner: FlexboxLayout) {
77+
this.children = [];
78+
this.owner.eachLayoutChild((child) => {
79+
this.children.push(child);
80+
});
81+
}
82+
83+
public get childrenCount(): number {
84+
return this.children.length;
85+
}
86+
87+
public childAt(index: number): View {
88+
return this.children[index];
89+
}
90+
}
91+
7092
class FlexLine {
7193

7294
_left: number = Number.MAX_VALUE;
@@ -117,6 +139,7 @@ export class FlexboxLayout extends FlexboxLayoutBase {
117139
private _orderCache: number[];
118140
private _flexLines: FlexLine[] = [];
119141
private _childrenFrozen: boolean[];
142+
private measureContext: MeasureContext;
120143

121144
_setNativeFlexDirection(flexDirection: FlexDirection) {
122145
// lint happy no-op
@@ -141,15 +164,16 @@ export class FlexboxLayout extends FlexboxLayoutBase {
141164
}
142165

143166
public onMeasure(widthMeasureSpec: number, heightMeasureSpec: number): void {
167+
this.measureContext = new MeasureContext(this);
144168
LayoutBase.adjustChildrenLayoutParams(this, widthMeasureSpec, heightMeasureSpec);
145169

146170
// Omit: super.onMeasure(widthMeasureSpec, heightMeasureSpec);
147171

148172
if (this._isOrderChangedFromLastMeasurement) {
149173
this._reorderedIndices = this._createReorderedIndices();
150174
}
151-
if (!this._childrenFrozen || this._childrenFrozen.length < this.getChildrenCount()) {
152-
this._childrenFrozen = new Array(this.getChildrenCount());
175+
if (!this._childrenFrozen || this._childrenFrozen.length < this.measureContext.childrenCount) {
176+
this._childrenFrozen = new Array(this.measureContext.childrenCount);
153177
}
154178

155179
switch (this.flexDirection) {
@@ -174,13 +198,13 @@ export class FlexboxLayout extends FlexboxLayoutBase {
174198
child = null;
175199
} else {
176200
let reorderedIndex = this._reorderedIndices[index];
177-
child = this.getChildAt(reorderedIndex);
201+
child = this.measureContext.childAt(reorderedIndex);
178202
}
179203
return child;
180204
}
181205

182206
private _createReorderedIndices(): number[] {
183-
let childCount = this.getChildrenCount();
207+
let childCount = this.measureContext.childrenCount;
184208
let orders = this._createOrders(childCount);
185209
return this._sortOrdersIntoReorderedIndices(childCount, orders);
186210
}
@@ -203,7 +227,7 @@ export class FlexboxLayout extends FlexboxLayoutBase {
203227
private _createOrders(childCount: number): Order[] {
204228
let orders: Order[] = [];
205229
for (let i = 0; i < childCount; i++) {
206-
let child = this.getChildAt(i);
230+
let child = this.measureContext.childAt(i);
207231
let order = new Order();
208232
order.order = FlexboxLayout.getOrder(child);
209233
order.index = i;
@@ -213,15 +237,15 @@ export class FlexboxLayout extends FlexboxLayoutBase {
213237
}
214238

215239
private get _isOrderChangedFromLastMeasurement(): boolean {
216-
let childCount = this.getChildrenCount();
240+
let childCount = this.measureContext.childrenCount;
217241
if (!this._orderCache) {
218242
this._orderCache = [];
219243
}
220244
if (this._orderCache.length !== childCount) {
221245
return true;
222246
}
223247
for (let i = 0; i < childCount; i++) {
224-
let view = this.getChildAt(i);
248+
let view = this.measureContext.childAt(i);
225249
if (view === null) {
226250
continue;
227251
}
@@ -241,7 +265,7 @@ export class FlexboxLayout extends FlexboxLayoutBase {
241265
this._flexLines.length = 0;
242266

243267
(() => {
244-
let childCount = this.getChildrenCount();
268+
let childCount = this.measureContext.childrenCount;
245269
let paddingStart = FlexboxLayout.getPaddingStart(this);
246270
let paddingEnd = FlexboxLayout.getPaddingEnd(this);
247271
let largestHeightInRow = Number.MIN_VALUE;
@@ -355,7 +379,7 @@ export class FlexboxLayout extends FlexboxLayoutBase {
355379

356380
this._flexLines.length = 0;
357381

358-
let childCount = this.getChildrenCount();
382+
let childCount = this.measureContext.childrenCount;
359383
let paddingTop = this.paddingTop;
360384
let paddingBottom = this.paddingBottom;
361385
let largestWidthInColumn = Number.MIN_VALUE;

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL