Skip to content

Commit

Permalink
Fixed Unnecessary SizeChanged Event Triggering (#27476)
Browse files Browse the repository at this point in the history
* Fixed Fixed Unnecessary SizeChanged Event Triggering

* Added test case

* Added comment on testcase

* Added test case

* Added comment on testcase

* Moved SizeAllocated() within if condition
  • Loading branch information
Dhivya-SF4094 authored Feb 14, 2025
1 parent c458a44 commit 86f2b95
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/Controls/src/Core/VisualElement/VisualElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1756,17 +1756,23 @@ private protected void RefreshInputTransparentProperty()

void UpdateBoundsComponents(Rect bounds)
{
if (_frame == bounds)
return;
_frame = bounds;

BatchBegin();

X = bounds.X;
Y = bounds.Y;
var previousWidth = Width;
var previousHeight = Height;
Width = bounds.Width;
Height = bounds.Height;

SizeAllocated(Width, Height);
SizeChanged?.Invoke(this, EventArgs.Empty);
if (previousHeight != Height || previousWidth != Width)
{
SizeAllocated(Width, Height);
SizeChanged?.Invoke(this, EventArgs.Empty);
}

BatchCommit();
}
Expand Down
79 changes: 79 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue27223.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
namespace Maui.Controls.Sample.Issues
{
[Issue(IssueTracker.Github, 27223, "SizeChanged event fires when size hasn't changed", PlatformAffected.UWP)]
public class Issue27223 : ContentPage
{
private int counter = 0;
private Label Counter;
private Editor ShowSizes;
private Editor ShowCoordinates;
private VerticalStackLayout stackLayout;

public Issue27223()
{
Grid mainGrid = new Grid
{
ColumnDefinitions =
{
new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) },
new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) },
new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }
}
};

Grid leftGrid = new Grid
{
RowDefinitions =
{
new RowDefinition { Height = GridLength.Auto },
new RowDefinition { Height = new GridLength(1, GridUnitType.Star) },
new RowDefinition { Height = GridLength.Auto }
}
};

Counter = new Label { AutomationId = "Label" };
leftGrid.Children.Add(Counter);
Grid.SetRow(Counter, 0);

stackLayout = new VerticalStackLayout();
stackLayout.SizeChanged += VerticalStackLayout_SizeChanged;
var button1 = new Button { Text = "button 1", AutomationId = "Button1" };
button1.Clicked += Button_Clicked;
stackLayout.Children.Add(button1);
stackLayout.Children.Add(new Button { Text = "button 2" });
stackLayout.Children.Add(new Button { Text = "button 3" });
leftGrid.Children.Add(stackLayout);
Grid.SetRow(stackLayout, 2);

ShowSizes = new Editor { BackgroundColor = Colors.SkyBlue, VerticalOptions = LayoutOptions.Fill };
ShowCoordinates = new Editor { BackgroundColor = Colors.DarkGoldenrod, VerticalOptions = LayoutOptions.Fill };

mainGrid.Children.Add(leftGrid);
Grid.SetColumn(leftGrid, 0);

mainGrid.Children.Add(ShowSizes);
Grid.SetColumn(ShowSizes, 1);

mainGrid.Children.Add(ShowCoordinates);
Grid.SetColumn(ShowCoordinates, 2);

Content = mainGrid;
}

private void Button_Clicked(object sender, EventArgs e)
{
#if WINDOWS
Window.Height = 500;
#endif
}

private void VerticalStackLayout_SizeChanged(object sender, EventArgs e)
{
if (sender is VerticalStackLayout ctrl)
{
counter++;
Counter.Text = $"resized {counter} times";
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#if WINDOWS // mac does not respond to the window size change. so, ignored test on mac.
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues
{
public class Issue27223 : _IssuesUITest
{
public Issue27223(TestDevice testDevice) : base(testDevice)
{
}

public override string Issue => "SizeChanged event fires when size hasn't changed";

[Test]
[Category(UITestCategories.Layout)]
public void SizeChangedOnlyFiresWhenSizeChanges()
{
App.WaitForElement("Label");
var initialValue = App.FindElement("Label").GetText();
App.Tap("Button1");
var finalValue = App.FindElement("Label").GetText();
Assert.That(initialValue, Is.EqualTo(finalValue));
}
}
}
#endif

0 comments on commit 86f2b95

Please sign in to comment.