1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4 
5 //
6 
7 //
8 
9 using System;
10 using System.Runtime.InteropServices;
11 
12 #if !FEATURE_SLJ_PROJECTION_COMPAT
13 #pragma warning disable 436   // Redefining types from Windows.Foundation
14 #endif // !FEATURE_SLJ_PROJECTION_COMPAT
15 
16 #if FEATURE_SLJ_PROJECTION_COMPAT
17 namespace System.Windows
18 #else // !FEATURE_SLJ_PROJECTION_COMPAT
19 
20 
21 namespace Windows.Foundation
22 #endif // FEATURE_SLJ_PROJECTION_COMPAT
23 {
24     //
25     // Size is the managed projection of Windows.Foundation.Size.  Any changes to the layout
26     // of this type must be exactly mirrored on the native WinRT side as well.
27     //
28     // Note that this type is owned by the Jupiter team.  Please contact them before making any
29     // changes here.
30     //
31 
32     [StructLayout(LayoutKind.Sequential)]
33     public struct Size
34     {
35         private float _width;
36         private float _height;
37 
38         private static readonly Size s_empty = CreateEmptySize();
39 
SizeSystem.Windows.Size40         public Size(double width, double height)
41         {
42             if (width < 0)
43                 throw new ArgumentOutOfRangeException(nameof(width), SR.ArgumentOutOfRange_NeedNonNegNum);
44             if (height < 0)
45                 throw new ArgumentOutOfRangeException(nameof(height), SR.ArgumentOutOfRange_NeedNonNegNum);
46             _width = (float)width;
47             _height = (float)height;
48         }
49 
50         public double Width
51         {
52             get { return _width; }
53             set
54             {
55                 if (value < 0)
56                     throw new ArgumentOutOfRangeException(nameof(Width), SR.ArgumentOutOfRange_NeedNonNegNum);
57 
58                 _width = (float)value;
59             }
60         }
61 
62         public double Height
63         {
64             get { return _height; }
65             set
66             {
67                 if (value < 0)
68                     throw new ArgumentOutOfRangeException(nameof(Height), SR.ArgumentOutOfRange_NeedNonNegNum);
69 
70                 _height = (float)value;
71             }
72         }
73 
74         public static Size Empty
75         {
76             get { return s_empty; }
77         }
78 
79 
80         public bool IsEmpty
81         {
82             get { return Width < 0; }
83         }
84 
CreateEmptySizeSystem.Windows.Size85         private static Size CreateEmptySize()
86         {
87             Size size = new Size();
88             // We can't set these via the property setters because negatives widths
89             // are rejected in those APIs.
90             size._width = Single.NegativeInfinity;
91             size._height = Single.NegativeInfinity;
92             return size;
93         }
94 
operator ==System.Windows.Size95         public static bool operator ==(Size size1, Size size2)
96         {
97             return size1.Width == size2.Width &&
98                    size1.Height == size2.Height;
99         }
100 
operator !=System.Windows.Size101         public static bool operator !=(Size size1, Size size2)
102         {
103             return !(size1 == size2);
104         }
105 
EqualsSystem.Windows.Size106         public override bool Equals(object o)
107         {
108             return o is Size && Size.Equals(this, (Size)o);
109         }
110 
EqualsSystem.Windows.Size111         public bool Equals(Size value)
112         {
113             return Size.Equals(this, value);
114         }
115 
GetHashCodeSystem.Windows.Size116         public override int GetHashCode()
117         {
118             if (IsEmpty)
119             {
120                 return 0;
121             }
122             else
123             {
124                 // Perform field-by-field XOR of HashCodes
125                 return Width.GetHashCode() ^
126                        Height.GetHashCode();
127             }
128         }
129 
EqualsSystem.Windows.Size130         private static bool Equals(Size size1, Size size2)
131         {
132             if (size1.IsEmpty)
133             {
134                 return size2.IsEmpty;
135             }
136             else
137             {
138                 return size1.Width.Equals(size2.Width) &&
139                        size1.Height.Equals(size2.Height);
140             }
141         }
142 
ToStringSystem.Windows.Size143         public override string ToString()
144         {
145             if (IsEmpty)
146             {
147                 return "Empty";
148             }
149 
150             return String.Format("{0},{1}", _width, _height);
151         }
152     }
153 }
154 
155 #if !FEATURE_SLJ_PROJECTION_COMPAT
156 #pragma warning restore 436
157 #endif // !FEATURE_SLJ_PROJECTION_COMPAT
158