summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core/Constraint.cs
diff options
context:
space:
mode:
authorJason Smith <jason.smith@xamarin.com>2016-03-22 20:02:25 (GMT)
committerJason Smith <jason.smith@xamarin.com>2016-03-22 23:13:41 (GMT)
commit17fdde66d94155fc62a034fa6658995bef6fd6e5 (patch)
treeb5e5073a2a7b15cdbe826faa5c763e270a505729 /Xamarin.Forms.Core/Constraint.cs
downloadxamarin-forms-17fdde66d94155fc62a034fa6658995bef6fd6e5.zip
xamarin-forms-17fdde66d94155fc62a034fa6658995bef6fd6e5.tar.gz
xamarin-forms-17fdde66d94155fc62a034fa6658995bef6fd6e5.tar.bz2
Initial import
Diffstat (limited to 'Xamarin.Forms.Core/Constraint.cs')
-rw-r--r--Xamarin.Forms.Core/Constraint.cs56
1 files changed, 56 insertions, 0 deletions
diff --git a/Xamarin.Forms.Core/Constraint.cs b/Xamarin.Forms.Core/Constraint.cs
new file mode 100644
index 0000000..bd219f0
--- /dev/null
+++ b/Xamarin.Forms.Core/Constraint.cs
@@ -0,0 +1,56 @@
+using System;
+using System.Collections.Generic;
+using System.Linq.Expressions;
+
+namespace Xamarin.Forms
+{
+ [TypeConverter(typeof(ConstraintTypeConverter))]
+ public sealed class Constraint
+ {
+ Func<RelativeLayout, double> _measureFunc;
+
+ 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);
+ }
+ }
+} \ No newline at end of file