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 #pragma warning disable 436   // Redefining types from Windows.Foundation
13 
14 namespace Windows.UI.Xaml
15 {
16     //
17     // Duration is the managed projection of Windows.UI.Xaml.Duration.  Any changes to the layout
18     // of this type must be exactly mirrored on the native WinRT side as well.
19     //
20     // DurationType is the managed projection of Windows.UI.Xaml.DurationType. Any changes to this
21     // enumeration must be exactly mirrored on the native WinRT side as well.
22     //
23     // Note that these types are owned by the Jupiter team.  Please contact them before making any
24     // changes here.
25     //
26 
27     public enum DurationType
28     {
29         Automatic,
30         TimeSpan,
31         Forever
32     }
33 
34     [StructLayout(LayoutKind.Sequential)]
35     public struct Duration
36     {
37         private TimeSpan _timeSpan;
38         private DurationType _durationType;
39 
DurationWindows.UI.Xaml.Duration40         public Duration(TimeSpan timeSpan)
41         {
42             _durationType = DurationType.TimeSpan;
43             _timeSpan = timeSpan;
44         }
45 
operator DurationWindows.UI.Xaml.Duration46         public static implicit operator Duration(TimeSpan timeSpan)
47         {
48             return new Duration(timeSpan);
49         }
50 
operator +Windows.UI.Xaml.Duration51         public static Duration operator +(Duration t1, Duration t2)
52         {
53             if (t1.HasTimeSpan && t2.HasTimeSpan)
54             {
55                 return new Duration(t1._timeSpan + t2._timeSpan);
56             }
57             else if (t1._durationType != DurationType.Automatic
58                      && t2._durationType != DurationType.Automatic)
59             {
60                 return Duration.Forever;
61             }
62             else
63             {
64                 // Automatic + anything is Automatic
65                 return Duration.Automatic;
66             }
67         }
68 
operator -Windows.UI.Xaml.Duration69         public static Duration operator -(Duration t1, Duration t2)
70         {
71             if (t1.HasTimeSpan && t2.HasTimeSpan)
72             {
73                 return new Duration(t1._timeSpan - t2._timeSpan);
74             }
75             else if (t1._durationType == DurationType.Forever
76                      && t2.HasTimeSpan)
77             {
78                 return Duration.Forever;
79             }
80             else
81             {
82                 return Duration.Automatic;
83             }
84         }
85 
operator ==Windows.UI.Xaml.Duration86         public static bool operator ==(Duration t1, Duration t2)
87         {
88             return t1.Equals(t2);
89         }
90 
operator !=Windows.UI.Xaml.Duration91         public static bool operator !=(Duration t1, Duration t2)
92         {
93             return !(t1.Equals(t2));
94         }
95 
operator >Windows.UI.Xaml.Duration96         public static bool operator >(Duration t1, Duration t2)
97         {
98             if (t1.HasTimeSpan && t2.HasTimeSpan)
99             {
100                 return t1._timeSpan > t2._timeSpan;
101             }
102             else if (t1.HasTimeSpan && t2._durationType == DurationType.Forever)
103             {
104                 return false;
105             }
106             else if (t1._durationType == DurationType.Forever && t2.HasTimeSpan)
107             {
108                 return true;
109             }
110             else
111             {
112                 return false;
113             }
114         }
115 
operator >=Windows.UI.Xaml.Duration116         public static bool operator >=(Duration t1, Duration t2)
117         {
118             if (t1._durationType == DurationType.Automatic && t2._durationType == DurationType.Automatic)
119             {
120                 return true;
121             }
122             else if (t1._durationType == DurationType.Automatic || t2._durationType == DurationType.Automatic)
123             {
124                 return false;
125             }
126             else
127             {
128                 return !(t1 < t2);
129             }
130         }
131 
operator <Windows.UI.Xaml.Duration132         public static bool operator <(Duration t1, Duration t2)
133         {
134             if (t1.HasTimeSpan && t2.HasTimeSpan)
135             {
136                 return t1._timeSpan < t2._timeSpan;
137             }
138             else if (t1.HasTimeSpan && t2._durationType == DurationType.Forever)
139             {
140                 return true;
141             }
142             else if (t1._durationType == DurationType.Forever && t2.HasTimeSpan)
143             {
144                 return false;
145             }
146             else
147             {
148                 return false;
149             }
150         }
151 
operator <=Windows.UI.Xaml.Duration152         public static bool operator <=(Duration t1, Duration t2)
153         {
154             if (t1._durationType == DurationType.Automatic && t2._durationType == DurationType.Automatic)
155             {
156                 return true;
157             }
158             else if (t1._durationType == DurationType.Automatic || t2._durationType == DurationType.Automatic)
159             {
160                 return false;
161             }
162             else
163             {
164                 return !(t1 > t2);
165             }
166         }
167 
CompareWindows.UI.Xaml.Duration168         public static int Compare(Duration t1, Duration t2)
169         {
170             if (t1._durationType == DurationType.Automatic)
171             {
172                 if (t2._durationType == DurationType.Automatic)
173                 {
174                     return 0;
175                 }
176                 else
177                 {
178                     return -1;
179                 }
180             }
181             else if (t2._durationType == DurationType.Automatic)
182             {
183                 return 1;
184             }
185             else
186             {
187                 if (t1 < t2)
188                 {
189                     return -1;
190                 }
191                 else if (t1 > t2)
192                 {
193                     return 1;
194                 }
195                 else
196                 {
197                     return 0;
198                 }
199             }
200         }
201 
operator +Windows.UI.Xaml.Duration202         public static Duration operator +(Duration duration)
203         {
204             return duration;
205         }
206 
207         public bool HasTimeSpan
208         {
209             get
210             {
211                 return (_durationType == DurationType.TimeSpan);
212             }
213         }
214 
215         public static Duration Automatic
216         {
217             get
218             {
219                 Duration duration = new Duration();
220                 duration._durationType = DurationType.Automatic;
221 
222                 return duration;
223             }
224         }
225 
226         public static Duration Forever
227         {
228             get
229             {
230                 Duration duration = new Duration();
231                 duration._durationType = DurationType.Forever;
232 
233                 return duration;
234             }
235         }
236 
237         public TimeSpan TimeSpan
238         {
239             get
240             {
241                 if (HasTimeSpan)
242                 {
243                     return _timeSpan;
244                 }
245                 else
246                 {
247                     throw new InvalidOperationException();
248                 }
249             }
250         }
251 
AddWindows.UI.Xaml.Duration252         public Duration Add(Duration duration)
253         {
254             return this + duration;
255         }
256 
EqualsWindows.UI.Xaml.Duration257         public override bool Equals(Object value)
258         {
259             return value is Duration && Equals((Duration)value);
260         }
261 
EqualsWindows.UI.Xaml.Duration262         public bool Equals(Duration duration)
263         {
264             if (HasTimeSpan)
265             {
266                 if (duration.HasTimeSpan)
267                 {
268                     return _timeSpan == duration._timeSpan;
269                 }
270                 else
271                 {
272                     return false;
273                 }
274             }
275             else
276             {
277                 return _durationType == duration._durationType;
278             }
279         }
280 
EqualsWindows.UI.Xaml.Duration281         public static bool Equals(Duration t1, Duration t2)
282         {
283             return t1.Equals(t2);
284         }
285 
GetHashCodeWindows.UI.Xaml.Duration286         public override int GetHashCode()
287         {
288             if (HasTimeSpan)
289             {
290                 return _timeSpan.GetHashCode();
291             }
292             else
293             {
294                 return _durationType.GetHashCode() + 17;
295             }
296         }
297 
SubtractWindows.UI.Xaml.Duration298         public Duration Subtract(Duration duration)
299         {
300             return this - duration;
301         }
302 
ToStringWindows.UI.Xaml.Duration303         public override string ToString()
304         {
305             if (HasTimeSpan)
306             {
307                 return _timeSpan.ToString(); // "00"; //TypeDescriptor.GetConverter(_timeSpan).ConvertToString(_timeSpan);
308             }
309             else if (_durationType == DurationType.Forever)
310             {
311                 return "Forever";
312             }
313             else // IsAutomatic
314             {
315                 return "Automatic";
316             }
317         }
318     }
319 }
320 
321 #pragma warning restore 436
322