Autogenerate enum defenitions for all languages

Summary: Keeping enums in sync between all the different bindings is tedious and error prone. Getting one of the values in the incorrect order could lead to many hours of debugging as they are passed as ints to C. This diff adds a simple python script to generate these enums for all languages. This also makes it much easier to add support for more languages in the future

Reviewed By: gkassabli

Differential Revision: D4174263

fbshipit-source-id: 478961a8f683e196704d3c6ea1505a05c85fcb10
This commit is contained in:
Emil Sjolander
2016-11-15 08:42:33 -08:00
committed by Facebook Github Bot
parent ac4d0ab2a1
commit d1c555fede
34 changed files with 699 additions and 166 deletions

106
CSSLayout/CSSEnums.h Normal file
View File

@@ -0,0 +1,106 @@
/**
* 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.
*/
typedef enum CSSOverflow {
CSSOverflowVisible,
CSSOverflowHidden,
CSSOverflowScroll,
CSSOverflowCount,
} CSSOverflow;
typedef enum CSSJustify {
CSSJustifyFlexStart,
CSSJustifyCenter,
CSSJustifyFlexEnd,
CSSJustifySpaceBetween,
CSSJustifySpaceAround,
CSSJustifyCount,
} CSSJustify;
typedef enum CSSFlexDirection {
CSSFlexDirectionColumn,
CSSFlexDirectionColumnReverse,
CSSFlexDirectionRow,
CSSFlexDirectionRowReverse,
CSSFlexDirectionCount,
} CSSFlexDirection;
typedef enum CSSAlign {
CSSAlignAuto,
CSSAlignFlexStart,
CSSAlignCenter,
CSSAlignFlexEnd,
CSSAlignStretch,
CSSAlignCount,
} CSSAlign;
typedef enum CSSEdge {
CSSEdgeLeft,
CSSEdgeTop,
CSSEdgeRight,
CSSEdgeBottom,
CSSEdgeStart,
CSSEdgeEnd,
CSSEdgeHorizontal,
CSSEdgeVertical,
CSSEdgeAll,
CSSEdgeCount,
} CSSEdge;
typedef enum CSSWrap {
CSSWrapNoWrap,
CSSWrapWrap,
CSSWrapCount,
} CSSWrap;
typedef enum CSSDirection {
CSSDirectionInherit,
CSSDirectionLTR,
CSSDirectionRTL,
CSSDirectionCount,
} CSSDirection;
typedef enum CSSExperimentalFeature {
CSSExperimentalFeatureCount,
} CSSExperimentalFeature;
typedef enum CSSLogLevel {
CSSLogLevelError,
CSSLogLevelWarn,
CSSLogLevelInfo,
CSSLogLevelDebug,
CSSLogLevelVerbose,
CSSLogLevelCount,
} CSSLogLevel;
typedef enum CSSDimension {
CSSDimensionWidth,
CSSDimensionHeight,
CSSDimensionCount,
} CSSDimension;
typedef enum CSSMeasureMode {
CSSMeasureModeUndefined,
CSSMeasureModeExactly,
CSSMeasureModeAtMost,
CSSMeasureModeCount,
} CSSMeasureMode;
typedef enum CSSPositionType {
CSSPositionTypeRelative,
CSSPositionTypeAbsolute,
CSSPositionTypeCount,
} CSSPositionType;
typedef enum CSSPrintOptions {
CSSPrintOptionsLayout = 1,
CSSPrintOptionsStyle = 2,
CSSPrintOptionsChildren = 4,
CSSPrintOptionsCount,
} CSSPrintOptions;

View File

@@ -122,6 +122,8 @@ static int _csslayoutAndroidLog(CSSLogLevel level, const char *format, va_list a
case CSSLogLevelVerbose: case CSSLogLevelVerbose:
androidLevel = ANDROID_LOG_VERBOSE; androidLevel = ANDROID_LOG_VERBOSE;
break; break;
case CSSLogLevelCount:
break;
} }
const int result = __android_log_vprint(androidLevel, "css-layout", format, args); const int result = __android_log_vprint(androidLevel, "css-layout", format, args);
return result; return result;
@@ -1824,6 +1826,7 @@ static void layoutNodeImpl(const CSSNodeRef node,
leadingMainDim = betweenMainDim / 2; leadingMainDim = betweenMainDim / 2;
break; break;
case CSSJustifyFlexStart: case CSSJustifyFlexStart:
case CSSJustifyCount:
break; break;
} }
@@ -2015,6 +2018,7 @@ static void layoutNodeImpl(const CSSNodeRef node,
break; break;
case CSSAlignAuto: case CSSAlignAuto:
case CSSAlignFlexStart: case CSSAlignFlexStart:
case CSSAlignCount:
break; break;
} }
@@ -2074,6 +2078,7 @@ static void layoutNodeImpl(const CSSNodeRef node,
break; break;
} }
case CSSAlignAuto: case CSSAlignAuto:
case CSSAlignCount:
break; break;
} }
} }

