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
This commit is contained in:
Родитель
5a608d85b9
Коммит
c6c0f79e10
|
@ -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);
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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<IWindowManager>()?.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();
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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<IWindowManager>()?.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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -40,7 +40,7 @@
|
|||
<DebugType>pdbonly</DebugType>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="mdoc" Version="5.6.3" PrivateAssets="All" />
|
||||
<PackageReference Include="mdoc" Version="5.6.4" PrivateAssets="All" />
|
||||
<PackageReference Include="MSBuild.Sdk.Extras" Version="1.2.2" PrivateAssets="All" />
|
||||
<Compile Include="**\*.shared.cs" />
|
||||
<Compile Include="**\*.shared.*.cs" />
|
||||
|
|
|
@ -100,15 +100,17 @@
|
|||
<Member Id="M:Xamarin.Essentials.DataTransfer.RequestAsync(System.String,System.String)" />
|
||||
<Member Id="M:Xamarin.Essentials.DataTransfer.RequestAsync(Xamarin.Essentials.ShareTextRequest)" />
|
||||
</Type>
|
||||
<Type Name="Xamarin.Essentials.DeviceDisplay" Id="T:Xamarin.Essentials.DeviceDisplay">
|
||||
<Member Id="E:Xamarin.Essentials.DeviceDisplay.ScreenMetricsChanaged" />
|
||||
<Member Id="P:Xamarin.Essentials.DeviceDisplay.ScreenMetrics" />
|
||||
</Type>
|
||||
<Type Name="Xamarin.Essentials.DeviceInfo" Id="T:Xamarin.Essentials.DeviceInfo">
|
||||
<Member Id="E:Xamarin.Essentials.DeviceInfo.ScreenMetricsChanaged" />
|
||||
<Member Id="P:Xamarin.Essentials.DeviceInfo.DeviceType" />
|
||||
<Member Id="P:Xamarin.Essentials.DeviceInfo.Idiom" />
|
||||
<Member Id="P:Xamarin.Essentials.DeviceInfo.Manufacturer" />
|
||||
<Member Id="P:Xamarin.Essentials.DeviceInfo.Model" />
|
||||
<Member Id="P:Xamarin.Essentials.DeviceInfo.Name" />
|
||||
<Member Id="P:Xamarin.Essentials.DeviceInfo.Platform" />
|
||||
<Member Id="P:Xamarin.Essentials.DeviceInfo.ScreenMetrics" />
|
||||
<Member Id="P:Xamarin.Essentials.DeviceInfo.Version" />
|
||||
<Member Id="P:Xamarin.Essentials.DeviceInfo.VersionString" />
|
||||
</Type>
|
||||
|
|
|
@ -100,15 +100,17 @@
|
|||
<Member Id="M:Xamarin.Essentials.DataTransfer.RequestAsync(System.String,System.String)" />
|
||||
<Member Id="M:Xamarin.Essentials.DataTransfer.RequestAsync(Xamarin.Essentials.ShareTextRequest)" />
|
||||
</Type>
|
||||
<Type Name="Xamarin.Essentials.DeviceDisplay" Id="T:Xamarin.Essentials.DeviceDisplay">
|
||||
<Member Id="E:Xamarin.Essentials.DeviceDisplay.ScreenMetricsChanaged" />
|
||||
<Member Id="P:Xamarin.Essentials.DeviceDisplay.ScreenMetrics" />
|
||||
</Type>
|
||||
<Type Name="Xamarin.Essentials.DeviceInfo" Id="T:Xamarin.Essentials.DeviceInfo">
|
||||
<Member Id="E:Xamarin.Essentials.DeviceInfo.ScreenMetricsChanaged" />
|
||||
<Member Id="P:Xamarin.Essentials.DeviceInfo.DeviceType" />
|
||||
<Member Id="P:Xamarin.Essentials.DeviceInfo.Idiom" />
|
||||
<Member Id="P:Xamarin.Essentials.DeviceInfo.Manufacturer" />
|
||||
<Member Id="P:Xamarin.Essentials.DeviceInfo.Model" />
|
||||
<Member Id="P:Xamarin.Essentials.DeviceInfo.Name" />
|
||||
<Member Id="P:Xamarin.Essentials.DeviceInfo.Platform" />
|
||||
<Member Id="P:Xamarin.Essentials.DeviceInfo.ScreenMetrics" />
|
||||
<Member Id="P:Xamarin.Essentials.DeviceInfo.Version" />
|
||||
<Member Id="P:Xamarin.Essentials.DeviceInfo.VersionString" />
|
||||
</Type>
|
||||
|
|
|
@ -100,15 +100,17 @@
|
|||
<Member Id="M:Xamarin.Essentials.DataTransfer.RequestAsync(System.String,System.String)" />
|
||||
<Member Id="M:Xamarin.Essentials.DataTransfer.RequestAsync(Xamarin.Essentials.ShareTextRequest)" />
|
||||
</Type>
|
||||
<Type Name="Xamarin.Essentials.DeviceDisplay" Id="T:Xamarin.Essentials.DeviceDisplay">
|
||||
<Member Id="E:Xamarin.Essentials.DeviceDisplay.ScreenMetricsChanaged" />
|
||||
<Member Id="P:Xamarin.Essentials.DeviceDisplay.ScreenMetrics" />
|
||||
</Type>
|
||||
<Type Name="Xamarin.Essentials.DeviceInfo" Id="T:Xamarin.Essentials.DeviceInfo">
|
||||
<Member Id="E:Xamarin.Essentials.DeviceInfo.ScreenMetricsChanaged" />
|
||||
<Member Id="P:Xamarin.Essentials.DeviceInfo.DeviceType" />
|
||||
<Member Id="P:Xamarin.Essentials.DeviceInfo.Idiom" />
|
||||
<Member Id="P:Xamarin.Essentials.DeviceInfo.Manufacturer" />
|
||||
<Member Id="P:Xamarin.Essentials.DeviceInfo.Model" />
|
||||
<Member Id="P:Xamarin.Essentials.DeviceInfo.Name" />
|
||||
<Member Id="P:Xamarin.Essentials.DeviceInfo.Platform" />
|
||||
<Member Id="P:Xamarin.Essentials.DeviceInfo.ScreenMetrics" />
|
||||
<Member Id="P:Xamarin.Essentials.DeviceInfo.Version" />
|
||||
<Member Id="P:Xamarin.Essentials.DeviceInfo.VersionString" />
|
||||
</Type>
|
||||
|
|
|
@ -100,15 +100,17 @@
|
|||
<Member Id="M:Xamarin.Essentials.DataTransfer.RequestAsync(System.String,System.String)" />
|
||||
<Member Id="M:Xamarin.Essentials.DataTransfer.RequestAsync(Xamarin.Essentials.ShareTextRequest)" />
|
||||
</Type>
|
||||
<Type Name="Xamarin.Essentials.DeviceDisplay" Id="T:Xamarin.Essentials.DeviceDisplay">
|
||||
<Member Id="E:Xamarin.Essentials.DeviceDisplay.ScreenMetricsChanaged" />
|
||||
<Member Id="P:Xamarin.Essentials.DeviceDisplay.ScreenMetrics" />
|
||||
</Type>
|
||||
<Type Name="Xamarin.Essentials.DeviceInfo" Id="T:Xamarin.Essentials.DeviceInfo">
|
||||
<Member Id="E:Xamarin.Essentials.DeviceInfo.ScreenMetricsChanaged" />
|
||||
<Member Id="P:Xamarin.Essentials.DeviceInfo.DeviceType" />
|
||||
<Member Id="P:Xamarin.Essentials.DeviceInfo.Idiom" />
|
||||
<Member Id="P:Xamarin.Essentials.DeviceInfo.Manufacturer" />
|
||||
<Member Id="P:Xamarin.Essentials.DeviceInfo.Model" />
|
||||
<Member Id="P:Xamarin.Essentials.DeviceInfo.Name" />
|
||||
<Member Id="P:Xamarin.Essentials.DeviceInfo.Platform" />
|
||||
<Member Id="P:Xamarin.Essentials.DeviceInfo.ScreenMetrics" />
|
||||
<Member Id="P:Xamarin.Essentials.DeviceInfo.Version" />
|
||||
<Member Id="P:Xamarin.Essentials.DeviceInfo.VersionString" />
|
||||
</Type>
|
||||
|
|
|
@ -0,0 +1,58 @@
|
|||
<Type Name="DeviceDisplay" FullName="Xamarin.Essentials.DeviceDisplay">
|
||||
<TypeSignature Language="C#" Value="public static class DeviceDisplay" />
|
||||
<TypeSignature Language="ILAsm" Value=".class public auto ansi abstract sealed beforefieldinit DeviceDisplay extends System.Object" />
|
||||
<TypeSignature Language="DocId" Value="T:Xamarin.Essentials.DeviceDisplay" />
|
||||
<AssemblyInfo>
|
||||
<AssemblyName>Xamarin.Essentials</AssemblyName>
|
||||
<AssemblyVersion>1.0.0.0</AssemblyVersion>
|
||||
</AssemblyInfo>
|
||||
<Base>
|
||||
<BaseTypeName>System.Object</BaseTypeName>
|
||||
</Base>
|
||||
<Interfaces />
|
||||
<Docs>
|
||||
<summary>Represents information about the device screen.</summary>
|
||||
<remarks>
|
||||
<para />
|
||||
</remarks>
|
||||
</Docs>
|
||||
<Members>
|
||||
<Member MemberName="ScreenMetrics">
|
||||
<MemberSignature Language="C#" Value="public static Xamarin.Essentials.ScreenMetrics ScreenMetrics { get; }" />
|
||||
<MemberSignature Language="ILAsm" Value=".property valuetype Xamarin.Essentials.ScreenMetrics ScreenMetrics" />
|
||||
<MemberSignature Language="DocId" Value="P:Xamarin.Essentials.DeviceDisplay.ScreenMetrics" />
|
||||
<MemberType>Property</MemberType>
|
||||
<AssemblyInfo>
|
||||
<AssemblyVersion>1.0.0.0</AssemblyVersion>
|
||||
<AssemblyName>Xamarin.Essentials</AssemblyName>
|
||||
</AssemblyInfo>
|
||||
<ReturnValue>
|
||||
<ReturnType>Xamarin.Essentials.ScreenMetrics</ReturnType>
|
||||
</ReturnValue>
|
||||
<Docs>
|
||||
<summary>Gets the screen metrics of the device.</summary>
|
||||
<value>The screen metrics.</value>
|
||||
<remarks>
|
||||
<para />
|
||||
</remarks>
|
||||
</Docs>
|
||||
</Member>
|
||||
<Member MemberName="ScreenMetricsChanaged">
|
||||
<MemberSignature Language="C#" Value="public static event Xamarin.Essentials.ScreenMetricsChanagedEventHandler ScreenMetricsChanaged;" />
|
||||
<MemberSignature Language="ILAsm" Value=".event class Xamarin.Essentials.ScreenMetricsChanagedEventHandler ScreenMetricsChanaged" />
|
||||
<MemberSignature Language="DocId" Value="E:Xamarin.Essentials.DeviceDisplay.ScreenMetricsChanaged" />
|
||||
<MemberType>Event</MemberType>
|
||||
<AssemblyInfo>
|
||||
<AssemblyVersion>1.0.0.0</AssemblyVersion>
|
||||
<AssemblyName>Xamarin.Essentials</AssemblyName>
|
||||
</AssemblyInfo>
|
||||
<ReturnValue>
|
||||
<ReturnType>Xamarin.Essentials.ScreenMetricsChanagedEventHandler</ReturnType>
|
||||
</ReturnValue>
|
||||
<Docs>
|
||||
<summary>Event that is triggered when the screen matrics change.</summary>
|
||||
<remarks>Such as screen rotation.</remarks>
|
||||
</Docs>
|
||||
</Member>
|
||||
</Members>
|
||||
</Type>
|
|
@ -11,7 +11,7 @@
|
|||
</Base>
|
||||
<Interfaces />
|
||||
<Docs>
|
||||
<summary>Represents information about the device and application.</summary>
|
||||
<summary>Represents information about the device.</summary>
|
||||
<remarks>
|
||||
<para />
|
||||
</remarks>
|
||||
|
@ -137,43 +137,6 @@
|
|||
</remarks>
|
||||
</Docs>
|
||||
</Member>
|
||||
<Member MemberName="ScreenMetrics">
|
||||
<MemberSignature Language="C#" Value="public static Xamarin.Essentials.ScreenMetrics ScreenMetrics { get; }" />
|
||||
<MemberSignature Language="ILAsm" Value=".property valuetype Xamarin.Essentials.ScreenMetrics ScreenMetrics" />
|
||||
<MemberSignature Language="DocId" Value="P:Xamarin.Essentials.DeviceInfo.ScreenMetrics" />
|
||||
<MemberType>Property</MemberType>
|
||||
<AssemblyInfo>
|
||||
<AssemblyVersion>1.0.0.0</AssemblyVersion>
|
||||
<AssemblyName>Xamarin.Essentials</AssemblyName>
|
||||
</AssemblyInfo>
|
||||
<ReturnValue>
|
||||
<ReturnType>Xamarin.Essentials.ScreenMetrics</ReturnType>
|
||||
</ReturnValue>
|
||||
<Docs>
|
||||
<summary>Gets the screen metrics of the device.</summary>
|
||||
<value>The screen metrics.</value>
|
||||
<remarks>
|
||||
<para />
|
||||
</remarks>
|
||||
</Docs>
|
||||
</Member>
|
||||
<Member MemberName="ScreenMetricsChanaged">
|
||||
<MemberSignature Language="C#" Value="public static event Xamarin.Essentials.ScreenMetricsChanagedEventHandler ScreenMetricsChanaged;" />
|
||||
<MemberSignature Language="ILAsm" Value=".event class Xamarin.Essentials.ScreenMetricsChanagedEventHandler ScreenMetricsChanaged" />
|
||||
<MemberSignature Language="DocId" Value="E:Xamarin.Essentials.DeviceInfo.ScreenMetricsChanaged" />
|
||||
<MemberType>Event</MemberType>
|
||||
<AssemblyInfo>
|
||||
<AssemblyVersion>1.0.0.0</AssemblyVersion>
|
||||
<AssemblyName>Xamarin.Essentials</AssemblyName>
|
||||
</AssemblyInfo>
|
||||
<ReturnValue>
|
||||
<ReturnType>Xamarin.Essentials.ScreenMetricsChanagedEventHandler</ReturnType>
|
||||
</ReturnValue>
|
||||
<Docs>
|
||||
<summary>Event that is triggered when the screen matrics change.</summary>
|
||||
<remarks>Such as screen rotation.</remarks>
|
||||
</Docs>
|
||||
</Member>
|
||||
<Member MemberName="Version">
|
||||
<MemberSignature Language="C#" Value="public static Version Version { get; }" />
|
||||
<MemberSignature Language="ILAsm" Value=".property class System.Version Version" />
|
||||
|
|
|
@ -82,6 +82,7 @@
|
|||
<Type Name="ConnectivityChangedEventArgs" Kind="Class" />
|
||||
<Type Name="ConnectivityChangedEventHandler" Kind="Delegate" />
|
||||
<Type Name="DataTransfer" Kind="Class" />
|
||||
<Type Name="DeviceDisplay" Kind="Class" />
|
||||
<Type Name="DeviceInfo" Kind="Class" />
|
||||
<Type Name="DeviceInfo+Idioms" Kind="Class" />
|
||||
<Type Name="DeviceInfo+Platforms" Kind="Class" />
|
||||
|
|
Загрузка…
Ссылка в новой задаче