Files
react-native/ReactCommon/fabric/components/textinput/androidtextinput/AndroidTextInputComponentDescriptor.h
T
Joshua Gross 5be86695a3 AndroidTextInput uses default padding from Android theme
Summary:
For backwards-compatibility with Paper, we're implementing a feature in Fabric that will allow TextInputs to use the default padding from the theme in Android.

Note that this uses some pretty ugly hacks that probably shouldn't be used inside of components at all: looking directly at rawProps, overriding props/Yoga styles in the component descriptor, etc. I would (personally) really like to kill this feature entirely unless and until we can find a more elegant solution.

Changelog: [Internal]

TextInputs are still not pixel-perfect with Paper, but they're much closer, and the underline visual glitchiness is no longer an issue.

Reviewed By: mdvacca

Differential Revision: D20109605

fbshipit-source-id: 543282843e0a9f03a504d72d7a014431099bd64c
2020-02-26 13:46:57 -08:00

138 lines
5.7 KiB
C++

/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <react/core/ConcreteComponentDescriptor.h>
#include "AndroidTextInputShadowNode.h"
#include <yoga/YGEnums.h>
#include <yoga/YGValue.h>
#include <yoga/CompactValue.h>
namespace facebook {
namespace react {
/*
* Descriptor for <AndroidTextInput> component.
*/
class AndroidTextInputComponentDescriptor final
: public ConcreteComponentDescriptor<AndroidTextInputShadowNode> {
public:
AndroidTextInputComponentDescriptor(
ComponentDescriptorParameters const &parameters)
: ConcreteComponentDescriptor<AndroidTextInputShadowNode>(parameters) {
// Every single `AndroidTextInputShadowNode` will have a reference to
// a shared `TextLayoutManager`.
textLayoutManager_ = std::make_shared<TextLayoutManager>(contextContainer_);
}
virtual State::Shared createInitialState(
ShadowNodeFragment const &fragment,
ShadowNodeFamily::Shared const &family) const override {
int surfaceId = family->getSurfaceId();
float defaultThemePaddingStart = NAN;
float defaultThemePaddingEnd = NAN;
float defaultThemePaddingTop = NAN;
float defaultThemePaddingBottom = NAN;
if (surfaceIdToThemePaddingMap_.find(surfaceId) != surfaceIdToThemePaddingMap_.end()) {
YGStyle::Edges theme = surfaceIdToThemePaddingMap_[surfaceId];
defaultThemePaddingStart = ((YGValue)theme[YGEdgeStart]).value;
defaultThemePaddingEnd = ((YGValue)theme[YGEdgeEnd]).value;
defaultThemePaddingTop = ((YGValue)theme[YGEdgeTop]).value;
defaultThemePaddingBottom = ((YGValue)theme[YGEdgeBottom]).value;
}
return std::make_shared<AndroidTextInputShadowNode::ConcreteState>(
std::make_shared<AndroidTextInputState const>(AndroidTextInputState(
0, {}, {}, {}, {}, {}, textLayoutManager_, defaultThemePaddingStart, defaultThemePaddingEnd, defaultThemePaddingTop, defaultThemePaddingBottom)),
family);
}
protected:
void adopt(UnsharedShadowNode shadowNode) const override {
assert(std::dynamic_pointer_cast<AndroidTextInputShadowNode>(shadowNode));
auto textInputShadowNode =
std::static_pointer_cast<AndroidTextInputShadowNode>(shadowNode);
// `ParagraphShadowNode` uses `TextLayoutManager` to measure text content
// and communicate text rendering metrics to mounting layer.
textInputShadowNode->setTextLayoutManager(textLayoutManager_);
textInputShadowNode->setContextContainer(
const_cast<ContextContainer *>(getContextContainer().get()));
// Get theme padding from cache, or set it from State.
// In theory, the Java ViewManager for TextInput should need to set state *exactly once*
// per surface to communicate the correct default padding, which will be cached here in C++.
// TODO T63008435: can this feature be removed entirely?
// TODO: figure out RTL/start/end/left/right stuff here
int surfaceId = textInputShadowNode->getSurfaceId();
const AndroidTextInputState &state = textInputShadowNode->getStateData();
if (surfaceIdToThemePaddingMap_.find(surfaceId) == surfaceIdToThemePaddingMap_.end() && !isnan(state.defaultThemePaddingStart)) {
YGStyle::Edges result;
result[YGEdgeStart] = (YGValue){state.defaultThemePaddingStart, YGUnitPoint};
result[YGEdgeEnd] = (YGValue){state.defaultThemePaddingEnd, YGUnitPoint};
result[YGEdgeTop] = (YGValue){state.defaultThemePaddingTop, YGUnitPoint};
result[YGEdgeBottom] = (YGValue){state.defaultThemePaddingBottom, YGUnitPoint};
surfaceIdToThemePaddingMap_.emplace(std::make_pair(surfaceId, result));
}
if (surfaceIdToThemePaddingMap_.find(surfaceId) != surfaceIdToThemePaddingMap_.end()) {
YGStyle::Edges theme = surfaceIdToThemePaddingMap_[surfaceId];
// Override padding
// Node is still unsealed during adoption, before layout is complete
// TODO: T62959168 account for RTL and paddingLeft when setting default paddingStart, and
// vice-versa with paddingRight/paddingEnd
YGStyle::Edges result = textInputShadowNode->getConcreteProps().yogaStyle.padding();
bool changedPadding = false;
if (!textInputShadowNode->getConcreteProps().hasPaddingStart) {
changedPadding = true;
result[YGEdgeStart] = theme[YGEdgeStart];
}
if (!textInputShadowNode->getConcreteProps().hasPaddingEnd) {
changedPadding = true;
result[YGEdgeEnd] = theme[YGEdgeEnd];
}
if (!textInputShadowNode->getConcreteProps().hasPaddingTop) {
changedPadding = true;
result[YGEdgeTop] = theme[YGEdgeTop];
}
if (!textInputShadowNode->getConcreteProps().hasPaddingBottom) {
changedPadding = true;
result[YGEdgeBottom] = theme[YGEdgeBottom];
}
// 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&>(textInputShadowNode->getConcreteProps()).yogaStyle.padding() = result;
// Communicate new props to Yoga part of the node
textInputShadowNode->setProps((YogaStylableProps)textInputShadowNode->getConcreteProps());
}
}
textInputShadowNode->dirtyLayout();
textInputShadowNode->enableMeasurement();
ConcreteComponentDescriptor::adopt(shadowNode);
}
private:
SharedTextLayoutManager textLayoutManager_;
mutable better::map<int, YGStyle::Edges> surfaceIdToThemePaddingMap_;
};
} // namespace react
} // namespace facebook