This repository was archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathFormsPanel.cs
106 lines (87 loc) · 3.18 KB
/
FormsPanel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System;
using System.Windows;
using System.Windows.Controls;
using WRect = System.Windows.Rect;
namespace Xamarin.Forms.Platform.WPF
{
public class FormsPanel : Panel
{
IElementController ElementController => Element as IElementController;
public Layout Element { get; private set; }
public FormsPanel(Layout element)
{
Element = element;
}
protected override System.Windows.Size ArrangeOverride(System.Windows.Size finalSize)
{
if (Element == null)
return finalSize;
Element.IsInNativeLayout = true;
var presentationSource = PresentationSource.FromVisual(this);
var stepX = presentationSource.CompositionTarget.TransformFromDevice.M11;
var stepY = presentationSource.CompositionTarget.TransformFromDevice.M22;
for (var i = 0; i < ElementController.LogicalChildren.Count; i++)
{
var child = ElementController.LogicalChildren[i] as VisualElement;
if (child == null)
continue;
IVisualElementRenderer renderer = Platform.GetRenderer(child);
if (renderer == null)
continue;
Rectangle bounds = child.Bounds;
var control = renderer.GetNativeElement();
var width = Math.Max(0, bounds.Width);
var height = Math.Max(0, bounds.Height);
if (stepX != 1 && stepY != 1 && stepX != 0 && stepY != 0)
{
control.Width = width = Math.Ceiling(width / stepX) * stepX;
control.Height = height = Math.Ceiling(height / stepY) * stepY;
}
control.Arrange(new WRect(bounds.X, bounds.Y, width, height));
}
Element.IsInNativeLayout = false;
return finalSize;
}
protected override System.Windows.Size MeasureOverride(System.Windows.Size availableSize)
{
if (Element == null || availableSize.Width * availableSize.Height == 0)
return new System.Windows.Size(0, 0);
Element.IsInNativeLayout = true;
double elementDesiredWidth = 0;
double elementDesiredHeight = 0;
foreach (FrameworkElement child in InternalChildren)
{
if (child.ActualWidth != child.Width || child.ActualHeight != child.Height)
{
double width = child.Width <= -1 || double.IsNaN(child.Width) ? ActualWidth : child.Width;
width = width == 0 ? double.PositiveInfinity : width;
double height = child.Height <= -1 || double.IsNaN(child.Height) ? ActualHeight : child.Height;
height = height == 0 ? double.PositiveInfinity : height;
child.Measure(new System.Windows.Size(width, height));
elementDesiredWidth = Math.Max(width, elementDesiredWidth);
elementDesiredHeight = Math.Max(width, elementDesiredHeight);
}
}
System.Windows.Size result;
if (double.IsInfinity(elementDesiredWidth) || double.IsPositiveInfinity(elementDesiredHeight))
{
Size request = Element.Measure(elementDesiredWidth, elementDesiredHeight, MeasureFlags.IncludeMargins).Request;
if (request.Width < 0)
request.Width = 0.0;
if (request.Height < 0)
request.Height = 0.0;
result = new System.Windows.Size(request.Width, request.Height);
}
else
{
result = availableSize;
}
Element.IsInNativeLayout = false;
if (double.IsPositiveInfinity(result.Height))
result.Height = 0.0;
if (double.IsPositiveInfinity(result.Width))
result.Width = 0.0;
return result;
}
}
}