summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core/GridLength.cs
blob: 6dac4338b053fb882feda063ff1140375ffee54e (plain)
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
using System;
using System.Diagnostics;

namespace Xamarin.Forms
{
	[TypeConverter(typeof(GridLengthTypeConverter))]
	[DebuggerDisplay("{Value}.{GridUnitType}")]
	public struct GridLength
	{
		public static GridLength Auto
		{
			get { return new GridLength(1, GridUnitType.Auto); }
		}

		public static GridLength Star
		{
			get { return new GridLength(1, GridUnitType.Star); }
		}

		public double Value { get; }

		public GridUnitType GridUnitType { get; }

		public bool IsAbsolute
		{
			get { return GridUnitType == GridUnitType.Absolute; }
		}

		public bool IsAuto
		{
			get { return GridUnitType == GridUnitType.Auto; }
		}

		public bool IsStar
		{
			get { return GridUnitType == GridUnitType.Star; }
		}

		public GridLength(double value) : this(value, GridUnitType.Absolute)
		{
		}

		public GridLength(double value, GridUnitType type)
		{
			if (value < 0 || double.IsNaN(value))
				throw new ArgumentException("value is less than 0 or is not a number", "value");
			if ((int)type < (int)GridUnitType.Absolute || (int)type > (int)GridUnitType.Auto)
				throw new ArgumentException("type is not a valid GridUnitType", "type");

			Value = value;
			GridUnitType = type;
		}

		public override bool Equals(object obj)
		{
			return obj != null && obj is GridLength && Equals((GridLength)obj);
		}

		bool Equals(GridLength other)
		{
			return GridUnitType == other.GridUnitType && Math.Abs(Value - other.Value) < double.Epsilon;
		}

		public override int GetHashCode()
		{
			return GridUnitType.GetHashCode() * 397 ^ Value.GetHashCode();
		}

		public static implicit operator GridLength(double absoluteValue)
		{
			return new GridLength(absoluteValue);
		}

		public override string ToString()
		{
			return string.Format("{0}.{1}", Value, GridUnitType);
		}
	}
}