-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[iOS] Fixed KeepLastItemInView for CV1 and added support for KeepLastItemInView for CV2 #28720
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
Closed
Closed
Changes from all commits
Commits
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
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
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 |
---|---|---|
|
@@ -11,16 +11,16 @@ namespace Microsoft.Maui.Controls.Handlers.Items2; | |
internal static class LayoutFactory2 | ||
{ | ||
public static UICollectionViewLayout CreateList(LinearItemsLayout linearItemsLayout, | ||
LayoutGroupingInfo groupingInfo, LayoutHeaderFooterInfo headerFooterInfo) | ||
LayoutGroupingInfo groupingInfo, LayoutHeaderFooterInfo headerFooterInfo, ItemsUpdatingScrollMode itemsUpdatingScrollMode) | ||
=> linearItemsLayout.Orientation == ItemsLayoutOrientation.Vertical | ||
? CreateVerticalList(linearItemsLayout, groupingInfo, headerFooterInfo) | ||
: CreateHorizontalList(linearItemsLayout, groupingInfo, headerFooterInfo); | ||
? CreateVerticalList(linearItemsLayout, groupingInfo, headerFooterInfo, itemsUpdatingScrollMode) | ||
: CreateHorizontalList(linearItemsLayout, groupingInfo, headerFooterInfo, itemsUpdatingScrollMode); | ||
|
||
public static UICollectionViewLayout CreateGrid(GridItemsLayout gridItemsLayout, | ||
LayoutGroupingInfo groupingInfo, LayoutHeaderFooterInfo headerFooterInfo) | ||
LayoutGroupingInfo groupingInfo, LayoutHeaderFooterInfo headerFooterInfo, ItemsUpdatingScrollMode itemsUpdatingScrollMode) | ||
=> gridItemsLayout.Orientation == ItemsLayoutOrientation.Vertical | ||
? CreateVerticalGrid(gridItemsLayout, groupingInfo, headerFooterInfo) | ||
: CreateHorizontalGrid(gridItemsLayout, groupingInfo, headerFooterInfo); | ||
? CreateVerticalGrid(gridItemsLayout, groupingInfo, headerFooterInfo, itemsUpdatingScrollMode) | ||
: CreateHorizontalGrid(gridItemsLayout, groupingInfo, headerFooterInfo, itemsUpdatingScrollMode); | ||
|
||
static NSCollectionLayoutBoundarySupplementaryItem[] CreateSupplementaryItems(LayoutGroupingInfo? groupingInfo, LayoutHeaderFooterInfo? layoutHeaderFooterInfo, | ||
UICollectionViewScrollDirection scrollDirection, NSCollectionLayoutDimension width, NSCollectionLayoutDimension height) | ||
|
@@ -83,7 +83,7 @@ static NSCollectionLayoutBoundarySupplementaryItem[] CreateSupplementaryItems(La | |
return []; | ||
} | ||
|
||
static UICollectionViewLayout CreateListLayout(UICollectionViewScrollDirection scrollDirection, LayoutGroupingInfo groupingInfo, LayoutHeaderFooterInfo layoutHeaderFooterInfo, LayoutSnapInfo snapInfo, NSCollectionLayoutDimension itemWidth, NSCollectionLayoutDimension itemHeight, NSCollectionLayoutDimension groupWidth, NSCollectionLayoutDimension groupHeight, double itemSpacing, Func<Thickness>? peekAreaInsetsFunc) | ||
static UICollectionViewLayout CreateListLayout(UICollectionViewScrollDirection scrollDirection, LayoutGroupingInfo groupingInfo, LayoutHeaderFooterInfo layoutHeaderFooterInfo, LayoutSnapInfo snapInfo, NSCollectionLayoutDimension itemWidth, NSCollectionLayoutDimension itemHeight, NSCollectionLayoutDimension groupWidth, NSCollectionLayoutDimension groupHeight, double itemSpacing, Func<Thickness>? peekAreaInsetsFunc, ItemsUpdatingScrollMode itemsUpdatingScrollMode) | ||
{ | ||
var layoutConfiguration = new UICollectionViewCompositionalLayoutConfiguration(); | ||
layoutConfiguration.ScrollDirection = scrollDirection; | ||
|
@@ -137,14 +137,14 @@ static UICollectionViewLayout CreateListLayout(UICollectionViewScrollDirection s | |
groupHeight); | ||
|
||
return section; | ||
}, layoutConfiguration); | ||
}, layoutConfiguration, itemsUpdatingScrollMode); | ||
|
||
return layout; | ||
} | ||
|
||
|
||
|
||
static UICollectionViewLayout CreateGridLayout(UICollectionViewScrollDirection scrollDirection, LayoutGroupingInfo groupingInfo, LayoutHeaderFooterInfo headerFooterInfo, LayoutSnapInfo snapInfo, NSCollectionLayoutDimension itemWidth, NSCollectionLayoutDimension itemHeight, NSCollectionLayoutDimension groupWidth, NSCollectionLayoutDimension groupHeight, double verticalItemSpacing, double horizontalItemSpacing, int columns) | ||
static UICollectionViewLayout CreateGridLayout(UICollectionViewScrollDirection scrollDirection, LayoutGroupingInfo groupingInfo, LayoutHeaderFooterInfo headerFooterInfo, LayoutSnapInfo snapInfo, NSCollectionLayoutDimension itemWidth, NSCollectionLayoutDimension itemHeight, NSCollectionLayoutDimension groupWidth, NSCollectionLayoutDimension groupHeight, double verticalItemSpacing, double horizontalItemSpacing, int columns, ItemsUpdatingScrollMode itemsUpdatingScrollMode) | ||
{ | ||
var layoutConfiguration = new UICollectionViewCompositionalLayoutConfiguration(); | ||
layoutConfiguration.ScrollDirection = scrollDirection; | ||
|
@@ -189,13 +189,13 @@ static UICollectionViewLayout CreateGridLayout(UICollectionViewScrollDirection s | |
groupHeight); | ||
|
||
return section; | ||
}, layoutConfiguration); | ||
}, layoutConfiguration, itemsUpdatingScrollMode); | ||
|
||
return layout; | ||
} | ||
|
||
public static UICollectionViewLayout CreateVerticalList(LinearItemsLayout linearItemsLayout, | ||
LayoutGroupingInfo groupingInfo, LayoutHeaderFooterInfo headerFooterInfo) | ||
LayoutGroupingInfo groupingInfo, LayoutHeaderFooterInfo headerFooterInfo, ItemsUpdatingScrollMode itemsUpdatingScrollMode) | ||
=> CreateListLayout(UICollectionViewScrollDirection.Vertical, | ||
groupingInfo, | ||
headerFooterInfo, | ||
|
@@ -207,11 +207,12 @@ public static UICollectionViewLayout CreateVerticalList(LinearItemsLayout linear | |
NSCollectionLayoutDimension.CreateFractionalWidth(1f), | ||
NSCollectionLayoutDimension.CreateEstimated(30f), | ||
linearItemsLayout.ItemSpacing, | ||
null); | ||
null, | ||
itemsUpdatingScrollMode); | ||
|
||
|
||
public static UICollectionViewLayout CreateHorizontalList(LinearItemsLayout linearItemsLayout, | ||
LayoutGroupingInfo groupingInfo, LayoutHeaderFooterInfo headerFooterInfo) | ||
LayoutGroupingInfo groupingInfo, LayoutHeaderFooterInfo headerFooterInfo, ItemsUpdatingScrollMode itemsUpdatingScrollMode) | ||
=> CreateListLayout(UICollectionViewScrollDirection.Horizontal, | ||
groupingInfo, | ||
headerFooterInfo, | ||
|
@@ -223,10 +224,11 @@ public static UICollectionViewLayout CreateHorizontalList(LinearItemsLayout line | |
NSCollectionLayoutDimension.CreateEstimated(30f), | ||
NSCollectionLayoutDimension.CreateFractionalHeight(1f), | ||
linearItemsLayout.ItemSpacing, | ||
null); | ||
null, | ||
itemsUpdatingScrollMode); | ||
|
||
public static UICollectionViewLayout CreateVerticalGrid(GridItemsLayout gridItemsLayout, | ||
LayoutGroupingInfo groupingInfo, LayoutHeaderFooterInfo headerFooterInfo) | ||
LayoutGroupingInfo groupingInfo, LayoutHeaderFooterInfo headerFooterInfo, ItemsUpdatingScrollMode itemsUpdatingScrollMode) | ||
=> CreateGridLayout(UICollectionViewScrollDirection.Vertical, | ||
groupingInfo, | ||
headerFooterInfo, | ||
|
@@ -241,11 +243,12 @@ public static UICollectionViewLayout CreateVerticalGrid(GridItemsLayout gridItem | |
NSCollectionLayoutDimension.CreateEstimated(30f), | ||
gridItemsLayout.VerticalItemSpacing, | ||
gridItemsLayout.HorizontalItemSpacing, | ||
gridItemsLayout.Span); | ||
gridItemsLayout.Span, | ||
itemsUpdatingScrollMode); | ||
|
||
|
||
public static UICollectionViewLayout CreateHorizontalGrid(GridItemsLayout gridItemsLayout, | ||
LayoutGroupingInfo groupingInfo, LayoutHeaderFooterInfo headerFooterInfo) | ||
LayoutGroupingInfo groupingInfo, LayoutHeaderFooterInfo headerFooterInfo, ItemsUpdatingScrollMode itemsUpdatingScrollMode) | ||
=> CreateGridLayout(UICollectionViewScrollDirection.Horizontal, | ||
groupingInfo, | ||
headerFooterInfo, | ||
|
@@ -260,7 +263,8 @@ public static UICollectionViewLayout CreateHorizontalGrid(GridItemsLayout gridIt | |
NSCollectionLayoutDimension.CreateFractionalHeight(1f), | ||
gridItemsLayout.VerticalItemSpacing, | ||
gridItemsLayout.HorizontalItemSpacing, | ||
gridItemsLayout.Span); | ||
gridItemsLayout.Span, | ||
itemsUpdatingScrollMode); | ||
|
||
|
||
#nullable disable | ||
|
@@ -396,9 +400,50 @@ public static UICollectionViewLayout CreateCarouselLayout( | |
class CustomUICollectionViewCompositionalLayout : UICollectionViewCompositionalLayout | ||
{ | ||
LayoutSnapInfo _snapInfo; | ||
public CustomUICollectionViewCompositionalLayout(LayoutSnapInfo snapInfo, UICollectionViewCompositionalLayoutSectionProvider sectionProvider, UICollectionViewCompositionalLayoutConfiguration configuration) : base(sectionProvider, configuration) | ||
ItemsUpdatingScrollMode _itemsUpdatingScrollMode; | ||
|
||
public CustomUICollectionViewCompositionalLayout(LayoutSnapInfo snapInfo, UICollectionViewCompositionalLayoutSectionProvider sectionProvider, UICollectionViewCompositionalLayoutConfiguration configuration, ItemsUpdatingScrollMode itemsUpdatingScrollMode) : base(sectionProvider, configuration) | ||
{ | ||
_snapInfo = snapInfo; | ||
_itemsUpdatingScrollMode = itemsUpdatingScrollMode; | ||
} | ||
|
||
public override void FinalizeCollectionViewUpdates() | ||
{ | ||
base.FinalizeCollectionViewUpdates(); | ||
|
||
if (_itemsUpdatingScrollMode == ItemsUpdatingScrollMode.KeepLastItemInView) | ||
{ | ||
ForceScrollToLastItem(CollectionView); | ||
} | ||
} | ||
|
||
void ForceScrollToLastItem(UICollectionView collectionView) | ||
{ | ||
var sections = (int)collectionView.NumberOfSections(); | ||
|
||
if (sections == 0) | ||
{ | ||
return; | ||
} | ||
|
||
for (int section = sections - 1; section >= 0; section--) | ||
{ | ||
var itemCount = collectionView.NumberOfItemsInSection(section); | ||
if (itemCount > 0) | ||
{ | ||
var lastIndexPath = NSIndexPath.FromItemSection(itemCount - 1, section); | ||
if (Configuration.ScrollDirection == UICollectionViewScrollDirection.Vertical) | ||
{ | ||
collectionView.ScrollToItem(lastIndexPath, UICollectionViewScrollPosition.Bottom, true); | ||
} | ||
else | ||
{ | ||
collectionView.ScrollToItem(lastIndexPath, UICollectionViewScrollPosition.Right, true); | ||
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. I wonder if we should look at RTL here 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. It turns out that the current implementation already supports RTL. I've included it in the UITest to be sure |
||
} | ||
return; | ||
} | ||
} | ||
} | ||
|
||
public override CGPoint TargetContentOffset(CGPoint proposedContentOffset, CGPoint scrollingVelocity) | ||
|
49 changes: 49 additions & 0 deletions
49
src/Controls/tests/TestCases.HostApp/Issues/Issue28720.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,49 @@ | ||
<?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.Issue28720"> | ||
<Grid RowDefinitions="Auto,100,100,100"> | ||
<Button Text="Add Item" | ||
Command="{Binding AddItemCommand}" | ||
AutomationId="AddItemButton" | ||
HorizontalOptions="Center" | ||
VerticalOptions="Center"/> | ||
<CollectionView | ||
ItemsUpdatingScrollMode="KeepLastItemInView" | ||
ItemsSource="{Binding Items}" | ||
Grid.Row="1"> | ||
<CollectionView.ItemsLayout> | ||
<LinearItemsLayout Orientation="Horizontal"/> | ||
</CollectionView.ItemsLayout> | ||
<CollectionView.ItemTemplate> | ||
<DataTemplate> | ||
<Label Text="{Binding . ,StringFormat='{0}cv1'}"/> | ||
</DataTemplate> | ||
</CollectionView.ItemTemplate> | ||
</CollectionView> | ||
<CollectionView | ||
ItemsUpdatingScrollMode="KeepLastItemInView" | ||
FlowDirection="RightToLeft" | ||
ItemsSource="{Binding Items}" | ||
Grid.Row="2"> | ||
<CollectionView.ItemsLayout> | ||
<LinearItemsLayout Orientation="Horizontal"/> | ||
</CollectionView.ItemsLayout> | ||
<CollectionView.ItemTemplate> | ||
<DataTemplate> | ||
<Label Text="{Binding . ,StringFormat='{0}cv2'}"/> | ||
</DataTemplate> | ||
</CollectionView.ItemTemplate> | ||
</CollectionView> | ||
<CollectionView | ||
ItemsUpdatingScrollMode="KeepLastItemInView" | ||
ItemsSource="{Binding Items}" | ||
Grid.Row="3"> | ||
<CollectionView.ItemTemplate> | ||
<DataTemplate> | ||
<Label Text="{Binding . ,StringFormat='{0}cv3'}"/> | ||
</DataTemplate> | ||
</CollectionView.ItemTemplate> | ||
</CollectionView> | ||
</Grid> | ||
</ContentPage> |
33 changes: 33 additions & 0 deletions
33
src/Controls/tests/TestCases.HostApp/Issues/Issue28720.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,33 @@ | ||
using System.Collections.ObjectModel; | ||
|
||
namespace Maui.Controls.Sample.Issues; | ||
|
||
[Issue(IssueTracker.Github, 28720, "Support for KeepLastItemInView for CV2", PlatformAffected.iOS)] | ||
public partial class Issue28720 : ContentPage | ||
{ | ||
private ObservableCollection<string> _items; | ||
public ObservableCollection<string> Items | ||
{ | ||
get => _items; | ||
set | ||
{ | ||
if (_items != value) | ||
{ | ||
_items = value; | ||
OnPropertyChanged(); | ||
} | ||
} | ||
} | ||
|
||
public Command AddItemCommand => new(() => | ||
{ | ||
Items.Add($"Item{_items.Count}"); | ||
}); | ||
|
||
public Issue28720() | ||
{ | ||
InitializeComponent(); | ||
Items = [.. Enumerable.Range(0, 20).Select(x => $"Item{x}")]; | ||
BindingContext = this; | ||
} | ||
} |
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
24 changes: 24 additions & 0 deletions
24
src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue28720.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,24 @@ | ||
using NUnit.Framework; | ||
using UITest.Appium; | ||
using UITest.Core; | ||
|
||
namespace Microsoft.Maui.TestCases.Tests.Issues; | ||
public class Issue28720 : _IssuesUITest | ||
{ | ||
public Issue28720(TestDevice testDevice) : base(testDevice) | ||
{ | ||
} | ||
|
||
public override string Issue => "Support for KeepLastItemInView for CV2"; | ||
|
||
[Test] | ||
[Category(UITestCategories.CollectionView)] | ||
public void KeepLastItemInViewShouldWork() | ||
{ | ||
App.WaitForElement("AddItemButton"); | ||
App.Click("AddItemButton"); | ||
App.WaitForElement("Item20cv1"); | ||
App.WaitForElement("Item20cv2"); | ||
App.WaitForElement("Item20cv3"); | ||
} | ||
} |
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.
Overriding FinalizeCollectionViewUpdates to scroll to the last item is a significant behavior change; please ensure tests verify the accuracy of this scrolling behavior.
Copilot uses AI. Check for mistakes.