Skip to content

Commit

Permalink
Merge pull request #2356 from unoplatform/dev/djo/listview-ios-fix-6
Browse files Browse the repository at this point in the history
[iOS] Fix bug with ListViewItem.Margin application
  • Loading branch information
davidjohnoliver authored Dec 19, 2019
2 parents 4088dea + fe0cd82 commit 9115ee0
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 13 deletions.
12 changes: 12 additions & 0 deletions src/SamplesApp/SamplesApp.Shared/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,18 @@ static void ConfigureFilters(ILoggerFactory factory)

// Binder memory references tracking
// { "ReferenceHolder", LogLevel.Debug },

// ListView-related messages
// { "Windows.UI.Xaml.Controls.ListViewBase", LogLevel.Debug },
// { "Windows.UI.Xaml.Controls.ListView", LogLevel.Debug },
// { "Windows.UI.Xaml.Controls.GridView", LogLevel.Debug },
// { "Windows.UI.Xaml.Controls.VirtualizingPanelLayout", LogLevel.Debug },
// { "Windows.UI.Xaml.Controls.NativeListViewBase", LogLevel.Debug },
// { "Windows.UI.Xaml.Controls.ListViewBaseSource", LogLevel.Debug }, //iOS
// { "Windows.UI.Xaml.Controls.ListViewBaseInternalContainer", LogLevel.Debug }, //iOS
// { "Windows.UI.Xaml.Controls.NativeListViewBaseAdapter", LogLevel.Debug }, //Android
// { "Windows.UI.Xaml.Controls.BufferViewCache", LogLevel.Debug }, //Android
// { "Windows.UI.Xaml.Controls.VirtualizingPanelGenerator", LogLevel.Debug }, //WASM
}
)
#if DEBUG
Expand Down
7 changes: 7 additions & 0 deletions src/SamplesApp/UITests.Shared/UITests.Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\ListView\ListView_Margin_On_Container.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\ListView\ListView_WithScrollViewer.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand Down Expand Up @@ -3079,6 +3083,9 @@
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\ImageTests\Image_Stretch_Modes_Picker.xaml.cs">
<DependentUpon>Image_Stretch_Modes_Picker.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\ListView\ListView_Margin_On_Container.xaml.cs">
<DependentUpon>ListView_Margin_On_Container.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\ListView\ListView_WithScrollViewer.xaml.cs">
<DependentUpon>ListView_WithScrollViewer.xaml</DependentUpon>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<UserControl x:Class="UITests.Shared.Windows_UI_Xaml_Controls.ListView.ListView_Margin_On_Container"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UITests.Shared.Windows_UI_Xaml_Controls.ListView"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">


<UserControl.Resources>
<Style x:Key="BillingAddressListViewStyle"
TargetType="ListViewItem">

<Setter Property="Background"
Value="LightBlue" />
<Setter Property="BorderThickness"
Value="0" />
<Setter Property="Padding"
Value="0" />
<Setter Property="Margin"
Value="0,42,0,0" />

<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<Grid x:Name="RootGrid"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="0,0,0,1"
Padding="{TemplateBinding Padding}">
<!-- ContentPresenter -->
<ContentPresenter x:Name="ContentPresenter"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

<!-- BillingAddress Template -->
<DataTemplate x:Key="BillingAddressTemplate">
<Grid Background="LightPink"
x:Name="ContentGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>

<!-- Info -->
<StackPanel Margin="12,0">

<!-- Location Name -->
<TextBlock MaxLines="1">
<Run Text="Maximus" />
<Run Text="Aurelius" />
</TextBlock>

<!-- Address Line 1 -->
<TextBlock Text="215 St. Jacques" />

<!-- Address Line 2 -->
<TextBlock Text="Suite 500" />

<!-- Address Line 2 -->
<TextBlock>
<Run Text="Montreal" /><Run Text="," />
<Run Text="QC" /><Run Text="," />
<Run Text="H2Y 1M6" />
</TextBlock>

<StackPanel Margin="0,4,0,0">

<!-- Phone Number Day -->
<TextBlock>
<Run Text="1 888 926-1276" />
<Run Text="(jour)" />
</TextBlock>

<!-- Phone Number Evening -->
<TextBlock>
<Run Text="1 888 926-1276" />
<Run Text="(soir)" />
</TextBlock>
</StackPanel>
</StackPanel>
</Grid>
</DataTemplate>
</UserControl.Resources>

<Grid>
<ListView x:Name="ListTest"
ItemsSource="012345678"
ItemContainerStyle="{StaticResource BillingAddressListViewStyle}"
ItemTemplate="{StaticResource BillingAddressTemplate}"
IsItemClickEnabled="True"
SelectionMode="Single"
Margin="0,8,0,0" />
</Grid>
</UserControl>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Uno.UI.Samples.Controls;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The User Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234236

namespace UITests.Shared.Windows_UI_Xaml_Controls.ListView
{
[SampleControlInfo(description: "ListView with Margin set on ListViewItems via ItemContainerStyle")]
public sealed partial class ListView_Margin_On_Container : UserControl
{

public ListView_Margin_On_Container()
{
this.InitializeComponent();
}
}
}
12 changes: 12 additions & 0 deletions src/SolutionTemplate/UnoSolutionTemplate/Shared/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,18 @@ static void ConfigureFilters(ILoggerFactory factory)

// DependencyObject memory references tracking
// { "ReferenceHolder", LogLevel.Debug },