View File

@@ -29,104 +29,15 @@ static const unsigned long __nan[2] = {0xffffffff, 0x7fffffff};
#define CSSUndefined NAN #define CSSUndefined NAN
#include "CSSMacros.h" #include "CSSMacros.h"
#include "CSSEnums.h"
CSS_EXTERN_C_BEGIN CSS_EXTERN_C_BEGIN
typedef enum CSSDirection {
CSSDirectionInherit,
CSSDirectionLTR,
CSSDirectionRTL,
} CSSDirection;
typedef enum CSSFlexDirection {
CSSFlexDirectionColumn,
CSSFlexDirectionColumnReverse,
CSSFlexDirectionRow,
CSSFlexDirectionRowReverse,
} CSSFlexDirection;
typedef enum CSSJustify {
CSSJustifyFlexStart,
CSSJustifyCenter,
CSSJustifyFlexEnd,
CSSJustifySpaceBetween,
CSSJustifySpaceAround,
} CSSJustify;
typedef enum CSSOverflow {
CSSOverflowVisible,
CSSOverflowHidden,
CSSOverflowScroll,
} CSSOverflow;
// Note: auto is only a valid value for alignSelf. It is NOT a valid value for
// alignItems.
typedef enum CSSAlign {
CSSAlignAuto,
CSSAlignFlexStart,
CSSAlignCenter,
CSSAlignFlexEnd,
CSSAlignStretch,
} CSSAlign;
typedef enum CSSPositionType {
CSSPositionTypeRelative,
CSSPositionTypeAbsolute,
} CSSPositionType;
typedef enum CSSWrap {
CSSWrapNoWrap,
CSSWrapWrap,
} CSSWrap;
typedef enum CSSMeasureMode {
CSSMeasureModeUndefined,
CSSMeasureModeExactly,
CSSMeasureModeAtMost,
CSSMeasureModeCount,
} CSSMeasureMode;
typedef enum CSSDimension {
CSSDimensionWidth,
CSSDimensionHeight,
} CSSDimension;
typedef enum CSSEdge {
CSSEdgeLeft,
CSSEdgeTop,
CSSEdgeRight,
CSSEdgeBottom,
CSSEdgeStart,
CSSEdgeEnd,
CSSEdgeHorizontal,
CSSEdgeVertical,
CSSEdgeAll,
CSSEdgeCount,
} CSSEdge;
typedef enum CSSPrintOptions {
CSSPrintOptionsLayout = 1,
CSSPrintOptionsStyle = 2,
CSSPrintOptionsChildren = 4,
} CSSPrintOptions;
typedef struct CSSSize { typedef struct CSSSize {
float width; float width;
float height; float height;
} CSSSize; } CSSSize;
typedef enum CSSLogLevel {
CSSLogLevelError,
CSSLogLevelWarn,
CSSLogLevelInfo,
CSSLogLevelDebug,
CSSLogLevelVerbose,
} CSSLogLevel;
typedef enum CSSExperimentalFeature {
CSSExperimentalFeatureCount,
} CSSExperimentalFeature;
typedef struct CSSNode *CSSNodeRef; typedef struct CSSNode *CSSNodeRef;
typedef CSSSize (*CSSMeasureFunc)(CSSNodeRef node, typedef CSSSize (*CSSMeasureFunc)(CSSNodeRef node,
float width, float width,

View File

@@ -1,4 +1,4 @@
/** /**
* Copyright (c) 2014-present, Facebook, Inc. * Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved. * All rights reserved.
* *
@@ -15,6 +15,6 @@ namespace Facebook.CSSLayout
FlexStart, FlexStart,
Center, Center,
FlexEnd, FlexEnd,
Stretch Stretch,
} }
} }

View File

@@ -1,4 +1,4 @@
/** /**
* Copyright (c) 2014-present, Facebook, Inc. * Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved. * All rights reserved.
* *
@@ -12,7 +12,11 @@ namespace Facebook.CSSLayout
public enum CSSDirection public enum CSSDirection
{ {
Inherit, Inherit,
LeftToRight, LTR,
RightToLeft RTL,
[System.Obsolete("Use LTR instead")]
LeftToRight = LTR,
[System.Obsolete("Use RTL instead")]
RightToLeft = RTL,
} }
} }

View File

@@ -1,4 +1,4 @@
/** /**
* Copyright (c) 2014-present, Facebook, Inc. * Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved. * All rights reserved.
* *
@@ -12,6 +12,6 @@ namespace Facebook.CSSLayout
public enum CSSDimension public enum CSSDimension
{ {
Width, Width,
Height Height,
} }
} }

View File

@@ -1,4 +1,4 @@
/** /**
* Copyright (c) 2014-present, Facebook, Inc. * Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved. * All rights reserved.
* *
@@ -20,6 +20,5 @@ namespace Facebook.CSSLayout
Horizontal, Horizontal,
Vertical, Vertical,
All, All,
Count,
} }
} }

View File

@@ -1,4 +1,4 @@
/** /**
* Copyright (c) 2014-present, Facebook, Inc. * Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved. * All rights reserved.
* *

View File

@@ -1,4 +1,4 @@
/** /**
* Copyright (c) 2014-present, Facebook, Inc. * Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved. * All rights reserved.
* *

View File

@@ -1,4 +1,4 @@
/** /**
* Copyright (c) 2014-present, Facebook, Inc. * Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved. * All rights reserved.
* *

View File

@@ -1,4 +1,4 @@
/** /**
* Copyright (c) 2014-present, Facebook, Inc. * Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved. * All rights reserved.
* *
@@ -14,6 +14,5 @@ namespace Facebook.CSSLayout
Undefined, Undefined,
Exactly, Exactly,
AtMost, AtMost,
Count,
} }
} }

View File

@@ -1,4 +1,4 @@
/** /**
* Copyright (c) 2014-present, Facebook, Inc. * Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved. * All rights reserved.
* *
@@ -13,5 +13,6 @@ namespace Facebook.CSSLayout
{ {
Visible, Visible,
Hidden, Hidden,
Scroll,
} }
} }

View File

@@ -1,4 +1,4 @@
/** /**
* Copyright (c) 2014-present, Facebook, Inc. * Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved. * All rights reserved.
* *
@@ -12,6 +12,6 @@ namespace Facebook.CSSLayout
public enum CSSPositionType public enum CSSPositionType
{ {
Relative, Relative,
Absolute Absolute,
} }
} }

View File

@@ -1,4 +1,4 @@
/** /**
* Copyright (c) 2014-present, Facebook, Inc. * Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved. * All rights reserved.
* *

View File

@@ -1,4 +1,4 @@
/** /**
* Copyright (c) 2014-present, Facebook, Inc. * Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved. * All rights reserved.
* *

182
enums.py Normal file
View File

@@ -0,0 +1,182 @@
# 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 = {
'CSSDirection': [
'Inherit',
'LTR',
'RTL',
],
'CSSFlexDirection': [
'Column',
'ColumnReverse',
'Row',
'RowReverse',
],
'CSSJustify': [
'FlexStart',
'Center',
'FlexEnd',
'SpaceBetween',
'SpaceAround',
],
'CSSOverflow': [
'Visible',
'Hidden',
'Scroll',
],
'CSSAlign': [
'Auto',
'FlexStart',
'Center',
'FlexEnd',
'Stretch',
],
'CSSPositionType': [
'Relative',
'Absolute',
],
'CSSWrap': [
'NoWrap',
'Wrap',
],
'CSSMeasureMode': [
'Undefined',
'Exactly',
'AtMost',
],
'CSSDimension': [
'Width',
'Height',
],
'CSSEdge': [
'Left',
'Top',
'Right',
'Bottom',
'Start',
'End',
'Horizontal',
'Vertical',
'All',
],
'CSSLogLevel': [
'Error',
'Warn',
'Info',
'Debug',
'Verbose',
],
'CSSExperimentalFeature': [],
'CSSPrintOptions': [
('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
root = os.path.dirname(__file__)
# write out C header
with open(root + '/CSSLayout/CSSEnums.h', 'w') as f:
f.write(LICENSE)
remaining = len(ENUMS)
for name, values in ENUMS.items():
f.write('typedef enum %s {\n' % name)
for value in values:
if isinstance(value, tuple):
f.write(' %s%s = %d,\n' % (name, value[0], value[1]))
else:
f.write(' %s%s,\n' % (name, value))
f.write(' %sCount,\n' % name)
f.write('} %s;\n' % name)
if remaining > 1:
f.write('\n')
remaining = remaining - 1
# write out java files
for name, values in ENUMS.items():
with open(root + '/java/com/facebook/csslayout/%s.java' % name, 'w') as f:
f.write(LICENSE)
f.write('package com.facebook.csslayout;\n\n')
f.write('public enum %s {\n' % name)
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')
f.write(' %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')
f.write(' public static %s fromInt(int value) {\n' % name)
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)))
f.write(' default: throw new IllegalArgumentException("Unkown enum value: " + value);\n')
f.write(' }\n')
f.write(' }\n')
f.write('}\n')
# write out csharp files
for name, values in ENUMS.items():
with open(root + '/csharp/Facebook.CSSLayout/%s.cs' % name, 'w') as f:
f.write(LICENSE)
f.write('namespace Facebook.CSSLayout\n{\n')
f.write(' public enum %s\n {\n' % name)
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')

View File

@@ -10,9 +10,30 @@
package com.facebook.csslayout; package com.facebook.csslayout;
public enum CSSAlign { public enum CSSAlign {
AUTO, AUTO(0),
FLEX_START, FLEX_START(1),
CENTER, CENTER(2),
FLEX_END, FLEX_END(3),
STRETCH, STRETCH(4);
private int mIntValue;
CSSAlign(int intValue) {
mIntValue = intValue;
}
public int intValue() {
return mIntValue;
}
public static CSSAlign fromInt(int value) {
switch (value) {
case 0: return AUTO;
case 1: return FLEX_START;
case 2: return CENTER;
case 3: return FLEX_END;
case 4: return STRETCH;
default: throw new IllegalArgumentException("Unkown enum value: " + value);
}
}
} }

View File

@@ -0,0 +1,33 @@
/**
* 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.
*/
package com.facebook.csslayout;
public enum CSSDimension {
WIDTH(0),
HEIGHT(1);
private int mIntValue;
CSSDimension(int intValue) {
mIntValue = intValue;
}
public int intValue() {
return mIntValue;
}
public static CSSDimension fromInt(int value) {
switch (value) {
case 0: return WIDTH;
case 1: return HEIGHT;
default: throw new IllegalArgumentException("Unkown enum value: " + value);
}
}
}

View File

@@ -10,7 +10,26 @@
package com.facebook.csslayout; package com.facebook.csslayout;
public enum CSSDirection { public enum CSSDirection {
INHERIT, INHERIT(0),
LTR, LTR(1),
RTL, RTL(2);
private int mIntValue;
CSSDirection(int intValue) {
mIntValue = intValue;
}
public int intValue() {
return mIntValue;
}
public static CSSDirection fromInt(int value) {
switch (value) {
case 0: return INHERIT;
case 1: return LTR;
case 2: return RTL;
default: throw new IllegalArgumentException("Unkown enum value: " + value);
}
}
} }

View File

@@ -0,0 +1,47 @@
/**
* 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.
*/
package com.facebook.csslayout;
public enum CSSEdge {
LEFT(0),
TOP(1),
RIGHT(2),
BOTTOM(3),
START(4),
END(5),
HORIZONTAL(6),
VERTICAL(7),
ALL(8);
private int mIntValue;
CSSEdge(int intValue) {
mIntValue = intValue;
}
public int intValue() {
return mIntValue;
}
public static CSSEdge fromInt(int value) {
switch (value) {
case 0: return LEFT;
case 1: return TOP;
case 2: return RIGHT;
case 3: return BOTTOM;
case 4: return START;
case 5: return END;
case 6: return HORIZONTAL;
case 7: return VERTICAL;
case 8: return ALL;
default: throw new IllegalArgumentException("Unkown enum value: " + value);
}
}
}

View File

@@ -10,4 +10,20 @@
package com.facebook.csslayout; package com.facebook.csslayout;
public enum CSSExperimentalFeature { public enum CSSExperimentalFeature {
__EMPTY(-1);
private int mIntValue;
CSSExperimentalFeature(int intValue) {
mIntValue = intValue;
}
public int intValue() {
return mIntValue;
}
public static CSSExperimentalFeature fromInt(int value) {
switch (value) {
default: throw new IllegalArgumentException("Unkown enum value: " + value);
}
}
} }

View File

@@ -10,8 +10,28 @@
package com.facebook.csslayout; package com.facebook.csslayout;
public enum CSSFlexDirection { public enum CSSFlexDirection {
COLUMN, COLUMN(0),
COLUMN_REVERSE, COLUMN_REVERSE(1),
ROW, ROW(2),
ROW_REVERSE ROW_REVERSE(3);
private int mIntValue;
CSSFlexDirection(int intValue) {
mIntValue = intValue;
}
public int intValue() {
return mIntValue;
}
public static CSSFlexDirection fromInt(int value) {
switch (value) {
case 0: return COLUMN;
case 1: return COLUMN_REVERSE;
case 2: return ROW;
case 3: return ROW_REVERSE;
default: throw new IllegalArgumentException("Unkown enum value: " + value);
}
}
} }

View File

@@ -10,9 +10,30 @@
package com.facebook.csslayout; package com.facebook.csslayout;
public enum CSSJustify { public enum CSSJustify {
FLEX_START, FLEX_START(0),
CENTER, CENTER(1),
FLEX_END, FLEX_END(2),
SPACE_BETWEEN, SPACE_BETWEEN(3),
SPACE_AROUND, SPACE_AROUND(4);
private int mIntValue;
CSSJustify(int intValue) {
mIntValue = intValue;
}
public int intValue() {
return mIntValue;
}
public static CSSJustify fromInt(int value) {
switch (value) {
case 0: return FLEX_START;
case 1: return CENTER;
case 2: return FLEX_END;
case 3: return SPACE_BETWEEN;
case 4: return SPACE_AROUND;
default: throw new IllegalArgumentException("Unkown enum value: " + value);
}
}
} }

View File

@@ -0,0 +1,39 @@
/**
* 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.
*/
package com.facebook.csslayout;
public enum CSSLogLevel {
ERROR(0),
WARN(1),
INFO(2),
DEBUG(3),
VERBOSE(4);
private int mIntValue;
CSSLogLevel(int intValue) {
mIntValue = intValue;
}
public int intValue() {
return mIntValue;
}
public static CSSLogLevel fromInt(int value) {
switch (value) {
case 0: return ERROR;
case 1: return WARN;
case 2: return INFO;
case 3: return DEBUG;
case 4: return VERBOSE;
default: throw new IllegalArgumentException("Unkown enum value: " + value);
}
}
}

View File

@@ -13,15 +13,9 @@ import com.facebook.proguard.annotations.DoNotStrip;
/** /**
* Inteface for recieving logs from native layer. Use by setting CSSNode.setLogger(myLogger); * Inteface for recieving logs from native layer. Use by setting CSSNode.setLogger(myLogger);
* LOG_LEVEL_ERROR indicated a fatal error. * See CSSLogLevel for the different log levels.
*/ */
public interface CSSLogger { public interface CSSLogger {
public final int LOG_LEVEL_ERROR = 0;
public final int LOG_LEVEL_WARN = 1;
public final int LOG_LEVEL_INFO = 2;
public final int LOG_LEVEL_DEBUG = 3;
public final int LOG_LEVEL_VERBOSE = 4;
@DoNotStrip @DoNotStrip
void log(int level, String message); void log(CSSLogLevel level, String message);
} }

View File

@@ -10,7 +10,26 @@
package com.facebook.csslayout; package com.facebook.csslayout;
public enum CSSMeasureMode { public enum CSSMeasureMode {
UNDEFINED, UNDEFINED(0),
EXACTLY, EXACTLY(1),
AT_MOST, AT_MOST(2);
private int mIntValue;
CSSMeasureMode(int intValue) {
mIntValue = intValue;
}
public int intValue() {
return mIntValue;
}
public static CSSMeasureMode fromInt(int value) {
switch (value) {
case 0: return UNDEFINED;
case 1: return EXACTLY;
case 2: return AT_MOST;
default: throw new IllegalArgumentException("Unkown enum value: " + value);
}
}
} }

View File

@@ -46,12 +46,12 @@ public class CSSNode implements CSSNodeAPI<CSSNode> {
public static void setExperimentalFeatureEnabled( public static void setExperimentalFeatureEnabled(
CSSExperimentalFeature feature, CSSExperimentalFeature feature,
boolean enabled) { boolean enabled) {
jni_CSSLayoutSetExperimentalFeatureEnabled(feature.ordinal(), enabled); jni_CSSLayoutSetExperimentalFeatureEnabled(feature.intValue(), enabled);
} }
private static native boolean jni_CSSLayoutIsExperimentalFeatureEnabled(int feature); private static native boolean jni_CSSLayoutIsExperimentalFeatureEnabled(int feature);
public static boolean isExperimentalFeatureEnabled(CSSExperimentalFeature feature) { public static boolean isExperimentalFeatureEnabled(CSSExperimentalFeature feature) {
return jni_CSSLayoutIsExperimentalFeatureEnabled(feature.ordinal()); return jni_CSSLayoutIsExperimentalFeatureEnabled(feature.intValue());
} }
private CSSNode mParent; private CSSNode mParent;
@@ -199,7 +199,7 @@ public class CSSNode implements CSSNodeAPI<CSSNode> {
private native void jni_CSSNodeStyleSetDirection(long nativePointer, int direction); private native void jni_CSSNodeStyleSetDirection(long nativePointer, int direction);
@Override @Override
public void setDirection(CSSDirection direction) { public void setDirection(CSSDirection direction) {
jni_CSSNodeStyleSetDirection(mNativePointer, direction.ordinal()); jni_CSSNodeStyleSetDirection(mNativePointer, direction.intValue());
} }
private native int jni_CSSNodeStyleGetFlexDirection(long nativePointer); private native int jni_CSSNodeStyleGetFlexDirection(long nativePointer);
@@ -211,7 +211,7 @@ public class CSSNode implements CSSNodeAPI<CSSNode> {
private native void jni_CSSNodeStyleSetFlexDirection(long nativePointer, int flexDirection); private native void jni_CSSNodeStyleSetFlexDirection(long nativePointer, int flexDirection);
@Override @Override
public void setFlexDirection(CSSFlexDirection flexDirection) { public void setFlexDirection(CSSFlexDirection flexDirection) {
jni_CSSNodeStyleSetFlexDirection(mNativePointer, flexDirection.ordinal()); jni_CSSNodeStyleSetFlexDirection(mNativePointer, flexDirection.intValue());
} }
private native int jni_CSSNodeStyleGetJustifyContent(long nativePointer); private native int jni_CSSNodeStyleGetJustifyContent(long nativePointer);
@@ -223,7 +223,7 @@ public class CSSNode implements CSSNodeAPI<CSSNode> {
private native void jni_CSSNodeStyleSetJustifyContent(long nativePointer, int justifyContent); private native void jni_CSSNodeStyleSetJustifyContent(long nativePointer, int justifyContent);
@Override @Override
public void setJustifyContent(CSSJustify justifyContent) { public void setJustifyContent(CSSJustify justifyContent) {
jni_CSSNodeStyleSetJustifyContent(mNativePointer, justifyContent.ordinal()); jni_CSSNodeStyleSetJustifyContent(mNativePointer, justifyContent.intValue());
} }
private native int jni_CSSNodeStyleGetAlignItems(long nativePointer); private native int jni_CSSNodeStyleGetAlignItems(long nativePointer);
@@ -235,7 +235,7 @@ public class CSSNode implements CSSNodeAPI<CSSNode> {
private native void jni_CSSNodeStyleSetAlignItems(long nativePointer, int alignItems); private native void jni_CSSNodeStyleSetAlignItems(long nativePointer, int alignItems);
@Override @Override
public void setAlignItems(CSSAlign alignItems) { public void setAlignItems(CSSAlign alignItems) {
jni_CSSNodeStyleSetAlignItems(mNativePointer, alignItems.ordinal()); jni_CSSNodeStyleSetAlignItems(mNativePointer, alignItems.intValue());
} }
private native int jni_CSSNodeStyleGetAlignSelf(long nativePointer); private native int jni_CSSNodeStyleGetAlignSelf(long nativePointer);
@@ -247,7 +247,7 @@ public class CSSNode implements CSSNodeAPI<CSSNode> {
private native void jni_CSSNodeStyleSetAlignSelf(long nativePointer, int alignSelf); private native void jni_CSSNodeStyleSetAlignSelf(long nativePointer, int alignSelf);
@Override @Override
public void setAlignSelf(CSSAlign alignSelf) { public void setAlignSelf(CSSAlign alignSelf) {
jni_CSSNodeStyleSetAlignSelf(mNativePointer, alignSelf.ordinal()); jni_CSSNodeStyleSetAlignSelf(mNativePointer, alignSelf.intValue());
} }
private native int jni_CSSNodeStyleGetAlignContent(long nativePointer); private native int jni_CSSNodeStyleGetAlignContent(long nativePointer);
@@ -259,7 +259,7 @@ public class CSSNode implements CSSNodeAPI<CSSNode> {
private native void jni_CSSNodeStyleSetAlignContent(long nativePointer, int alignContent); private native void jni_CSSNodeStyleSetAlignContent(long nativePointer, int alignContent);
@Override @Override
public void setAlignContent(CSSAlign alignContent) { public void setAlignContent(CSSAlign alignContent) {
jni_CSSNodeStyleSetAlignContent(mNativePointer, alignContent.ordinal()); jni_CSSNodeStyleSetAlignContent(mNativePointer, alignContent.intValue());
} }
private native int jni_CSSNodeStyleGetPositionType(long nativePointer); private native int jni_CSSNodeStyleGetPositionType(long nativePointer);
@@ -271,13 +271,13 @@ public class CSSNode implements CSSNodeAPI<CSSNode> {
private native void jni_CSSNodeStyleSetPositionType(long nativePointer, int positionType); private native void jni_CSSNodeStyleSetPositionType(long nativePointer, int positionType);
@Override @Override
public void setPositionType(CSSPositionType positionType) { public void setPositionType(CSSPositionType positionType) {
jni_CSSNodeStyleSetPositionType(mNativePointer, positionType.ordinal()); jni_CSSNodeStyleSetPositionType(mNativePointer, positionType.intValue());
} }
private native void jni_CSSNodeStyleSetFlexWrap(long nativePointer, int wrapType); private native void jni_CSSNodeStyleSetFlexWrap(long nativePointer, int wrapType);
@Override @Override
public void setWrap(CSSWrap flexWrap) { public void setWrap(CSSWrap flexWrap) {
jni_CSSNodeStyleSetFlexWrap(mNativePointer, flexWrap.ordinal()); jni_CSSNodeStyleSetFlexWrap(mNativePointer, flexWrap.intValue());
} }
private native int jni_CSSNodeStyleGetOverflow(long nativePointer); private native int jni_CSSNodeStyleGetOverflow(long nativePointer);
@@ -289,7 +289,7 @@ public class CSSNode implements CSSNodeAPI<CSSNode> {
private native void jni_CSSNodeStyleSetOverflow(long nativePointer, int overflow); private native void jni_CSSNodeStyleSetOverflow(long nativePointer, int overflow);
@Override @Override
public void setOverflow(CSSOverflow overflow) { public void setOverflow(CSSOverflow overflow) {
jni_CSSNodeStyleSetOverflow(mNativePointer, overflow.ordinal()); jni_CSSNodeStyleSetOverflow(mNativePointer, overflow.intValue());
} }
private native void jni_CSSNodeStyleSetFlex(long nativePointer, float flex); private native void jni_CSSNodeStyleSetFlex(long nativePointer, float flex);

View File

@@ -10,7 +10,26 @@
package com.facebook.csslayout; package com.facebook.csslayout;
public enum CSSOverflow { public enum CSSOverflow {
VISIBLE, VISIBLE(0),
HIDDEN, HIDDEN(1),
SCROLL, SCROLL(2);
private int mIntValue;
CSSOverflow(int intValue) {
mIntValue = intValue;
}
public int intValue() {
return mIntValue;
}
public static CSSOverflow fromInt(int value) {
switch (value) {
case 0: return VISIBLE;
case 1: return HIDDEN;
case 2: return SCROLL;
default: throw new IllegalArgumentException("Unkown enum value: " + value);
}
}
} }

View File

@@ -10,6 +10,24 @@
package com.facebook.csslayout; package com.facebook.csslayout;
public enum CSSPositionType { public enum CSSPositionType {
RELATIVE, RELATIVE(0),
ABSOLUTE, ABSOLUTE(1);
private int mIntValue;
CSSPositionType(int intValue) {
mIntValue = intValue;
}
public int intValue() {
return mIntValue;
}
public static CSSPositionType fromInt(int value) {
switch (value) {
case 0: return RELATIVE;
case 1: return ABSOLUTE;
default: throw new IllegalArgumentException("Unkown enum value: " + value);
}
}
} }

View File

@@ -0,0 +1,35 @@
/**
* 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.
*/
package com.facebook.csslayout;
public enum CSSPrintOptions {
LAYOUT(1),
STYLE(2),
CHILDREN(4);
private int mIntValue;
CSSPrintOptions(int intValue) {
mIntValue = intValue;
}
public int intValue() {
return mIntValue;
}
public static CSSPrintOptions fromInt(int value) {
switch (value) {
case 1: return LAYOUT;
case 2: return STYLE;
case 4: return CHILDREN;
default: throw new IllegalArgumentException("Unkown enum value: " + value);
}
}
}

View File

@@ -54,7 +54,7 @@ public class CSSStyle {
alignItems = CSSAlign.STRETCH; alignItems = CSSAlign.STRETCH;
alignSelf = CSSAlign.AUTO; alignSelf = CSSAlign.AUTO;
positionType = CSSPositionType.RELATIVE; positionType = CSSPositionType.RELATIVE;
flexWrap = CSSWrap.NOWRAP; flexWrap = CSSWrap.NO_WRAP;
overflow = CSSOverflow.VISIBLE; overflow = CSSOverflow.VISIBLE;
flexGrow = 0; flexGrow = 0;
flexShrink = 0; flexShrink = 0;

View File

@@ -10,6 +10,24 @@
package com.facebook.csslayout; package com.facebook.csslayout;
public enum CSSWrap { public enum CSSWrap {
NOWRAP, NO_WRAP(0),
WRAP, WRAP(1);
private int mIntValue;
CSSWrap(int intValue) {
mIntValue = intValue;
}
public int intValue() {
return mIntValue;
}
public static CSSWrap fromInt(int value) {
switch (value) {
case 0: return NO_WRAP;
case 1: return WRAP;
default: throw new IllegalArgumentException("Unkown enum value: " + value);
}
}
} }

View File

@@ -68,14 +68,21 @@ static CSSSize _jniMeasureFunc(CSSNodeRef node,
return CSSSize{measuredWidth, measuredHeight}; return CSSSize{measuredWidth, measuredHeight};
} }
struct JCSSLogLevel : public JavaClass<JCSSLogLevel> {
static constexpr auto kJavaDescriptor = "Lcom/facebook/csslayout/CSSLogLevel;";
};
static global_ref<jobject> *jLogger; static global_ref<jobject> *jLogger;
static int _jniLog(CSSLogLevel level, const char *format, va_list args) { static int _jniLog(CSSLogLevel level, const char *format, va_list args) {
char buffer[256]; char buffer[256];
int result = vsnprintf(buffer, sizeof(buffer), format, args); int result = vsnprintf(buffer, sizeof(buffer), format, args);
static auto logFunc = static auto logFunc =
findClassLocal("com/facebook/csslayout/CSSLogger")->getMethod<void(jint, jstring)>("log"); findClassLocal("com/facebook/csslayout/CSSLogger")->getMethod<void(local_ref<JCSSLogLevel>, jstring)>("log");
logFunc(jLogger->get(), static_cast<jint>(level), Environment::current()->NewStringUTF(buffer));
static auto logLevelFromInt = JCSSLogLevel::javaClassStatic()->getStaticMethod<JCSSLogLevel::javaobject(jint)>("fromInt");
logFunc(jLogger->get(), logLevelFromInt(JCSSLogLevel::javaClassStatic(), static_cast<jint>(level)), Environment::current()->NewStringUTF(buffer));
return result; return result;
} }

View File

@@ -40,35 +40,35 @@ public class CSSNodeTest {
assertEquals(100, (int) node.getLayoutHeight()); assertEquals(100, (int) node.getLayoutHeight());
} }
private int mLogLevel; private CSSLogLevel mLogLevel;
private String mLogMessage; private String mLogMessage;
@Test @Test
public void testLogger() { public void testLogger() {
CSSNode.setLogger(new CSSLogger() { CSSNode.setLogger(new CSSLogger() {
public void log(int level, String message) { public void log(CSSLogLevel level, String message) {
mLogLevel = level; mLogLevel = level;
mLogMessage = message; mLogMessage = message;
} }
}); });
CSSNode.jni_CSSLog(CSSLogger.LOG_LEVEL_DEBUG, "Hello"); CSSNode.jni_CSSLog(CSSLogLevel.DEBUG.intValue(), "Hello");
assertEquals(CSSLogger.LOG_LEVEL_DEBUG, mLogLevel); assertEquals(CSSLogLevel.DEBUG, mLogLevel);
assertEquals("Hello", mLogMessage); assertEquals("Hello", mLogMessage);
} }
@Test @Test
public void testUpdateLogger() { public void testUpdateLogger() {
CSSNode.setLogger(new CSSLogger() { CSSNode.setLogger(new CSSLogger() {
public void log(int level, String message) {} public void log(CSSLogLevel level, String message) {}
}); });
CSSNode.setLogger(new CSSLogger() { CSSNode.setLogger(new CSSLogger() {
public void log(int level, String message) { public void log(CSSLogLevel level, String message) {
mLogLevel = level; mLogLevel = level;
mLogMessage = message; mLogMessage = message;
} }
}); });
CSSNode.jni_CSSLog(CSSLogger.LOG_LEVEL_VERBOSE, "Flexbox"); CSSNode.jni_CSSLog(CSSLogLevel.VERBOSE.intValue(), "Flexbox");
assertEquals(CSSLogger.LOG_LEVEL_VERBOSE, mLogLevel); assertEquals(CSSLogLevel.VERBOSE, mLogLevel);
assertEquals("Flexbox", mLogMessage); assertEquals("Flexbox", mLogMessage);
} }
} }