-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathDeviceDisplay.ios.cs
68 lines (57 loc) · 2.25 KB
/
DeviceDisplay.ios.cs
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
#nullable enable
using System;
using Foundation;
using UIKit;
namespace Microsoft.Maui.Devices
{
partial class DeviceDisplayImplementation : IDeviceDisplay
{
NSObject? observer;
protected override bool GetKeepScreenOn() => UIApplication.SharedApplication.IdleTimerDisabled;
protected override void SetKeepScreenOn(bool keepScreenOn) => UIApplication.SharedApplication.IdleTimerDisabled = keepScreenOn;
protected override DisplayInfo GetMainDisplayInfo()
{
var bounds = UIScreen.MainScreen.Bounds;
var scale = UIScreen.MainScreen.Scale;
var rate = (OperatingSystem.IsIOSVersionAtLeast(10, 3) || OperatingSystem.IsTvOSVersionAtLeast(10, 3))
? UIScreen.MainScreen.MaximumFramesPerSecond
: 0;
return new DisplayInfo(
width: bounds.Width * scale,
height: bounds.Height * scale,
density: scale,
orientation: CalculateOrientation(),
rotation: CalculateRotation(),
rate: rate);
}
[System.Runtime.Versioning.UnsupportedOSPlatform("ios13.0")]
protected override void StartScreenMetricsListeners()
{
var notificationCenter = NSNotificationCenter.DefaultCenter;
var notification = UIApplication.DidChangeStatusBarOrientationNotification;
observer = notificationCenter.AddObserver(notification, OnMainDisplayInfoChanged);
}
protected override void StopScreenMetricsListeners()
{
observer?.Dispose();
observer = null;
}
void OnMainDisplayInfoChanged(NSNotification obj) =>
OnMainDisplayInfoChanged();
#pragma warning disable CA1416 // UIApplication.StatusBarOrientation has [UnsupportedOSPlatform("ios9.0")]. (Deprecated but still works)
static DisplayOrientation CalculateOrientation() =>
UIApplication.SharedApplication.StatusBarOrientation.IsLandscape()
? DisplayOrientation.Landscape
: DisplayOrientation.Portrait;
static DisplayRotation CalculateRotation() =>
UIApplication.SharedApplication.StatusBarOrientation switch
{
UIInterfaceOrientation.Portrait => DisplayRotation.Rotation0,
UIInterfaceOrientation.PortraitUpsideDown => DisplayRotation.Rotation180,
UIInterfaceOrientation.LandscapeLeft => DisplayRotation.Rotation270,
UIInterfaceOrientation.LandscapeRight => DisplayRotation.Rotation90,
_ => DisplayRotation.Unknown,
};
#pragma warning restore CA1416
}
}