2022-09-29 22:25:24 -07:00
|
|
|
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
2016-11-15 08:42:33 -08:00
|
|
|
#
|
2018-02-16 18:24:55 -08:00
|
|
|
# This source code is licensed under the MIT license found in the
|
|
|
|
# LICENSE file in the root directory of this source tree.
|
2016-11-15 08:42:33 -08:00
|
|
|
|
2019-04-11 05:19:00 -07:00
|
|
|
from __future__ import absolute_import, division, print_function, unicode_literals
|
|
|
|
|
2016-11-15 08:42:33 -08:00
|
|
|
import os
|
|
|
|
|
2019-04-11 05:19:00 -07:00
|
|
|
|
2016-11-15 08:42:33 -08:00
|
|
|
ENUMS = {
|
2019-04-11 05:19:00 -07:00
|
|
|
"Direction": ["Inherit", "LTR", "RTL"],
|
|
|
|
"Unit": ["Undefined", "Point", "Percent", "Auto"],
|
|
|
|
"FlexDirection": ["Column", "ColumnReverse", "Row", "RowReverse"],
|
|
|
|
"Justify": [
|
|
|
|
"FlexStart",
|
|
|
|
"Center",
|
|
|
|
"FlexEnd",
|
|
|
|
"SpaceBetween",
|
|
|
|
"SpaceAround",
|
|
|
|
"SpaceEvenly",
|
|
|
|
],
|
|
|
|
"Overflow": ["Visible", "Hidden", "Scroll"],
|
|
|
|
"Align": [
|
|
|
|
"Auto",
|
|
|
|
"FlexStart",
|
|
|
|
"Center",
|
|
|
|
"FlexEnd",
|
|
|
|
"Stretch",
|
|
|
|
"Baseline",
|
|
|
|
"SpaceBetween",
|
|
|
|
"SpaceAround",
|
|
|
|
],
|
2022-09-29 22:25:24 -07:00
|
|
|
"PositionType": ["Static", "Relative", "Absolute"],
|
2019-04-11 05:19:00 -07:00
|
|
|
"Display": ["Flex", "None"],
|
|
|
|
"Wrap": ["NoWrap", "Wrap", "WrapReverse"],
|
|
|
|
"MeasureMode": ["Undefined", "Exactly", "AtMost"],
|
|
|
|
"Dimension": ["Width", "Height"],
|
|
|
|
"Edge": [
|
|
|
|
"Left",
|
|
|
|
"Top",
|
|
|
|
"Right",
|
|
|
|
"Bottom",
|
|
|
|
"Start",
|
|
|
|
"End",
|
|
|
|
"Horizontal",
|
|
|
|
"Vertical",
|
|
|
|
"All",
|
|
|
|
],
|
|
|
|
"NodeType": ["Default", "Text"],
|
|
|
|
"LogLevel": ["Error", "Warn", "Info", "Debug", "Verbose", "Fatal"],
|
|
|
|
"ExperimentalFeature": [
|
2016-11-23 05:25:50 -08:00
|
|
|
# Mimic web flex-basis behavior.
|
2019-04-11 05:19:00 -07:00
|
|
|
"WebFlexBasis"
|
2016-11-15 08:42:33 -08:00
|
|
|
],
|
2019-04-11 05:19:00 -07:00
|
|
|
"PrintOptions": [("Layout", 1), ("Style", 2), ("Children", 4)],
|
Add YGGutter Enum
Summary:
This adds the YGGutter enum, used to choose between row/column gap variants (row-gap, column-gap, gap).
This used later in changes from https://github.com/facebook/yoga/pull/1116, in the APIs which deal with setting gap on style on yoga node.
Note the original PR called this `YGGap`, but this ending up leading to a couple public method signatures that could appear ambiguous:
1. `SetGap(YGGap gap, float gapLength)`: Enums like `YGAlign` are the vaues for an `align` prop. `YGGap` controls the variant of the gap (like `YGEdge` does for left/right/top/bottom variants). So the enum reads as if it is the `gapValue`, and it looks like we have two of the same parameter.
2. `SetGap(YGGap gapDirection, float gap)`: This is misleading, because the direction gaps flow is the cross-axis of flex-direction.
3. `GetGap(YGGap gap)`: `gap` is the variant, but looks like an out param.
The [CSS Box Alignment](https://www.w3.org/TR/css-align-3/#column-row-gap) spec refers to these gaps as "Gutters", which removes the ambiguity.
Changelog:
[General][Added] - Add YGGutter Enum
Reviewed By: yungsters
Differential Revision: D39922412
fbshipit-source-id: 4b0baf800fecb3d03560a4267c7fb4c4330fd39e
2022-09-29 22:25:24 -07:00
|
|
|
"Gutter": ["Column", "Row", "All"],
|
2016-11-15 08:42:33 -08:00
|
|
|
}
|
|
|
|
|
2022-09-29 22:25:24 -07:00
|
|
|
# Generated Java enums used to emit @DoNotStrip, but D17519844 removed them
|
|
|
|
# manually from all but YogaLogLevel. TODO: Is it safe to remove from it as
|
|
|
|
# well?
|
|
|
|
DO_NOT_STRIP = ["LogLevel"]
|
|
|
|
|
|
|
|
|
|
|
|
def get_license(ext):
|
|
|
|
prologue = "/**" if ext == "js" else "/*"
|
|
|
|
return """{}
|
|
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
2016-11-15 08:42:33 -08:00
|
|
|
*
|
2018-02-16 18:24:55 -08:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2016-11-15 08:42:33 -08:00
|
|
|
*/
|
|
|
|
|
2022-10-17 23:35:01 -07:00
|
|
|
// @{} by enums.py
|
2022-09-29 22:25:24 -07:00
|
|
|
|
|
|
|
""".format(
|
2022-10-17 23:35:01 -07:00
|
|
|
prologue, "generated"
|
2022-09-29 22:25:24 -07:00
|
|
|
)
|
2016-11-15 08:42:33 -08:00
|
|
|
|
2019-04-11 05:19:00 -07:00
|
|
|
|
2016-11-15 08:42:33 -08:00
|
|
|
def to_java_upper(symbol):
|
|
|
|
symbol = str(symbol)
|
2019-04-11 05:19:00 -07:00
|
|
|
out = ""
|
2016-11-15 08:42:33 -08:00
|
|
|
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]):
|
2019-04-11 05:19:00 -07:00
|
|
|
out += "_"
|
2016-11-15 08:42:33 -08:00
|
|
|
out += c.upper()
|
|
|
|
return out
|
|
|
|
|
2019-04-11 05:19:00 -07:00
|
|
|
|
2017-04-03 09:34:42 -07:00
|
|
|
def to_log_lower(symbol):
|
|
|
|
symbol = str(symbol)
|
2019-04-11 05:19:00 -07:00
|
|
|
out = ""
|
2017-04-03 09:34:42 -07:00
|
|
|
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]):
|
2019-04-11 05:19:00 -07:00
|
|
|
out += "-"
|
2017-04-03 09:34:42 -07:00
|
|
|
out += c.lower()
|
|
|
|
return out
|
|
|
|
|
2016-11-15 08:42:33 -08:00
|
|
|
|
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
|
2019-04-11 05:19:00 -07:00
|
|
|
with open(root + "/yoga/YGEnums.h", "w") as f:
|
2022-09-29 22:25:24 -07:00
|
|
|
f.write(get_license("cpp"))
|
|
|
|
f.write("#pragma once\n")
|
2016-12-05 04:58:55 -08:00
|
|
|
f.write('#include "YGMacros.h"\n\n')
|
2022-10-17 23:35:01 -07:00
|
|
|
f.write("// clang-format off\n\n\n")
|
2022-09-29 22:25:24 -07:00
|
|
|
|
2022-10-17 23:35:01 -07:00
|
|
|
f.write("YG_EXTERN_C_BEGIN\n\n")
|
2022-09-29 22:25:24 -07:00
|
|
|
items = sorted(ENUMS.items())
|
|
|
|
for name, values in items:
|
2022-10-17 23:35:01 -07:00
|
|
|
if isinstance(values[0], tuple):
|
2022-09-29 22:25:24 -07:00
|
|
|
f.write("YG_ENUM_DECL(\n")
|
|
|
|
else:
|
|
|
|
f.write("YG_ENUM_SEQ_DECL(\n")
|
|
|
|
|
|
|
|
f.write(" YG%s,\n" % name)
|
2016-11-15 08:42:33 -08:00
|
|
|
for value in values:
|
|
|
|
if isinstance(value, tuple):
|
2022-09-29 22:25:24 -07:00
|
|
|
f.write(" YG%s%s = %d" % (name, value[0], value[1]))
|
2016-11-15 08:42:33 -08:00
|
|
|
else:
|
2022-09-29 22:25:24 -07:00
|
|
|
f.write(" YG%s%s" % (name, value))
|
|
|
|
if value == values[-1]:
|
|
|
|
f.write(")\n")
|
|
|
|
else:
|
|
|
|
f.write(",\n")
|
2019-04-11 05:19:00 -07:00
|
|
|
f.write("\n")
|
|
|
|
f.write("YG_EXTERN_C_END\n")
|
2016-11-15 08:42:33 -08:00
|
|
|
|
2017-04-03 09:34:42 -07:00
|
|
|
# write out C body for printing
|
2019-04-11 05:19:00 -07:00
|
|
|
with open(root + "/yoga/YGEnums.cpp", "w") as f:
|
2022-09-29 22:25:24 -07:00
|
|
|
f.write(get_license("cpp"))
|
2017-04-03 09:34:42 -07:00
|
|
|
f.write('#include "YGEnums.h"\n\n')
|
2022-09-29 22:25:24 -07:00
|
|
|
items = sorted(ENUMS.items())
|
|
|
|
for name, values in items:
|
|
|
|
f.write("const char* YG%sToString(const YG%s value) {\n" % (name, name))
|
|
|
|
f.write(" switch (value) {\n")
|
2017-04-03 09:34:42 -07:00
|
|
|
for value in values:
|
|
|
|
if isinstance(value, tuple):
|
2019-04-11 05:19:00 -07:00
|
|
|
f.write(" case YG%s%s:\n" % (name, value[0]))
|
2017-04-03 09:34:42 -07:00
|
|
|
f.write(' return "%s";\n' % to_log_lower(value[0]))
|
|
|
|
else:
|
2019-04-11 05:19:00 -07:00
|
|
|
f.write(" case YG%s%s:\n" % (name, value))
|
2017-04-03 09:34:42 -07:00
|
|
|
f.write(' return "%s";\n' % to_log_lower(value))
|
2019-04-11 05:19:00 -07:00
|
|
|
f.write(" }\n")
|
2017-04-03 09:34:42 -07:00
|
|
|
f.write(' return "unknown";\n')
|
2022-09-29 22:25:24 -07:00
|
|
|
f.write("}\n")
|
|
|
|
if name != items[-1][0]:
|
|
|
|
f.write("\n")
|
2017-04-03 09:34:42 -07:00
|
|
|
|
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()):
|
2019-04-11 05:19:00 -07:00
|
|
|
with open(root + "/java/com/facebook/yoga/Yoga%s.java" % name, "w") as f:
|
2022-09-29 22:25:24 -07:00
|
|
|
f.write(get_license("java"))
|
2019-04-11 05:19:00 -07:00
|
|
|
f.write("package com.facebook.yoga;\n\n")
|
2022-09-29 22:25:24 -07:00
|
|
|
if name in DO_NOT_STRIP:
|
|
|
|
f.write("import com.facebook.proguard.annotations.DoNotStrip;\n\n")
|
|
|
|
f.write("@DoNotStrip\n")
|
2019-04-11 05:19:00 -07: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):
|
2019-04-11 05:19:00 -07:00
|
|
|
f.write(" %s(%d)" % (to_java_upper(value[0]), value[1]))
|
2016-11-15 08:42:33 -08:00
|
|
|
else:
|
2019-04-11 05:19:00 -07:00
|
|
|
f.write(" %s(%d)" % (to_java_upper(value), values.index(value)))
|
2016-11-15 08:42:33 -08:00
|
|
|
if values.index(value) is len(values) - 1:
|
2019-04-11 05:19:00 -07:00
|
|
|
f.write(";\n")
|
2016-11-15 08:42:33 -08:00
|
|
|
else:
|
2019-04-11 05:19:00 -07:00
|
|
|
f.write(",\n")
|
2016-11-15 08:42:33 -08:00
|
|
|
else:
|
2019-04-11 05:19:00 -07:00
|
|
|
f.write("__EMPTY(-1);")
|
|
|
|
f.write("\n")
|
2022-09-29 22:25:24 -07:00
|
|
|
f.write(" private final int mIntValue;\n")
|
2019-04-11 05:19:00 -07:00
|
|
|
f.write("\n")
|
|
|
|
f.write(" Yoga%s(int intValue) {\n" % name)
|
|
|
|
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")
|
2022-09-29 22:25:24 -07:00
|
|
|
if name in DO_NOT_STRIP:
|
|
|
|
f.write(" @DoNotStrip\n")
|
2019-04-11 05:19:00 -07:00
|
|
|
f.write(" public static Yoga%s fromInt(int value) {\n" % name)
|
|
|
|
f.write(" switch (value) {\n")
|
2016-11-15 08:42:33 -08:00
|
|
|
for value in values:
|
|
|
|
if isinstance(value, tuple):
|
2019-04-11 05:19:00 -07:00
|
|
|
f.write(
|
|
|
|
" case %d: return %s;\n" % (value[1], to_java_upper(value[0]))
|
|
|
|
)
|
2016-11-15 08:42:33 -08:00
|
|
|
else:
|
2019-04-11 05:19:00 -07:00
|
|
|
f.write(
|
|
|
|
" case %d: return %s;\n"
|
|
|
|
% (values.index(value), to_java_upper(value))
|
|
|
|
)
|
|
|
|
f.write(
|
|
|
|
' default: throw new IllegalArgumentException("Unknown enum value: " + value);\n'
|
|
|
|
)
|
|
|
|
f.write(" }\n")
|
|
|
|
f.write(" }\n")
|
|
|
|
f.write("}\n")
|
2016-11-15 08:42:33 -08:00
|
|
|
|
|
|
|
# write out csharp files
|
2017-02-03 04:21:50 -08:00
|
|
|
for name, values in sorted(ENUMS.items()):
|
2019-04-11 05:19:00 -07:00
|
|
|
with open(root + "/csharp/Facebook.Yoga/Yoga%s.cs" % name, "w") as f:
|
2022-09-29 22:25:24 -07:00
|
|
|
f.write(get_license("cs"))
|
2019-04-11 05:19:00 -07:00
|
|
|
f.write("namespace Facebook.Yoga\n{\n")
|
2017-04-25 10:16:26 -07:00
|
|
|
if isinstance(next(iter(values or []), None), tuple):
|
2019-04-11 05:19:00 -07:00
|
|
|
f.write(" [System.Flags]\n")
|
|
|
|
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):
|
2019-04-11 05:19:00 -07:00
|
|
|
f.write(" %s = %d,\n" % (value[0], value[1]))
|
2016-11-15 08:42:33 -08:00
|
|
|
else:
|
2019-04-11 05:19:00 -07:00
|
|
|
f.write(" %s,\n" % value)
|
|
|
|
f.write(" }\n")
|
|
|
|
f.write("}\n")
|
2017-01-04 04:33:39 -08:00
|
|
|
|
|
|
|
# write out javascript file
|
2019-04-11 05:19:00 -07:00
|
|
|
with open(root + "/javascript/sources/YGEnums.js", "w") as f:
|
2022-09-29 22:25:24 -07:00
|
|
|
f.write(get_license("js"))
|
|
|
|
f.write("// @flow\n")
|
|
|
|
f.write("// @format\n")
|
|
|
|
f.write("const CONSTANTS = {\n")
|
|
|
|
items = sorted(ENUMS.items())
|
|
|
|
for name, values in items:
|
2019-04-11 05:19:00 -07:00
|
|
|
f.write(" %s_COUNT: %s,\n" % (to_java_upper(name), len(values)))
|
2017-01-04 04:33:39 -08:00
|
|
|
base = 0
|
|
|
|
for value in values:
|
|
|
|
if isinstance(value, tuple):
|
2019-04-11 05:19:00 -07:00
|
|
|
f.write(
|
|
|
|
" %s_%s: %d,\n"
|
|
|
|
% (to_java_upper(name), to_java_upper(value[0]), value[1])
|
|
|
|
)
|
2017-01-04 04:33:39 -08:00
|
|
|
base = value[1] + 1
|
|
|
|
else:
|
2019-04-11 05:19:00 -07:00
|
|
|
f.write(
|
|
|
|
" %s_%s: %d,\n" % (to_java_upper(name), to_java_upper(value), base)
|
|
|
|
)
|
2017-01-04 04:33:39 -08:00
|
|
|
base += 1
|
2022-09-29 22:25:24 -07:00
|
|
|
|
|
|
|
if name != items[-1][0]:
|
|
|
|
f.write("\n")
|
2019-04-11 05:19:00 -07:00
|
|
|
f.write("};\n")
|
2022-09-29 22:25:24 -07:00
|
|
|
|
|
|
|
for name, values in sorted(ENUMS.items()):
|
|
|
|
f.write("export type Yoga${} =\n".format(name))
|
|
|
|
for value in values:
|
|
|
|
unpackedValue = value[0] if isinstance(value, tuple) else value
|
|
|
|
f.write(
|
|
|
|
" | typeof CONSTANTS.{}_{}".format(
|
|
|
|
to_java_upper(name), to_java_upper(unpackedValue)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
if values[-1] == value:
|
|
|
|
f.write(";\n")
|
|
|
|
else:
|
|
|
|
f.write("\n")
|
|
|
|
|
|
|
|
f.write("\n")
|
|
|
|
f.write("module.exports = CONSTANTS;\n")
|