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 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
}
17 changes: 17 additions & 0 deletions src/Uno.UI.Toolkit/UnoRuntimePlatform.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Uno.UI.Toolkit;

public enum UnoRuntimePlatform
{
Unknown,
Android,
iOS,
MacCatalyst,
MacOSX,
WebAssembly,
Windows,
SkiaGtk,
SkiaWpfIslands,
SkiaX11,
SkiaFrameBuffer,
SkiaMacOS
}
32 changes: 32 additions & 0 deletions src/Uno.UI/Helpers/PlatformRuntimeHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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 __MACOS__
UnoRuntimePlatform.MacOSX;
#elif __WASM__
UnoRuntimePlatform.WebAssembly;
#elif __SKIA__
SkiaPlatform;
#else
UnoRuntimePlatform.Unknown;
#endif

}
23 changes: 23 additions & 0 deletions src/Uno.UI/Helpers/UnoRuntimePlatform.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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,
MacOSX,
Copy link
Member

Choose a reason for hiding this comment

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

I think this entry is not necessary, as it is actually only temporary anyway, as we will be dropping the legacy macOS implementation

WebAssembly,
Windows,
SkiaGtk,
SkiaWpfIslands,
SkiaX11,
SkiaFrameBuffer,
SkiaMacOS
}
13 changes: 13 additions & 0 deletions src/Uno.UI/Helpers/Xaml/UnoRuntimePlatformExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Uno.UI.Helpers;

internal static class UnoRuntimePlatformExtensions
{
public static bool IsSkia(UnoRuntimePlatform platform)
{
return platform == UnoRuntimePlatform.SkiaFrameBuffer
|| platform == UnoRuntimePlatform.SkiaGtk
|| platform == UnoRuntimePlatform.SkiaMacOS
|| platform == UnoRuntimePlatform.SkiaWpfIslands
|| platform == UnoRuntimePlatform.SkiaX11;
}
}
Loading