Skip to content

Commit

Permalink
v2.0
Browse files Browse the repository at this point in the history
use [MethodImpl(MethodImplOptions.AggressiveInlining)]
  • Loading branch information
AigioL committed May 23, 2022
1 parent a163efe commit 726892f
Show file tree
Hide file tree
Showing 17 changed files with 331 additions and 143 deletions.
12 changes: 9 additions & 3 deletions OperatingSystem2.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Xamarin.Essentials;
#endif
using System.Runtime.Versioning;
using System.Runtime.CompilerServices;

namespace System
{
Expand All @@ -15,7 +16,8 @@ partial class OperatingSystem2
/// 指示当前应用程序是否正在 Android 上运行。
/// </summary>
[SupportedOSPlatformGuard("android")]
public static bool IsAndroid =>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsAndroid() =>
#if __ANDROID__
true;
#elif NET5_0 || NET6_0 || NET7_0
Expand Down Expand Up @@ -47,7 +49,8 @@ partial class OperatingSystem2
/// 指示当前应用程序是否正在 Windows Subsystem for Android™️ 上运行。
/// </summary>
[SupportedOSPlatformGuard("android")]
public static bool IsRunningOnWSA =>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsRunningOnWSA() =>
#if __ANDROID__
_IsRunningOnWSA.Value;
#else
Expand All @@ -60,6 +63,7 @@ partial class OperatingSystem2
/// <param name="sdkInt"></param>
/// <returns></returns>
[SupportedOSPlatformGuard("android")]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsAndroidVersionAtLeast(int sdkInt)
{
#if __ANDROID__
Expand All @@ -76,6 +80,7 @@ public static bool IsAndroidVersionAtLeast(int sdkInt)
/// <param name="sdkInt"></param>
/// <returns></returns>
[SupportedOSPlatformGuard("android")]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsAndroidVersionAtLeast(BuildVersionCodes sdkInt)
{
return Build.VERSION.SdkInt >= sdkInt;
Expand All @@ -91,14 +96,15 @@ public static bool IsAndroidVersionAtLeast(BuildVersionCodes sdkInt)
/// <param name="revision"></param>
/// <returns></returns>
[SupportedOSPlatformGuard("android")]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsAndroidVersionAtLeast(int major, int minor = 0, int build = 0, int revision = 0)
{
#if NET5_0 || NET6_0 || NET7_0
return OperatingSystem.IsAndroidVersionAtLeast(major, minor, build, revision);
#elif __HAVE_XAMARIN_ESSENTIALS__
return
#if !__ANDROID__
IsAndroid &&
IsAndroid() &&
#endif
IsVersionAtLeast(DeviceInfo.Version, major, minor, build, revision);
#else
Expand Down
19 changes: 13 additions & 6 deletions OperatingSystem2.application.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;

namespace System
{
Expand Down Expand Up @@ -87,22 +88,28 @@ public static class Types
}

/// <inheritdoc cref="TypeNames.Avalonia"/>
public static bool UseAvalonia => Types.Avalonia != null;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool UseAvalonia() => Types.Avalonia != null;

/// <inheritdoc cref="TypeNames.XamarinForms"/>
public static bool UseXamarinForms => Types.XamarinForms != null;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool UseXamarinForms() => Types.XamarinForms != null;

/// <inheritdoc cref="TypeNames.Maui"/>
public static bool UseMaui => Types.Maui != null;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool UseMaui() => Types.Maui != null;

/// <inheritdoc cref="TypeNames.WindowsForms"/>
public static bool UseWindowsForms => Types.WindowsForms != null;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool UseWindowsForms() => Types.WindowsForms != null;

/// <inheritdoc cref="TypeNames.WPF"/>
public static bool UseWPF => Types.WPF != null;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool UseWPF() => Types.WPF != null;

/// <inheritdoc cref="TypeNames.Uno"/>
public static bool UseUno => Types.Uno != null;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool UseUno() => Types.Uno != null;
}
}
}
4 changes: 3 additions & 1 deletion OperatingSystem2.browser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Runtime.InteropServices;
#endif
using System.Runtime.Versioning;
using System.Runtime.CompilerServices;

namespace System
{
Expand All @@ -11,7 +12,8 @@ partial class OperatingSystem2
/// 指示当前应用程序是否在浏览器中作为 WASM 运行。
/// </summary>
[SupportedOSPlatformGuard("browser")]
public static bool IsBrowser =>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsBrowser() =>
#if NET5_0 || NET6_0 || NET7_0
OperatingSystem.IsBrowser();
#elif __HAVE_RUNTIME_INFORMATION__
Expand Down
74 changes: 15 additions & 59 deletions OperatingSystem2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#if WINDOWS_UWP
using Windows.System.Profile;
#endif
using System.Runtime.CompilerServices;

namespace System
{
Expand All @@ -14,6 +15,7 @@ namespace System
public static partial class OperatingSystem2
{
#if __HAVE_XAMARIN_ESSENTIALS__ || (!NET5_0 && !NET6_0 && !NET7_0)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static int GetInt32(int value) => value < 0 ? 0 : value;

static bool IsVersionAtLeast(int major_left, int minor_left, int build_left, int revision_left, int major_right, int minor_right, int build_right, int revision_right)
Expand Down Expand Up @@ -48,83 +50,37 @@ static bool IsVersionAtLeast(Version version_left, int major_right = 0, int mino
/// <summary>
/// 当前是否使用 Mono 运行时。
/// </summary>
public static bool IsRunningOnMono { get; } =
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsRunningOnMono()
{
#if __XAMARIN_ANDROID_v1_0__ || XAMARIN_MAC || XAMARIN_IOS || XAMARIN_WATCHOS || XAMARIN_TVOS
true;
return true;
#else
Type.GetType("Mono.Runtime") != null;
return IsRunningOnMono_;
#endif
}

#if !WINDOWS_UWP && !(NETSTANDARD1_0 || NETSTANDARD1_1 || NET35 || NET40)
static byte isAvaloniaDesktop;
#endif
static bool IsDesktop_()
{
#if WINDOWS_UWP
return AnalyticsInfo.VersionInfo.DeviceFamily == "Windows.Desktop";
#if __XAMARIN_ANDROID_v1_0__ || XAMARIN_MAC || XAMARIN_IOS || XAMARIN_WATCHOS || XAMARIN_TVOS
#else
#if __HAVE_XAMARIN_ESSENTIALS__
var idiom = DeviceInfo.Idiom;
if (idiom == DeviceIdiom.Desktop) return true;
if (idiom != DeviceIdiom.Unknown) return false;
#endif
if (Application.UseWindowsForms || Application.UseWPF)
{
return true;
}
#if !(NETSTANDARD1_0 || NETSTANDARD1_1 || NET35 || NET40)
else if (Application.UseAvalonia)
{
if (isAvaloniaDesktop == 0)
{
try
{
var currentAvaloniaApplication = Application.Types.Avalonia!.GetProperty("Current", BindingFlags.Static | BindingFlags.Public)?.GetValue(null);
if (currentAvaloniaApplication != null)
{
var applicationLifetime = Application.Types.Avalonia.GetProperty("ApplicationLifetime", BindingFlags.Instance | BindingFlags.Public)?.GetValue(currentAvaloniaApplication);
if (applicationLifetime != null)
{
if (applicationLifetime.GetType().ToString().Contains("Desktop"))
{
isAvaloniaDesktop = 1;
}
}
}
}
catch
{
isAvaloniaDesktop = 2;
}
}
return isAvaloniaDesktop == 1;
}
static bool IsRunningOnMono_ { get; } = Type.GetType("Mono.Runtime") != null;
#endif
return false;
#endif
}

/// <summary>
/// 指示当前应用程序是否正在 Desktop 上运行。
/// </summary>
public static bool IsDesktop =>
_IsDesktop.Value;
static readonly Lazy<bool> _IsDesktop = new(IsDesktop_);

/// <summary>
/// 指示当前应用程序是否正在仅支持应用商店的平台上运行。
/// </summary>
public static bool IsOnlySupportedStore =>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsOnlySupportedStore() =>
#if NETFRAMEWORK || __MACOS__ || NET5_0_WINDOWS || NET6_0_WINDOWS || NET7_0_WINDOWS || __ANDROID__
false;
#else
IsIOS || IsWatchOS || IsTvOS;
IsIOS() || IsWatchOS() || IsTvOS();
#endif

/// <summary>
/// 获取当前系统版本号。
/// </summary>
public static Version Version =>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Version Version() =>
#if NETSTANDARD1_0
throw new PlatformNotSupportedException();
#elif NETSTANDARD1_1 || WINDOWS_UWP
Expand Down
8 changes: 4 additions & 4 deletions OperatingSystem2.csproj
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<Project Sdk="MSBuild.Sdk.Extras">

<PropertyGroup>
<TargetFrameworks>net6.0;net6.0-windows;net6.0-macos10.14;net6.0-android;net6.0-ios;net6.0-maccatalyst;net5.0;net5.0-windows;netcoreapp3.0;netcoreapp3.1;netstandard1.0;netstandard1.1;netstandard2.0;MonoAndroid11.0;Xamarin.iOS10;Xamarin.TVOS10;Xamarin.WatchOS10;Xamarin.Mac20</TargetFrameworks>
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">$(TargetFrameworks);net35;net40;net471;uap10.0.19041;</TargetFrameworks>
<TargetFrameworks>net6.0;net6.0-windows;net6.0-macos;net6.0-android;net6.0-ios;net6.0-maccatalyst;net5.0;net5.0-windows;netcoreapp3.0;netcoreapp3.1;netstandard1.0;netstandard1.1;netstandard2.0;MonoAndroid12.0;Xamarin.iOS10;Xamarin.TVOS10;Xamarin.WatchOS10;Xamarin.Mac20</TargetFrameworks>
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">$(TargetFrameworks);net35;net40;net45;net471;uap10.0.19041;</TargetFrameworks>
<AssemblyName>System.OperatingSystem2</AssemblyName>
<RootNamespace>System</RootNamespace>
<LangVersion>9.0</LangVersion>
Expand All @@ -21,7 +21,7 @@
<Description>表示有关操作系统的信息,如版本和平台标识符的通用库。</Description>
<PackageId>OperatingSystem2</PackageId>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Version>1.4.3</Version>
<Version>2.0.0</Version>
<AndroidUseIntermediateDesignerFile>false</AndroidUseIntermediateDesignerFile>
<DisableImplicitNamespaceImports>true</DisableImplicitNamespaceImports>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
Expand All @@ -39,7 +39,7 @@
</PropertyGroup>

<ItemGroup Condition=" $(TargetFramework.StartsWith('netstandard')) OR $(TargetFramework.StartsWith('MonoAndroid')) OR $(TargetFramework.StartsWith('Xamarin')) ">
<PackageReference Include="Xamarin.Essentials" Version="1.7.0" />
<PackageReference Include="Xamarin.Essentials" Version="1.7.3" />
</ItemGroup>

<ItemGroup>
Expand Down
111 changes: 111 additions & 0 deletions OperatingSystem2.desktop.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
using System.Reflection;
#if __HAVE_XAMARIN_ESSENTIALS__
using Xamarin.Essentials;
#endif
#if WINDOWS_UWP
using Windows.System.Profile;
#endif
using System.Runtime.CompilerServices;

namespace System
{
partial class OperatingSystem2
{
#if !WINDOWS_UWP && !(NETSTANDARD1_0 || NETSTANDARD1_1 || NET35 || NET40 || NET45)
static byte isAvaloniaDesktop;
#endif
static EDesktopType GetDesktopType()
{
#if WINDOWS_UWP
if (AnalyticsInfo.VersionInfo.DeviceFamily == "Windows.Desktop")
return EDesktopType.WindowsDesktop;
#else
#if __HAVE_XAMARIN_ESSENTIALS__
var idiom = DeviceInfo.Idiom;
if (idiom == DeviceIdiom.Desktop) return EDesktopType.DeviceIdiomDesktop;
if (idiom != DeviceIdiom.Unknown) return EDesktopType.Unknown;
#endif
if (Application.UseWindowsForms() || Application.UseWPF())
{
return EDesktopType.WinFormsOrWPF;
}
#if !(NETSTANDARD1_0 || NETSTANDARD1_1 || NET35 || NET40 || NET45)
else if (Application.UseAvalonia())
{
if (isAvaloniaDesktop == 0)
{
try
{
var currentAvaloniaApplication = Application.Types.Avalonia!.GetProperty("Current", BindingFlags.Static | BindingFlags.Public)?.GetValue(null);
if (currentAvaloniaApplication != null)
{
var applicationLifetime = Application.Types.Avalonia.GetProperty("ApplicationLifetime", BindingFlags.Instance | BindingFlags.Public)?.GetValue(currentAvaloniaApplication);
if (applicationLifetime != null)
{
if (applicationLifetime.GetType().ToString().Contains("Desktop"))
{
isAvaloniaDesktop = 1;
}
}
}
}
catch
{
isAvaloniaDesktop = 2;
}
}
if (isAvaloniaDesktop == 1)
{
return EDesktopType.AvaloniaDesktop;
}
}
#endif
#endif
return EDesktopType.Unknown;
}

/// <summary>
/// 指示当前应用程序是否正在 Desktop 上运行。
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsDesktop() => DesktopType != EDesktopType.Unknown;

/// <summary>
///
/// </summary>
public static EDesktopType DesktopType =>
_DesktopType.Value;
static readonly Lazy<EDesktopType> _DesktopType = new(GetDesktopType);

/// <summary>
///
/// </summary>
public enum EDesktopType
{
/// <summary>
///
/// </summary>
Unknown,

/// <summary>
/// UWP(Windows.Desktop)
/// </summary>
WindowsDesktop,

/// <summary>
///
/// </summary>
WinFormsOrWPF,

/// <summary>
///
/// </summary>
AvaloniaDesktop,

/// <summary>
///
/// </summary>
DeviceIdiomDesktop,
}
}
}
Loading

0 comments on commit 726892f

Please sign in to comment.