diff --git a/src/Uno.UI.Runtime.Skia.Gtk/GtkHost.cs b/src/Uno.UI.Runtime.Skia.Gtk/GtkHost.cs index f6deef09b4f2..b5711d8b7b5c 100644 --- a/src/Uno.UI.Runtime.Skia.Gtk/GtkHost.cs +++ b/src/Uno.UI.Runtime.Skia.Gtk/GtkHost.cs @@ -11,6 +11,7 @@ using Uno.UI.Runtime.Skia.Gtk.UI.Controls; using GtkApplication = Gtk.Application; using WinUIApplication = Microsoft.UI.Xaml.Application; +using Uno.UI.Helpers; namespace Uno.UI.Runtime.Skia.Gtk { @@ -36,6 +37,8 @@ public GtkHost(Func appBuilder) { _current = this; _appBuilder = appBuilder; + + PlatformRuntimeHelper.SkiaPlatform = UnoRuntimePlatform.SkiaGtk; } internal static GtkHost? Current => _current; diff --git a/src/Uno.UI.Runtime.Skia.Linux.FrameBuffer/FramebufferHost.cs b/src/Uno.UI.Runtime.Skia.Linux.FrameBuffer/FramebufferHost.cs index f1037ea77135..572c7f4cc40b 100644 --- a/src/Uno.UI.Runtime.Skia.Linux.FrameBuffer/FramebufferHost.cs +++ b/src/Uno.UI.Runtime.Skia.Linux.FrameBuffer/FramebufferHost.cs @@ -16,6 +16,7 @@ using WUX = Microsoft.UI.Xaml; using System.Threading.Tasks; using Uno.UI.Runtime.Skia.Linux.FrameBuffer.UI; +using Uno.UI.Helpers; namespace Uno.UI.Runtime.Skia.Linux.FrameBuffer { @@ -46,6 +47,8 @@ public FrameBufferHost(Func appBuilder) _eventLoop = new EventLoop(); _coreApplicationExtension = new CoreApplicationExtension(_terminationGate); + + PlatformRuntimeHelper.SkiaPlatform = UnoRuntimePlatform.SkiaFrameBuffer; } /// diff --git a/src/Uno.UI.Runtime.Skia.MacOS/MacSkiaHost.cs b/src/Uno.UI.Runtime.Skia.MacOS/MacSkiaHost.cs index 04e9a8d9f29a..099235b39389 100644 --- a/src/Uno.UI.Runtime.Skia.MacOS/MacSkiaHost.cs +++ b/src/Uno.UI.Runtime.Skia.MacOS/MacSkiaHost.cs @@ -2,6 +2,7 @@ using Microsoft.UI.Xaml; using Uno.Foundation.Logging; +using Uno.UI.Helpers; namespace Uno.UI.Runtime.Skia.MacOS; @@ -36,6 +37,8 @@ public MacSkiaHost(Func appBuilder) { _current = this; _appBuilder = appBuilder; + + PlatformRuntimeHelper.SkiaPlatform = UnoRuntimePlatform.SkiaMacOS; } internal static MacSkiaHost Current => _current!; diff --git a/src/Uno.UI.Runtime.Skia.Wpf/WpfHost.cs b/src/Uno.UI.Runtime.Skia.Wpf/WpfHost.cs index 45a2543e3a30..d95c3fd85846 100644 --- a/src/Uno.UI.Runtime.Skia.Wpf/WpfHost.cs +++ b/src/Uno.UI.Runtime.Skia.Wpf/WpfHost.cs @@ -3,6 +3,7 @@ using System; using System.Threading.Tasks; using System.Windows.Threading; +using Uno.UI.Helpers; using Uno.UI.Runtime.Skia.Wpf.Extensions; using Uno.UI.Runtime.Skia.Wpf.Extensions.UI.Xaml.Controls; using Uno.UI.Runtime.Skia.Wpf.Hosting; @@ -32,6 +33,8 @@ public WpfHost(Dispatcher dispatcher, Func appBuilder) _current = this; _dispatcher = dispatcher; _appBuilder = appBuilder; + + PlatformRuntimeHelper.SkiaPlatform = UnoRuntimePlatform.SkiaWpfIslands; } internal WpfHost(Func appBuilder, Func? wpfAppBuilder) @@ -41,6 +44,8 @@ internal WpfHost(Func appBuilder, Func? wpfApp _current = this; _dispatcher = _wpfApp.Dispatcher; _appBuilder = appBuilder; + + PlatformRuntimeHelper.SkiaPlatform = UnoRuntimePlatform.SkiaWpfIslands; } internal static WpfHost? Current => _current; diff --git a/src/Uno.UI.Runtime.Skia.X11/X11ApplicationHost.cs b/src/Uno.UI.Runtime.Skia.X11/X11ApplicationHost.cs index 71376b4296f3..cf9112ad26ba 100644 --- a/src/Uno.UI.Runtime.Skia.X11/X11ApplicationHost.cs +++ b/src/Uno.UI.Runtime.Skia.X11/X11ApplicationHost.cs @@ -10,6 +10,7 @@ using Uno.Foundation.Extensibility; using Uno.Foundation.Logging; using Uno.Helpers; +using Uno.UI.Helpers; using Uno.UI.Hosting; using Uno.UI.Runtime.Skia.Extensions.System; using Uno.UI.Xaml.Controls; @@ -47,6 +48,8 @@ static X11ApplicationHost() } } + PlatformRuntimeHelper.SkiaPlatform = UnoRuntimePlatform.SkiaX11; + ApiExtensibility.Register(typeof(Uno.ApplicationModel.Core.ICoreApplicationExtension), _ => new X11CoreApplicationExtension()); ApiExtensibility.Register(typeof(Windows.UI.ViewManagement.IApplicationViewExtension), o => new X11ApplicationViewExtension(o)); ApiExtensibility.Register(typeof(Windows.Graphics.Display.IDisplayInformationExtension), o => new X11DisplayInformationExtension(o, null)); @@ -85,6 +88,8 @@ public X11ApplicationHost(Func appBuilder, int renderFrameRate = 60 }, UI.Dispatching.NativeDispatcherPriority.Normal); CoreDispatcher.DispatchOverride = _eventLoop.Schedule; CoreDispatcher.HasThreadAccessOverride = () => _isDispatcherThread; + + PlatformRuntimeHelper.SkiaPlatform = UnoRuntimePlatform.SkiaX11; } internal static int RenderFrameRate => _renderFrameRate; diff --git a/src/Uno.UI.RuntimeTests/Tests/Uno_UI_Toolkit/Given_RuntimePlatformDetected.cs b/src/Uno.UI.RuntimeTests/Tests/Uno_UI_Toolkit/Given_RuntimePlatformDetected.cs new file mode 100644 index 000000000000..94bb843201d3 --- /dev/null +++ b/src/Uno.UI.RuntimeTests/Tests/Uno_UI_Toolkit/Given_RuntimePlatformDetected.cs @@ -0,0 +1,81 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Private.Infrastructure; +using Uno.Disposables; +using Uno.UI.RuntimeTests.Extensions; +using Uno.UI.RuntimeTests.Helpers; +using Uno.UI.Toolkit; +using Uno.UI.Helpers; +using Windows.UI; +using Windows.UI.ViewManagement; +using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Controls; +using Microsoft.UI.Xaml.Media; +using Microsoft.UI.Xaml.Shapes; +using static Private.Infrastructure.TestServices; +using FluentAssertions; + +namespace Uno.UI.RuntimeTests.Tests.Uno_UI_Toolkit +{ + [TestClass] + [RunsOnUIThread] + public class Given_RuntimePlatformDetected + { +#if __SKIA__ + [TestMethod] + public void When_IsSkia() + { + Uno.UI.Helpers.PlatformRuntimeHelper.Current.IsSkia().Should().BeTrue(); + } +#endif + +#if __IOS__ + [TestMethod] + public void When_IsIOS() + { + Uno.UI.Helpers.PlatformRuntimeHelper.Current.IsIOS().Should().BeTrue(); + } +#endif + +#if __ANDROID__ + [TestMethod] + public void When_IsAndroid() + { + Uno.UI.Helpers.PlatformRuntimeHelper.Current.IsAndroid().Should().BeTrue(); + } +#endif + +#if __MACCATALYST__ + [TestMethod] + public void When_IsMacCatalyst() + { + Uno.UI.Helpers.PlatformRuntimeHelper.Current.IsMacCatalyst().Should().BeTrue(); + } +#endif + +#if __WASM__ + [TestMethod] + public void When_IsWebAssembly() + { + Uno.UI.Helpers.PlatformRuntimeHelper.Current.IsWebAssembly().Should().BeTrue(); + } +#endif + +#if !HAS_UNO + [TestMethod] + public void When_IsWindows() + { + Uno.UI.Toolkit.PlatformRuntimeHelper.Current.Should().Be(Uno.UI.Toolkit.UnoRuntimePlatform.Windows); + } +#else + [TestMethod] + public void When_IsUnoIsKnown() + { + Uno.UI.Helpers.PlatformRuntimeHelper.Current.Should().NotBe(Uno.UI.Toolkit.UnoRuntimePlatform.Unknown); + } +#endif + } +} diff --git a/src/Uno.UI.Toolkit/RuntimeHelper.cs b/src/Uno.UI.Toolkit/RuntimeHelper.cs new file mode 100644 index 000000000000..7836784d51c3 --- /dev/null +++ b/src/Uno.UI.Toolkit/RuntimeHelper.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Uno.UI.Helpers; + +namespace Uno.UI.Toolkit; + +public static class PlatformRuntimeHelper +{ + public static UnoRuntimePlatform Current => +#if !HAS_UNO + Uno.UI.Toolkit.UnoRuntimePlatform.Windows; +#else + (Uno.UI.Toolkit.UnoRuntimePlatform)PlatformRuntimeHelper.Current; +#endif +} diff --git a/src/Uno.UI.Toolkit/UnoRuntimePlatform.cs b/src/Uno.UI.Toolkit/UnoRuntimePlatform.cs new file mode 100644 index 000000000000..0a0b3158043a --- /dev/null +++ b/src/Uno.UI.Toolkit/UnoRuntimePlatform.cs @@ -0,0 +1,16 @@ +namespace Uno.UI.Toolkit; + +public enum UnoRuntimePlatform +{ + Unknown, + Android, + iOS, + MacCatalyst, + WebAssembly, + Windows, + SkiaGtk, + SkiaWpfIslands, + SkiaX11, + SkiaFrameBuffer, + SkiaMacOS +} diff --git a/src/Uno.UI/Helpers/PlatformRuntimeHelper.cs b/src/Uno.UI/Helpers/PlatformRuntimeHelper.cs new file mode 100644 index 000000000000..2a0ac2555493 --- /dev/null +++ b/src/Uno.UI/Helpers/PlatformRuntimeHelper.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Uno.UI.Helpers; + +public static class PlatformRuntimeHelper +{ + internal static UnoRuntimePlatform SkiaPlatform { get; set; } = UnoRuntimePlatform.Unknown; + + public static UnoRuntimePlatform Current => GetPlatform(); + + private static UnoRuntimePlatform GetPlatform() => +#if __ANDROID__ + UnoRuntimePlatform.Android; +#elif __IOS__ + UnoRuntimePlatform.iOS; +#elif __MACCATALYST__ + UnoRuntimePlatform.MacCatalyst; +#elif __WASM__ + UnoRuntimePlatform.WebAssembly; +#elif __SKIA__ + SkiaPlatform; +#else + UnoRuntimePlatform.Unknown; +#endif + +} diff --git a/src/Uno.UI/Helpers/UnoRuntimePlatform.cs b/src/Uno.UI/Helpers/UnoRuntimePlatform.cs new file mode 100644 index 000000000000..0d29c868a370 --- /dev/null +++ b/src/Uno.UI/Helpers/UnoRuntimePlatform.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Uno.UI.Helpers; + +public enum UnoRuntimePlatform +{ + Unknown, + Android, + iOS, + MacCatalyst, + WebAssembly, + Windows, + SkiaGtk, + SkiaWpfIslands, + SkiaX11, + SkiaFrameBuffer, + SkiaMacOS +} diff --git a/src/Uno.UI/Helpers/Xaml/UnoRuntimePlatformExtensions.cs b/src/Uno.UI/Helpers/Xaml/UnoRuntimePlatformExtensions.cs new file mode 100644 index 000000000000..d3f02b73022f --- /dev/null +++ b/src/Uno.UI/Helpers/Xaml/UnoRuntimePlatformExtensions.cs @@ -0,0 +1,20 @@ +namespace Uno.UI.Helpers; + +public static class UnoRuntimePlatformExtensions +{ + public static bool IsSkia(this UnoRuntimePlatform platform) => platform == UnoRuntimePlatform.SkiaFrameBuffer + || platform == UnoRuntimePlatform.SkiaGtk + || platform == UnoRuntimePlatform.SkiaMacOS + || platform == UnoRuntimePlatform.SkiaWpfIslands + || platform == UnoRuntimePlatform.SkiaX11; + + public static bool IsIOS(this UnoRuntimePlatform platform) => platform == UnoRuntimePlatform.iOS; + + public static bool IsMacCatalyst(this UnoRuntimePlatform platform) => platform == UnoRuntimePlatform.MacCatalyst; + + public static bool IsWebAssembly(this UnoRuntimePlatform platform) => platform == UnoRuntimePlatform.WebAssembly; + + public static bool IsWindows(this UnoRuntimePlatform platform) => platform == UnoRuntimePlatform.Windows; + + public static bool IsAndroid(this UnoRuntimePlatform platform) => platform == UnoRuntimePlatform.Android; +}