Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Platform runtime helper #18758

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
3 changes: 3 additions & 0 deletions src/Uno.UI.Runtime.Skia.Gtk/GtkHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -36,6 +37,8 @@ public GtkHost(Func<WinUIApplication> appBuilder)
{
_current = this;
_appBuilder = appBuilder;

PlatformRuntimeHelper.SkiaPlatform = UnoRuntimePlatform.SkiaGtk;
}

internal static GtkHost? Current => _current;
Expand Down
3 changes: 3 additions & 0 deletions src/Uno.UI.Runtime.Skia.Linux.FrameBuffer/FramebufferHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -46,6 +47,8 @@ public FrameBufferHost(Func<WUX.Application> appBuilder)

_eventLoop = new EventLoop();
_coreApplicationExtension = new CoreApplicationExtension(_terminationGate);

PlatformRuntimeHelper.SkiaPlatform = UnoRuntimePlatform.SkiaFrameBuffer;
}

/// <summary>
Expand Down
3 changes: 3 additions & 0 deletions src/Uno.UI.Runtime.Skia.MacOS/MacSkiaHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.UI.Xaml;

using Uno.Foundation.Logging;
using Uno.UI.Helpers;

namespace Uno.UI.Runtime.Skia.MacOS;

Expand Down Expand Up @@ -36,6 +37,8 @@ public MacSkiaHost(Func<Application> appBuilder)
{
_current = this;
_appBuilder = appBuilder;

PlatformRuntimeHelper.SkiaPlatform = UnoRuntimePlatform.SkiaMacOS;
}

internal static MacSkiaHost Current => _current!;
Expand Down
5 changes: 5 additions & 0 deletions src/Uno.UI.Runtime.Skia.Wpf/WpfHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -32,6 +33,8 @@ public WpfHost(Dispatcher dispatcher, Func<WinUIApplication> appBuilder)
_current = this;
_dispatcher = dispatcher;
_appBuilder = appBuilder;

PlatformRuntimeHelper.SkiaPlatform = UnoRuntimePlatform.SkiaWpfIslands;
}

internal WpfHost(Func<WinUIApplication> appBuilder, Func<WpfApplication>? wpfAppBuilder)
Expand All @@ -41,6 +44,8 @@ internal WpfHost(Func<WinUIApplication> appBuilder, Func<WpfApplication>? wpfApp
_current = this;
_dispatcher = _wpfApp.Dispatcher;
_appBuilder = appBuilder;

PlatformRuntimeHelper.SkiaPlatform = UnoRuntimePlatform.SkiaWpfIslands;
}

internal static WpfHost? Current => _current;
Expand Down
5 changes: 5 additions & 0 deletions src/Uno.UI.Runtime.Skia.X11/X11ApplicationHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -85,6 +88,8 @@ public X11ApplicationHost(Func<Application> appBuilder, int renderFrameRate = 60
}, UI.Dispatching.NativeDispatcherPriority.Normal);
CoreDispatcher.DispatchOverride = _eventLoop.Schedule;
CoreDispatcher.HasThreadAccessOverride = () => _isDispatcherThread;

PlatformRuntimeHelper.SkiaPlatform = UnoRuntimePlatform.SkiaX11;
}

internal static int RenderFrameRate => _renderFrameRate;
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
}
18 changes: 18 additions & 0 deletions src/Uno.UI.Toolkit/RuntimeHelper.cs
Original file line number Diff line number Diff line change
@@ -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
}
16 changes: 16 additions & 0 deletions src/Uno.UI.Toolkit/UnoRuntimePlatform.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Uno.UI.Toolkit;

public enum UnoRuntimePlatform
{
Unknown,
Android,
iOS,
MacCatalyst,
WebAssembly,
Windows,
SkiaGtk,
SkiaWpfIslands,
SkiaX11,
SkiaFrameBuffer,
SkiaMacOS
}
30 changes: 30 additions & 0 deletions src/Uno.UI/Helpers/PlatformRuntimeHelper.cs
Original file line number Diff line number Diff line change
@@ -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() =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a runtime test that gets the value from GetPlatform() at runtime and checks that the result is not Unknown - this will future proof us against potential new platforms being added and us forgetting to update this code path

#if __ANDROID__
UnoRuntimePlatform.Android;
#elif __IOS__
UnoRuntimePlatform.iOS;
#elif __MACCATALYST__
UnoRuntimePlatform.MacCatalyst;
#elif __WASM__
UnoRuntimePlatform.WebAssembly;
#elif __SKIA__
SkiaPlatform;
#else
UnoRuntimePlatform.Unknown;
#endif

}
22 changes: 22 additions & 0 deletions src/Uno.UI/Helpers/UnoRuntimePlatform.cs
Original file line number Diff line number Diff line change
@@ -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
}
20 changes: 20 additions & 0 deletions src/Uno.UI/Helpers/Xaml/UnoRuntimePlatformExtensions.cs
Original file line number Diff line number Diff line change
@@ -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;
}
Loading