Skip to content

Commit

Permalink
Partially revert "[Windows] Update CollectionView changing ItemsLayout (
Browse files Browse the repository at this point in the history
#14532)" (#18356)

This partially reverts commit d569287,
keeping the AdaptiveCollectionView sample.

The reverted change was switching to only use FormsGridView as backing
control for CollectionView what causes issues like
#16234.
  • Loading branch information
emaf authored Oct 26, 2023
1 parent d1fee35 commit ac996a0
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 119 deletions.
42 changes: 9 additions & 33 deletions src/Controls/src/Core/Handlers/Items/ItemsViewHandler.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ public abstract partial class ItemsViewHandler<TItemsView> : ViewHandler<TItemsV
bool _emptyViewDisplayed;
double _previousHorizontalOffset;
double _previousVerticalOffset;
double _previousItemSpacing;
double _previousHorizontalItemSpacing;
double _previousVerticalItemSpacing;

protected ListViewBase ListViewBase => PlatformView;
protected TItemsView ItemsView => VirtualView;
protected TItemsView Element => VirtualView;
Expand Down Expand Up @@ -339,38 +335,18 @@ protected virtual void UpdateEmptyView()

protected virtual void UpdateItemsLayout()
{
if (ListViewBase is FormsGridView gridView)
{
if (Layout is LinearItemsLayout linearItemsLayout)
{
gridView.Orientation = linearItemsLayout.ToPlatform();
ListViewBase.IsSynchronizedWithCurrentItem = false;

gridView.Span = 1;

if (linearItemsLayout.ItemSpacing != _previousItemSpacing)
{
_previousItemSpacing = linearItemsLayout.ItemSpacing;
gridView.ItemContainerStyle = linearItemsLayout.GetItemContainerStyle();
}
}

if (Layout is GridItemsLayout gridItemsLayout)
{
gridView.Orientation = gridItemsLayout.ToPlatform();

gridView.Span = gridItemsLayout.Span;
FindScrollViewer(ListViewBase);

if (gridItemsLayout.HorizontalItemSpacing != _previousHorizontalItemSpacing ||
gridItemsLayout.VerticalItemSpacing != _previousVerticalItemSpacing)
{
_previousHorizontalItemSpacing = gridItemsLayout.HorizontalItemSpacing;
_previousVerticalItemSpacing = gridItemsLayout.VerticalItemSpacing;
gridView.ItemContainerStyle = gridItemsLayout.GetItemContainerStyle();
}
}
}
_defaultHorizontalScrollVisibility = null;
_defaultVerticalScrollVisibility = null;

FindScrollViewer(ListViewBase);
UpdateItemTemplate();
UpdateItemsSource();
UpdateVerticalScrollBarVisibility();
UpdateHorizontalScrollBarVisibility();
UpdateEmptyView();
}

void FindScrollViewer(ListViewBase listView)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
using System;
using System.ComponentModel;
using Microsoft.Maui.Controls.Platform;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using WASDKApp = Microsoft.UI.Xaml.Application;
using WListView = Microsoft.UI.Xaml.Controls.ListView;
using WScrollMode = Microsoft.UI.Xaml.Controls.ScrollMode;
using WSetter = Microsoft.UI.Xaml.Setter;
using WStyle = Microsoft.UI.Xaml.Style;

namespace Microsoft.Maui.Controls.Handlers.Items
{
Expand Down Expand Up @@ -81,9 +87,9 @@ protected override ListViewBase SelectListViewBase()
case GridItemsLayout gridItemsLayout:
return CreateGridView(gridItemsLayout);
case LinearItemsLayout listItemsLayout when listItemsLayout.Orientation == ItemsLayoutOrientation.Vertical:
return CreateGridView(new GridItemsLayout(ItemsLayoutOrientation.Vertical) { Span = 1 });
return CreateVerticalListView(listItemsLayout);
case LinearItemsLayout listItemsLayout when listItemsLayout.Orientation == ItemsLayoutOrientation.Horizontal:
return CreateGridView(new GridItemsLayout(ItemsLayoutOrientation.Horizontal) { Span = 1 });
return CreateHorizontalListView(listItemsLayout);
}