// ListView-related messages
// { "Windows.UI.Xaml.Controls.ListViewBase", LogLevel.Debug },
// { "Windows.UI.Xaml.Controls.ListView", LogLevel.Debug },
// { "Windows.UI.Xaml.Controls.GridView", LogLevel.Debug },
// { "Windows.UI.Xaml.Controls.VirtualizingPanelLayout", LogLevel.Debug },
// { "Windows.UI.Xaml.Controls.NativeListViewBase", LogLevel.Debug },
// { "Windows.UI.Xaml.Controls.ListViewBaseSource", LogLevel.Debug }, //iOS
// { "Windows.UI.Xaml.Controls.ListViewBaseInternalContainer", LogLevel.Debug }, //iOS
// { "Windows.UI.Xaml.Controls.NativeListViewBaseAdapter", LogLevel.Debug }, //Android
// { "Windows.UI.Xaml.Controls.BufferViewCache", LogLevel.Debug }, //Android
// { "Windows.UI.Xaml.Controls.VirtualizingPanelGenerator", LogLevel.Debug }, //WASM
}
)
#if DEBUG
Expand Down
9 changes: 9 additions & 0 deletions src/Uno.UI/Extensions/CGSizeExtensions.iOSmacOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Text;
using CoreGraphics;
using Windows.Foundation;
using Windows.UI.Xaml;

namespace CoreGraphics
{
Expand All @@ -16,5 +17,13 @@ public static Windows.Foundation.Size ToFoundationSize(this CGSize size)

public static bool HasZeroArea(this CGSize size)
=> size.Height == 0 || size.Width == 0;

internal static CGSize Add(this CGSize left, Thickness right)
{
return new CGSize(
left.Width + right.Left + right.Right,
left.Height + right.Top + right.Bottom
);
}
}
}
20 changes: 10 additions & 10 deletions src/Uno.UI/UI/Xaml/Controls/ListViewBase/ListViewBaseSource.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,11 @@ public override UICollectionViewCell GetCell(UICollectionView collectionView, NS
}

FrameworkElement.InitializePhaseBinding(selectorItem);
// Ensure the item has a parent, since it's added to the native collection view
// which does not automatically sets the parent DependencyObject.
selectorItem.SetParent(Owner?.XamlParent);
}

// Ensure the item has a parent, since it's added to the native collection view
// which does not automatically sets the parent DependencyObject.
selectorItem.SetParent(Owner?.XamlParent);
}
else if (this.Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Debug))
{
this.Log().Debug($"Reusing view at indexPath={indexPath}, previously bound to {selectorItem.DataContext}.");
Expand Down Expand Up @@ -792,7 +792,7 @@ public override UICollectionViewLayoutAttributes PreferredLayoutAttributesFittin
return null;
}

if(Content == null)
if (Content == null)
{
this.Log().Error("Empty ListViewBaseInternalContainer content.");
return null;
Expand Down Expand Up @@ -840,13 +840,13 @@ public override UICollectionViewLayoutAttributes PreferredLayoutAttributesFittin
// cachedAttributes may be null if we have modified the collection with DeleteItems
var frame = cachedAttributes?.Frame ?? layoutAttributes.Frame;
SetExtent(ref frame, _measuredContentSize.Value);
if (this.Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Debug))
{
this.Log().Debug($"Adjusting layout attributes for item at {layoutAttributes.IndexPath}({layoutAttributes.RepresentedElementKind}), Content={Content?.Content}. Previous frame={layoutAttributes.Frame}, new frame={frame}.");
}
var sizesAreDifferent = frame.Size != layoutAttributes.Frame.Size;
if (sizesAreDifferent)
{
if (this.Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Debug))
{
this.Log().Debug($"Adjusting layout attributes for item at {layoutAttributes.IndexPath}({layoutAttributes.RepresentedElementKind}), Content={Content?.Content}. Previous frame={layoutAttributes.Frame}, new frame={frame}.");
}
Owner.NativeLayout.HasDynamicElementSizes = true;
this.Frame = frame;
SetNeedsLayout();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,10 @@ private bool IsLayoutRequiredOnSizeChange(CGSize oldAvailableSize, CGSize newAva
/// <returns>The total collection size</returns>
private CGSize PrepareLayoutInternal(bool createLayoutInfo, bool isCollectionChanged, CGSize size)
{
if (this.Log().IsEnabled(LogLevel.Debug))
{
this.Log().LogDebug($"PrepareLayoutInternal() - recalculating layout, createLayoutInfo={createLayoutInfo}, dirtyState={_dirtyState}, size={size} ");
}
Dictionary<NSIndexPath, CGSize?> oldItemSizes = null;
Dictionary<int, CGSize?> oldGroupHeaderSizes = null;

Expand Down
7 changes: 4 additions & 3 deletions src/Uno.UI/UI/Xaml/FrameworkElement.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public override void LayoutSubviews()

if (RequiresMeasure)
{
XamlMeasure(Bounds.Size);
// Add back the Margin (which is normally 'outside' the view's bounds) - the layouter will subtract it again
XamlMeasure(Bounds.Size.Add(Margin));
}

OnBeforeArrange();
Expand All @@ -64,7 +65,7 @@ public override void LayoutSubviews()
RequiresArrange = false;
}
}
catch(Exception e)
catch (Exception e)
{
this.Log().Error($"Layout failed in {GetType()}", e);
}
Expand Down Expand Up @@ -122,7 +123,7 @@ public override CGSize SizeThatFits(CGSize size)

var xamlMeasure = XamlMeasure(size);

if(xamlMeasure != null)
if (xamlMeasure != null)
{
return _lastMeasure = xamlMeasure.Value;
}
Expand Down

0 comments on commit 9115ee0

Please sign in to comment.