Skip to content

Fix CV1 GridItemsLayout centering single item AND Fix Empty view not resizing when bounds change #29639

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
merged 3 commits into from
Jun 21, 2025

Conversation

albyrock87
Copy link
Contributor

@albyrock87 albyrock87 commented May 23, 2025

Description of Change

GridViewLayout has a special positioning override when there's only one item.

if (ScrollDirection == UICollectionViewScrollDirection.Vertical && NeedsSingleItemHorizontalAlignmentAdjustment(layoutAttributesForRectElements))
{
	// If there's exactly one item in a vertically scrolling grid, for some reason UICollectionViewFlowLayout
	// tries to center it. This corrects that issue.
	var currentFrame = layoutAttributesForRectElements[0].Frame;
	var newFrame = new CGRect(CollectionView.Frame.Left + CollectionView.ContentInset.Right,
	currentFrame.Top, currentFrame.Width, currentFrame.Height);
	layoutAttributesForRectElements[0].Frame = newFrame;
}

So we have to invalidate the layout entirely to trigger that special case, instead of just simply invalidate single cells.

Issues Fixed

Fixes #29595
Fixes #29634

@albyrock87 albyrock87 requested a review from a team as a code owner May 23, 2025 11:50
@dotnet-policy-service dotnet-policy-service bot added the community ✨ Community Contribution label May 23, 2025
@albyrock87 albyrock87 changed the title Fix CV1 GridLayout special behavior Fix CV1 GridItemsLayout not centering single item, Empty view not resizing when bounds change May 23, 2025
@jsuarezruiz jsuarezruiz added the area-controls-collectionview CollectionView, CarouselView, IndicatorView label May 26, 2025
Copy link
Member

@rmarinho rmarinho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we just need to update the screenshot.

@rmarinho rmarinho moved this from Todo to Ready To Review in MAUI SDK Ongoing May 26, 2025
@rmarinho rmarinho added this to the .NET 9 SR8 milestone May 26, 2025
@dotnet dotnet deleted a comment from azure-pipelines bot May 26, 2025
@dotnet dotnet deleted a comment from azure-pipelines bot May 26, 2025
@dotnet dotnet deleted a comment from jsuarezruiz May 26, 2025
@rmarinho
Copy link
Member

@albyrock87 seems theres still a slight difference, I wonder if we should change the threshold of the comparison .

