Skip to content

Commit 0c8850f

Browse files
kmagierafacebook-github-bot-9
authored andcommitted
Only send layout update operation to nativehierarchymanager when layout actually changes.
Reviewed By: andreicoman11 Differential Revision: D2679408 fb-gh-sync-id: 7f0a972e9e12f70402e2d285edef458a61ca1c39
1 parent 06e5140 commit 0c8850f

2 files changed

Lines changed: 29 additions & 23 deletions

File tree

ReactAndroid/src/main/java/com/facebook/react/uimanager/ReactShadowNode.java

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.facebook.infer.annotation.Assertions;
1919
import com.facebook.react.bridge.ReadableMap;
2020
import com.facebook.react.bridge.ReadableMapKeySetIterator;
21+
import com.facebook.react.uimanager.events.EventDispatcher;
2122

2223
/**
2324
* Base node class for representing virtual tree of React nodes. Shadow nodes are used primarily
@@ -202,18 +203,37 @@ public void onCollectExtraUpdates(UIViewOperationQueue uiViewOperationQueue) {
202203
float absoluteX,
203204
float absoluteY,
204205
UIViewOperationQueue uiViewOperationQueue,
205-
NativeViewHierarchyOptimizer nativeViewHierarchyOptimizer) {
206+
NativeViewHierarchyOptimizer nativeViewHierarchyOptimizer,
207+
EventDispatcher eventDispatcher) {
206208
if (mNodeUpdated) {
207209
onCollectExtraUpdates(uiViewOperationQueue);
208210
}
209211

210212
if (hasNewLayout()) {
211-
mAbsoluteLeft = Math.round(absoluteX + getLayoutX());
212-
mAbsoluteTop = Math.round(absoluteY + getLayoutY());
213-
mAbsoluteRight = Math.round(absoluteX + getLayoutX() + getLayoutWidth());
214-
mAbsoluteBottom = Math.round(absoluteY + getLayoutY() + getLayoutHeight());
215-
216-
nativeViewHierarchyOptimizer.handleUpdateLayout(this);
213+
float absoluteLeft = Math.round(absoluteX + getLayoutX());
214+
float absoluteTop = Math.round(absoluteY + getLayoutY());
215+
float absoluteRight = Math.round(absoluteX + getLayoutX() + getLayoutWidth());
216+
float absoluteBottom = Math.round(absoluteY + getLayoutY() + getLayoutHeight());
217+
// If the layout didn't change this should calculate exactly same values, it's fine to compare
218+
// floats with "==" in this case
219+
if (absoluteLeft != mAbsoluteLeft || absoluteTop != mAbsoluteTop ||
220+
absoluteRight != mAbsoluteRight || absoluteBottom != mAbsoluteBottom) {
221+
mAbsoluteLeft = absoluteLeft;
222+
mAbsoluteTop = absoluteTop;
223+
mAbsoluteRight = absoluteRight;
224+
mAbsoluteBottom = absoluteBottom;
225+
226+
nativeViewHierarchyOptimizer.handleUpdateLayout(this);
227+
if (mShouldNotifyOnLayout) {
228+
eventDispatcher.dispatchEvent(
229+
OnLayoutEvent.obtain(
230+
getReactTag(),
231+
getScreenX(),
232+
getScreenY(),
233+
getScreenWidth(),
234+
getScreenHeight()));
235+
}
236+
}
217237
}
218238
}
219239

@@ -264,10 +284,6 @@ public void setShouldNotifyOnLayout(boolean shouldNotifyOnLayout) {
264284
mShouldNotifyOnLayout = shouldNotifyOnLayout;
265285
}
266286

267-
/* package */ boolean shouldNotifyOnLayout() {
268-
return mShouldNotifyOnLayout;
269-
}
270-
271287
/**
272288
* Adds a child that the native view hierarchy will have at this index in the native view
273289
* corresponding to this node.

ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -814,18 +814,8 @@ private void applyUpdatesRecursive(ReactShadowNode cssNode, float absoluteX, flo
814814
absoluteX,
815815
absoluteY,
816816
mOperationsQueue,
817-
mNativeViewHierarchyOptimizer);
818-
819-
// notify JS about layout event if requested
820-
if (cssNode.shouldNotifyOnLayout()) {
821-
mEventDispatcher.dispatchEvent(
822-
OnLayoutEvent.obtain(
823-
tag,
824-
cssNode.getScreenX(),
825-
cssNode.getScreenY(),
826-
cssNode.getScreenWidth(),
827-
cssNode.getScreenHeight()));
828-
}
817+
mNativeViewHierarchyOptimizer,
818+
mEventDispatcher);
829819
}
830820
cssNode.markUpdateSeen();
831821
}

0 commit comments

Comments
 (0)