Skip to content

Commit

Permalink
Support for UpdatePaddingMountItem
Browse files Browse the repository at this point in the history
Summary: Some views (TextInput!) need padding props.

Reviewed By: mdvacca

Differential Revision: D17081799

fbshipit-source-id: 4f5d6a139bb4dd878f90af0ed4a30fe3810e3429
  • Loading branch information
JoshuaGross authored and facebook-github-bot committed Aug 28, 2019
1 parent 51aacd5 commit 44bfc4b
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.facebook.react.fabric.mounting.mountitems.UpdateEventEmitterMountItem;
import com.facebook.react.fabric.mounting.mountitems.UpdateLayoutMountItem;
import com.facebook.react.fabric.mounting.mountitems.UpdateLocalDataMountItem;
import com.facebook.react.fabric.mounting.mountitems.UpdatePaddingMountItem;
import com.facebook.react.fabric.mounting.mountitems.UpdatePropsMountItem;
import com.facebook.react.uimanager.StateWrapper;
import com.facebook.react.uimanager.UIManagerModule;
Expand Down Expand Up @@ -108,6 +109,7 @@ private static void loadClasses() {
UpdateEventEmitterMountItem.class.getClass();
UpdateLayoutMountItem.class.getClass();
UpdateLocalDataMountItem.class.getClass();
UpdatePaddingMountItem.class.getClass();
UpdatePropsMountItem.class.getClass();
LayoutMetricsConversions.class.getClass();
MountingManager.class.getClass();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import com.facebook.react.fabric.mounting.mountitems.UpdateEventEmitterMountItem;
import com.facebook.react.fabric.mounting.mountitems.UpdateLayoutMountItem;
import com.facebook.react.fabric.mounting.mountitems.UpdateLocalDataMountItem;
import com.facebook.react.fabric.mounting.mountitems.UpdatePaddingMountItem;
import com.facebook.react.fabric.mounting.mountitems.UpdatePropsMountItem;
import com.facebook.react.fabric.mounting.mountitems.UpdateStateMountItem;
import com.facebook.react.modules.core.ReactChoreographer;
Expand Down Expand Up @@ -285,6 +286,12 @@ private MountItem updateLayoutMountItem(
return new UpdateLayoutMountItem(reactTag, x, y, width, height, layoutDirection);
}

@DoNotStrip
@SuppressWarnings("unused")
private MountItem updatePaddingMountItem(int reactTag, int left, int top, int right, int bottom) {
return new UpdatePaddingMountItem(reactTag, left, top, right, bottom);
}

@DoNotStrip
@SuppressWarnings("unused")
private MountItem updatePropsMountItem(int reactTag, ReadableMap map) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,34 @@ local_ref<JMountItem::javaobject> createUpdateLayoutMountItem(
return nullptr;
}

local_ref<JMountItem::javaobject> createUpdatePaddingMountItem(
const jni::global_ref<jobject> &javaUIManager,
const ShadowViewMutation &mutation) {

auto oldChildShadowView = mutation.oldChildShadowView;
auto newChildShadowView = mutation.newChildShadowView;

if (oldChildShadowView.layoutMetrics.contentInsets == newChildShadowView.layoutMetrics.contentInsets) {
return nullptr;
}

static auto updateLayoutInstruction =
jni::findClassStatic(UIManagerJavaDescriptor)
->getMethod<alias_ref<JMountItem>(jint, jint, jint, jint, jint)>(
"updatePaddingMountItem");

auto layoutMetrics = newChildShadowView.layoutMetrics;
auto pointScaleFactor = layoutMetrics.pointScaleFactor;
auto contentInsets = layoutMetrics.contentInsets;

int left = round(contentInsets.left * pointScaleFactor);
int top = round(contentInsets.top * pointScaleFactor);
int right = round(contentInsets.right * pointScaleFactor);
int bottom = round(contentInsets.bottom * pointScaleFactor);

return updateLayoutInstruction(javaUIManager, newChildShadowView.tag, left, top, right, bottom);
}

local_ref<JMountItem::javaobject> createInsertMountItem(
const jni::global_ref<jobject> &javaUIManager,
const ShadowViewMutation &mutation) {
Expand Down Expand Up @@ -559,6 +587,11 @@ void Binding::schedulerDidFinishTransaction(
if (updateLayoutMountItem) {
mountItems[position++] = updateLayoutMountItem;
}

auto updatePaddingMountItem = createUpdatePaddingMountItem(localJavaUIManager, mutation);
if (updatePaddingMountItem) {
mountItems[position++] = updatePaddingMountItem;
}
}

if (mutation.oldChildShadowView.eventEmitter !=
Expand Down Expand Up @@ -602,6 +635,13 @@ void Binding::schedulerDidFinishTransaction(
if (updateLayoutMountItem) {
mountItems[position++] = updateLayoutMountItem;
}

// Padding
auto updatePaddingMountItem =
createUpdatePaddingMountItem(localJavaUIManager, mutation);
if (updatePaddingMountItem) {
mountItems[position++] = updatePaddingMountItem;
}
}

// EventEmitter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,24 @@ public void updateLayout(int reactTag, int x, int y, int width, int height) {
viewToUpdate.layout(x, y, x + width, y + height);
}

@UiThread
public void updatePadding(int reactTag, int left, int top, int right, int bottom) {
UiThreadUtil.assertOnUiThread();

ViewState viewState = getViewState(reactTag);
// Do not layout Root Views
if (viewState.mIsRoot) {
return;
}

View viewToUpdate = viewState.mView;
if (viewToUpdate == null) {
throw new IllegalStateException("Unable to find View for tag: " + reactTag);
}

viewToUpdate.setPadding(left, top, right, bottom);
}

@UiThread
public void deleteView(int reactTag) {
UiThreadUtil.assertOnUiThread();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* Copyright (c) 2014-present, Facebook, Inc.
*
* <p>This source code is licensed under the MIT license found in the LICENSE file in the root
* directory of this source tree.
*/
package com.facebook.react.fabric.mounting.mountitems;

import com.facebook.react.fabric.mounting.MountingManager;

/**
* A MountItem that represents setting the padding properties of a view (left, top, right, bottom).
* This is distinct from layout because it happens far less frequently from layout; so it is a perf
* optimization to transfer this data and execute the methods strictly less than the layout-related
* properties.
*/
public class UpdatePaddingMountItem implements MountItem {

private final int mReactTag;
private final int mLeft;
private final int mTop;
private final int mRight;
private final int mBottom;

public UpdatePaddingMountItem(int reactTag, int left, int top, int right, int bottom) {
mReactTag = reactTag;
mLeft = left;
mTop = top;
mRight = right;
mBottom = bottom;
}

@Override
public void execute(MountingManager mountingManager) {
mountingManager.updatePadding(mReactTag, mLeft, mTop, mRight, mBottom);
}

@Override
public String toString() {
return "UpdatePaddingMountItem ["
+ mReactTag
+ "] - left: "
+ mLeft
+ " - top: "
+ mTop
+ " - right: "
+ mRight
+ " - bottom: "
+ mBottom;
}
}

0 comments on commit 44bfc4b

Please sign in to comment.