summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.WinRT/WindowsDeviceInfo.cs
blob: 235fe2db34c30c16b405efe5eacd60c0c8d20e0b (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
´╗┐using System;
using Windows.Foundation;
using Windows.Graphics.Display;
using Windows.UI.Xaml;

#if WINDOWS_UWP

namespace Xamarin.Forms.Platform.UWP
#else

namespace Xamarin.Forms.Platform.WinRT
#endif
{
	internal class WindowsDeviceInfo : DeviceInfo
	{
		DisplayInformation _information;
		bool _isDisposed;

		public WindowsDeviceInfo()
		{
			// TODO: Screen size and DPI can change at any time
			_information = DisplayInformation.GetForCurrentView();
			_information.OrientationChanged += OnOrientationChanged;
			CurrentOrientation = GetDeviceOrientation(_information.CurrentOrientation);
		}

		public override Size PixelScreenSize
		{
			get
			{
				double scaling = ScalingFactor;
				Size scaled = ScaledScreenSize;
				double width = Math.Round(scaled.Width * scaling);
				double height = Math.Round(scaled.Height * scaling);

				return new Size(width, height);
			}
		}

		public override Size ScaledScreenSize
		{
			get
			{
				Rect windowSize = Window.Current.Bounds;
				return new Size(windowSize.Width, windowSize.Height);
			}
		}

		public override double ScalingFactor
		{
			get
			{
				ResolutionScale scale = _information.ResolutionScale;
				switch (scale)
				{
					case ResolutionScale.Scale120Percent:
						return 1.2;
					case ResolutionScale.Scale140Percent:
						return 1.4;
					case ResolutionScale.Scale150Percent:
						return 1.5;
					case ResolutionScale.Scale160Percent:
						return 1.6;
					case ResolutionScale.Scale180Percent:
						return 1.8;
					case ResolutionScale.Scale225Percent:
						return 2.25;
					case ResolutionScale.Scale100Percent:
					default:
						return 1;
				}
			}
		}

		protected override void Dispose(bool disposing)
		{
			if (_isDisposed)
				return;

			if (disposing)
			{
				_information.OrientationChanged -= OnOrientationChanged;
				_information = null;
			}

			_isDisposed = true;

			base.Dispose(disposing);
		}

		static DeviceOrientation GetDeviceOrientation(DisplayOrientations orientations)
		{
			switch (orientations)
			{
				case DisplayOrientations.Landscape:
				case DisplayOrientations.LandscapeFlipped:
					return DeviceOrientation.Landscape;

				case DisplayOrientations.Portrait:
				case DisplayOrientations.PortraitFlipped:
					return DeviceOrientation.Portrait;

				default:
				case DisplayOrientations.None:
					return DeviceOrientation.Other;
			}
		}

		void OnOrientationChanged(DisplayInformation sender, object args)
		{
			CurrentOrientation = GetDeviceOrientation(sender.CurrentOrientation);
		}
	}
}