Skip to content

Commit ea0b254

Browse files
committed
feat(grid): Replaced the <Grid /> logic by the WinUI's one.
1 parent 201d277 commit ea0b254

31 files changed

+2863
-2211
lines changed

src/SamplesApp/SamplesApp.UnitTests.Shared/Controls/UITests/Views/Controls/SampleChooserControl.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@
340340

341341
<!--NAVIGATION BUTTONS-->
342342
<Border Background="{StaticResource Color08Brush}"
343-
Grid.ColumnSpan="2"
343+
344344
BorderThickness="0,1,0,0"
345345
BorderBrush="{StaticResource Color05Brush}"
346346
Height="50"

src/SamplesApp/UITests.Shared/UITests.Shared.projitems

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,6 +1201,10 @@
12011201
<SubType>Designer</SubType>
12021202
<Generator>MSBuild:Compile</Generator>
12031203
</Page>
1204+
<Page Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\GridTestsControl\Grid_Margin_And_Padding.xaml">
1205+
<SubType>Designer</SubType>
1206+
<Generator>MSBuild:Compile</Generator>
1207+
</Page>
12041208
<Page Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\GridTestsControl\Grid_Spacing_Adjustable.xaml">
12051209
<SubType>Designer</SubType>
12061210
<Generator>MSBuild:Compile</Generator>
@@ -4865,6 +4869,9 @@
48654869
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\Flyout\Flyout_Unloaded.xaml.cs">
48664870
<DependentUpon>Flyout_Unloaded.xaml</DependentUpon>
48674871
</Compile>
4872+
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\GridTestsControl\Grid_Margin_And_Padding.xaml.cs">
4873+
<DependentUpon>Grid_Margin_And_Padding.xaml</DependentUpon>
4874+
</Compile>
48684875
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\GridTestsControl\Grid_Spacing_Adjustable.xaml.cs">
48694876
<DependentUpon>Grid_Spacing_Adjustable.xaml</DependentUpon>
48704877
</Compile>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<Page
2+
x:Class="UITests.Windows_UI_Xaml_Controls.GridTestsControl.Grid_Margin_And_Padding"
3+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
4+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5+
xmlns:local="using:UITests.Windows_UI_Xaml_Controls.GridTestsControl"
6+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
7+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
8+
mc:Ignorable="d"
9+
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
10+
11+
<Grid Margin="20">
12+
<StackPanel Spacing="6">
13+
<StackPanel Orientation="Horizontal" Spacing="5">
14+
<Grid Width="100" Height="100" Background="Blue" Padding="10">
15+
<Grid Margin="10" Background="Red" />
16+
</Grid>
17+
18+
<Grid Width="100" Height="100" Background="Blue" Padding="20">
19+
<Grid Background="Red" Grid.Row="6000" />
20+
</Grid>
21+
22+
<Grid Width="100" Height="100" Background="Blue">
23+
<Grid.ColumnDefinitions>
24+
<ColumnDefinition Width="Auto" />
25+
<ColumnDefinition Width="*" />
26+
</Grid.ColumnDefinitions>
27+
<Grid Background="Red" Margin="20" Width="80" Grid.Column="95200" />
28+
</Grid>
29+
30+
<Grid Width="100" Height="100" Background="Blue">
31+
<Grid.RowDefinitions>
32+
<RowDefinition Height="Auto" /> <!-- 0 -->
33+
<RowDefinition Height="Auto" /> <!-- 1 -->
34+
<RowDefinition Height="Auto" /> <!-- 2 -->
35+
<RowDefinition Height="Auto" /> <!-- 3 -->
36+
<RowDefinition Height="Auto" /> <!-- 4 -->
37+
<RowDefinition Height="Auto" /> <!-- 5 -->
38+
<RowDefinition Height="Auto" /> <!-- 6 -->
39+
<RowDefinition Height="Auto" /> <!-- 7 -->
40+
<RowDefinition Height="Auto" /> <!-- 8 -->
41+
<RowDefinition Height="Auto" /> <!-- 9 -->
42+
<RowDefinition Height="Auto" /> <!-- 10 -->
43+
<RowDefinition Height="Auto" /> <!-- 11 -->
44+
<RowDefinition Height="Auto" /> <!-- 12 -->
45+
<RowDefinition Height="Auto" /> <!-- 13 -->
46+
<RowDefinition Height="*" /> <!-- 14 -->
47+
<RowDefinition Height="Auto" />
48+
<RowDefinition Height="Auto" />
49+
<RowDefinition Height="Auto" />
50+
</Grid.RowDefinitions>
51+
<Grid Background="Red" Margin="20" Grid.Row="14" />
52+
</Grid>
53+
54+
<Grid Width="100" Height="100" Background="Blue">
55+
<Grid Background="Red" Margin="20" Grid.RowSpan="100" Grid.ColumnSpan="100" />
56+
</Grid>
57+
58+
<Border Width="100" Height="100" Background="Blue" Padding="20">
59+
<Rectangle Fill="Red" />
60+
</Border>
61+
</StackPanel>
62+
</StackPanel>
63+
<Line Stroke="Yellow" StrokeThickness="3" X1="0" X2="625" Y1="20" Y2="20" Opacity="0.75" StrokeDashArray="1 2.5" />
64+
<Line Stroke="Yellow" StrokeThickness="3" X1="0" X2="625" Y1="80" Y2="80" Opacity="0.75" StrokeDashArray="1 2.5" />
65+
</Grid>
66+
</Page>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using Windows.UI.Xaml.Controls;
2+
using Uno.UI.Samples.Controls;
3+
4+
namespace UITests.Windows_UI_Xaml_Controls.GridTestsControl
5+
{
6+
[Sample]
7+
public sealed partial class Grid_Margin_And_Padding : Page
8+
{
9+
public Grid_Margin_And_Padding()
10+
{
11+
this.InitializeComponent();
12+
}
13+
}
14+
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Diagnostics;
5+
6+
namespace Uno.Collections
7+
{
8+
internal class StackVector<T> : IEnumerable<T> where T : struct
9+
{
10+
internal delegate bool RefPredicateDelegate(ref T item);
11+
12+
private Memory<T> _inner;
13+
private int _length;
14+
15+
public StackVector(int capacity, int initialLength = 0)
16+
{
17+
_inner = new Memory<T>(new T[capacity]);
18+
_length = initialLength;
19+
}
20+
21+
public IEnumerator<T> GetEnumerator() => new StackVectorEnumerator(this);
22+
23+
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
24+
25+
public int Count => _length;
26+
27+
public Memory<T> Memory => _inner.Slice(0, _length);
28+
29+
public void Resize(int newCount)
30+
{
31+
if (_inner.Length < newCount)
32+
{
33+
// This is a specialized list: we know
34+
// there's no need to recover items from previous
35+
// list.
36+
// The resize takes place only to reuse the working
37+
// memory, not to store it.
38+
39+
_inner = new Memory<T>(new T[newCount]);
40+
41+
IncrementResizeCount();
42+
}
43+
44+
_length = newCount;
45+
}
46+
47+
#if DEBUG
48+
internal int _numberOfResizes;
49+
internal static int _totalNumberOfResizes;
50+
#endif
51+
52+
[Conditional("DEBUG")]
53+
private void IncrementResizeCount()
54+
{
55+
_numberOfResizes++;
56+
_totalNumberOfResizes++;
57+
}
58+
59+
public ref T PushBack()
60+
{
61+
var newLength = _length + 1;
62+
Resize(newLength);
63+
return ref _inner.Span[newLength - 1];
64+
65+
}
66+
67+
public bool FirstOrDefault(RefPredicateDelegate predicate, ref T item)
68+
{
69+
for (var i = 0; i < _inner.Length; i++)
70+
{
71+
ref var itm = ref _inner.Span[i];
72+
if (predicate(ref itm))
73+
{
74+
item = itm;
75+
return true;
76+
}
77+
}
78+
79+
return false;
80+
}
81+
82+
public bool LastOrDefault(ref T last)
83+
{
84+
var length = _length;
85+
86+
if(length == 0)
87+
{
88+
return false;
89+
}
90+
91+
last = ref _inner.Span[length - 1];
92+
return true;
93+
}
94+
95+
public ref T this[int index]
96+
{
97+
get
98+
{
99+
if(index < 0 || index >= _length)
100+
{
101+
throw new ArgumentOutOfRangeException(nameof(index));
102+
}
103+
return ref _inner.Span[index];
104+
}
105+
}
106+
107+
private class StackVectorEnumerator : IEnumerator<T>
108+
{
109+
private readonly StackVector<T> _owner;
110+
private int _index = -1;
111+
112+
public StackVectorEnumerator(StackVector<T> owner)
113+
{
114+
_owner = owner;
115+
}
116+
117+
bool IEnumerator.MoveNext()
118+
{
119+
_index++;
120+
return _index < _owner._length;
121+
}
122+
123+
void IEnumerator.Reset() => _index = -1;
124+
125+
T IEnumerator<T>.Current => _owner._inner.Span[_index];
126+
127+
object IEnumerator.Current => _owner._inner.Span[_index];
128+
129+
void IDisposable.Dispose() { }
130+
}
131+
}
132+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System.Linq;
2+
using System.Runtime.CompilerServices;
3+
using FluentAssertions;
4+
using Microsoft.VisualStudio.TestTools.UnitTesting;
5+
using Uno.Collections;
6+
7+
namespace Uno.UI.Tests.Foundation
8+
{
9+
[TestClass]
10+
public class Given_StackVector
11+
{
12+
private struct MyStruct
13+
{
14+
internal int A;
15+
}
16+
17+
[TestMethod]
18+
public void EmptyStackVector()
19+
{
20+
var sut = new StackVector<MyStruct>(2);
21+
22+
sut.Count.Should().Be(0);
23+
sut.Should().HaveCount(0);
24+
25+
sut.ToArray().Should().HaveCount(0);
26+
sut.Memory.Length.Should().Be(0);
27+
}
28+
29+
[TestMethod]
30+
public void NonEmptyStackVector_Then_CountCheck()
31+
{
32+
var sut = new StackVector<MyStruct>(2, 2);
33+
34+
sut.Count.Should().Be(2);
35+
sut.Should().HaveCount(2);
36+
37+
sut.ToArray().Should().HaveCount(2);
38+
sut.Memory.Length.Should().Be(2);
39+
}
40+
41+
[TestMethod]
42+
public unsafe void ItemsInStackVector()
43+
{
44+
var sut = new StackVector<MyStruct>(2);
45+
46+
ref var item1 = ref sut.PushBack();
47+
item1.A = 1;
48+
49+
sut.Count.Should().Be(1);
50+
sut.Should().HaveCount(1);
51+
52+
ref var item2 = ref sut.PushBack();
53+
item2.A = 2;
54+
55+
sut.Count.Should().Be(2);
56+
sut.Should().HaveCount(2);
57+
58+
sut.Select(i => i.A)
59+
.Should()
60+
.BeEquivalentTo(new[] {1, 2});
61+
62+
var ptr1 = Unsafe.AsPointer(ref item1);
63+
var ptr2 = Unsafe.AsPointer(ref item2);
64+
65+
foreach (ref var item in sut.Memory.Span)
66+
{
67+
// Ensure we're getting the same references
68+
69+
var ptr = Unsafe.AsPointer(ref item);
70+
if (ptr != ptr1 && ptr != ptr2)
71+
{
72+
Assert.Fail("Lost reference");
73+
}
74+
}
75+
}
76+
}
77+
}

src/Uno.UI.Tests/Windows_UI_Xaml/Given_StaticResource.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public void When_Double_Resource_In_Application_Merged_Non_View()
5757
var control = new Test_Control();
5858
app.HostView.Children.Add(control);
5959

60-
var rowDef = control.TestGrid.RowDefinitions.First();
60+
var rowDef = control.TestGrid.RowDefinitions.First<RowDefinition>();
6161
Assert.AreEqual(256, rowDef.Height.Value);
6262
}
6363

src/Uno.UI/Extensions/UIElementExtensions.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,18 @@ internal static int GetDebugDepth(this object? elt) =>
4040
_ => elt.GetParent()?.GetDebugDepth() + 1?? 0,
4141
};
4242

