Skip to content

Commit

Permalink
[Windows] Address CollectionView virtualization (#18813)
Browse files Browse the repository at this point in the history
* Add hard value for height w/ ItemContent control to partially allow virtualization to work

* Update test to be more robust

* Add real-width for ItemContentControl

---------

Co-authored-by: Mike Corsaro <[email protected]>
  • Loading branch information
Foda and Mike Corsaro authored Nov 22, 2023
1 parent 6a5bbf0 commit 926a002
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,12 @@ protected override WSize MeasureOverride(WSize availableSize)
{
if (_renderer == null)
{
// Make sure we supply a real number for sizes otherwise virtualization won't function
if (double.IsFinite(availableSize.Width) && !double.IsFinite(availableSize.Height))
return new WSize(availableSize.Width, 32);
else if (!double.IsFinite(availableSize.Width) && double.IsFinite(availableSize.Height))
return new WSize(88, availableSize.Height);

return base.MeasureOverride(availableSize);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Handlers.Items;
using Microsoft.Maui.Handlers;
using Microsoft.Maui.Platform;
using Microsoft.UI.Xaml;
using Xunit;
using WSetter = Microsoft.UI.Xaml.Setter;
Expand Down Expand Up @@ -101,6 +102,66 @@ void ValidateItemContainerStyle(CollectionView collectionView)
Assert.Equal(0d, minHeight);
}

[Fact]
public async Task ValidateItemsVirtualize()
{
SetupBuilder();

const int listItemCount = 1000;

var collectionView = new CollectionView()
{
ItemTemplate = new Controls.DataTemplate(() =>
{
var template = new Grid()
{
ColumnDefinitions = new ColumnDefinitionCollection(
[
new ColumnDefinition(25),
new ColumnDefinition(25),
new ColumnDefinition(25),
new ColumnDefinition(25),
new ColumnDefinition(25),
])
};

for (int i = 0; i < 5; i++)
{
var label = new Label();
label.SetBinding(Label.TextProperty, new Binding("Symbol"));
Grid.SetColumn(label, i);
template.Add(label);
}

return template;
}),
ItemsSource = Enumerable.Range(0, listItemCount)
.Select(x => new { Symbol = x })
.ToList()
};

await CreateHandlerAndAddToWindow<CollectionViewHandler>(collectionView, async handler =>
{
var listView = (UI.Xaml.Controls.ListView)collectionView.Handler.PlatformView;

int childCount = 0;
int prevChildCount = -1;

await Task.Delay(2000);

await AssertionExtensions.Wait(() =>
{
// Wait until the list stops growing
prevChildCount = childCount;
childCount = listView.GetChildren<UI.Xaml.Controls.TextBlock>().Count();
return childCount == prevChildCount;
}, 10000);

// If this is broken we'll get way more than 1000 elements
Assert.True(childCount < 1000);
});
}

[Fact]
public async Task ValidateSendRemainingItemsThresholdReached()
{
Expand Down

0 comments on commit 926a002

Please sign in to comment.