Skip to content

Commit c39bbe4

Browse files
authored
fix(OnLayoutEvent): Only fire onLayout event when layout changes (microsoft#1336)
Prior to this change, the onLayout event fired even when the layout actually stayed the same. Fixes microsoft#1335
1 parent 183e1b8 commit c39bbe4

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed

ReactWindows/ReactNative.Shared/UIManager/ReactShadowNode.cs

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@ public class ReactShadowNode : IDisposable
3333
private int _totalNativeChildren;
3434
private ReactShadowNode _nativeParent;
3535
private IList<ReactShadowNode> _nativeChildren;
36-
private float _absoluteLeft;
37-
private float _absoluteTop;
38-
private float _absoluteRight;
39-
private float _absoluteBottom;
4036

4137
/// <summary>
4238
/// Instantiates a <see cref="ReactShadowNode"/>.
@@ -498,22 +494,22 @@ public int TotalNativeChildren
498494
/// <summary>
499495
/// The screen horizontal position.
500496
/// </summary>
501-
public int ScreenX => (int)Math.Round(LayoutX);
497+
public int ScreenX { get; private set; }
502498

503499
/// <summary>
504500
/// The screen vertical position.
505501
/// </summary>
506-
public int ScreenY => (int)Math.Round(LayoutY);
502+
public int ScreenY { get; private set; }
507503

508504
/// <summary>
509505
/// The screen width.
510506
/// </summary>
511-
public int ScreenWidth => (int)Math.Round(_absoluteRight - _absoluteLeft);
507+
public int ScreenWidth { get; private set; }
512508

513509
/// <summary>
514510
/// The screen height.
515511
/// </summary>
516-
public int ScreenHeight => (int)Math.Round(_absoluteBottom - _absoluteTop);
512+
public int ScreenHeight { get; private set; }
517513

518514
/// <summary>
519515
/// The measure function.
@@ -1089,12 +1085,35 @@ internal bool DispatchUpdates(
10891085

10901086
if (HasNewLayout)
10911087
{
1092-
_absoluteLeft = absoluteX + LayoutX;
1093-
_absoluteTop = absoluteY + LayoutY;
1094-
_absoluteRight = absoluteX + LayoutX + LayoutWidth;
1095-
_absoluteBottom = absoluteY + LayoutY + LayoutHeight;
1096-
nativeViewHierarchyOptimizer.HandleUpdateLayout(this);
1097-
return true;
1088+
var layoutX = LayoutX;
1089+
var layoutY = LayoutY;
1090+
var newAbsoluteLeft = (int)Math.Round(absoluteX + layoutX);
1091+
var newAbsoluteTop = (int)Math.Round(absoluteY + layoutY);
1092+
var newAbsoluteRight = (int)Math.Round(absoluteX + layoutX + LayoutWidth);
1093+
var newAbsoluteBottom = (int)Math.Round(absoluteY + layoutY + LayoutHeight);
1094+
1095+
var newScreenX = (int)Math.Round(layoutX);
1096+
var newScreenY = (int)Math.Round(layoutY);
1097+
var newScreenWidth = newAbsoluteRight - newAbsoluteLeft;
1098+
var newScreenHeight = newAbsoluteBottom - newAbsoluteTop;
1099+
1100+
var layoutHasChanged =
1101+
newScreenX != ScreenX ||
1102+
newScreenY != ScreenY ||
1103+
newScreenWidth != ScreenWidth ||
1104+
newScreenHeight != ScreenHeight;
1105+
1106+
ScreenX = newScreenX;
1107+
ScreenY = newScreenY;
1108+
ScreenWidth = newScreenWidth;
1109+
ScreenHeight = newScreenHeight;
1110+
1111+
if (layoutHasChanged)
1112+
{
1113+
nativeViewHierarchyOptimizer.HandleUpdateLayout(this);
1114+
}
1115+
1116+
return layoutHasChanged;
10981117
}
10991118
else
11001119
{

ReactWindows/ReactNative.Shared/UIManager/UIImplementation.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -895,13 +895,13 @@ private void ApplyUpdatesRecursive(
895895
var tag = cssNode.ReactTag;
896896
if (!_shadowNodeRegistry.IsRootNode(tag))
897897
{
898-
cssNode.DispatchUpdates(
898+
var frameDidChange = cssNode.DispatchUpdates(
899899
absoluteX,
900900
absoluteY,
901901
_operationsQueue,
902902
_nativeViewHierarchyOptimizer);
903903

904-
if (cssNode.ShouldNotifyOnLayout)
904+
if (frameDidChange && cssNode.ShouldNotifyOnLayout)
905905
{
906906
_eventDispatcher.DispatchEvent(
907907
OnLayoutEvent.Obtain(

0 commit comments

Comments
 (0)