Skip to content

Commit

Permalink
fix: adjust frame coords for LayoutSlot in ListView/GridView
Browse files Browse the repository at this point in the history
  • Loading branch information
kazo0 committed Jan 11, 2022
1 parent b0fc2a7 commit ac7948e
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Uno.UI.RuntimeTests.Helpers;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using static Private.Infrastructure.TestServices;

namespace Uno.UI.RuntimeTests.Tests.Windows_UI_Xaml_Controls
{
#if HAS_UNO
[TestClass]
[RunsOnUIThread]
#if !__IOS__
[Ignore]
#endif
public class Given_GridView_Items
{
[TestMethod]
public async Task When_GridViewItems_LayoutSlots()
{
var gridView = new GridView
{
ItemContainerStyle = TestsResourceHelper.GetResource<Style>("MyGridViewItemStyle")
};

var gvi = new GridViewItem();
var gvi2 = new GridViewItem();

gridView.ItemsSource = new[] { gvi, gvi2 };

WindowHelper.WindowContent = gridView;
await WindowHelper.WaitForLoaded(gridView);
await WindowHelper.WaitForIdle();

RectAssert.AreEqual(new Rect
{
X = 0d,
Y = 0d,
Width = 104d,
Height = 104d,
},
gvi.LayoutSlot);

RectAssert.AreEqual(new Rect
{
X = 0d,
Y = 0d,
Width = 100d,
Height = 100d,
},
gvi.LayoutSlotWithMarginsAndAlignments);

RectAssert.AreEqual(new Rect
{
X = 104d,
Y = 0d,
Width = 104d,
Height = 104d,
},
gvi2.LayoutSlot);

RectAssert.AreEqual(new Rect
{
X = 104d,
Y = 0d,
Width = 100d,
Height = 100d,
},
gvi2.LayoutSlotWithMarginsAndAlignments);
}
}
#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,30 @@
HorizontalAlignment="Center" />
</Border>
</DataTemplate>

<Style x:Key="MyGridViewItemStyle"
TargetType="GridViewItem">
<Setter Property="Background" Value="Transparent" />
<Setter Property="TabNavigation" Value="Local" />
<Setter Property="IsHoldingEnabled" Value="True" />
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="MinWidth" Value="0" />
<Setter Property="MinHeight" Value="0" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="GridViewItem">
<Grid x:Name="ContentBorder">
<Rectangle x:Name="BorderRectangle"
Width="100"
Height="100"
IsHitTestVisible="False"
Fill="Green"
Stroke="Red"
StrokeThickness="2" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
17 changes: 13 additions & 4 deletions src/Uno.UI/UI/Xaml/Controls/ListViewBase/ListViewBaseSource.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,6 @@ public override CGRect Frame
{
base.Frame = value;
UpdateContentViewFrame();
UpdateContentLayoutSlots(value);
}
}

Expand All @@ -772,7 +771,6 @@ public override CGRect Bounds
}
base.Bounds = value;
UpdateContentViewFrame();
UpdateContentLayoutSlots(Frame);
}
}

Expand All @@ -797,8 +795,19 @@ private void UpdateContentLayoutSlots(Rect frame)
var content = Content;
if (content != null)
{
LayoutInformation.SetLayoutSlot(content, frame);
content.LayoutSlotWithMarginsAndAlignments = frame;
var layoutSlot = LayoutInformation.GetLayoutSlot(content);
var layoutSlotWithMarginsAndAlignments = content.LayoutSlotWithMarginsAndAlignments;

//The LayoutInformation within ArrangeChild does not take into account the offset relative to the native ListView, so we apply that offset here.
//This is needed for TransformToVisual to work
layoutSlot.X = frame.X;
layoutSlot.Y = frame.Y;

layoutSlotWithMarginsAndAlignments.X += frame.X;
layoutSlotWithMarginsAndAlignments.Y += frame.Y;

LayoutInformation.SetLayoutSlot(content, layoutSlot);
content.LayoutSlotWithMarginsAndAlignments = layoutSlotWithMarginsAndAlignments;
}
}

Expand Down

0 comments on commit ac7948e

Please sign in to comment.