throw new NotImplementedException("The layout is not implemented");
Expand Down Expand Up @@ -196,10 +202,77 @@ static ListViewBase CreateGridView(GridItemsLayout gridItemsLayout)
: Orientation.Vertical,

Span = gridItemsLayout.Span,
ItemContainerStyle = gridItemsLayout.GetItemContainerStyle()
ItemContainerStyle = GetItemContainerStyle(gridItemsLayout)
};
}

static ListViewBase CreateVerticalListView(LinearItemsLayout listItemsLayout)
{
return new FormsListView()
{
ItemContainerStyle = GetVerticalItemContainerStyle(listItemsLayout)
};
}

static ListViewBase CreateHorizontalListView(LinearItemsLayout listItemsLayout)
{
var horizontalListView = new FormsListView()
{
ItemsPanel = (ItemsPanelTemplate)WASDKApp.Current.Resources["HorizontalListItemsPanel"],
ItemContainerStyle = GetHorizontalItemContainerStyle(listItemsLayout)
};
ScrollViewer.SetVerticalScrollBarVisibility(horizontalListView, Microsoft.UI.Xaml.Controls.ScrollBarVisibility.Hidden);
ScrollViewer.SetVerticalScrollMode(horizontalListView, WScrollMode.Disabled);
ScrollViewer.SetHorizontalScrollMode(horizontalListView, WScrollMode.Auto);
ScrollViewer.SetHorizontalScrollBarVisibility(horizontalListView, Microsoft.UI.Xaml.Controls.ScrollBarVisibility.Auto);

return horizontalListView;
}

static WStyle GetItemContainerStyle(GridItemsLayout layout)
{
var h = layout?.HorizontalItemSpacing ?? 0;
var v = layout?.VerticalItemSpacing ?? 0;
var margin = WinUIHelpers.CreateThickness(h, v, h, v);

var style = new WStyle(typeof(GridViewItem));

style.Setters.Add(new WSetter(FrameworkElement.MarginProperty, margin));
style.Setters.Add(new WSetter(Control.PaddingProperty, WinUIHelpers.CreateThickness(0)));
style.Setters.Add(new WSetter(Control.HorizontalContentAlignmentProperty, HorizontalAlignment.Stretch));

return style;
}

static WStyle GetVerticalItemContainerStyle(LinearItemsLayout layout)
{
var v = layout?.ItemSpacing ?? 0;
var margin = WinUIHelpers.CreateThickness(0, v, 0, v);

var style = new WStyle(typeof(ListViewItem));

style.Setters.Add(new WSetter(FrameworkElement.MinHeightProperty, 0));
style.Setters.Add(new WSetter(FrameworkElement.MarginProperty, margin));
style.Setters.Add(new WSetter(Control.PaddingProperty, WinUIHelpers.CreateThickness(0)));
style.Setters.Add(new WSetter(Control.HorizontalContentAlignmentProperty, HorizontalAlignment.Stretch));

return style;
}

static WStyle GetHorizontalItemContainerStyle(LinearItemsLayout layout)
{
var h = layout?.ItemSpacing ?? 0;
var padding = WinUIHelpers.CreateThickness(h, 0, h, 0);

var style = new WStyle(typeof(ListViewItem));

style.Setters.Add(new WSetter(FrameworkElement.MinWidthProperty, 0));
style.Setters.Add(new WSetter(Control.PaddingProperty, padding));
style.Setters.Add(new WSetter(Control.VerticalContentAlignmentProperty, VerticalAlignment.Stretch));

return style;
}

