Remove yoga::Style::Edges usage in AndroidTextInputComponentDescriptor (#41598)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/41598

This code merges native, Android provided theme text input padding, with Yoga style. We are removing operations on all edges as aggregate, so this replaces that.

This was previously part of D50998164

Changelog: [Internal]

Reviewed By: javache

Differential Revision: D51503493

fbshipit-source-id: c6e2f3183a05861745fdd8f044d12e3dd8205804
This commit is contained in:
Nick Gerleman
2023-11-21 23:17:32 -08:00
committed by Facebook GitHub Bot
parent c4519a01ac
commit 08f89eb2ae
@@ -40,7 +40,7 @@ class AndroidTextInputComponentDescriptor final
const ShadowNodeFamily::Shared& family) const override {
int surfaceId = family->getSurfaceId();
yoga::Style::Edges theme;
ThemePadding theme;
// TODO: figure out RTL/start/end/left/right stuff here
if (surfaceIdToThemePaddingMap_.find(surfaceId) !=
surfaceIdToThemePaddingMap_.end()) {
@@ -59,11 +59,10 @@ class AndroidTextInputComponentDescriptor final
fabricUIManager, surfaceId, defaultTextInputPaddingArray)) {
jfloat* defaultTextInputPadding =
env->GetFloatArrayElements(defaultTextInputPaddingArray, 0);
theme[YGEdgeStart] = (YGValue){defaultTextInputPadding[0], YGUnitPoint};
theme[YGEdgeEnd] = (YGValue){defaultTextInputPadding[1], YGUnitPoint};
theme[YGEdgeTop] = (YGValue){defaultTextInputPadding[2], YGUnitPoint};
theme[YGEdgeBottom] =
(YGValue){defaultTextInputPadding[3], YGUnitPoint};
theme.start = defaultTextInputPadding[0];
theme.end = defaultTextInputPadding[1];
theme.top = defaultTextInputPadding[2];
theme.bottom = defaultTextInputPadding[3];
surfaceIdToThemePaddingMap_.emplace(std::make_pair(surfaceId, theme));
env->ReleaseFloatArrayElements(
defaultTextInputPaddingArray, defaultTextInputPadding, JNI_ABORT);
@@ -73,14 +72,7 @@ class AndroidTextInputComponentDescriptor final
return std::make_shared<AndroidTextInputShadowNode::ConcreteState>(
std::make_shared<const AndroidTextInputState>(AndroidTextInputState(
0,
{},
{},
{},
((YGValue)theme[YGEdgeStart]).value,
((YGValue)theme[YGEdgeEnd]).value,
((YGValue)theme[YGEdgeTop]).value,
((YGValue)theme[YGEdgeBottom]).value)),
0, {}, {}, {}, theme.start, theme.end, theme.top, theme.bottom)),
family);
}
@@ -99,7 +91,7 @@ class AndroidTextInputComponentDescriptor final
int surfaceId = textInputShadowNode.getSurfaceId();
if (surfaceIdToThemePaddingMap_.find(surfaceId) !=
surfaceIdToThemePaddingMap_.end()) {
yoga::Style::Edges theme = surfaceIdToThemePaddingMap_[surfaceId];
const auto& theme = surfaceIdToThemePaddingMap_[surfaceId];
auto& textInputProps = textInputShadowNode.getConcreteProps();
@@ -108,29 +100,33 @@ class AndroidTextInputComponentDescriptor final
// TODO: T62959168 account for RTL and paddingLeft when setting default
// paddingStart, and vice-versa with paddingRight/paddingEnd.
// For now this assumes no RTL.
yoga::Style::Edges result = textInputProps.yogaStyle.padding();
auto& style = const_cast<yoga::Style&>(textInputProps.yogaStyle);
bool changedPadding = false;
if (!textInputProps.hasPadding && !textInputProps.hasPaddingStart &&
!textInputProps.hasPaddingLeft &&
!textInputProps.hasPaddingHorizontal) {
changedPadding = true;
result[YGEdgeStart] = theme[YGEdgeStart];
style.padding()[YGEdgeStart] =
yoga::CompactValue::of<YGUnitPoint>(theme.start);
}
if (!textInputProps.hasPadding && !textInputProps.hasPaddingEnd &&
!textInputProps.hasPaddingRight &&
!textInputProps.hasPaddingHorizontal) {
changedPadding = true;
result[YGEdgeEnd] = theme[YGEdgeEnd];
style.padding()[YGEdgeEnd] =
yoga::CompactValue::of<YGUnitPoint>(theme.end);
}
if (!textInputProps.hasPadding && !textInputProps.hasPaddingTop &&
!textInputProps.hasPaddingVertical) {
changedPadding = true;
result[YGEdgeTop] = theme[YGEdgeTop];
style.padding()[YGEdgeTop] =
yoga::CompactValue::of<YGUnitPoint>(theme.top);
}
if (!textInputProps.hasPadding && !textInputProps.hasPaddingBottom &&
!textInputProps.hasPaddingVertical) {
changedPadding = true;
result[YGEdgeBottom] = theme[YGEdgeBottom];
style.padding()[YGEdgeBottom] =
yoga::CompactValue::of<YGUnitPoint>(theme.bottom);
}
// If the TextInput initially does not have paddingLeft or paddingStart, a
@@ -141,21 +137,18 @@ class AndroidTextInputComponentDescriptor final
if ((textInputProps.hasPadding || textInputProps.hasPaddingLeft ||
textInputProps.hasPaddingHorizontal) &&
!textInputProps.hasPaddingStart) {
result[YGEdgeStart] = YGValueUndefined;
style.padding()[YGEdgeStart] = yoga::CompactValue::ofUndefined();
}
if ((textInputProps.hasPadding || textInputProps.hasPaddingRight ||
textInputProps.hasPaddingHorizontal) &&
!textInputProps.hasPaddingEnd) {
result[YGEdgeEnd] = YGValueUndefined;
style.padding()[YGEdgeEnd] = yoga::CompactValue::ofUndefined();
}
// Note that this is expensive: on every adopt, we need to set the Yoga
// props again, which normally only happens during prop parsing. Every
// commit, state update, etc, will incur this cost.
if (changedPadding) {
// Set new props on node
const_cast<AndroidTextInputProps&>(textInputProps).yogaStyle.padding() =
result;
// Communicate new props to Yoga part of the node
textInputShadowNode.updateYogaProps();
}
@@ -168,13 +161,19 @@ class AndroidTextInputComponentDescriptor final
}
private:
struct ThemePadding {
float start{};
float end{};
float top{};
float bottom{};
};
// TODO T68526882: Unify with Binding::UIManagerJavaDescriptor
constexpr static auto UIManagerJavaDescriptor =
"com/facebook/react/fabric/FabricUIManager";
SharedTextLayoutManager textLayoutManager_;
mutable std::unordered_map<int, yoga::Style::Edges>
surfaceIdToThemePaddingMap_;
mutable std::unordered_map<int, ThemePadding> surfaceIdToThemePaddingMap_;
};
} // namespace facebook::react