8001856de8c9183bd043b55d25eb3fa219228ef0.svn-base
9.64 KB
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
/**
* Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
* @format
*/
const CONSTANTS = require('./YGEnums');
import type {
Yoga$Edge,
Yoga$FlexWrap,
Yoga$Align,
Yoga$FlexDirection,
Yoga$Direction,
Yoga$PositionType,
Yoga$Overflow,
Yoga$JustifyContent,
Yoga$Display,
Yoga$ExperimentalFeature,
} from './YGEnums';
class Layout {
left: number;
right: number;
top: number;
bottom: number;
width: number;
height: number;
constructor(left, right, top, bottom, width, height) {
this.left = left;
this.right = right;
this.top = top;
this.bottom = bottom;
this.width = width;
this.height = height;
}
fromJS(expose) {
expose(
this.left,
this.right,
this.top,
this.bottom,
this.width,
this.height,
);
}
toString() {
return `<Layout#${this.left}:${this.right};${this.top}:${this.bottom};${
this.width
}:${this.height}>`;
}
}
class Size {
static fromJS({width, height}) {
return new Size(width, height);
}
width: number;
height: number;
constructor(width, height) {
this.width = width;
this.height = height;
}
fromJS(expose) {
expose(this.width, this.height);
}
toString() {
return `<Size#${this.width}x${this.height}>`;
}
}
class Value {
unit: number;
value: number;
constructor(unit, value) {
this.unit = unit;
this.value = value;
}
fromJS(expose) {
expose(this.unit, this.value);
}
toString() {
switch (this.unit) {
case CONSTANTS.UNIT_POINT:
return String(this.value);
case CONSTANTS.UNIT_PERCENT:
return `${this.value}%`;
case CONSTANTS.UNIT_AUTO:
return 'auto';
default: {
return `${this.value}?`;
}
}
}
valueOf() {
return this.value;
}
}
export type Yoga$Config = {
isExperimentalFeatureEnabled(feature: Yoga$ExperimentalFeature): boolean,
setExperimentalFeatureEnabled(
feature: Yoga$ExperimentalFeature,
enabled: boolean,
): void,
setPointScaleFactor(factor: number): void,
};
export type Yoga$Node = {
calculateLayout(
width?: number,
height?: number,
direction?: Yoga$Direction,
): void,
copyStyle(node: Yoga$Node): void,
free(): void,
freeRecursive(): void,
getAlignContent(): Yoga$Align,
getAlignItems(): Yoga$Align,
getAlignSelf(): Yoga$Align,
getAspectRatio(): number,
getBorder(edge: Yoga$Edge): number,
getChild(index: number): Yoga$Node,
getChildCount(): number,
getComputedBorder(edge: Yoga$Edge): number,
getComputedBottom(): number,
getComputedHeight(): number,
getComputedLayout(): Layout,
getComputedLeft(): number,
getComputedMargin(edge: Yoga$Edge): number,
getComputedPadding(edge: Yoga$Edge): number,
getComputedRight(): number,
getComputedTop(): number,
getComputedWidth(): number,
getDisplay(): Yoga$Display,
getFlexBasis(): number,
getFlexDirection(): Yoga$FlexDirection,
getFlexGrow(): number,
getFlexShrink(): number,
getFlexWrap(): Yoga$FlexWrap,
getHeight(): Value,
getJustifyContent(): Yoga$JustifyContent,
getMargin(edge: Yoga$Edge): Value,
getMaxHeight(): Value,
getMaxWidth(): Value,
getMinHeight(): Value,
getMinWidth(): Value,
getOverflow(): Yoga$Overflow,
getPadding(edge: Yoga$Edge): Value,
getParent(): ?Yoga$Node,
getPosition(edge: Yoga$Edge): Value,
getPositionType(): Yoga$PositionType,
getWidth(): Value,
insertChild(child: Yoga$Node, index: number): void,
isDirty(): boolean,
markDirty(): void,
removeChild(child: Yoga$Node): void,
reset(): void,
setAlignContent(alignContent: Yoga$Align): void,
setAlignItems(alignItems: Yoga$Align): void,
setAlignSelf(alignSelf: Yoga$Align): void,
setAspectRatio(aspectRatio: number): void,
setBorder(edge: Yoga$Edge, borderWidth: number): void,
setDisplay(display: Yoga$Display): void,
setFlex(flex: number): void,
setFlexBasis(flexBasis: number | string): void,
setFlexBasisPercent(flexBasis: number): void,
setFlexDirection(flexDirection: Yoga$FlexDirection): void,
setFlexGrow(flexGrow: number): void,
setFlexShrink(flexShrink: number): void,
setFlexWrap(flexWrap: Yoga$FlexWrap): void,
setHeight(height: number | string): void,
setHeightAuto(): void,
setHeightPercent(height: number): void,
setJustifyContent(justifyContent: Yoga$JustifyContent): void,
setMargin(edge: Yoga$Edge, margin: number): void,
setMarginAuto(edge: Yoga$Edge): void,
setMarginPercent(edge: Yoga$Edge, margin: number): void,
setMaxHeight(maxHeight: number | string): void,
setMaxHeightPercent(maxHeight: number): void,
setMaxWidth(maxWidth: number | string): void,
setMaxWidthPercent(maxWidth: number): void,
setMeasureFunc(measureFunc: ?Function): void,
setMinHeight(minHeight: number | string): void,
setMinHeightPercent(minHeight: number): void,
setMinWidth(minWidth: number | string): void,
setMinWidthPercent(minWidth: number): void,
setOverflow(overflow: Yoga$Overflow): void,
setPadding(edge: Yoga$Edge, padding: number | string): void,
setPaddingPercent(edge: Yoga$Edge, padding: number): void,
setPosition(edge: Yoga$Edge, position: number | string): void,
setPositionPercent(edge: Yoga$Edge, position: number): void,
setPositionType(positionType: Yoga$PositionType): void,
setWidth(width: number | string): void,
setWidthAuto(): void,
setWidthPercent(width: number): void,
unsetMeasureFun(): void,
};
type Yoga = {
Config: {
create(): Yoga$Config,
destroy(config: Yoga$Config): any,
},
Node: {
create(): Yoga$Node,
createDefault(): Yoga$Node,
createWithConfig(config: Yoga$Config): Yoga$Node,
destroy(node: Yoga$Node): any,
},
Layout: Layout,
Size: Size,
Value: Value,
getInstanceCount(): number,
...typeof CONSTANTS,
};
module.exports = (bind: any, lib: any): Yoga => {
function patch(prototype, name, fn) {
let original = prototype[name];
prototype[name] = function(...args) {
return fn.call(this, original, ...args);
};
}
for (let fnName of [
'setPosition',
'setMargin',
'setFlexBasis',
'setWidth',
'setHeight',
'setMinWidth',
'setMinHeight',
'setMaxWidth',
'setMaxHeight',
'setPadding',
]) {
let methods = {
[CONSTANTS.UNIT_POINT]: lib.Node.prototype[fnName],
[CONSTANTS.UNIT_PERCENT]: lib.Node.prototype[`${fnName}Percent`],
[CONSTANTS.UNIT_AUTO]: lib.Node.prototype[`${fnName}Auto`],
};
patch(lib.Node.prototype, fnName, function(original, ...args) {
// We patch all these functions to add support for the following calls:
// .setWidth(100) / .setWidth("100%") / .setWidth(.getWidth()) / .setWidth("auto")
let value = args.pop();
let unit, asNumber;
if (value === 'auto') {
unit = CONSTANTS.UNIT_AUTO;
asNumber = undefined;
} else if (value instanceof Value) {
unit = value.unit;
asNumber = value.valueOf();
} else {
unit =
typeof value === 'string' && value.endsWith('%')
? CONSTANTS.UNIT_PERCENT
: CONSTANTS.UNIT_POINT;
asNumber = parseFloat(value);
if (!Number.isNaN(value) && Number.isNaN(asNumber)) {
throw new Error(`Invalid value ${value} for ${fnName}`);
}
}
if (!methods[unit])
throw new Error(
`Failed to execute "${fnName}": Unsupported unit '${value}'`,
);
if (asNumber !== undefined) {
return methods[unit].call(this, ...args, asNumber);
} else {
return methods[unit].call(this, ...args);
}
});
}
patch(lib.Config.prototype, 'free', function() {
// Since we handle the memory allocation ourselves (via lib.Config.create),
// we also need to handle the deallocation
lib.Config.destroy(this);
});
patch(lib.Node, 'create', function(_, config) {
// We decide the constructor we want to call depending on the parameters
return config
? lib.Node.createWithConfig(config)
: lib.Node.createDefault();
});
patch(lib.Node.prototype, 'free', function() {
// Since we handle the memory allocation ourselves (via lib.Node.create),
// we also need to handle the deallocation
lib.Node.destroy(this);
});
patch(lib.Node.prototype, 'freeRecursive', function() {
for (let t = 0, T = this.getChildCount(); t < T; ++t) {
this.getChild(0).freeRecursive();
}
this.free();
});
patch(lib.Node.prototype, 'setMeasureFunc', function(original, measureFunc) {
// This patch is just a convenience patch, since it helps write more
// idiomatic source code (such as .setMeasureFunc(null))
// We also automatically convert the return value of the measureFunc
// to a Size object, so that we can return anything that has .width and
// .height properties
if (measureFunc) {
return original.call(this, (...args) =>
Size.fromJS(measureFunc(...args)),
);
} else {
return this.unsetMeasureFunc();
}
});
patch(lib.Node.prototype, 'calculateLayout', function(
original,
width = NaN,
height = NaN,
direction = CONSTANTS.DIRECTION_LTR,
) {
// Just a small patch to add support for the function default parameters
return original.call(this, width, height, direction);
});
return {
Config: lib.Config,
Node: lib.Node,
Layout: bind('Layout', Layout),
Size: bind('Size', Size),
Value: bind('Value', Value),
getInstanceCount(...args) {
return lib.getInstanceCount(...args);
},
...CONSTANTS,
};
};