2016-11-15 08:42:33 -08:00
|
|
|
# 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.
|
|
|
|
|
|
|
|
from __future__ import absolute_import
|
|
|
|
from __future__ import division
|
|
|
|
from __future__ import print_function
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
import os
|
|
|
|
|
|
|
|
ENUMS = {
|
2016-12-02 05:47:43 -08:00
|
|
|
'Direction': [
|
2016-11-15 08:42:33 -08:00
|
|
|
'Inherit',
|
|
|
|
'LTR',
|
|
|
|
'RTL',
|
|
|
|
],
|
Add feature to use percentage as value unit
Summary:
Adds the feature to use percentage as a value unit.
You can use the function ```YGPx(float)``` and ```YGPercent(float)``` for convenience.
I did some benchmarks:
```
Without Percentage Feature - Release x86:
Stack with flex: median: 0.000000 ms, stddev: 0.146683 ms
Align stretch in undefined axis: median: 0.000000 ms, stddev: 0.136525 ms
Nested flex: median: 0.000000 ms, stddev: 0.490101 ms
Huge nested layout: median: 23.000000 ms, stddev: 0.928291 ms
Stack with flex: median: 0.000000 ms, stddev: 0.170587 ms
Align stretch in undefined axis: median: 0.000000 ms, stddev: 0.143384 ms
Nested flex: median: 0.000000 ms, stddev: 0.477791 ms
Huge nested layout: median: 22.000000 ms, stddev: 2.129779 ms
With Percentage Feature - Release x86:
Stack with flex: median: 0.000000 ms, stddev: 0.132951 ms
Align stretch in undefined axis: median: 0.000000 ms, stddev: 0.136525 ms
Nested flex: median: 0.000000 ms, stddev: 0.489570 ms
Huge nested layout: median: 21.000000 ms, stddev: 1.390476 ms
Closes https://github.com/facebook/yoga/pull/258
Reviewed By: dshahidehpour
Differential Revision: D4361945
Pulled By: emilsjolander
fbshipit-source-id: a8f5bc63ad352eb9410d792729e56664468cd76a
2017-01-02 05:20:37 -08:00
|
|
|
'Unit': [
|
|
|
|
'Undefined',
|
|
|
|
'Pixel',
|
|
|
|
'Percent',
|
|
|
|
],
|
2016-12-02 05:47:43 -08:00
|
|
|
'FlexDirection': [
|
2016-11-15 08:42:33 -08:00
|
|
|
'Column',
|
|
|
|
'ColumnReverse',
|
|
|
|
'Row',
|
|
|
|
'RowReverse',
|
|
|
|
],
|
2016-12-02 05:47:43 -08:00
|
|
|
'Justify': [
|
2016-11-15 08:42:33 -08:00
|
|
|
'FlexStart',
|
|
|
|
'Center',
|
|
|
|
'FlexEnd',
|
|
|
|
'SpaceBetween',
|
|
|
|
'SpaceAround',
|
|
|
|
],
|
2016-12-02 05:47:43 -08:00
|
|
|
'Overflow': [
|
2016-11-15 08:42:33 -08:00
|
|
|
'Visible',
|
|
|
|
'Hidden',
|
|
|
|
'Scroll',
|
|
|
|
],
|
2016-12-02 05:47:43 -08:00
|
|
|
'Align': [
|
2016-11-15 08:42:33 -08:00
|
|
|
'Auto',
|
|
|
|
'FlexStart',
|
|
|
|
'Center',
|
|
|
|
'FlexEnd',
|
|
|
|
'Stretch',
|
2017-01-06 06:51:56 -08:00
|
|
|
'Baseline',
|
2016-11-15 08:42:33 -08:00
|
|
|
],
|
2016-12-02 05:47:43 -08:00
|
|
|
'PositionType': [
|
2016-11-15 08:42:33 -08:00
|
|
|
'Relative',
|
|
|
|
'Absolute',
|
|
|
|
],
|
2017-02-06 09:31:22 -08:00
|
|
|
'Display': [
|
|
|
|
'Flex',
|
|
|
|
'None',
|
|
|
|
],
|
2016-12-02 05:47:43 -08:00
|
|
|
'Wrap': [
|
2016-11-15 08:42:33 -08:00
|
|
|
'NoWrap',
|
|
|
|
'Wrap',
|
|
|
|
],
|
2016-12-02 05:47:43 -08:00
|
|
|
'MeasureMode': [
|
2016-11-15 08:42:33 -08:00
|
|
|
'Undefined',
|
|
|
|
'Exactly',
|
|
|
|
'AtMost',
|
|
|
|
],
|
2016-12-02 05:47:43 -08:00
|
|
|
'Dimension': [
|
2016-11-15 08:42:33 -08:00
|
|
|
'Width',
|
|
|
|
'Height',
|
|
|
|
],
|
2016-12-02 05:47:43 -08:00
|
|
|
'Edge': [
|
2016-11-15 08:42:33 -08:00
|
|
|
'Left',
|
|
|
|
'Top',
|
|
|
|
'Right',
|
|
|
|
'Bottom',
|
|
|
|
'Start',
|
|
|
|
'End',
|
|
|
|
'Horizontal',
|
|
|
|
'Vertical',
|
|
|
|
'All',
|
|
|
|
],
|
2016-12-02 05:47:43 -08:00
|
|
|
'LogLevel': [
|
2016-11-15 08:42:33 -08:00
|
|
|
'Error',
|
|
|
|
'Warn',
|
|
|
|
'Info',
|
|
|
|
'Debug',
|
|
|
|
'Verbose',
|
|
|
|
],
|
2016-12-02 05:47:43 -08:00
|
|
|
'ExperimentalFeature': [
|
2016-11-22 05:33:36 -08:00
|
|
|
'Rounding',
|
2016-11-23 05:25:50 -08:00
|
|
|
# Mimic web flex-basis behavior.
|
|
|
|
'WebFlexBasis',
|
2016-11-22 05:33:36 -08:00
|
|
|
],
|
2016-12-02 05:47:43 -08:00
|
|
|
'PrintOptions': [
|
2016-11-15 08:42:33 -08:00
|
|
|
('Layout', 1),
|
|
|
|
('Style', 2),
|
|
|
|
('Children', 4),
|
|
|
|
],
|
|
|
|
}
|
|
|
|
|
|
|
|
LICENSE = """/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
def to_java_upper(symbol):
|
|
|
|
symbol = str(symbol)
|
|
|
|
out = ''
|
|
|
|
for i in range(0, len(symbol)):
|
|
|
|
c = symbol[i]
|
|
|
|
if str.istitle(c) and i is not 0 and not str.istitle(symbol[i - 1]):
|
|
|
|
out += '_'
|
|
|
|
out += c.upper()
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
2016-12-22 02:57:19 -08:00
|
|
|
root = os.path.dirname(os.path.abspath(__file__))
|
2016-11-15 08:42:33 -08:00
|
|
|
|
2017-01-08 11:42:43 -08:00
|
|
|
# write out C & Objective-C headers
|
2016-12-07 05:12:11 -08:00
|
|
|
with open(root + '/yoga/YGEnums.h', 'w') as f:
|
2016-11-15 08:42:33 -08:00
|
|
|
f.write(LICENSE)
|
2016-12-05 04:58:55 -08:00
|
|
|
f.write('#pragma once\n\n')
|
|
|
|
f.write('#include "YGMacros.h"\n\n')
|
|
|
|
f.write('YG_EXTERN_C_BEGIN\n\n')
|
2017-02-03 04:21:50 -08:00
|
|
|
for name, values in sorted(ENUMS.items()):
|
2017-01-08 11:42:43 -08:00
|
|
|
f.write('#define YG%sCount %s\n' % (name, len(values)))
|
|
|
|
f.write('typedef YG_ENUM_BEGIN(YG%s) {\n' % name)
|
2016-11-15 08:42:33 -08:00
|
|
|
for value in values:
|
|
|
|
if isinstance(value, tuple):
|
2016-12-02 05:47:43 -08:00
|
|
|
f.write(' YG%s%s = %d,\n' % (name, value[0], value[1]))
|
2016-11-15 08:42:33 -08:00
|
|
|
else:
|
2016-12-02 05:47:43 -08:00
|
|
|
f.write(' YG%s%s,\n' % (name, value))
|
2017-01-08 11:42:43 -08:00
|
|
|
f.write('} YG_ENUM_END(YG%s);\n' % name)
|
2016-12-05 04:58:55 -08:00
|
|
|
f.write('\n')
|
|
|
|
f.write('YG_EXTERN_C_END\n')
|
2016-11-15 08:42:33 -08:00
|
|
|
|
|
|
|
# write out java files
|
2017-02-03 04:21:50 -08:00
|
|
|
for name, values in sorted(ENUMS.items()):
|
2016-12-07 05:12:11 -08:00
|
|
|
with open(root + '/java/com/facebook/yoga/Yoga%s.java' % name, 'w') as f:
|
2016-11-15 08:42:33 -08:00
|
|
|
f.write(LICENSE)
|
2016-12-05 02:56:20 -08:00
|
|
|
f.write('package com.facebook.yoga;\n\n')
|
2016-11-28 09:20:59 -08:00
|
|
|
f.write('import com.facebook.proguard.annotations.DoNotStrip;\n\n')
|
|
|
|
f.write('@DoNotStrip\n')
|
2016-12-02 05:47:43 -08:00
|
|
|
f.write('public enum Yoga%s {\n' % name)
|
2016-11-15 08:42:33 -08:00
|
|
|
if len(values) > 0:
|
|
|
|
for value in values:
|
|
|
|
if isinstance(value, tuple):
|
|
|
|
f.write(' %s(%d)' % (to_java_upper(value[0]), value[1]))
|
|
|
|
else:
|
|
|
|
f.write(' %s(%d)' % (to_java_upper(value), values.index(value)))
|
|
|
|
if values.index(value) is len(values) - 1:
|
|
|
|
f.write(';\n')
|
|
|
|
else:
|
|
|
|
f.write(',\n')
|
|
|
|
else:
|
|
|
|
f.write('__EMPTY(-1);')
|
|
|
|
f.write('\n')
|
|
|
|
f.write(' private int mIntValue;\n')
|
|
|
|
f.write('\n')
|
2016-12-02 05:47:43 -08:00
|
|
|
f.write(' Yoga%s(int intValue) {\n' % name)
|
2016-11-15 08:42:33 -08:00
|
|
|
f.write(' mIntValue = intValue;\n')
|
|
|
|
f.write(' }\n')
|
|
|
|
f.write('\n')
|
|
|
|
f.write(' public int intValue() {\n')
|
|
|
|
f.write(' return mIntValue;\n')
|
|
|
|
f.write(' }\n')
|
|
|
|
f.write('\n')
|
2016-12-02 05:47:43 -08:00
|
|
|
f.write(' public static Yoga%s fromInt(int value) {\n' % name)
|
2016-11-15 08:42:33 -08:00
|
|
|
f.write(' switch (value) {\n')
|
|
|
|
for value in values:
|
|
|
|
if isinstance(value, tuple):
|
|
|
|
f.write(' case %d: return %s;\n' % (value[1], to_java_upper(value[0])))
|
|
|
|
else:
|
|
|
|
f.write(' case %d: return %s;\n' % (values.index(value), to_java_upper(value)))
|
2017-01-19 16:15:22 -08:00
|
|
|
f.write(' default: throw new IllegalArgumentException("Unknown enum value: " + value);\n')
|
2016-11-15 08:42:33 -08:00
|
|
|
f.write(' }\n')
|
|
|
|
f.write(' }\n')
|
|
|
|
f.write('}\n')
|
|
|
|
|
|
|
|
# write out csharp files
|
2017-02-03 04:21:50 -08:00
|
|
|
for name, values in sorted(ENUMS.items()):
|
2016-12-02 11:18:16 -08:00
|
|
|
with open(root + '/csharp/Facebook.Yoga/Yoga%s.cs' % name, 'w') as f:
|
2016-11-15 08:42:33 -08:00
|
|
|
f.write(LICENSE)
|
2016-12-02 11:18:16 -08:00
|
|
|
f.write('namespace Facebook.Yoga\n{\n')
|
2016-12-02 05:47:43 -08:00
|
|
|
f.write(' public enum Yoga%s\n {\n' % name)
|
2016-11-15 08:42:33 -08:00
|
|
|
for value in values:
|
|
|
|
if isinstance(value, tuple):
|
|
|
|
f.write(' %s = %d,\n' % (value[0], value[1]))
|
|
|
|
else:
|
|
|
|
f.write(' %s,\n' % value)
|
|
|
|
f.write(' }\n')
|
|
|
|
f.write('}\n')
|
2017-01-04 04:33:39 -08:00
|
|
|
|
|
|
|
# write out javascript file
|
|
|
|
with open(root + '/javascript/sources/YGEnums.js', 'w') as f:
|
|
|
|
f.write(LICENSE)
|
|
|
|
f.write('module.exports = {\n\n')
|
2017-02-03 04:21:50 -08:00
|
|
|
for name, values in sorted(ENUMS.items()):
|
2017-01-04 04:33:39 -08:00
|
|
|
f.write(' %s_COUNT: %s,\n' % (to_java_upper(name), len(values)))
|
|
|
|
base = 0
|
|
|
|
for value in values:
|
|
|
|
if isinstance(value, tuple):
|
|
|
|
f.write(' %s_%s: %d,\n' % (to_java_upper(name), to_java_upper(value[0]), value[1]))
|
|
|
|
base = value[1] + 1
|
|
|
|
else:
|
|
|
|
f.write(' %s_%s: %d,\n' % (to_java_upper(name), to_java_upper(value), base))
|
|
|
|
base += 1
|
2017-01-19 16:15:22 -08:00
|
|
|
f.write('\n')
|
2017-01-04 04:33:39 -08:00
|
|
|
f.write('};\n')
|