Skip to content

Commit 44bfc4b

Browse files
JoshuaGrossfacebook-github-bot
authored andcommitted
Support for UpdatePaddingMountItem
Summary: Some views (TextInput!) need padding props. Reviewed By: mdvacca Differential Revision: D17081799 fbshipit-source-id: 4f5d6a139bb4dd878f90af0ed4a30fe3810e3429
1 parent 51aacd5 commit 44bfc4b

File tree

5 files changed

+118
-0
lines changed

5 files changed

+118
-0
lines changed

ReactAndroid/src/main/java/com/facebook/react/fabric/FabricJSIModuleProvider.java

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.facebook.react.fabric.mounting.mountitems.UpdateEventEmitterMountItem;
2323
import com.facebook.react.fabric.mounting.mountitems.UpdateLayoutMountItem;
2424
import com.facebook.react.fabric.mounting.mountitems.UpdateLocalDataMountItem;
25+
import com.facebook.react.fabric.mounting.mountitems.UpdatePaddingMountItem;
2526
import com.facebook.react.fabric.mounting.mountitems.UpdatePropsMountItem;
2627
import com.facebook.react.uimanager.StateWrapper;
2728
import com.facebook.react.uimanager.UIManagerModule;
@@ -108,6 +109,7 @@ private static void loadClasses() {
108109
UpdateEventEmitterMountItem.class.getClass();
109110
UpdateLayoutMountItem.class.getClass();
110111
UpdateLocalDataMountItem.class.getClass();
112+
UpdatePaddingMountItem.class.getClass();
111113
UpdatePropsMountItem.class.getClass();
112114
LayoutMetricsConversions.class.getClass();
113115
MountingManager.class.getClass();

ReactAndroid/src/main/java/com/facebook/react/fabric/FabricUIManager.java

+7
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import com.facebook.react.fabric.mounting.mountitems.UpdateEventEmitterMountItem;
5555
import com.facebook.react.fabric.mounting.mountitems.UpdateLayoutMountItem;
5656
import com.facebook.react.fabric.mounting.mountitems.UpdateLocalDataMountItem;
57+
import com.facebook.react.fabric.mounting.mountitems.UpdatePaddingMountItem;
5758
import com.facebook.react.fabric.mounting.mountitems.UpdatePropsMountItem;
5859
import com.facebook.react.fabric.mounting.mountitems.UpdateStateMountItem;
5960
import com.facebook.react.modules.core.ReactChoreographer;
@@ -285,6 +286,12 @@ private MountItem updateLayoutMountItem(
285286
return new UpdateLayoutMountItem(reactTag, x, y, width, height, layoutDirection);
286287
}
287288

289+
@DoNotStrip
290+
@SuppressWarnings("unused")
291+
private MountItem updatePaddingMountItem(int reactTag, int left, int top, int right, int bottom) {
292+
return new UpdatePaddingMountItem(reactTag, left, top, right, bottom);
293+
}
294+
288295
@DoNotStrip
289296
@SuppressWarnings("unused")
290297
private MountItem updatePropsMountItem(int reactTag, ReadableMap map) {

ReactAndroid/src/main/java/com/facebook/react/fabric/jni/Binding.cpp

+40
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,34 @@ local_ref<JMountItem::javaobject> createUpdateLayoutMountItem(
338338
return nullptr;
339339
}
340340

341+
local_ref<JMountItem::javaobject> createUpdatePaddingMountItem(
342+
const jni::global_ref<jobject> &javaUIManager,
343+
const ShadowViewMutation &mutation) {
344+
345+
auto oldChildShadowView = mutation.oldChildShadowView;
346+
auto newChildShadowView = mutation.newChildShadowView;
347+
348+
if (oldChildShadowView.layoutMetrics.contentInsets == newChildShadowView.layoutMetrics.contentInsets) {
349+
return nullptr;
350+
}
351+
352+
static auto updateLayoutInstruction =
353+
jni::findClassStatic(UIManagerJavaDescriptor)
354+
->getMethod<alias_ref<JMountItem>(jint, jint, jint, jint, jint)>(
355+
"updatePaddingMountItem");
356+
357+
auto layoutMetrics = newChildShadowView.layoutMetrics;
358+
auto pointScaleFactor = layoutMetrics.pointScaleFactor;
359+
auto contentInsets = layoutMetrics.contentInsets;
360+
361+
int left = round(contentInsets.left * pointScaleFactor);
362+
int top = round(contentInsets.top * pointScaleFactor);
363+
int right = round(contentInsets.right * pointScaleFactor);
364+
int bottom = round(contentInsets.bottom * pointScaleFactor);
365+
366+
return updateLayoutInstruction(javaUIManager, newChildShadowView.tag, left, top, right, bottom);
367+
}
368+
341369
local_ref<JMountItem::javaobject> createInsertMountItem(
342370
const jni::global_ref<jobject> &javaUIManager,
343371
const ShadowViewMutation &mutation) {
@@ -559,6 +587,11 @@ void Binding::schedulerDidFinishTransaction(
559587
if (updateLayoutMountItem) {
560588
mountItems[position++] = updateLayoutMountItem;
561589
}
590+
591+
auto updatePaddingMountItem = createUpdatePaddingMountItem(localJavaUIManager, mutation);
592+
if (updatePaddingMountItem) {
593+
mountItems[position++] = updatePaddingMountItem;
594+
}
562595
}
563596

564597
if (mutation.oldChildShadowView.eventEmitter !=
@@ -602,6 +635,13 @@ void Binding::schedulerDidFinishTransaction(
602635
if (updateLayoutMountItem) {
603636
mountItems[position++] = updateLayoutMountItem;
604637
}
638+
639+
// Padding
640+
auto updatePaddingMountItem =
641+
createUpdatePaddingMountItem(localJavaUIManager, mutation);
642+
if (updatePaddingMountItem) {
643+
mountItems[position++] = updatePaddingMountItem;
644+
}
605645
}
606646

607647
// EventEmitter

ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/MountingManager.java

+18
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,24 @@ public void updateLayout(int reactTag, int x, int y, int width, int height) {
246246
viewToUpdate.layout(x, y, x + width, y + height);
247247
}
248248

249+
@UiThread
250+
public void updatePadding(int reactTag, int left, int top, int right, int bottom) {
251+
UiThreadUtil.assertOnUiThread();
252+
253+
ViewState viewState = getViewState(reactTag);
254+
// Do not layout Root Views
255+
if (viewState.mIsRoot) {
256+
return;
257+
}
258+
259+
View viewToUpdate = viewState.mView;
260+
if (viewToUpdate == null) {
261+
throw new IllegalStateException("Unable to find View for tag: " + reactTag);
262+
}
263+
264+
viewToUpdate.setPadding(left, top, right, bottom);
265+
}
266+
249267
@UiThread
250268
public void deleteView(int reactTag) {
251269
UiThreadUtil.assertOnUiThread();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc.
3+
*
4+
* <p>This source code is licensed under the MIT license found in the LICENSE file in the root
5+
* directory of this source tree.
6+
*/
7+
package com.facebook.react.fabric.mounting.mountitems;
8+
9+
import com.facebook.react.fabric.mounting.MountingManager;
10+
11+
/**
12+
* A MountItem that represents setting the padding properties of a view (left, top, right, bottom).
13+
* This is distinct from layout because it happens far less frequently from layout; so it is a perf
14+
* optimization to transfer this data and execute the methods strictly less than the layout-related
15+
* properties.
16+
*/
17+
public class UpdatePaddingMountItem implements MountItem {
18+
19+
private final int mReactTag;
20+
private final int mLeft;
21+
private final int mTop;
22+
private final int mRight;
23+
private final int mBottom;
24+
25+
public UpdatePaddingMountItem(int reactTag, int left, int top, int right, int bottom) {
26+
mReactTag = reactTag;
27+
mLeft = left;
28+
mTop = top;
29+
mRight = right;
30+
mBottom = bottom;
31+
}
32+
33+
@Override
34+
public void execute(MountingManager mountingManager) {
35+
mountingManager.updatePadding(mReactTag, mLeft, mTop, mRight, mBottom);
36+
}
37+
38+
@Override
39+
public String toString() {
40+
return "UpdatePaddingMountItem ["
41+
+ mReactTag
42+
+ "] - left: "
43+
+ mLeft
44+
+ " - top: "
45+
+ mTop
46+
+ " - right: "
47+
+ mRight
48+
+ " - bottom: "
49+
+ mBottom;
50+
}
51+
}

0 commit comments

Comments
 (0)