Skip to content

Commit

Permalink
Expose box sizing getters and setters in Yoga
Browse files Browse the repository at this point in the history
Differential Revision: D63135970
  • Loading branch information
joevilches authored and facebook-github-bot committed Sep 25, 2024
1 parent eb7c439 commit 62de1cd
Show file tree
Hide file tree
Showing 12 changed files with 134 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

// @generated by enums.py

package com.facebook.yoga;

public enum YogaBoxSizing {
CONTENT_BOX(0),
BORDER_BOX(1);

private final int mIntValue;

YogaBoxSizing(int intValue) {
mIntValue = intValue;
}

public int intValue() {
return mIntValue;
}

public static YogaBoxSizing fromInt(int value) {
switch (value) {
case 0: return CONTENT_BOX;
case 1: return BORDER_BOX;
default: throw new IllegalArgumentException("Unknown enum value: " + value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public class YogaNative {
static native void jni_YGNodeStyleSetAlignContentJNI(long nativePointer, int alignContent);
static native int jni_YGNodeStyleGetPositionTypeJNI(long nativePointer);
static native void jni_YGNodeStyleSetPositionTypeJNI(long nativePointer, int positionType);
static native int jni_YGNodeStyleGetBoxSizingJNI(long nativePointer);
static native void jni_YGNodeStyleSetBoxSizingJNI(long nativePointer, int boxSizing);
static native int jni_YGNodeStyleGetFlexWrapJNI(long nativePointer);
static native void jni_YGNodeStyleSetFlexWrapJNI(long nativePointer, int wrapType);
static native int jni_YGNodeStyleGetOverflowJNI(long nativePointer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ public interface Inputs {

public abstract void setPositionType(YogaPositionType positionType);

public abstract YogaBoxSizing getBoxSizing();

public abstract void setBoxSizing(YogaBoxSizing boxSizing);

public abstract YogaWrap getWrap();

public abstract void setWrap(YogaWrap flexWrap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,14 @@ public void setPositionType(YogaPositionType positionType) {
YogaNative.jni_YGNodeStyleSetPositionTypeJNI(mNativePointer, positionType.intValue());
}

public YogaBoxSizing getBoxSizing() {
return YogaBoxSizing.fromInt(YogaNative.jni_YGNodeStyleGetBoxSizingJNI(mNativePointer));
}

public void setBoxSizing(YogaBoxSizing boxSizing) {
YogaNative.jni_YGNodeStyleSetBoxSizingJNI(mNativePointer, boxSizing.intValue());
}

public YogaWrap getWrap() {
return YogaWrap.fromInt(YogaNative.jni_YGNodeStyleGetFlexWrapJNI(mNativePointer));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ public interface YogaProps {

void setBaselineFunction(YogaBaselineFunction yogaBaselineFunction);

void setBoxSizing(YogaBoxSizing boxSizing);

/* Getters */

YogaValue getWidth();
Expand Down Expand Up @@ -148,4 +150,6 @@ public interface YogaProps {
YogaValue getPosition(YogaEdge edge);

float getBorder(YogaEdge edge);

YogaBoxSizing getBoxSizing();
}
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ YG_NODE_JNI_STYLE_PROP(jint, YGAlign, AlignItems);
YG_NODE_JNI_STYLE_PROP(jint, YGAlign, AlignSelf);
YG_NODE_JNI_STYLE_PROP(jint, YGAlign, AlignContent);
YG_NODE_JNI_STYLE_PROP(jint, YGPositionType, PositionType);
YG_NODE_JNI_STYLE_PROP(jint, YGBoxSizing, BoxSizing);
YG_NODE_JNI_STYLE_PROP(jint, YGWrap, FlexWrap);
YG_NODE_JNI_STYLE_PROP(jint, YGOverflow, Overflow);
YG_NODE_JNI_STYLE_PROP(jint, YGDisplay, Display);
Expand Down Expand Up @@ -819,6 +820,12 @@ static JNINativeMethod methods[] = {
{"jni_YGNodeStyleSetPositionTypeJNI",
"(JI)V",
(void*)jni_YGNodeStyleSetPositionTypeJNI},
{"jni_YGNodeStyleGetBoxSizingJNI",
"(J)I",
(void*)jni_YGNodeStyleGetBoxSizingJNI},
{"jni_YGNodeStyleSetBoxSizingJNI",
"(JI)V",
(void*)jni_YGNodeStyleSetBoxSizingJNI},
{"jni_YGNodeStyleGetFlexWrapJNI",
"(J)I",
(void*)jni_YGNodeStyleGetFlexWrapJNI},
Expand Down
10 changes: 10 additions & 0 deletions packages/react-native/ReactCommon/yoga/yoga/YGEnums.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ const char* YGAlignToString(const YGAlign value) {
return "unknown";
}

const char* YGBoxSizingToString(const YGBoxSizing value) {
switch (value) {
case YGBoxSizingContentBox:
return "content-box";
case YGBoxSizingBorderBox:
return "border-box";
}
return "unknown";
}

const char* YGDimensionToString(const YGDimension value) {
switch (value) {
case YGDimensionWidth:
Expand Down
5 changes: 5 additions & 0 deletions packages/react-native/ReactCommon/yoga/yoga/YGEnums.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ YG_ENUM_DECL(
YGAlignSpaceAround,
YGAlignSpaceEvenly)

YG_ENUM_DECL(
YGBoxSizing,
YGBoxSizingContentBox,
YGBoxSizingBorderBox)

YG_ENUM_DECL(
YGDimension,
YGDimensionWidth,
Expand Down
9 changes: 9 additions & 0 deletions packages/react-native/ReactCommon/yoga/yoga/YGNodeStyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,15 @@ float YGNodeStyleGetAspectRatio(const YGNodeConstRef node) {
return op.isUndefined() ? YGUndefined : op.unwrap();
}

void YGNodeStyleSetBoxSizing(YGNodeRef node, YGBoxSizing boxSizing) {
updateStyle<&Style::boxSizing, &Style::setBoxSizing>(
node, scopedEnum(boxSizing));
}

YGBoxSizing YGNodeStyleGetBoxSizing(const YGNodeConstRef node) {
return unscopedEnum(resolveRef(node)->style().boxSizing());
}

void YGNodeStyleSetWidth(YGNodeRef node, float points) {
updateStyle<&Style::dimension, &Style::setDimension>(
node, Dimension::Width, value::points(points));
Expand Down
3 changes: 3 additions & 0 deletions packages/react-native/ReactCommon/yoga/yoga/YGNodeStyle.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ YG_EXPORT void
YGNodeStyleSetGapPercent(YGNodeRef node, YGGutter gutter, float gapLength);
YG_EXPORT float YGNodeStyleGetGap(YGNodeConstRef node, YGGutter gutter);

YG_EXPORT void YGNodeStyleSetBoxSizing(YGNodeRef node, YGBoxSizing boxSizing);
YG_EXPORT YGBoxSizing YGNodeStyleGetBoxSizing(YGNodeConstRef node);

YG_EXPORT void YGNodeStyleSetWidth(YGNodeRef node, float width);
YG_EXPORT void YGNodeStyleSetWidthPercent(YGNodeRef node, float width);
YG_EXPORT void YGNodeStyleSetWidthAuto(YGNodeRef node);
Expand Down
40 changes: 40 additions & 0 deletions packages/react-native/ReactCommon/yoga/yoga/enums/BoxSizing.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

// @generated by enums.py
// clang-format off
#pragma once

#include <cstdint>
#include <yoga/YGEnums.h>
#include <yoga/enums/YogaEnums.h>

namespace facebook::yoga {

enum class BoxSizing : uint8_t {
ContentBox = YGBoxSizingContentBox,
BorderBox = YGBoxSizingBorderBox,
};

template <>
constexpr int32_t ordinalCount<BoxSizing>() {
return 2;
}

constexpr BoxSizing scopedEnum(YGBoxSizing unscoped) {
return static_cast<BoxSizing>(unscoped);
}

constexpr YGBoxSizing unscopedEnum(BoxSizing scoped) {
return static_cast<YGBoxSizing>(scoped);
}

inline const char* toString(BoxSizing e) {
return YGBoxSizingToString(unscopedEnum(e));
}

} // namespace facebook::yoga
9 changes: 9 additions & 0 deletions packages/react-native/ReactCommon/yoga/yoga/style/Style.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include <yoga/algorithm/FlexDirection.h>
#include <yoga/enums/Align.h>
#include <yoga/enums/BoxSizing.h>
#include <yoga/enums/Dimension.h>
#include <yoga/enums/Direction.h>
#include <yoga/enums/Display.h>
Expand Down Expand Up @@ -206,6 +207,13 @@ class YG_EXPORT Style {
value == 0.0f || std::isinf(value.unwrap()) ? FloatOptional{} : value);
}

BoxSizing boxSizing() const {
return boxSizing_;
}
void setBoxSizing(BoxSizing value) {
boxSizing_ = value;
}

bool horizontalInsetsDefined() const {
return position_[yoga::to_underlying(Edge::Left)].isDefined() ||
position_[yoga::to_underlying(Edge::Right)].isDefined() ||
Expand Down Expand Up @@ -675,6 +683,7 @@ class YG_EXPORT Style {
Wrap flexWrap_ : bitCount<Wrap>() = Wrap::NoWrap;
Overflow overflow_ : bitCount<Overflow>() = Overflow::Visible;
Display display_ : bitCount<Display>() = Display::Flex;
BoxSizing boxSizing_ : bitCount<BoxSizing>() = BoxSizing::BorderBox;

StyleValueHandle flex_{};
StyleValueHandle flexGrow_{};
Expand Down

0 comments on commit 62de1cd

Please sign in to comment.