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 7 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.SkiaWpf;
}

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.SkiaWpf;
}

internal static WpfHost? Current => _current;
Expand Down
3 changes: 3 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 @@ -85,6 +86,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
}
18 changes: 18 additions & 0 deletions src/Uno.UI.Toolkit/UnoRuntimePlatform.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace Uno.UI.Toolkit;

public enum UnoRuntimePlatform
{
Android,
iOS,
MacCatalyst,
MacOSX,
WebAssembly,
Windows,
Skia,
SkiaGtk,
SkiaWpf,
SkiaX11,
SkiaFrameBuffer,
SkiaMacOS,
Unknown
}
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.Skia;

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

}
24 changes: 24 additions & 0 deletions src/Uno.UI/Helpers/UnoRuntimePlatform.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Uno.UI.Helpers;

public enum UnoRuntimePlatform
{
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,
Skia,
SkiaGtk,
SkiaWpf,
SkiaX11,
SkiaFrameBuffer,
SkiaMacOS,
Copy link
Member

Choose a reason for hiding this comment

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

SkiaIslands?

Copy link
Member

Choose a reason for hiding this comment

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

In such case I would go with SkiaWpfIslands to make it even more specific

Copy link
Contributor

Choose a reason for hiding this comment

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

When\where\how would Skia* (e.g. SkiaMacOS) be returned ?

There's no code to allow to set it (from the host) since it's returned from a method. An internal Set... method (only for __SKIA__ could be used by the different hosts).

Also if Skia* values are returned when would the lone Skia value be returned ?

You might want to add an extension method, on the enum, like IsSkia() to make it easier to detect all Skia-based platforms (and remove Skia).

Copy link
Member

Choose a reason for hiding this comment

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

This sounds good, in some cases you want to make some code skia specific (maybe because of native rendering limitations), in other cases it is the "native platform" that matters only; so a combination of IsSkia() and GetPlatform() makes sense; and dropping Skia enum value completely (and use unknown as the "default")

Unknown
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 Unknown should be the default value - (= 0)

}
38 changes: 38 additions & 0 deletions src/Uno.UI/Helpers/Xaml/UnoRuntimePlatformExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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.SkiaWpf
|| platform == UnoRuntimePlatform.SkiaX11;
}

public static bool IsAndroid(this UnoRuntimePlatform platform)
{
return platform == UnoRuntimePlatform.Android;
}

public static bool IsiOS(this UnoRuntimePlatform platform)
{
return platform == UnoRuntimePlatform.iOS;
}

public static bool IsMacCatalyst(this UnoRuntimePlatform platform)
{
return platform == UnoRuntimePlatform.MacCatalyst;
}

public static bool IsMacOS(this UnoRuntimePlatform platform)
{
return platform == UnoRuntimePlatform.MacOSX;
}

public static bool IsWebAssembly(this UnoRuntimePlatform platform)
{
return platform == UnoRuntimePlatform.WebAssembly;
}
}
Loading