diff --git a/src/Android/Avalonia.Android/AndroidPlatform.cs b/src/Android/Avalonia.Android/AndroidPlatform.cs index d5d5f211e9..28f6d5349b 100644 --- a/src/Android/Avalonia.Android/AndroidPlatform.cs +++ b/src/Android/Avalonia.Android/AndroidPlatform.cs @@ -18,6 +18,7 @@ namespace Avalonia public static AppBuilder UseAndroid(this AppBuilder builder) { return builder + .UseStandardRuntimePlatformSubsystem() .UseWindowingSubsystem(() => AndroidPlatform.Initialize(), "Android") .UseSkia(); } diff --git a/src/Avalonia.Base/Platform/IRuntimePlatform.cs b/src/Avalonia.Base/Platform/IRuntimePlatform.cs index a2b8079476..c6efadc99a 100644 --- a/src/Avalonia.Base/Platform/IRuntimePlatform.cs +++ b/src/Avalonia.Base/Platform/IRuntimePlatform.cs @@ -18,7 +18,6 @@ namespace Avalonia.Platform public bool IsMobile { get; set; } } - [Unstable] public enum FormFactorType { Unknown, diff --git a/src/Avalonia.Base/Platform/StandardRuntimePlatformServices.cs b/src/Avalonia.Base/Platform/StandardRuntimePlatformServices.cs index fe8ddb7a9e..70919bc477 100644 --- a/src/Avalonia.Base/Platform/StandardRuntimePlatformServices.cs +++ b/src/Avalonia.Base/Platform/StandardRuntimePlatformServices.cs @@ -3,18 +3,15 @@ using Avalonia.Compatibility; using Avalonia.Platform.Internal; using Avalonia.Platform.Interop; -namespace Avalonia.Platform +namespace Avalonia.Platform; + +internal static class StandardRuntimePlatformServices { - public static class StandardRuntimePlatformServices + public static void Register(Assembly? assembly = null) { - public static void Register(Assembly? assembly = null) - { - var standardPlatform = new StandardRuntimePlatform(); - - AssetLoader.RegisterResUriParsers(); - AvaloniaLocator.CurrentMutable - .Bind().ToConstant(standardPlatform) - .Bind().ToConstant(new StandardAssetLoader(assembly)); - } + AssetLoader.RegisterResUriParsers(); + AvaloniaLocator.CurrentMutable + .Bind().ToSingleton() + .Bind().ToConstant(new StandardAssetLoader(assembly)); } } diff --git a/src/Avalonia.Controls/AppBuilder.cs b/src/Avalonia.Controls/AppBuilder.cs index 77cc9d4dcb..3bc00bbc9c 100644 --- a/src/Avalonia.Controls/AppBuilder.cs +++ b/src/Avalonia.Controls/AppBuilder.cs @@ -12,23 +12,23 @@ namespace Avalonia /// /// Initializes platform-specific services for an . /// - public class AppBuilder + public sealed class AppBuilder { private static bool s_setupWasAlreadyCalled; private Action? _optionsInitializers; private Func? _appFactory; private IApplicationLifetime? _lifetime; - - /// - /// Gets or sets the instance. - /// - public IRuntimePlatform RuntimePlatform { get; set; } /// /// Gets or sets a method to call the initialize the runtime platform services (e. g. AssetLoader) /// - public Action RuntimePlatformServicesInitializer { get; private set; } + public Action? RuntimePlatformServicesInitializer { get; private set; } + /// + /// Gets the name of the currently selected windowing subsystem. + /// + public string? RuntimePlatformServicesName { get; private set; } + /// /// Gets the instance being initialized. /// @@ -70,21 +70,10 @@ namespace Avalonia /// /// Initializes a new instance of the class. /// - public AppBuilder() - : this(new StandardRuntimePlatform(), - builder => StandardRuntimePlatformServices.Register(builder.ApplicationType?.Assembly)) + private AppBuilder() { } - /// - /// Initializes a new instance of the class. - /// - protected AppBuilder(IRuntimePlatform platform, Action platformServices) - { - RuntimePlatform = platform; - RuntimePlatformServicesInitializer = () => platformServices(this); - } - /// /// Begin configuring an . /// @@ -96,7 +85,6 @@ namespace Avalonia return new AppBuilder() { ApplicationType = typeof(TApp), - // Needed for CoreRT compatibility _appFactory = () => new TApp() }; } @@ -155,7 +143,7 @@ namespace Avalonia $"Input type either needs to have BuildAvaloniaApp -> AppBuilder method or inherit Application type."); } - protected AppBuilder Self => this; + private AppBuilder Self => this; public AppBuilder AfterSetup(Action callback) { @@ -225,6 +213,30 @@ namespace Avalonia RenderingSubsystemName = name; return Self; } + + /// + /// Specifies a runtime platform subsystem to use. + /// + /// The method to call to initialize the runtime platform subsystem. + /// The name of the runtime platform subsystem. + /// An instance. + public AppBuilder UseRuntimePlatformSubsystem(Action initializer, string name = "") + { + RuntimePlatformServicesInitializer = initializer; + RuntimePlatformServicesName = name; + return Self; + } + + /// + /// Specifies a standard runtime platform subsystem to use. + /// + /// An instance. + public AppBuilder UseStandardRuntimePlatformSubsystem() + { + RuntimePlatformServicesInitializer = () => StandardRuntimePlatformServices.Register(ApplicationType?.Assembly); + RuntimePlatformServicesName = nameof(StandardRuntimePlatform); + return Self; + } /// /// Configures platform-specific options diff --git a/src/Avalonia.DesignerSupport/Remote/RemoteDesignerEntryPoint.cs b/src/Avalonia.DesignerSupport/Remote/RemoteDesignerEntryPoint.cs index 6a6bc8c746..06d63a65ba 100644 --- a/src/Avalonia.DesignerSupport/Remote/RemoteDesignerEntryPoint.cs +++ b/src/Avalonia.DesignerSupport/Remote/RemoteDesignerEntryPoint.cs @@ -138,6 +138,7 @@ namespace Avalonia.DesignerSupport.Remote CommandLineArgs args, object obj) { var builder = (AppBuilder)obj; + builder = builder.UseStandardRuntimePlatformSubsystem(); if (args.Method == Methods.AvaloniaRemote) builder.UseWindowingSubsystem(() => PreviewerWindowingPlatform.Initialize(transport)); if (args.Method == Methods.Html) diff --git a/src/Avalonia.Native/AvaloniaNativePlatformExtensions.cs b/src/Avalonia.Native/AvaloniaNativePlatformExtensions.cs index 2b989ce733..52f66d3b0c 100644 --- a/src/Avalonia.Native/AvaloniaNativePlatformExtensions.cs +++ b/src/Avalonia.Native/AvaloniaNativePlatformExtensions.cs @@ -8,7 +8,9 @@ namespace Avalonia { public static AppBuilder UseAvaloniaNative(this AppBuilder builder) { - builder.UseWindowingSubsystem(() => + builder + .UseStandardRuntimePlatformSubsystem() + .UseWindowingSubsystem(() => { var platform = AvaloniaNativePlatform.Initialize( AvaloniaLocator.Current.GetService() ?? diff --git a/src/Avalonia.X11/X11Platform.cs b/src/Avalonia.X11/X11Platform.cs index a880e4ba1a..3a39a2a6b6 100644 --- a/src/Avalonia.X11/X11Platform.cs +++ b/src/Avalonia.X11/X11Platform.cs @@ -289,7 +289,9 @@ namespace Avalonia { public static AppBuilder UseX11(this AppBuilder builder) { - builder.UseWindowingSubsystem(() => + builder + .UseStandardRuntimePlatformSubsystem() + .UseWindowingSubsystem(() => new AvaloniaX11Platform().Initialize(AvaloniaLocator.Current.GetService() ?? new X11PlatformOptions())); return builder; diff --git a/src/Browser/Avalonia.Browser/BrowserAppBuilder.cs b/src/Browser/Avalonia.Browser/BrowserAppBuilder.cs index 38784871b1..dbc5f9f4a9 100644 --- a/src/Browser/Avalonia.Browser/BrowserAppBuilder.cs +++ b/src/Browser/Avalonia.Browser/BrowserAppBuilder.cs @@ -98,6 +98,7 @@ public static class BrowserAppBuilder this AppBuilder builder) { return builder + .UseBrowserRuntimePlatformSubsystem() .UseWindowingSubsystem(BrowserWindowingPlatform.Register) .UseSkia(); } diff --git a/src/Browser/Avalonia.Browser/BrowserRuntimePlatform.cs b/src/Browser/Avalonia.Browser/BrowserRuntimePlatform.cs index 67bb040410..eee2b70c9c 100644 --- a/src/Browser/Avalonia.Browser/BrowserRuntimePlatform.cs +++ b/src/Browser/Avalonia.Browser/BrowserRuntimePlatform.cs @@ -1,4 +1,5 @@ using System; +using System.Reflection; using System.Runtime.InteropServices; using System.Runtime.InteropServices.JavaScript; using System.Text.RegularExpressions; @@ -7,6 +8,23 @@ using Avalonia.Platform; namespace Avalonia.Browser; +internal static class BrowserRuntimePlatformServices +{ + public static AppBuilder UseBrowserRuntimePlatformSubsystem(this AppBuilder builder) + { + builder.UseRuntimePlatformSubsystem(() => Register(builder.ApplicationType?.Assembly), nameof(BrowserRuntimePlatform)); + return builder; + } + + public static void Register(Assembly? assembly = null) + { + AssetLoader.RegisterResUriParsers(); + AvaloniaLocator.CurrentMutable + .Bind().ToSingleton() + .Bind().ToConstant(new StandardAssetLoader(assembly)); + } +} + internal class BrowserRuntimePlatform : StandardRuntimePlatform { private static readonly Lazy Info = new(() => diff --git a/src/Headless/Avalonia.Headless/AvaloniaHeadlessPlatform.cs b/src/Headless/Avalonia.Headless/AvaloniaHeadlessPlatform.cs index 2d8506f92b..9bb587c9b1 100644 --- a/src/Headless/Avalonia.Headless/AvaloniaHeadlessPlatform.cs +++ b/src/Headless/Avalonia.Headless/AvaloniaHeadlessPlatform.cs @@ -105,8 +105,10 @@ namespace Avalonia.Headless public static AppBuilder UseHeadless(this AppBuilder builder, AvaloniaHeadlessPlatformOptions opts) { if(opts.UseHeadlessDrawing) - builder.UseRenderingSubsystem(HeadlessPlatformRenderInterface.Initialize, "Headless"); - return builder.UseWindowingSubsystem(() => AvaloniaHeadlessPlatform.Initialize(opts), "Headless"); + builder = builder.UseRenderingSubsystem(HeadlessPlatformRenderInterface.Initialize, "Headless"); + return builder + .UseStandardRuntimePlatformSubsystem() + .UseWindowingSubsystem(() => AvaloniaHeadlessPlatform.Initialize(opts), "Headless"); } } } diff --git a/src/Linux/Avalonia.LinuxFramebuffer/LinuxFramebufferPlatform.cs b/src/Linux/Avalonia.LinuxFramebuffer/LinuxFramebufferPlatform.cs index 10ec4372c7..e4a48c78fe 100644 --- a/src/Linux/Avalonia.LinuxFramebuffer/LinuxFramebufferPlatform.cs +++ b/src/Linux/Avalonia.LinuxFramebuffer/LinuxFramebufferPlatform.cs @@ -76,7 +76,10 @@ namespace Avalonia.LinuxFramebuffer internal static LinuxFramebufferLifetime Initialize(AppBuilder builder, IOutputBackend outputBackend, IInputBackend? inputBackend) { var platform = new LinuxFramebufferPlatform(outputBackend); - builder.UseSkia().UseWindowingSubsystem(platform.Initialize, "fbdev"); + builder + .UseStandardRuntimePlatformSubsystem() + .UseSkia() + .UseWindowingSubsystem(platform.Initialize, "fbdev"); return new LinuxFramebufferLifetime(platform._fb, inputBackend); } } diff --git a/src/Windows/Avalonia.Win32/Win32Platform.cs b/src/Windows/Avalonia.Win32/Win32Platform.cs index e9019803be..8f74fb49e8 100644 --- a/src/Windows/Avalonia.Win32/Win32Platform.cs +++ b/src/Windows/Avalonia.Win32/Win32Platform.cs @@ -25,7 +25,9 @@ namespace Avalonia { public static AppBuilder UseWin32(this AppBuilder builder) { - return builder.UseWindowingSubsystem( + return builder + .UseStandardRuntimePlatformSubsystem() + .UseWindowingSubsystem( () => Win32.Win32Platform.Initialize( AvaloniaLocator.Current.GetService() ?? new Win32PlatformOptions()), "Win32"); diff --git a/src/iOS/Avalonia.iOS/Platform.cs b/src/iOS/Avalonia.iOS/Platform.cs index de664c93e0..34018560e2 100644 --- a/src/iOS/Avalonia.iOS/Platform.cs +++ b/src/iOS/Avalonia.iOS/Platform.cs @@ -16,6 +16,7 @@ namespace Avalonia public static AppBuilder UseiOS(this AppBuilder builder) { return builder + .UseStandardRuntimePlatformSubsystem() .UseWindowingSubsystem(iOS.Platform.Register, "iOS") .UseSkia(); }