-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Constraint.cs
59 lines (47 loc) · 1.41 KB
/
Constraint.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
#nullable disable
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq.Expressions;
using Microsoft.Maui.Controls.Internals;
namespace Microsoft.Maui.Controls.Compatibility
{
[System.ComponentModel.TypeConverter(typeof(ConstraintTypeConverter))]
public class Constraint
{
Func<RelativeLayout, double> _measureFunc;
public Constraint()
{
}
internal IEnumerable<View> RelativeTo { get; set; }
public static Constraint Constant(double size)
{
var result = new Constraint { _measureFunc = parent => size };
return result;
}
public static Constraint FromExpression(Expression<Func<double>> expression)
{
Func<double> compiled = expression.Compile();
var result = new Constraint
{
_measureFunc = layout => compiled(),
RelativeTo = ExpressionSearch.Default.FindObjects<View>(expression).ToArray() // make sure we have our own copy
};
return result;
}
public static Constraint RelativeToParent(Func<RelativeLayout, double> measure)
{
var result = new Constraint { _measureFunc = measure };
return result;
}
public static Constraint RelativeToView(View view, Func<RelativeLayout, View, double> measure)
{
var result = new Constraint { _measureFunc = layout => measure(layout, view), RelativeTo = new[] { view } };
return result;
}
internal double Compute(RelativeLayout parent)
{
return _measureFunc(parent);
}
}
}