Skip to content

Commit

Permalink
[Windows] Notify changes in CollectionView Layouts (#13137)
Browse files Browse the repository at this point in the history
* Notify changes in CollectionView Layouts on Windows

* Update src/Controls/src/Core/Handlers/Items/StructuredItemsViewHandler.Windows.cs

Co-authored-by: Manuel de la Pena <mandel@microsoft.com>

* Changes based on PR feedback

---------

Co-authored-by: Manuel de la Pena <mandel@microsoft.com>
  • Loading branch information
jsuarezruiz and mandel-macaque committed Mar 15, 2023
1 parent 22de2df commit d89ef37
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#nullable disable
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;
Expand All @@ -17,6 +19,32 @@ public partial class StructuredItemsViewHandler<TItemsView> : ItemsViewHandler<T

protected override IItemsLayout Layout { get => ItemsView?.ItemsLayout; }

protected override void ConnectHandler(ListViewBase platformView)
{
base.ConnectHandler(platformView);

if (Layout is not null)
Layout.PropertyChanged += LayoutPropertyChanged;
}

protected override void DisconnectHandler(ListViewBase platformView)
{
base.DisconnectHandler(platformView);

if (Layout is not null)
Layout.PropertyChanged -= LayoutPropertyChanged;
}

void LayoutPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == GridItemsLayout.SpanProperty.PropertyName)
UpdateItemsLayoutSpan();
else if (e.PropertyName == GridItemsLayout.HorizontalItemSpacingProperty.PropertyName || e.PropertyName == GridItemsLayout.VerticalItemSpacingProperty.PropertyName)
UpdateItemsLayoutItemSpacing();
else if (e.PropertyName == LinearItemsLayout.ItemSpacingProperty.PropertyName)
UpdateItemsLayoutItemSpacing();
}

public static void MapHeaderTemplate(StructuredItemsViewHandler<TItemsView> handler, StructuredItemsView itemsView)
{
handler.UpdateHeader();
Expand Down Expand Up @@ -229,5 +257,34 @@ static WStyle GetHorizontalItemContainerStyle(LinearItemsLayout layout)

return style;
}

void UpdateItemsLayoutSpan()
{
if (ListViewBase is FormsGridView formsGridView)
{
formsGridView.Span = ((GridItemsLayout)Layout).Span;
}
}

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

if (Layout is LinearItemsLayout linearItemsLayout)
{
switch (ListViewBase)
{
case FormsListView formsListView:
formsListView.ItemContainerStyle = GetVerticalItemContainerStyle(linearItemsLayout);
break;
case WListView listView:
listView.ItemContainerStyle = GetHorizontalItemContainerStyle(linearItemsLayout);
break;
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ override Microsoft.Maui.Controls.ImageButton.IsEnabledCore.get -> bool
override Microsoft.Maui.Controls.SearchBar.IsEnabledCore.get -> bool
~Microsoft.Maui.Controls.WebView.UserAgent.get -> string
~Microsoft.Maui.Controls.WebView.UserAgent.set -> void
~override Microsoft.Maui.Controls.Handlers.Items.StructuredItemsViewHandler<TItemsView>.ConnectHandler(Microsoft.UI.Xaml.Controls.ListViewBase platformView) -> void
~override Microsoft.Maui.Controls.Handlers.Items.StructuredItemsViewHandler<TItemsView>.DisconnectHandler(Microsoft.UI.Xaml.Controls.ListViewBase platformView) -> void
~override Microsoft.Maui.Controls.LayoutOptions.Equals(object obj) -> bool
~override Microsoft.Maui.Controls.Region.Equals(object obj) -> bool
~override Microsoft.Maui.Controls.Shapes.Matrix.Equals(object obj) -> bool
Expand Down

0 comments on commit d89ef37

Please sign in to comment.