-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Trigger remeasure when constraints on CV changes #22270
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
45502d6
Trigger remeasure when constraints on CV changes
PureWeen 13d7f84
- add tests
PureWeen 2626014
- add additional code so we really only measure once
PureWeen 9b9b85f
- make sure we only use the measure for the first item
PureWeen 563706a
- add test validating first item measured
PureWeen 5cba881
- add additional tests
PureWeen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
src/Controls/samples/Controls.Sample.UITests/Issues/Issue21967.xaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| <?xml version="1.0" encoding="utf-8" ?> | ||
| <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" | ||
| xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | ||
| x:Class="Maui.Controls.Sample.Issues.Issue21967" | ||
| Title="Issue21967"> | ||
| <Grid | ||
| RowDefinitions="Auto,Auto,Auto,*" | ||
| RowSpacing="8" | ||
| Margin="8"> | ||
|
|
||
| <Label Grid.Row="0" Text="Click the resize button and elements in the CV should resize."> | ||
| </Label> | ||
|
|
||
| <Button | ||
| Grid.Row="1" | ||
| Text="Set to Full Size" | ||
| AutomationId="FullSize" | ||
| x:Name="buttonFullSize" | ||
| HorizontalOptions="Center" | ||
| VerticalOptions="Center"/> | ||
|
|
||
| <Button | ||
| Grid.Row="2" | ||
| Text="Resize" | ||
| AutomationId="Resize" | ||
| x:Name="button" | ||
| HorizontalOptions="Center" | ||
| VerticalOptions="Center"/> | ||
|
|
||
| <CollectionView | ||
| Grid.Row="3" | ||
| x:Name="cv" | ||
| ItemSizingStrategy="MeasureFirstItem"> | ||
|
|
||
| <CollectionView.ItemsLayout> | ||
| <GridItemsLayout | ||
| Orientation="Vertical" | ||
| HorizontalItemSpacing="8" | ||
| VerticalItemSpacing="8" | ||
| Span="2"/> | ||
| </CollectionView.ItemsLayout> | ||
|
|
||
| <CollectionView.ItemTemplate> | ||
| <DataTemplate> | ||
| <Grid BackgroundColor="LightGray"> | ||
| <Label | ||
| Text="{Binding Text}" | ||
| AutomationId="{Binding AutomationId}" | ||
| HorizontalOptions="Fill" | ||
| VerticalOptions="Fill"/> | ||
| </Grid> | ||
| </DataTemplate> | ||
| </CollectionView.ItemTemplate> | ||
|
|
||
| </CollectionView> | ||
|
|
||
| </Grid> | ||
| </ContentPage> |
49 changes: 49 additions & 0 deletions
49
src/Controls/samples/Controls.Sample.UITests/Issues/Issue21967.xaml.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| using System; | ||
| using Microsoft.Maui; | ||
| using Microsoft.Maui.Controls; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
|
|
||
| namespace Maui.Controls.Sample.Issues; | ||
|
|
||
| [Issue(IssueTracker.Github, 21967, "CollectionView causes invalid measurements on resize", PlatformAffected.Android)] | ||
| public partial class Issue21967 : ContentPage | ||
| { | ||
| public readonly record struct Data(string Text, string AutomationId); | ||
| public Issue21967() | ||
| { | ||
| InitializeComponent(); | ||
| cv.ItemsSource = | ||
| new List<string> { "Item1", "Item2", "Item3", "Item4", "Item5" } | ||
| .Select((x, i) => | ||
| { | ||
| string text = x; | ||
|
|
||
| if (i == 1) | ||
| { | ||
| // Generate long text that would measure really big to ensure everything stays measured the | ||
| text = Enumerable.Range(0,1000).Select(x => x.ToString()).Aggregate((x, y) => x + " " + y); | ||
| } | ||
| return new Data(text, x); | ||
| }) | ||
| .ToList(); | ||
|
|
||
|
|
||
| button.Clicked += (_, _) => | ||
| { | ||
| if (cv.WidthRequest == 200) | ||
| { | ||
| cv.WidthRequest = 100; | ||
| } | ||
| else | ||
| { | ||
| cv.WidthRequest = 200; | ||
| } | ||
| }; | ||
|
|
||
| buttonFullSize.Clicked += (_, _) => | ||
| { | ||
| cv.WidthRequest = Microsoft.Maui.Primitives.Dimension.Unset; | ||
| }; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,8 +9,16 @@ namespace Microsoft.Maui.Controls.Handlers.Items | |
| { | ||
| public class ItemContentView : ViewGroup | ||
| { | ||
| Size? _size; | ||
| Action<Size> _reportMeasure; | ||
| Size? _pixelSize; | ||
| WeakReference _reportMeasure; | ||
| WeakReference _retrieveStaticSize; | ||
| int _previousPixelWidth = -1; | ||
| int _previousPixelHeight = -1; | ||
|
|
||
| Action<Size> ReportMeasure | ||
| { | ||
| get => _reportMeasure?.Target as Action<Size>; | ||
| } | ||
|
|
||
| protected IPlatformViewHandler Content; | ||
| internal IView View => Content?.VirtualView; | ||
|
|
@@ -23,6 +31,12 @@ public ItemContentView(Context context) : base(context) | |
|
|
||
| AView PlatformView => Content?.ContainerView ?? Content?.PlatformView; | ||
|
|
||
| internal Func<Size?> RetrieveStaticSize | ||
| { | ||
| get => _retrieveStaticSize?.Target as Func<Size?>; | ||
| set => _retrieveStaticSize = new WeakReference(value); | ||
| } | ||
|
|
||
| internal void RealizeContent(View view, ItemsView itemsView) | ||
| { | ||
| Content = CreateHandler(view, itemsView); | ||
|
|
@@ -53,13 +67,16 @@ internal void Recycle() | |
| } | ||
|
|
||
| Content = null; | ||
| _size = null; | ||
| _pixelSize = null; | ||
| _reportMeasure = null; | ||
| _previousPixelWidth = -1; | ||
| _previousPixelHeight = -1; | ||
| } | ||
|
|
||
| internal void HandleItemSizingStrategy(Action<Size> reportMeasure, Size? size) | ||
| { | ||
| _reportMeasure = reportMeasure; | ||
| _size = size; | ||
| _reportMeasure = new WeakReference(reportMeasure); | ||
| _pixelSize = size; | ||
| } | ||
|
|
||
| protected override void OnLayout(bool changed, int l, int t, int r, int b) | ||
|
|
@@ -83,39 +100,100 @@ protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec) | |
| return; | ||
| } | ||
|
|
||
| int pixelWidth = MeasureSpec.GetSize(widthMeasureSpec); | ||
| int pixelHeight = MeasureSpec.GetSize(heightMeasureSpec); | ||
| var widthMode = MeasureSpec.GetMode(widthMeasureSpec); | ||
| var heightMode = MeasureSpec.GetMode(heightMeasureSpec); | ||
|
|
||
| // This checks to see if a static size has been set already on the adapter | ||
| // The problem is that HandleItemSizingStrategy(Action<Size> reportMeasure, Size? size) | ||
| // is called during "Bind" and then measure is called on all visible cells after that | ||
| // the result of this is that every single cell does an individual measure opposed to just using | ||
| // the first cell to set the size. | ||
|
|
||
| var possibleNewSize = RetrieveStaticSize?.Invoke(); | ||
|
|
||
| // This means a different cell has already set our new size | ||
| // so let's just use that instead of perform our own speculative measure | ||
| if (possibleNewSize is not null && | ||
| _pixelSize is not null && | ||
| !_pixelSize.Equals(possibleNewSize)) | ||
| { | ||
| _pixelSize = possibleNewSize; | ||
| } | ||
| else | ||
| { | ||
| _pixelSize = _pixelSize ?? possibleNewSize; | ||
|
|
||
| // If the measure changes significantly, we need to invalidate the pixel size | ||
| // This will happen if the user rotates the device or even just changes the height/width | ||
| // on the CollectionView itself. | ||
| // The Abs comparison I think is currently just a workaround for this | ||
| // https://github.com/dotnet/maui/issues/22271 | ||
| // Once we fix 22271 I don't think we need this | ||
| // I'm also curious if we would still need this https://github.com/dotnet/maui/pull/21140 | ||
| if (pixelWidth != 0 && Math.Abs(_previousPixelWidth - pixelWidth) > 1) | ||
| { | ||
| // We only need to worry about clearing pixel size if we've | ||
| // already made a first pass | ||
| if (_previousPixelWidth != -1) | ||
| { | ||
| _pixelSize = null; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| if (pixelHeight != 0 && Math.Abs(_previousPixelHeight - pixelHeight) > 1) | ||
| { | ||
| // We only need to worry about clearing pixel size if we've | ||
| // already made a first pass | ||
| if (_previousPixelHeight != -1) | ||
| { | ||
| _pixelSize = null; | ||
| } | ||
|
|
||
| _pixelSize = null; | ||
| } | ||
| } | ||
|
|
||
| if (pixelWidth != 0) | ||
| { | ||
| _previousPixelWidth = pixelWidth; | ||
| } | ||
|
|
||
| if (pixelHeight != 0) | ||
| { | ||
| _previousPixelHeight = pixelHeight; | ||
| } | ||
|
|
||
| // If we're using ItemSizingStrategy.MeasureFirstItem and now we have a set size, use that | ||
| // Even though we already know the size we still need to pass the measure through to the children. | ||
| if (_size is not null) | ||
| if (_pixelSize is not null) | ||
| { | ||
| var w = (int)_size.Value.Width; | ||
| var h = (int)_size.Value.Height; | ||
| var w = (int)this.FromPixels(_pixelSize.Value.Width); | ||
| var h = (int)this.FromPixels(_pixelSize.Value.Height); | ||
|
|
||
| // If the platform childs measure has been invalidated, it's going to still want to | ||
| // participate in the measure lifecycle in order to update its internal | ||
| // book keeping. | ||
| _ = View.Measure | ||
| ( | ||
| _ = (View.Handler as IPlatformViewHandler)?.MeasureVirtualView( | ||
| MeasureSpec.MakeMeasureSpec(w, MeasureSpecMode.Exactly), | ||
| MeasureSpec.MakeMeasureSpec(h, MeasureSpecMode.Exactly) | ||
| ); | ||
|
|
||
| SetMeasuredDimension(w, h); | ||
| SetMeasuredDimension((int)_pixelSize.Value.Width, (int)_pixelSize.Value.Height); | ||
| return; | ||
| } | ||
|
|
||
| int pixelWidth = MeasureSpec.GetSize(widthMeasureSpec); | ||
| int pixelHeight = MeasureSpec.GetSize(heightMeasureSpec); | ||
|
|
||
| var widthSpec = MeasureSpec.GetMode(widthMeasureSpec) == MeasureSpecMode.Unspecified | ||
| var width = widthMode == MeasureSpecMode.Unspecified | ||
| ? double.PositiveInfinity | ||
| : this.FromPixels(pixelWidth); | ||
|
|
||
| var heightSpec = MeasureSpec.GetMode(heightMeasureSpec) == MeasureSpecMode.Unspecified | ||
| var height = heightMode == MeasureSpecMode.Unspecified | ||
| ? double.PositiveInfinity | ||
| : this.FromPixels(pixelHeight); | ||
|
|
||
|
|
||
| var measure = View.Measure(widthSpec, heightSpec); | ||
| var measure = View.Measure(width, height); | ||
|
|
||
| if (pixelWidth == 0) | ||
| { | ||
|
|
@@ -127,8 +205,7 @@ protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec) | |
| pixelHeight = (int)this.ToPixels(measure.Height); | ||
| } | ||
|
|
||
| _reportMeasure?.Invoke(new Size(pixelWidth, pixelHeight)); | ||
| _reportMeasure = null; // Make sure we only report back the measure once | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can't really set this to null because if the constraints on the CV change we need to report a new measure. |
||
| ReportMeasure?.Invoke(new Size(pixelWidth, pixelHeight)); | ||
|
|
||
| SetMeasuredDimension(pixelWidth, pixelHeight); | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| using NUnit.Framework; | ||
| using OpenQA.Selenium; | ||
| using UITest.Appium; | ||
| using UITest.Core; | ||
|
|
||
| namespace Microsoft.Maui.AppiumTests.Issues | ||
| { | ||
| public class Issue21967 : _IssuesUITest | ||
| { | ||
| public Issue21967(TestDevice device) : base(device) | ||
| { | ||
| } | ||
|
|
||
| public override string Issue => "CollectionView causes invalid measurements on resize"; | ||
|
|
||
| [Test] | ||
| [Category(UITestCategories.CollectionView)] | ||
| public void CollectionViewItemsResizeWhenContraintsOnCollectionViewChange() | ||
| { | ||
| var largestSize = App.WaitForElement("Item1").GetRect(); | ||
| App.Tap("Resize"); | ||
| var mediumSize = App.WaitForElement("Item1").GetRect(); | ||
| App.Tap("Resize"); | ||
| var smallSize = App.WaitForElement("Item1").GetRect(); | ||
|
|
||
| Assert.Greater(largestSize.Width, mediumSize.Width); | ||
| Assert.Greater(mediumSize.Width, smallSize.Width); | ||
| } | ||
|
|
||
| [Test] | ||
| [Category(UITestCategories.CollectionView)] | ||
| public void CollectionViewFirstItemCorrectlySetsTheMeasure() | ||
| { | ||
| var itemSize = App.WaitForElement("Item1").GetRect(); | ||
| Assert.Greater(200, itemSize.Height); | ||
| } | ||
|
|
||
| [Test] | ||
| [Category(UITestCategories.CollectionView)] | ||
| public void CollectionViewWorksWhenRotatingDevice() | ||
| { | ||
| this.IgnoreIfPlatforms(new TestDevice[] { TestDevice.Mac, TestDevice.Windows }); | ||
|
|
||
| try | ||
| { | ||
| App.WaitForElement("FullSize"); | ||
| App.Tap("FullSize"); | ||
| App.SetOrientationPortrait(); | ||
| var itemSizePortrait = App.WaitForElement("Item1").GetRect(); | ||
| App.SetOrientationLandscape(); | ||
| var itemSizeLandscape = App.WaitForElement("Item1").GetRect(); | ||
| App.SetOrientationPortrait(); | ||
| var itemSizePortrait2 = App.WaitForElement("Item1").GetRect(); | ||
|
|
||
| Assert.Greater(itemSizeLandscape.Width, itemSizePortrait.Width); | ||
| Assert.AreEqual(itemSizePortrait2.Width, itemSizePortrait.Width); | ||
| } | ||
| finally | ||
| { | ||
| App.SetOrientationPortrait(); | ||
| } | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we update the sample to use the Page Width? In this way, we can validate with an UITest the UI rotating the device.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea, that's caused by this unfortunately
#22271