void UpdateItemsLayoutSpan()
{
if (ListViewBase is FormsGridView formsGridView)
Expand All @@ -210,13 +283,22 @@ void UpdateItemsLayoutSpan()

void UpdateItemsLayoutItemSpacing()
{
if (ListViewBase is FormsGridView formsGridView)
if (ListViewBase is FormsGridView formsGridView && Layout is GridItemsLayout gridLayout)
{
if (Layout is GridItemsLayout gridItemsLayout)
formsGridView.ItemContainerStyle = gridItemsLayout.GetItemContainerStyle();
formsGridView.ItemContainerStyle = GetItemContainerStyle(gridLayout);
}

if (Layout is LinearItemsLayout linearItemsLayout)
formsGridView.ItemContainerStyle = linearItemsLayout.GetItemContainerStyle();
if (Layout is LinearItemsLayout linearItemsLayout)
{
switch (ListViewBase)
{
case FormsListView formsListView:
formsListView.ItemContainerStyle = GetVerticalItemContainerStyle(linearItemsLayout);
break;
case WListView listView:
listView.ItemContainerStyle = GetHorizontalItemContainerStyle(linearItemsLayout);
break;
}
}
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ Microsoft.Maui.Controls.InputView.IsTextPredictionEnabled.get -> bool
Microsoft.Maui.Controls.InputView.IsTextPredictionEnabled.set -> void
Microsoft.Maui.Controls.IWindowCreator
Microsoft.Maui.Controls.IWindowCreator.CreateWindow(Microsoft.Maui.Controls.Application! app, Microsoft.Maui.IActivationState? activationState) -> Microsoft.Maui.Controls.Window!
Microsoft.Maui.Controls.Platform.CollectionViewExtensions
Microsoft.Maui.Controls.Platform.ShapesExtensions
Microsoft.Maui.Controls.PointerGestureRecognizer.PointerEnteredCommandParameter.get -> object!
Microsoft.Maui.Controls.PointerGestureRecognizer.PointerExitedCommandParameter.get -> object!
Expand All @@ -113,9 +112,6 @@ static Microsoft.Maui.Controls.LayoutOptions.operator !=(Microsoft.Maui.Controls
static Microsoft.Maui.Controls.LayoutOptions.operator ==(Microsoft.Maui.Controls.LayoutOptions left, Microsoft.Maui.Controls.LayoutOptions right) -> bool
static Microsoft.Maui.Controls.Picker.MapHorizontalOptions(Microsoft.Maui.Handlers.IPickerHandler! handler, Microsoft.Maui.Controls.Picker! picker) -> void
static Microsoft.Maui.Controls.Picker.MapVerticalOptions(Microsoft.Maui.Handlers.IPickerHandler! handler, Microsoft.Maui.Controls.Picker! picker) -> void
static Microsoft.Maui.Controls.Platform.CollectionViewExtensions.GetItemContainerStyle(this Microsoft.Maui.Controls.GridItemsLayout? layout) -> Microsoft.UI.Xaml.Style?
static Microsoft.Maui.Controls.Platform.CollectionViewExtensions.GetItemContainerStyle(this Microsoft.Maui.Controls.LinearItemsLayout? layout) -> Microsoft.UI.Xaml.Style?
static Microsoft.Maui.Controls.Platform.CollectionViewExtensions.ToPlatform(this Microsoft.Maui.Controls.ItemsLayout! layout) -> Microsoft.UI.Xaml.Controls.Orientation
static Microsoft.Maui.Controls.Platform.PickerExtensions.UpdateHorizontalOptions(this Microsoft.UI.Xaml.FrameworkElement! platformView, Microsoft.Maui.Controls.View! view) -> void
static Microsoft.Maui.Controls.Platform.PickerExtensions.UpdateVerticalOptions(this Microsoft.UI.Xaml.FrameworkElement! platformView, Microsoft.Maui.Controls.View! view) -> void
static Microsoft.Maui.Controls.Platform.ShapesExtensions.UpdatePath(this Microsoft.Maui.Graphics.Win2D.W2DGraphicsView! platformView, Microsoft.Maui.IShapeView! shapeView) -> void
Expand Down

0 comments on commit ac996a0

Please sign in to comment.