VisualTestUtils.VisualTestFailedException :
Snapshot different than baseline: VerifyGridItemsLayoutSingleAndEmptyViewResize.png (0.50% difference)
If the correct baseline has changed (this isn't a a bug), then update the baseline image.
See test attachment or download the build artifacts to get the new snapshot file.

@dotnet dotnet deleted a comment from azure-pipelines bot May 29, 2025
@dotnet dotnet deleted a comment from azure-pipelines bot May 29, 2025
@dotnet dotnet deleted a comment from azure-pipelines bot May 29, 2025
@mattleibow mattleibow changed the title Fix CV1 GridItemsLayout not centering single item, Empty view not resizing when bounds change Fix CV1 GridItemsLayout centering single item AND Fix Empty view not resizing when bounds change May 30, 2025
Copy link
Member

@mattleibow mattleibow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR, I have a few questions:

  • I am not seeing a visible difference in the final logic of the CV2 code
  • Similar with CV1 - except for a final if and layout all
  • The tests is very complex and I am not sure what we are testing
  • The PR has a fix for 2 things, but there is only 1 test
  • The CV code has logic for horizontal and vertical grids, should we add tests for each
  • Does this just manifest if the items change after the page loads, or also on the page init? Should we cover both cases?

List<NSIndexPath> invalidatedPaths = null;
List<TemplatedCell2> invalidatedCells = null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did this have to change? I see before we get the index and store it here, then invalidate. Now you get the cell, store it, and then later get the index.

Does this different path do different things? Looks like in the end you still use the same cell index in the invalidate.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to avoid a reference to NS objects when possible.

Comment on lines +262 to +265
if (ItemsSource.ItemCount == 1)
{
CollectionView.CollectionViewLayout.InvalidateLayout();
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this extra code path not needed in CV2?

using System.Windows.Input;
using Microsoft.Maui.Controls.Shapes;

namespace Maui.Controls.Sample.Issues
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is really complex and there are many things here. Does this issue not repro in a case without all of this special work?

I see multiple collection views, shell, pages, tabs and all sorts of delay things.

Can this not be broken into a simple test where the page loads and then the UI test taps a button to add an item if we need things loaded later.

Also, the fix for the empty view has no tests I think?

@github-project-automation github-project-automation bot moved this from Ready To Review to Changes Requested in MAUI SDK Ongoing May 30, 2025
@PureWeen PureWeen added p/0 Work that we can't release without p/1 Work that is important, and has been scheduled for release in this or an upcoming sprint and removed p/0 Work that we can't release without labels Jun 2, 2025
@albyrock87 albyrock87 force-pushed the fix-29595 branch 2 times, most recently from 6ebf2cb to ca9132b Compare June 5, 2025 11:08
@PureWeen PureWeen added this to the .NET 9 SR9 milestone Jun 9, 2025
@PureWeen PureWeen requested review from mattleibow and rmarinho June 9, 2025 23:29
@PureWeen PureWeen added p/0 Work that we can't release without and removed p/1 Work that is important, and has been scheduled for release in this or an upcoming sprint labels Jun 9, 2025
@PureWeen
Copy link
Member

/azp run

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@PureWeen
Copy link
Member

/azp run MAUI-UITests-public

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@PureWeen PureWeen changed the base branch from main to inflight/current June 21, 2025 12:09
@PureWeen PureWeen merged commit d56e03b into dotnet:inflight/current Jun 21, 2025
71 of 78 checks passed
@github-project-automation github-project-automation bot moved this from Changes Requested to Done in MAUI SDK Ongoing Jun 21, 2025
@PureWeen
Copy link
Member

/backport to release/9.0.1xx-sr8

Copy link
Contributor

Started backporting to release/9.0.1xx-sr8: https://github.com/dotnet/maui/actions/runs/15795546965

PureWeen added a commit that referenced this pull request Jun 21, 2025
…resizing when bounds change (#29639)

* Fix CV1 empty view not resizing when bounds change and grid layout not left aligning single item

* - rework test

* - fix test

---------

Co-authored-by: Shane Neuville <shneuvil@microsoft.com>
PureWeen added a commit that referenced this pull request Jun 25, 2025
…resizing when bounds change (#29639)

* Fix CV1 empty view not resizing when bounds change and grid layout not left aligning single item

* - rework test

* - fix test

---------

Co-authored-by: Shane Neuville <shneuvil@microsoft.com>
PureWeen added a commit that referenced this pull request Jun 25, 2025
…resizing when bounds change (#29639)

* Fix CV1 empty view not resizing when bounds change and grid layout not left aligning single item

* - rework test

* - fix test

---------

Co-authored-by: Shane Neuville <shneuvil@microsoft.com>
github-actions bot pushed a commit that referenced this pull request Jun 26, 2025
…resizing when bounds change (#29639)

* Fix CV1 empty view not resizing when bounds change and grid layout not left aligning single item

* - rework test

* - fix test

---------

Co-authored-by: Shane Neuville <shneuvil@microsoft.com>
github-actions bot pushed a commit that referenced this pull request Jun 26, 2025
…resizing when bounds change (#29639)

* Fix CV1 empty view not resizing when bounds change and grid layout not left aligning single item

* - rework test

* - fix test

---------

Co-authored-by: Shane Neuville <shneuvil@microsoft.com>
PureWeen added a commit that referenced this pull request Jun 27, 2025
…resizing when bounds change (#29639)

* Fix CV1 empty view not resizing when bounds change and grid layout not left aligning single item

* - rework test

* - fix test

---------

Co-authored-by: Shane Neuville <shneuvil@microsoft.com>
github-actions bot pushed a commit that referenced this pull request Jun 27, 2025
…resizing when bounds change (#29639)

* Fix CV1 empty view not resizing when bounds change and grid layout not left aligning single item

* - rework test

* - fix test

---------

Co-authored-by: Shane Neuville <shneuvil@microsoft.com>
PureWeen added a commit that referenced this pull request Jun 27, 2025
…resizing when bounds change (#29639)

* Fix CV1 empty view not resizing when bounds change and grid layout not left aligning single item

* - rework test

* - fix test

---------

Co-authored-by: Shane Neuville <shneuvil@microsoft.com>
PureWeen added a commit that referenced this pull request Jun 28, 2025
For more information about inflight process check
https://github.com/dotnet/maui/wiki/Inflight-Branch-Process

# .NET MAUI Release Notes - Inflight/Candidate Branch

## What's Changed

### MAUI Product Fixes
* [iOS] CarouselView with CarouselViewHandler2 make app crash when
Loop="False" and user scroll to the last position - fixes #26863 by
@kubaflo in #26868
* Fixes Setting BackgroundColor to null does not actually changes
BackgroundColor - fixes #22914 and #19576 by @jgonzalez-gft in
#22917
* Fixed the picker title's color - fixes #16737 by @kubaflo in
#23075
* [android] Fallback to default icons in SearchHandler by @aheubusch in
#25067
* ScrollView's Background on iOS - fixes #24016 by @kubaflo in
#25541
* [iOS] Enabled MultiTouch Support for Handling Multi-Touch Points in
GraphicsView - fixes #29461 by @prakashKannanSf3972 in
#29895
* Optimize converters for GridLength, ColumnDefinition, and
RowDefinition - performance improvement by @emiller in
#20048
* Add defensive IsAlive check to Android ViewExtensions.OnUnloaded -
fixes #28051 by @jfversluis in #29934
* [Windows] Fixed runtime update issue for SearchBar PlaceholderColor
and BackgroundColor - fixes #29962 by @Tamilarasan-Paranthaman in
#29965
* Weak subscription to CanExecuteChange events - fixes #16124 by
@sneumaier in #29837
* [iOS, Mac] Fix for downsized image retaining original dimensions in
GraphicsView - fixes #30006 by @SyedAbdulAzeemSF4852 in
#30007
* [Android] Prevent Picker from Gaining Focus on Touch - fixes #19739,
#8546, #13503, #24862, #28121, #21704, #15394 by @bhavanesh2001 in
#29068
* Fix CV1 GridItemsLayout centering single item AND Fix Empty view not
resizing when bounds change - fixes #29595, #29634 by @albyrock87 in
#29639

### Testing
* [Testing] Feature Matrix UITest Cases for Button by @TamilarasanSF4853
in #29803
* [Testing] Feature matrix UITest Cases for BoxView Control by
@HarishKumarSF4517 in #29808
* [Testing] Enable HandlerDoesNotLeak for Button and ProgressBar by
@bhavanesh2001 in #29896
* [Testing] Add Validation Test For Issue28051 On Android by
@prakashKannanSf3972 in #30026
* [Testing] Fixed Test case failure in PR 30115 - [2025/06/23] Candidate
by @HarishKumarSF4517 in #30136

### Dependency Updates
* Bump to 1.7.250606001 of WindowsAppSDK by @sneumaier in
#29915

### Housekeeping
* [housekeeping] Update namespaces in HostApp and Shared tests projects
by @bhavanesh2001 in #29904
* Update SetterSpecificity.cs Remove Extra Line From Bad Merge by
@sneumaier in #29987
* Revert - Fixed the Label not sized correctly on Android by @Ahamed-Ali
in #30023
* Revert "Fixes Setting BackgroundColor to null does not actually
changes BackgroundColor #22914 (#22917)" by @mattleibow in
#30031
* [create-pull-request] automated change by @github-actions[bot] in
#30019
* [create-pull-request] automated change by @github-actions[bot] in
#30043
* [create-pull-request] automated change by @github-actions[bot] in
#30078
* Update Controls.TestCases.HostApp.csproj by @HarishKumarSF4517 in
#30124

## New Contributors
* @albyrock87 made their first contribution in
#29639
* @SyedAbdulAzeemSF4852 made their first contribution in
#30007
* @emiller made their first contribution in
#20048
* @jgonzalez-gft made their first contribution in
#22917
* @aheubusch made their first contribution in
#25067

**Full Changelog**:
https://github.com/dotnet/maui/compare/main..inflight/candidate
@github-actions github-actions bot locked and limited conversation to collaborators Jul 22, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-controls-collectionview CollectionView, CarouselView, IndicatorView community ✨ Community Contribution p/0 Work that we can't release without
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

CollectionView iOS cutoff On iOS, CollectionView with GridItemsLayout and Span set is centering the item when the collection has only one item.
5 participants