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 using System;
6 using System.Runtime.InteropServices;
7 
8 #if !FEATURE_SLJ_PROJECTION_COMPAT
9 #pragma warning disable 436   // Redefining types from Windows.Foundation
10 #endif // !FEATURE_SLJ_PROJECTION_COMPAT
11 
12 #if FEATURE_SLJ_PROJECTION_COMPAT
13 namespace System.Windows
14 #else // !FEATURE_SLJ_PROJECTION_COMPAT
15 
16 
17 namespace Windows.Foundation
18 #endif // FEATURE_SLJ_PROJECTION_COMPAT
19 {
20     //
21     // Size is the managed projection of Windows.Foundation.Size.  Any changes to the layout
22     // of this type must be exactly mirrored on the native WinRT side as well.
23     //
24     // Note that this type is owned by the Jupiter team.  Please contact them before making any
25     // changes here.
26     //
27     [StructLayout(LayoutKind.Sequential)]
28     public struct Size
29     {
30         private float _width;
31         private float _height;
32 
33         private readonly static Size s_empty = CreateEmptySize();
34 
SizeSystem.Windows.Size35         public Size(double width, double height)
36         {
37             if (width < 0)
38                 throw new ArgumentException("width");
39             if (height < 0)
40                 throw new ArgumentException("height");
41 
42             _width = (float)width;
43             _height = (float)height;
44         }
45 
46         public double Width
47         {
48             get { return _width; }
49             set
50             {
51                 if (value < 0)
52                     throw new ArgumentException("Width");
53 
54                 _width = (float)value;
55             }
56         }
57 
58         public double Height
59         {
60             get { return _height; }
61             set
62             {
63                 if (value < 0)
64                     throw new ArgumentException("Height");
65 
66                 _height = (float)value;
67             }
68         }
69 
70         public static Size Empty
71         {
72             get { return s_empty; }
73         }
74 
75 
76         public bool IsEmpty
77         {
78             get { return Width < 0; }
79         }
80 
CreateEmptySizeSystem.Windows.Size81         static private Size CreateEmptySize()
82         {
83             Size size = new Size();
84             // We can't set these via the property setters because negatives widths
85             // are rejected in those APIs.
86             size._width = Single.NegativeInfinity;
87             size._height = Single.NegativeInfinity;
88             return size;
89         }
90 
operator ==System.Windows.Size91         public static bool operator ==(Size size1, Size size2)
92         {
93             return size1.Width == size2.Width &&
94                    size1.Height == size2.Height;
95         }
96 
operator !=System.Windows.Size97         public static bool operator !=(Size size1, Size size2)
98         {
99             return !(size1 == size2);
100         }
101 
EqualsSystem.Windows.Size102         public override bool Equals(object o)
103         {
104             if ((null == o) || !(o is Size))
105             {
106                 return false;
107             }
108 
109             Size value = (Size)o;
110             return Size.Equals(this, value);
111         }
112 
EqualsSystem.Windows.Size113         public bool Equals(Size value)
114         {
115             return Size.Equals(this, value);
116         }
117 
GetHashCodeSystem.Windows.Size118         public override int GetHashCode()
119         {
120             if (IsEmpty)
121             {
122                 return 0;
123             }
124             else
125             {
126                 // Perform field-by-field XOR of HashCodes
127                 return Width.GetHashCode() ^
128                        Height.GetHashCode();
129             }
130         }
131 
EqualsSystem.Windows.Size132         private static bool Equals(Size size1, Size size2)
133         {
134             if (size1.IsEmpty)
135             {
136                 return size2.IsEmpty;
137             }
138             else
139             {
140                 return size1.Width.Equals(size2.Width) &&
141                        size1.Height.Equals(size2.Height);
142             }
143         }
144 
ToStringSystem.Windows.Size145         public override string ToString()
146         {
147             if (IsEmpty)
148             {
149                 return "Empty";
150             }
151 
152             return String.Format("{0},{1}", _width, _height);
153         }
154     }
155 }
156 
157 #if !FEATURE_SLJ_PROJECTION_COMPAT
158 #pragma warning restore 436
159 #endif // !FEATURE_SLJ_PROJECTION_COMPAT
160