From c6c0f79e108a87a3144c2445752a48e2e40ed56e Mon Sep 17 00:00:00 2001 From: Matthew Leibowitz Date: Tue, 24 Apr 2018 20:27:27 +0200 Subject: [PATCH] GH-197: Moving screen metrics to a new API class (#199) * Moving screen metrics to a new API class #197 * Added more keywords to the sample --- .../DeviceTests.Shared/DeviceInfo_Tests.cs | 2 +- .../Samples/ViewModel/DeviceInfoViewModel.cs | 6 +- Samples/Samples/ViewModel/HomeViewModel.cs | 2 +- .../DeviceDisplay/DeviceDisplay.android.cs | 107 ++++++++++++++++++ .../DeviceDisplay/DeviceDisplay.ios.cs | 74 ++++++++++++ .../DeviceDisplay.netstandard.cs | 11 ++ .../DeviceDisplay/DeviceDisplay.shared.cs | 91 +++++++++++++++ .../DeviceDisplay/DeviceDisplay.uwp.cs | 104 +++++++++++++++++ .../DeviceInfo/DeviceInfo.android.cs | 90 --------------- .../DeviceInfo/DeviceInfo.ios.cs | 65 ----------- .../DeviceInfo/DeviceInfo.netstandard.cs | 6 - .../DeviceInfo/DeviceInfo.shared.cs | 84 -------------- .../DeviceInfo/DeviceInfo.uwp.cs | 94 --------------- Xamarin.Essentials/Xamarin.Essentials.csproj | 2 +- .../xamarin-essentials-android.xml | 6 +- .../xamarin-essentials-ios.xml | 6 +- .../xamarin-essentials-uwp.xml | 6 +- .../en/FrameworksIndex/xamarin-essentials.xml | 6 +- docs/en/Xamarin.Essentials/DeviceDisplay.xml | 58 ++++++++++ docs/en/Xamarin.Essentials/DeviceInfo.xml | 39 +------ docs/en/index.xml | 1 + 21 files changed, 469 insertions(+), 391 deletions(-) create mode 100644 Xamarin.Essentials/DeviceDisplay/DeviceDisplay.android.cs create mode 100644 Xamarin.Essentials/DeviceDisplay/DeviceDisplay.ios.cs create mode 100644 Xamarin.Essentials/DeviceDisplay/DeviceDisplay.netstandard.cs create mode 100644 Xamarin.Essentials/DeviceDisplay/DeviceDisplay.shared.cs create mode 100644 Xamarin.Essentials/DeviceDisplay/DeviceDisplay.uwp.cs create mode 100644 docs/en/Xamarin.Essentials/DeviceDisplay.xml diff --git a/DeviceTests/DeviceTests.Shared/DeviceInfo_Tests.cs b/DeviceTests/DeviceTests.Shared/DeviceInfo_Tests.cs index 6c4f68a..15dd6ae 100644 --- a/DeviceTests/DeviceTests.Shared/DeviceInfo_Tests.cs +++ b/DeviceTests/DeviceTests.Shared/DeviceInfo_Tests.cs @@ -71,7 +71,7 @@ namespace DeviceTests { return Utils.OnMainThread(() => { - var metrics = DeviceInfo.ScreenMetrics; + var metrics = DeviceDisplay.ScreenMetrics; Assert.True(metrics.Width > 0); Assert.True(metrics.Height > 0); diff --git a/Samples/Samples/ViewModel/DeviceInfoViewModel.cs b/Samples/Samples/ViewModel/DeviceInfoViewModel.cs index ce90399..62c522b 100644 --- a/Samples/Samples/ViewModel/DeviceInfoViewModel.cs +++ b/Samples/Samples/ViewModel/DeviceInfoViewModel.cs @@ -30,13 +30,13 @@ namespace Samples.ViewModel { base.OnAppearing(); - DeviceInfo.ScreenMetricsChanaged += OnScreenMetricsChanged; - ScreenMetrics = DeviceInfo.ScreenMetrics; + DeviceDisplay.ScreenMetricsChanaged += OnScreenMetricsChanged; + ScreenMetrics = DeviceDisplay.ScreenMetrics; } public override void OnDisappearing() { - DeviceInfo.ScreenMetricsChanaged -= OnScreenMetricsChanged; + DeviceDisplay.ScreenMetricsChanaged -= OnScreenMetricsChanged; base.OnDisappearing(); } diff --git a/Samples/Samples/ViewModel/HomeViewModel.cs b/Samples/Samples/ViewModel/HomeViewModel.cs index cf4ce8a..043a200 100644 --- a/Samples/Samples/ViewModel/HomeViewModel.cs +++ b/Samples/Samples/ViewModel/HomeViewModel.cs @@ -77,7 +77,7 @@ namespace Samples.ViewModel "Device Info", typeof(DeviceInfoPage), "Find out about the device with ease.", - new[] { "hardware", "device", "info" }), + new[] { "hardware", "device", "info", "screen", "display", "orientation", "rotation" }), new SampleItem( "📧", "Email", diff --git a/Xamarin.Essentials/DeviceDisplay/DeviceDisplay.android.cs b/Xamarin.Essentials/DeviceDisplay/DeviceDisplay.android.cs new file mode 100644 index 0000000..b82bfb9 --- /dev/null +++ b/Xamarin.Essentials/DeviceDisplay/DeviceDisplay.android.cs @@ -0,0 +1,107 @@ +using System; +using Android.App; +using Android.Content; +using Android.Content.Res; +using Android.OS; +using Android.Provider; +using Android.Runtime; +using Android.Views; + +namespace Xamarin.Essentials +{ + public static partial class DeviceDisplay + { + static OrientationEventListener orientationListener; + + static ScreenMetrics GetScreenMetrics() + { + var displayMetrics = Essentials.Platform.CurrentContext.Resources?.DisplayMetrics; + + return new ScreenMetrics + { + Orientation = CalculateOrientation(), + Rotation = CalculateRotation(), + Width = displayMetrics?.WidthPixels ?? 0, + Height = displayMetrics?.HeightPixels ?? 0, + Density = displayMetrics?.Density ?? 0 + }; + } + + static void StartScreenMetricsListeners() + { + orientationListener = new Listener(Application.Context, OnScreenMetricsChanaged); + orientationListener.Enable(); + } + + static void StopScreenMetricsListeners() + { + orientationListener?.Disable(); + orientationListener?.Dispose(); + orientationListener = null; + } + + static void OnScreenMetricsChanaged() + { + var metrics = GetScreenMetrics(); + OnScreenMetricsChanaged(metrics); + } + + static ScreenRotation CalculateRotation() + { + var service = Essentials.Platform.CurrentContext.GetSystemService(Context.WindowService); + var display = service?.JavaCast()?.DefaultDisplay; + + if (display != null) + { + switch (display.Rotation) + { + case SurfaceOrientation.Rotation270: + return ScreenRotation.Rotation270; + case SurfaceOrientation.Rotation180: + return ScreenRotation.Rotation180; + case SurfaceOrientation.Rotation90: + return ScreenRotation.Rotation90; + case SurfaceOrientation.Rotation0: + return ScreenRotation.Rotation0; + } + } + + return ScreenRotation.Rotation0; + } + + static ScreenOrientation CalculateOrientation() + { + var config = Essentials.Platform.CurrentContext.Resources?.Configuration; + + if (config != null) + { + switch (config.Orientation) + { + case Orientation.Landscape: + return ScreenOrientation.Landscape; + case Orientation.Portrait: + case Orientation.Square: + return ScreenOrientation.Portrait; + } + } + + return ScreenOrientation.Unknown; + } + + static string GetSystemSetting(string name) + => Settings.System.GetString(Essentials.Platform.CurrentContext.ContentResolver, name); + } + + class Listener : OrientationEventListener + { + Action onChanged; + + public Listener(Context context, Action handler) + : base(context) + { + onChanged = handler; + } + + public override void OnOrientationChanged(int orientation) => onChanged(); + } +} diff --git a/Xamarin.Essentials/DeviceDisplay/DeviceDisplay.ios.cs b/Xamarin.Essentials/DeviceDisplay/DeviceDisplay.ios.cs new file mode 100644 index 0000000..f1eed88 --- /dev/null +++ b/Xamarin.Essentials/DeviceDisplay/DeviceDisplay.ios.cs @@ -0,0 +1,74 @@ +using Foundation; +using ObjCRuntime; +using UIKit; + +namespace Xamarin.Essentials +{ + public static partial class DeviceDisplay + { + static NSObject observer; + + static ScreenMetrics GetScreenMetrics() + { + var bounds = UIScreen.MainScreen.Bounds; + var scale = UIScreen.MainScreen.Scale; + + return new ScreenMetrics + { + Width = bounds.Width * scale, + Height = bounds.Height * scale, + Density = scale, + Orientation = CalculateOrientation(), + Rotation = CalculateRotation() + }; + } + + static void StartScreenMetricsListeners() + { + var notificationCenter = NSNotificationCenter.DefaultCenter; + var notification = UIApplication.DidChangeStatusBarOrientationNotification; + observer = notificationCenter.AddObserver(notification, OnScreenMetricsChanaged); + } + + static void StopScreenMetricsListeners() + { + observer?.Dispose(); + observer = null; + } + + static void OnScreenMetricsChanaged(NSNotification obj) + { + var metrics = GetScreenMetrics(); + OnScreenMetricsChanaged(metrics); + } + + static ScreenOrientation CalculateOrientation() + { + var orientation = UIApplication.SharedApplication.StatusBarOrientation; + + if (orientation.IsLandscape()) + return ScreenOrientation.Landscape; + + return ScreenOrientation.Portrait; + } + + static ScreenRotation CalculateRotation() + { + var orientation = UIApplication.SharedApplication.StatusBarOrientation; + + switch (orientation) + { + case UIInterfaceOrientation.Portrait: + return ScreenRotation.Rotation0; + case UIInterfaceOrientation.PortraitUpsideDown: + return ScreenRotation.Rotation180; + case UIInterfaceOrientation.LandscapeLeft: + return ScreenRotation.Rotation270; + case UIInterfaceOrientation.LandscapeRight: + return ScreenRotation.Rotation90; + } + + return ScreenRotation.Rotation0; + } + } +} diff --git a/Xamarin.Essentials/DeviceDisplay/DeviceDisplay.netstandard.cs b/Xamarin.Essentials/DeviceDisplay/DeviceDisplay.netstandard.cs new file mode 100644 index 0000000..4a6ee86 --- /dev/null +++ b/Xamarin.Essentials/DeviceDisplay/DeviceDisplay.netstandard.cs @@ -0,0 +1,11 @@ +namespace Xamarin.Essentials +{ + public static partial class DeviceDisplay + { + static ScreenMetrics GetScreenMetrics() => throw new NotImplementedInReferenceAssemblyException(); + + static void StartScreenMetricsListeners() => throw new NotImplementedInReferenceAssemblyException(); + + static void StopScreenMetricsListeners() => throw new NotImplementedInReferenceAssemblyException(); + } +} diff --git a/Xamarin.Essentials/DeviceDisplay/DeviceDisplay.shared.cs b/Xamarin.Essentials/DeviceDisplay/DeviceDisplay.shared.cs new file mode 100644 index 0000000..80a3bdd --- /dev/null +++ b/Xamarin.Essentials/DeviceDisplay/DeviceDisplay.shared.cs @@ -0,0 +1,91 @@ +using System; + +namespace Xamarin.Essentials +{ + public static partial class DeviceDisplay + { + static event ScreenMetricsChanagedEventHandler ScreenMetricsChanagedInternal; + + public static ScreenMetrics ScreenMetrics => GetScreenMetrics(); + + public static event ScreenMetricsChanagedEventHandler ScreenMetricsChanaged + { + add + { + var wasRunning = ScreenMetricsChanagedInternal != null; + + ScreenMetricsChanagedInternal += value; + + if (!wasRunning && ScreenMetricsChanagedInternal != null) + StartScreenMetricsListeners(); + } + + remove + { + var wasRunning = ScreenMetricsChanagedInternal != null; + + ScreenMetricsChanagedInternal -= value; + + if (wasRunning && ScreenMetricsChanagedInternal == null) + StopScreenMetricsListeners(); + } + } + + static void OnScreenMetricsChanaged(ScreenMetrics metrics) + => OnScreenMetricsChanaged(new ScreenMetricsChanagedEventArgs(metrics)); + + static void OnScreenMetricsChanaged(ScreenMetricsChanagedEventArgs e) + => ScreenMetricsChanagedInternal?.Invoke(e); + } + + public delegate void ScreenMetricsChanagedEventHandler(ScreenMetricsChanagedEventArgs e); + + public class ScreenMetricsChanagedEventArgs : EventArgs + { + public ScreenMetricsChanagedEventArgs(ScreenMetrics metrics) + { + Metrics = metrics; + } + + public ScreenMetrics Metrics { get; } + } + + [Preserve(AllMembers = true)] + public struct ScreenMetrics + { + internal ScreenMetrics(double width, double height, double density, ScreenOrientation orientation, ScreenRotation rotation) + { + Width = width; + Height = height; + Density = density; + Orientation = orientation; + Rotation = rotation; + } + + public double Width { get; set; } + + public double Height { get; set; } + + public double Density { get; set; } + + public ScreenOrientation Orientation { get; set; } + + public ScreenRotation Rotation { get; set; } + } + + public enum ScreenOrientation + { + Unknown, + + Portrait, + Landscape + } + + public enum ScreenRotation + { + Rotation0, + Rotation90, + Rotation180, + Rotation270 + } +} diff --git a/Xamarin.Essentials/DeviceDisplay/DeviceDisplay.uwp.cs b/Xamarin.Essentials/DeviceDisplay/DeviceDisplay.uwp.cs new file mode 100644 index 0000000..28abccf --- /dev/null +++ b/Xamarin.Essentials/DeviceDisplay/DeviceDisplay.uwp.cs @@ -0,0 +1,104 @@ +using Windows.Graphics.Display; +using Windows.Security.ExchangeActiveSyncProvisioning; +using Windows.System.Profile; +using Windows.UI.ViewManagement; + +namespace Xamarin.Essentials +{ + public static partial class DeviceDisplay + { + static ScreenMetrics GetScreenMetrics(DisplayInformation di = null) + { + di = di ?? DisplayInformation.GetForCurrentView(); + + var rotation = CalculateRotation(di); + var perpendicular = + rotation == ScreenRotation.Rotation90 || + rotation == ScreenRotation.Rotation270; + + var w = di.ScreenWidthInRawPixels; + var h = di.ScreenHeightInRawPixels; + + return new ScreenMetrics + { + Width = perpendicular ? h : w, + Height = perpendicular ? w : h, + Density = di.LogicalDpi / 96.0, + Orientation = CalculateOrientation(di), + Rotation = rotation + }; + } + + static void StartScreenMetricsListeners() + { + Xamarin.Essentials.Platform.BeginInvokeOnMainThread(() => + { + var di = DisplayInformation.GetForCurrentView(); + + di.DpiChanged += OnDisplayInformationChanged; + di.OrientationChanged += OnDisplayInformationChanged; + }); + } + + static void StopScreenMetricsListeners() + { + Xamarin.Essentials.Platform.BeginInvokeOnMainThread(() => + { + var di = DisplayInformation.GetForCurrentView(); + + di.DpiChanged -= OnDisplayInformationChanged; + di.OrientationChanged -= OnDisplayInformationChanged; + }); + } + + static void OnDisplayInformationChanged(DisplayInformation di, object args) + { + var metrics = GetScreenMetrics(di); + OnScreenMetricsChanaged(metrics); + } + + static ScreenOrientation CalculateOrientation(DisplayInformation di) + { + switch (di.CurrentOrientation) + { + case DisplayOrientations.Landscape: + case DisplayOrientations.LandscapeFlipped: + return ScreenOrientation.Landscape; + case DisplayOrientations.Portrait: + case DisplayOrientations.PortraitFlipped: + return ScreenOrientation.Portrait; + } + + return ScreenOrientation.Unknown; + } + + static ScreenRotation CalculateRotation(DisplayInformation di) + { + var native = di.NativeOrientation; + var current = di.CurrentOrientation; + + if (native == DisplayOrientations.Portrait) + { + switch (current) + { + case DisplayOrientations.Landscape: return ScreenRotation.Rotation90; + case DisplayOrientations.Portrait: return ScreenRotation.Rotation0; + case DisplayOrientations.LandscapeFlipped: return ScreenRotation.Rotation270; + case DisplayOrientations.PortraitFlipped: return ScreenRotation.Rotation180; + } + } + else if (native == DisplayOrientations.Landscape) + { + switch (current) + { + case DisplayOrientations.Landscape: return ScreenRotation.Rotation0; + case DisplayOrientations.Portrait: return ScreenRotation.Rotation270; + case DisplayOrientations.LandscapeFlipped: return ScreenRotation.Rotation180; + case DisplayOrientations.PortraitFlipped: return ScreenRotation.Rotation90; + } + } + + return ScreenRotation.Rotation0; + } + } +} diff --git a/Xamarin.Essentials/DeviceInfo/DeviceInfo.android.cs b/Xamarin.Essentials/DeviceInfo/DeviceInfo.android.cs index 218c707..7ff5a75 100644 --- a/Xamarin.Essentials/DeviceInfo/DeviceInfo.android.cs +++ b/Xamarin.Essentials/DeviceInfo/DeviceInfo.android.cs @@ -13,8 +13,6 @@ namespace Xamarin.Essentials { const int tabletCrossover = 600; - static OrientationEventListener orientationListener; - static string GetModel() => Build.Model; static string GetManufacturer() => Build.Manufacturer; @@ -107,95 +105,7 @@ namespace Xamarin.Essentials return DeviceType.Physical; } - static ScreenMetrics GetScreenMetrics() - { - var displayMetrics = Essentials.Platform.CurrentContext.Resources?.DisplayMetrics; - - return new ScreenMetrics - { - Orientation = CalculateOrientation(), - Rotation = CalculateRotation(), - Width = displayMetrics?.WidthPixels ?? 0, - Height = displayMetrics?.HeightPixels ?? 0, - Density = displayMetrics?.Density ?? 0 - }; - } - - static void StartScreenMetricsListeners() - { - orientationListener = new Listener(Application.Context, OnScreenMetricsChanaged); - orientationListener.Enable(); - } - - static void StopScreenMetricsListeners() - { - orientationListener?.Disable(); - orientationListener?.Dispose(); - orientationListener = null; - } - - static void OnScreenMetricsChanaged() - { - var metrics = GetScreenMetrics(); - OnScreenMetricsChanaged(metrics); - } - - static ScreenRotation CalculateRotation() - { - var service = Essentials.Platform.CurrentContext.GetSystemService(Context.WindowService); - var display = service?.JavaCast()?.DefaultDisplay; - - if (display != null) - { - switch (display.Rotation) - { - case SurfaceOrientation.Rotation270: - return ScreenRotation.Rotation270; - case SurfaceOrientation.Rotation180: - return ScreenRotation.Rotation180; - case SurfaceOrientation.Rotation90: - return ScreenRotation.Rotation90; - case SurfaceOrientation.Rotation0: - return ScreenRotation.Rotation0; - } - } - - return ScreenRotation.Rotation0; - } - - static ScreenOrientation CalculateOrientation() - { - var config = Essentials.Platform.CurrentContext.Resources?.Configuration; - - if (config != null) - { - switch (config.Orientation) - { - case Orientation.Landscape: - return ScreenOrientation.Landscape; - case Orientation.Portrait: - case Orientation.Square: - return ScreenOrientation.Portrait; - } - } - - return ScreenOrientation.Unknown; - } - static string GetSystemSetting(string name) => Settings.System.GetString(Essentials.Platform.CurrentContext.ContentResolver, name); } - - class Listener : OrientationEventListener - { - Action onChanged; - - public Listener(Context context, Action handler) - : base(context) - { - onChanged = handler; - } - - public override void OnOrientationChanged(int orientation) => onChanged(); - } } diff --git a/Xamarin.Essentials/DeviceInfo/DeviceInfo.ios.cs b/Xamarin.Essentials/DeviceInfo/DeviceInfo.ios.cs index 31641a9..46f63f2 100644 --- a/Xamarin.Essentials/DeviceInfo/DeviceInfo.ios.cs +++ b/Xamarin.Essentials/DeviceInfo/DeviceInfo.ios.cs @@ -6,8 +6,6 @@ namespace Xamarin.Essentials { public static partial class DeviceInfo { - static NSObject observer; - static string GetModel() => UIDevice.CurrentDevice.Model; static string GetManufacturer() => "Apple"; @@ -37,68 +35,5 @@ namespace Xamarin.Essentials static DeviceType GetDeviceType() => Runtime.Arch == Arch.DEVICE ? DeviceType.Physical : DeviceType.Virtual; - - static ScreenMetrics GetScreenMetrics() - { - var bounds = UIScreen.MainScreen.Bounds; - var scale = UIScreen.MainScreen.Scale; - - return new ScreenMetrics - { - Width = bounds.Width * scale, - Height = bounds.Height * scale, - Density = scale, - Orientation = CalculateOrientation(), - Rotation = CalculateRotation() - }; - } - - static void StartScreenMetricsListeners() - { - var notificationCenter = NSNotificationCenter.DefaultCenter; - var notification = UIApplication.DidChangeStatusBarOrientationNotification; - observer = notificationCenter.AddObserver(notification, OnScreenMetricsChanaged); - } - - static void StopScreenMetricsListeners() - { - observer?.Dispose(); - observer = null; - } - - static void OnScreenMetricsChanaged(NSNotification obj) - { - var metrics = GetScreenMetrics(); - OnScreenMetricsChanaged(metrics); - } - - static ScreenOrientation CalculateOrientation() - { - var orientation = UIApplication.SharedApplication.StatusBarOrientation; - - if (orientation.IsLandscape()) - return ScreenOrientation.Landscape; - - return ScreenOrientation.Portrait; - } - - static ScreenRotation CalculateRotation() - { - var orientation = UIApplication.SharedApplication.StatusBarOrientation; - - switch (orientation) - { - case UIInterfaceOrientation.Portrait: - return ScreenRotation.Rotation0; - case UIInterfaceOrientation.PortraitUpsideDown: - return ScreenRotation.Rotation180; - case UIInterfaceOrientation.LandscapeLeft: - return ScreenRotation.Rotation270; - case UIInterfaceOrientation.LandscapeRight: - return ScreenRotation.Rotation90; - } - - return ScreenRotation.Rotation0; - } } } diff --git a/Xamarin.Essentials/DeviceInfo/DeviceInfo.netstandard.cs b/Xamarin.Essentials/DeviceInfo/DeviceInfo.netstandard.cs index 6c29a39..4e54c25 100644 --- a/Xamarin.Essentials/DeviceInfo/DeviceInfo.netstandard.cs +++ b/Xamarin.Essentials/DeviceInfo/DeviceInfo.netstandard.cs @@ -15,11 +15,5 @@ static string GetIdiom() => throw new NotImplementedInReferenceAssemblyException(); static DeviceType GetDeviceType() => throw new NotImplementedInReferenceAssemblyException(); - - static ScreenMetrics GetScreenMetrics() => throw new NotImplementedInReferenceAssemblyException(); - - static void StartScreenMetricsListeners() => throw new NotImplementedInReferenceAssemblyException(); - - static void StopScreenMetricsListeners() => throw new NotImplementedInReferenceAssemblyException(); } } diff --git a/Xamarin.Essentials/DeviceInfo/DeviceInfo.shared.cs b/Xamarin.Essentials/DeviceInfo/DeviceInfo.shared.cs index 4f64509..cd05683 100644 --- a/Xamarin.Essentials/DeviceInfo/DeviceInfo.shared.cs +++ b/Xamarin.Essentials/DeviceInfo/DeviceInfo.shared.cs @@ -4,8 +4,6 @@ namespace Xamarin.Essentials { public static partial class DeviceInfo { - static event ScreenMetricsChanagedEventHandler ScreenMetricsChanagedInternal; - public static string Model => GetModel(); public static string Manufacturer => GetManufacturer(); @@ -22,37 +20,6 @@ namespace Xamarin.Essentials public static DeviceType DeviceType => GetDeviceType(); - public static ScreenMetrics ScreenMetrics => GetScreenMetrics(); - - public static event ScreenMetricsChanagedEventHandler ScreenMetricsChanaged - { - add - { - var wasRunning = ScreenMetricsChanagedInternal != null; - - ScreenMetricsChanagedInternal += value; - - if (!wasRunning && ScreenMetricsChanagedInternal != null) - StartScreenMetricsListeners(); - } - - remove - { - var wasRunning = ScreenMetricsChanagedInternal != null; - - ScreenMetricsChanagedInternal -= value; - - if (wasRunning && ScreenMetricsChanagedInternal == null) - StopScreenMetricsListeners(); - } - } - - static void OnScreenMetricsChanaged(ScreenMetrics metrics) - => OnScreenMetricsChanaged(new ScreenMetricsChanagedEventArgs(metrics)); - - static void OnScreenMetricsChanaged(ScreenMetricsChanagedEventArgs e) - => ScreenMetricsChanagedInternal?.Invoke(e); - public static class Idioms { // try to match Xamarin.Forms: @@ -79,60 +46,9 @@ namespace Xamarin.Essentials } } - public delegate void ScreenMetricsChanagedEventHandler(ScreenMetricsChanagedEventArgs e); - - public class ScreenMetricsChanagedEventArgs : EventArgs - { - public ScreenMetricsChanagedEventArgs(ScreenMetrics metrics) - { - Metrics = metrics; - } - - public ScreenMetrics Metrics { get; } - } - public enum DeviceType { Physical, Virtual } - - [Preserve(AllMembers = true)] - public struct ScreenMetrics - { - internal ScreenMetrics(double width, double height, double density, ScreenOrientation orientation, ScreenRotation rotation) - { - Width = width; - Height = height; - Density = density; - Orientation = orientation; - Rotation = rotation; - } - - public double Width { get; set; } - - public double Height { get; set; } - - public double Density { get; set; } - - public ScreenOrientation Orientation { get; set; } - - public ScreenRotation Rotation { get; set; } - } - - public enum ScreenOrientation - { - Unknown, - - Portrait, - Landscape - } - - public enum ScreenRotation - { - Rotation0, - Rotation90, - Rotation180, - Rotation270 - } } diff --git a/Xamarin.Essentials/DeviceInfo/DeviceInfo.uwp.cs b/Xamarin.Essentials/DeviceInfo/DeviceInfo.uwp.cs index ca8b3fa..176b129 100644 --- a/Xamarin.Essentials/DeviceInfo/DeviceInfo.uwp.cs +++ b/Xamarin.Essentials/DeviceInfo/DeviceInfo.uwp.cs @@ -69,99 +69,5 @@ namespace Xamarin.Essentials return DeviceType.Physical; } - - static ScreenMetrics GetScreenMetrics(DisplayInformation di = null) - { - di = di ?? DisplayInformation.GetForCurrentView(); - - var rotation = CalculateRotation(di); - var perpendicular = - rotation == ScreenRotation.Rotation90 || - rotation == ScreenRotation.Rotation270; - - var w = di.ScreenWidthInRawPixels; - var h = di.ScreenHeightInRawPixels; - - return new ScreenMetrics - { - Width = perpendicular ? h : w, - Height = perpendicular ? w : h, - Density = di.LogicalDpi / 96.0, - Orientation = CalculateOrientation(di), - Rotation = rotation - }; - } - - static void StartScreenMetricsListeners() - { - Xamarin.Essentials.Platform.BeginInvokeOnMainThread(() => - { - var di = DisplayInformation.GetForCurrentView(); - - di.DpiChanged += OnDisplayInformationChanged; - di.OrientationChanged += OnDisplayInformationChanged; - }); - } - - static void StopScreenMetricsListeners() - { - Xamarin.Essentials.Platform.BeginInvokeOnMainThread(() => - { - var di = DisplayInformation.GetForCurrentView(); - - di.DpiChanged -= OnDisplayInformationChanged; - di.OrientationChanged -= OnDisplayInformationChanged; - }); - } - - static void OnDisplayInformationChanged(DisplayInformation di, object args) - { - var metrics = GetScreenMetrics(di); - OnScreenMetricsChanaged(metrics); - } - - static ScreenOrientation CalculateOrientation(DisplayInformation di) - { - switch (di.CurrentOrientation) - { - case DisplayOrientations.Landscape: - case DisplayOrientations.LandscapeFlipped: - return ScreenOrientation.Landscape; - case DisplayOrientations.Portrait: - case DisplayOrientations.PortraitFlipped: - return ScreenOrientation.Portrait; - } - - return ScreenOrientation.Unknown; - } - - static ScreenRotation CalculateRotation(DisplayInformation di) - { - var native = di.NativeOrientation; - var current = di.CurrentOrientation; - - if (native == DisplayOrientations.Portrait) - { - switch (current) - { - case DisplayOrientations.Landscape: return ScreenRotation.Rotation90; - case DisplayOrientations.Portrait: return ScreenRotation.Rotation0; - case DisplayOrientations.LandscapeFlipped: return ScreenRotation.Rotation270; - case DisplayOrientations.PortraitFlipped: return ScreenRotation.Rotation180; - } - } - else if (native == DisplayOrientations.Landscape) - { - switch (current) - { - case DisplayOrientations.Landscape: return ScreenRotation.Rotation0; - case DisplayOrientations.Portrait: return ScreenRotation.Rotation270; - case DisplayOrientations.LandscapeFlipped: return ScreenRotation.Rotation180; - case DisplayOrientations.PortraitFlipped: return ScreenRotation.Rotation90; - } - } - - return ScreenRotation.Rotation0; - } } } diff --git a/Xamarin.Essentials/Xamarin.Essentials.csproj b/Xamarin.Essentials/Xamarin.Essentials.csproj index 3570040..a803207 100644 --- a/Xamarin.Essentials/Xamarin.Essentials.csproj +++ b/Xamarin.Essentials/Xamarin.Essentials.csproj @@ -40,7 +40,7 @@ pdbonly - + diff --git a/docs/en/FrameworksIndex/xamarin-essentials-android.xml b/docs/en/FrameworksIndex/xamarin-essentials-android.xml index 0a28a5d..2665632 100644 --- a/docs/en/FrameworksIndex/xamarin-essentials-android.xml +++ b/docs/en/FrameworksIndex/xamarin-essentials-android.xml @@ -100,15 +100,17 @@ + + + + - - diff --git a/docs/en/FrameworksIndex/xamarin-essentials-ios.xml b/docs/en/FrameworksIndex/xamarin-essentials-ios.xml index f9324a7..0f869f4 100644 --- a/docs/en/FrameworksIndex/xamarin-essentials-ios.xml +++ b/docs/en/FrameworksIndex/xamarin-essentials-ios.xml @@ -100,15 +100,17 @@ + + + + - - diff --git a/docs/en/FrameworksIndex/xamarin-essentials-uwp.xml b/docs/en/FrameworksIndex/xamarin-essentials-uwp.xml index f35d614..7bc04f1 100644 --- a/docs/en/FrameworksIndex/xamarin-essentials-uwp.xml +++ b/docs/en/FrameworksIndex/xamarin-essentials-uwp.xml @@ -100,15 +100,17 @@ + + + + - - diff --git a/docs/en/FrameworksIndex/xamarin-essentials.xml b/docs/en/FrameworksIndex/xamarin-essentials.xml index 07ea7d5..e99e067 100644 --- a/docs/en/FrameworksIndex/xamarin-essentials.xml +++ b/docs/en/FrameworksIndex/xamarin-essentials.xml @@ -100,15 +100,17 @@ + + + + - - diff --git a/docs/en/Xamarin.Essentials/DeviceDisplay.xml b/docs/en/Xamarin.Essentials/DeviceDisplay.xml new file mode 100644 index 0000000..68013a8 --- /dev/null +++ b/docs/en/Xamarin.Essentials/DeviceDisplay.xml @@ -0,0 +1,58 @@ + + + + + + Xamarin.Essentials + 1.0.0.0 + + + System.Object + + + + Represents information about the device screen. + + + + + + + + + + Property + + 1.0.0.0 + Xamarin.Essentials + + + Xamarin.Essentials.ScreenMetrics + + + Gets the screen metrics of the device. + The screen metrics. + + + + + + + + + + Event + + 1.0.0.0 + Xamarin.Essentials + + + Xamarin.Essentials.ScreenMetricsChanagedEventHandler + + + Event that is triggered when the screen matrics change. + Such as screen rotation. + + + + diff --git a/docs/en/Xamarin.Essentials/DeviceInfo.xml b/docs/en/Xamarin.Essentials/DeviceInfo.xml index 0e9a75d..c3b541e 100644 --- a/docs/en/Xamarin.Essentials/DeviceInfo.xml +++ b/docs/en/Xamarin.Essentials/DeviceInfo.xml @@ -11,7 +11,7 @@ - Represents information about the device and application. + Represents information about the device. @@ -137,43 +137,6 @@ - - - - - Property - - 1.0.0.0 - Xamarin.Essentials - - - Xamarin.Essentials.ScreenMetrics - - - Gets the screen metrics of the device. - The screen metrics. - - - - - - - - - - Event - - 1.0.0.0 - Xamarin.Essentials - - - Xamarin.Essentials.ScreenMetricsChanagedEventHandler - - - Event that is triggered when the screen matrics change. - Such as screen rotation. - - diff --git a/docs/en/index.xml b/docs/en/index.xml index d19c09e..5f34768 100644 --- a/docs/en/index.xml +++ b/docs/en/index.xml @@ -82,6 +82,7 @@ +