forked from xamarin/Xamarin.Forms
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathIOSDeviceInfo.cs
More file actions
73 lines (57 loc) · 1.79 KB
/
Copy pathIOSDeviceInfo.cs
File metadata and controls
73 lines (57 loc) · 1.79 KB
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
using System;
using Foundation;
using UIKit;
using Xamarin.Forms.Internals;
namespace Xamarin.Forms.Platform.iOS
{
internal class IOSDeviceInfo : DeviceInfo
{
Size _pixelScreenSize;
Size _scaledScreenSize;
double _scalingFactor;
bool _disposed;
readonly NSObject _notification;
public IOSDeviceInfo()
{
_notification = UIDevice.Notifications.ObserveOrientationDidChange(OrientationChanged);
UpdateScreenSize();
}
public override Size PixelScreenSize => _pixelScreenSize;
public override Size ScaledScreenSize => _scaledScreenSize;
public override double ScalingFactor => _scalingFactor;
void UpdateScreenSize()
{
_scalingFactor = UIScreen.MainScreen.Scale;
var boundsWidth = UIScreen.MainScreen.Bounds.Width;
var boundsHeight = UIScreen.MainScreen.Bounds.Height;
// We can't rely directly on the MainScreen bounds because they may not have been updated yet
// But CurrentOrientation is up-to-date, so we can use it to work out the dimensions
var width = CurrentOrientation.IsLandscape()
? Math.Max(boundsHeight, boundsWidth)
: Math.Min(boundsHeight, boundsWidth);
var height = CurrentOrientation.IsPortrait()
? Math.Max(boundsHeight, boundsWidth)
: Math.Min(boundsHeight, boundsWidth);
_scaledScreenSize = new Size(width, height);
_pixelScreenSize = new Size(_scaledScreenSize.Width * _scalingFactor, _scaledScreenSize.Height * _scalingFactor);
}
void OrientationChanged(object sender, NSNotificationEventArgs args)
{
CurrentOrientation = UIDevice.CurrentDevice.Orientation.ToDeviceOrientation();
UpdateScreenSize();
}
protected override void Dispose(bool disposing)
{
if (_disposed)
{
return;
}
_disposed = true;
if (disposing)
{
_notification.Dispose();
}
base.Dispose(disposing);
}
}
}