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
This commit is contained in:
Joshua Gross
2020-02-26 13:46:57 -08:00
committed by Facebook Github Bot
parent 6ea963de6a
commit 5be86695a3
7 changed files with 178 additions and 8 deletions
@@ -23,9 +23,11 @@ import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.core.view.ViewCompat;
import com.facebook.common.logging.FLog;
import com.facebook.infer.annotation.Assertions;
import com.facebook.react.bridge.Dynamic;
@@ -1229,11 +1231,42 @@ public class ReactTextInputManager extends BaseViewManager<ReactEditText, Layout
return androidTextBreakStrategy;
}
/**
* May be overriden by subclasses that would like to provide their own instance of the internal
* {@code EditText} this class uses to determine the expected size of the view.
*/
protected EditText createInternalEditText(ThemedReactContext themedReactContext) {
return new EditText(themedReactContext);
}
@Override
public Object updateState(
ReactEditText view, ReactStylesDiffMap props, @Nullable StateWrapper stateWrapper) {
// TODO T55794595: Add support for updating state with null stateWrapper
ReadableNativeMap state = stateWrapper.getState();
// Do we need to communicate theme back to C++?
// If so, this should only need to be done once per surface.
if (!state.getBoolean("hasThemeData")) {
WritableNativeMap update = new WritableNativeMap();
EditText editText = createInternalEditText((ThemedReactContext) view.getContext());
// Even though we check `data["textChanged"].empty()` before using the value in C++,
// state updates crash without this value on key exception. It's unintuitive why
// folly::dynamic is crashing there and if there's any way to fix on the native side,
// so leave this here until we can figure out a better way of key-existence-checking in C++.
update.putNull("textChanged");
update.putDouble(
"themePaddingStart", PixelUtil.toDIPFromPixel(ViewCompat.getPaddingStart(editText)));
update.putDouble(
"themePaddingEnd", PixelUtil.toDIPFromPixel(ViewCompat.getPaddingEnd(editText)));
update.putDouble("themePaddingTop", PixelUtil.toDIPFromPixel(editText.getPaddingTop()));
update.putDouble("themePaddingBottom", PixelUtil.toDIPFromPixel(editText.getPaddingBottom()));
stateWrapper.updateState(update);
}
ReadableMap attributedString = state.getMap("attributedString");
ReadableMap paragraphAttributes = state.getMap("paragraphAttributes");