SizeExtensions.cs
1 // Copyright (c) Microsoft Corporation 2 // The Microsoft Corporation licenses this file to you under the MIT license. 3 // See the LICENSE file in the project root for more information. 4 5 using Windows.Foundation; 6 7 namespace Peek.UI.Extensions 8 { 9 public static class SizeExtensions 10 { 11 public static Size Fit(this Size sizeToFit, Size maxSize, Size minSize) 12 { 13 double fittedWidth = sizeToFit.Width; 14 double fittedHeight = sizeToFit.Height; 15 16 double ratioWidth = sizeToFit.Width / maxSize.Width; 17 double ratioHeight = sizeToFit.Height / maxSize.Height; 18 19 if (ratioWidth > ratioHeight) 20 { 21 if (ratioWidth > 1) 22 { 23 fittedWidth = maxSize.Width; 24 fittedHeight = sizeToFit.Height / ratioWidth; 25 } 26 } 27 else 28 { 29 if (ratioHeight > 1) 30 { 31 fittedWidth = sizeToFit.Width / ratioHeight; 32 fittedHeight = maxSize.Height; 33 } 34 } 35 36 if (fittedWidth < minSize.Width) 37 { 38 fittedWidth = minSize.Width; 39 } 40 41 if (fittedHeight < minSize.Height) 42 { 43 fittedHeight = minSize.Height; 44 } 45 46 return new Size(fittedWidth, fittedHeight); 47 } 48 } 49 }