43-
internal static Thickness GetPadding(this UIElement uiElement)
43+
internal static CornerRadius GetCornerRadius(this UIElement uiElement)
44+
{
45+
if (uiElement is FrameworkElement fe && fe.TryGetCornerRadius(out var cornerRadius))
46+
{
47+
return cornerRadius;
48+
}
49+
50+
var property = uiElement.FindDependencyPropertyUsingReflection<Thickness>("CornerRadius");
51+
return property != null && uiElement.GetValue(property) is CornerRadius t ? t : default;
52+
}
53+
54+
internal static Thickness GetPadding(this UIElement uiElement)
4455
{
4556
if(uiElement is FrameworkElement fe && fe.TryGetPadding(out var padding))
4657
{
Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
using Android.Graphics;
22
using Android.Views;
3-
using System;
4-
using System.Collections.Generic;
5-
using System.Linq;
6-
using System.Text;
73
using Windows.UI.Xaml;
8-
using Windows.UI.Xaml.Controls;
9-
using Windows.UI.Xaml.Shapes;
4+
using Uno.UI.Extensions;
105

116
namespace Uno.UI
127
{
13-
public class FrameworkElementOutlineProvider : ViewOutlineProvider
14-
{
8+
public class FrameworkElementOutlineProvider : ViewOutlineProvider
9+
{
1510
public override void GetOutline(View view, Outline outline)
1611
{
1712
var rect = new RectF(0, 0, view.Width, view.Height);
@@ -27,17 +22,7 @@ public override void GetOutline(View view, Outline outline)
2722

2823
private static CornerRadius GetCornerRadius(View view)
2924
{
30-
switch (view)
31-
{
32-
case Border border:
33-
return border.CornerRadius;
34-
case Panel panel:
35-
return panel.CornerRadius;
36-
case Control ctl:
37-
return ctl.CornerRadius;
38-
default:
39-
return CornerRadius.None;
40-
}
25+
return view is UIElement ue ? ue.GetCornerRadius() : CornerRadius.None;
4126
}
4227
}
4328
}

0 commit comments

Comments
 (0)