diff --git a/src/Android/Avalonia.Android/AndroidPlatform.cs b/src/Android/Avalonia.Android/AndroidPlatform.cs index 75856e4b52..01b48ffd89 100644 --- a/src/Android/Avalonia.Android/AndroidPlatform.cs +++ b/src/Android/Avalonia.Android/AndroidPlatform.cs @@ -15,7 +15,7 @@ namespace Avalonia { public static class AndroidApplicationExtensions { - public static T UseAndroid(this T builder) where T : AppBuilderBase, new() + public static AppBuilder UseAndroid(this AppBuilder builder) { return builder .UseWindowingSubsystem(() => AndroidPlatform.Initialize(), "Android") diff --git a/src/Android/Avalonia.Android/AvaloniaSplashActivity.cs b/src/Android/Avalonia.Android/AvaloniaSplashActivity.cs index 5b5ebd1bd9..ec26ee5599 100644 --- a/src/Android/Avalonia.Android/AvaloniaSplashActivity.cs +++ b/src/Android/Avalonia.Android/AvaloniaSplashActivity.cs @@ -1,6 +1,6 @@ using Android.OS; using AndroidX.AppCompat.App; -using AndroidX.Lifecycle; +using Avalonia.Controls; namespace Avalonia.Android { diff --git a/src/Avalonia.Controls/AppBuilder.cs b/src/Avalonia.Controls/AppBuilder.cs index 5bcd87162e..cf79fcd1a8 100644 --- a/src/Avalonia.Controls/AppBuilder.cs +++ b/src/Avalonia.Controls/AppBuilder.cs @@ -1,4 +1,8 @@ -using Avalonia.Controls; +using System; +using System.Diagnostics.CodeAnalysis; +using System.Reflection; +using System.Linq; +using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Platform; namespace Avalonia @@ -6,15 +10,244 @@ namespace Avalonia /// /// Initializes platform-specific services for an . /// - public sealed class AppBuilder : AppBuilderBase + public 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; } + + /// + /// Gets the instance being initialized. + /// + public Application? Instance { get; private set; } + + /// + /// Gets the type of the Instance (even if it's not created yet) + /// + public Type? ApplicationType { get; private set; } + + /// + /// Gets or sets a method to call the initialize the windowing subsystem. + /// + public Action? WindowingSubsystemInitializer { get; private set; } + + /// + /// Gets the name of the currently selected windowing subsystem. + /// + public string? WindowingSubsystemName { get; private set; } + + /// + /// Gets or sets a method to call the initialize the windowing subsystem. + /// + public Action? RenderingSubsystemInitializer { get; private set; } + + /// + /// Gets the name of the currently selected rendering subsystem. + /// + public string? RenderingSubsystemName { get; private set; } + + /// + /// Gets or sets a method to call after the is setup. + /// + public Action AfterSetupCallback { get; private set; } = builder => { }; + + + public Action AfterPlatformServicesSetupCallback { get; private set; } = builder => { }; + /// /// Initializes a new instance of the class. /// public AppBuilder() - : base(new StandardRuntimePlatform(), + : this(new StandardRuntimePlatform(), builder => StandardRuntimePlatformServices.Register(builder.ApplicationType?.Assembly)) { } + + /// + /// Initializes a new instance of the class. + /// + protected AppBuilder(IRuntimePlatform platform, Action platformServices) + { + RuntimePlatform = platform; + RuntimePlatformServicesInitializer = () => platformServices(this); + } + + /// + /// Begin configuring an . + /// + /// The subclass of to configure. + /// An instance. + public static AppBuilder Configure() + where TApp : Application, new() + { + return new AppBuilder() + { + ApplicationType = typeof(TApp), + // Needed for CoreRT compatibility + _appFactory = () => new TApp() + }; + } + + /// + /// Begin configuring an . + /// + /// Factory function for . + /// The subclass of to configure. + /// is useful for passing of dependencies to . + /// An instance. + public static AppBuilder Configure(Func appFactory) + where TApp : Application + { + return new AppBuilder() + { + ApplicationType = typeof(TApp), + _appFactory = appFactory + }; + } + + protected AppBuilder Self => this; + + public AppBuilder AfterSetup(Action callback) + { + AfterSetupCallback = (Action)Delegate.Combine(AfterSetupCallback, callback); + return Self; + } + + + public AppBuilder AfterPlatformServicesSetup(Action callback) + { + AfterPlatformServicesSetupCallback = (Action)Delegate.Combine(AfterPlatformServicesSetupCallback, callback); + return Self; + } + + public delegate void AppMainDelegate(Application app, string[] args); + + public void Start(AppMainDelegate main, string[] args) + { + Setup(); + main(Instance!, args); + } + + /// + /// Sets up the platform-specific services for the application, but does not run it. + /// + /// + public AppBuilder SetupWithoutStarting() + { + Setup(); + return Self; + } + + /// + /// Sets up the platform-specific services for the application and initialized it with a particular lifetime, but does not run it. + /// + /// + /// + public AppBuilder SetupWithLifetime(IApplicationLifetime lifetime) + { + _lifetime = lifetime; + Setup(); + return Self; + } + + /// + /// Specifies a windowing subsystem to use. + /// + /// The method to call to initialize the windowing subsystem. + /// The name of the windowing subsystem. + /// An instance. + public AppBuilder UseWindowingSubsystem(Action initializer, string name = "") + { + WindowingSubsystemInitializer = initializer; + WindowingSubsystemName = name; + return Self; + } + + /// + /// Specifies a rendering subsystem to use. + /// + /// The method to call to initialize the rendering subsystem. + /// The name of the rendering subsystem. + /// An instance. + public AppBuilder UseRenderingSubsystem(Action initializer, string name = "") + { + RenderingSubsystemInitializer = initializer; + RenderingSubsystemName = name; + return Self; + } + + /// + /// Configures platform-specific options + /// + public AppBuilder With(T options) + { + _optionsInitializers += () => { AvaloniaLocator.CurrentMutable.Bind().ToConstant(options); }; + return Self; + } + + /// + /// Configures platform-specific options + /// + public AppBuilder With(Func options) + { + _optionsInitializers += () => { AvaloniaLocator.CurrentMutable.Bind().ToFunc(options); }; + return Self; + } + + /// + /// Sets up the platform-specific services for the . + /// + private void Setup() + { + if (RuntimePlatformServicesInitializer == null) + { + throw new InvalidOperationException("No runtime platform services configured."); + } + + if (WindowingSubsystemInitializer == null) + { + throw new InvalidOperationException("No windowing system configured."); + } + + if (RenderingSubsystemInitializer == null) + { + throw new InvalidOperationException("No rendering system configured."); + } + + if (_appFactory == null) + { + throw new InvalidOperationException("No Application factory configured."); + } + + if (s_setupWasAlreadyCalled) + { + throw new InvalidOperationException("Setup was already called on one of AppBuilder instances"); + } + + s_setupWasAlreadyCalled = true; + _optionsInitializers?.Invoke(); + RuntimePlatformServicesInitializer(); + RenderingSubsystemInitializer(); + WindowingSubsystemInitializer(); + AfterPlatformServicesSetupCallback(Self); + Instance = _appFactory(); + Instance.ApplicationLifetime = _lifetime; + AvaloniaLocator.CurrentMutable.BindToSelf(Instance); + Instance.RegisterServices(); + Instance.Initialize(); + AfterSetupCallback(Self); + Instance.OnFrameworkInitializationCompleted(); + } } } diff --git a/src/Avalonia.Controls/AppBuilderBase.cs b/src/Avalonia.Controls/AppBuilderBase.cs deleted file mode 100644 index a100d38d78..0000000000 --- a/src/Avalonia.Controls/AppBuilderBase.cs +++ /dev/null @@ -1,244 +0,0 @@ -using System; -using System.Diagnostics.CodeAnalysis; -using System.Reflection; -using System.Linq; -using Avalonia.Controls.ApplicationLifetimes; -using Avalonia.Platform; - -namespace Avalonia.Controls -{ - /// - /// Base class for initializing platform-specific services for an . - /// - /// The type of the AppBuilder class itself. - public abstract class AppBuilderBase where TAppBuilder : AppBuilderBase, new() - { - 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; } - - /// - /// Gets the instance being initialized. - /// - public Application? Instance { get; private set; } - - /// - /// Gets the type of the Instance (even if it's not created yet) - /// - public Type? ApplicationType { get; private set; } - - /// - /// Gets or sets a method to call the initialize the windowing subsystem. - /// - public Action? WindowingSubsystemInitializer { get; private set; } - - /// - /// Gets the name of the currently selected windowing subsystem. - /// - public string? WindowingSubsystemName { get; private set; } - - /// - /// Gets or sets a method to call the initialize the windowing subsystem. - /// - public Action? RenderingSubsystemInitializer { get; private set; } - - /// - /// Gets the name of the currently selected rendering subsystem. - /// - public string? RenderingSubsystemName { get; private set; } - - /// - /// Gets or sets a method to call after the is setup. - /// - public Action AfterSetupCallback { get; private set; } = builder => { }; - - - public Action AfterPlatformServicesSetupCallback { get; private set; } = builder => { }; - - protected AppBuilderBase(IRuntimePlatform platform, Action platformServices) - { - RuntimePlatform = platform; - RuntimePlatformServicesInitializer = () => platformServices((TAppBuilder)this); - } - - /// - /// Begin configuring an . - /// - /// The subclass of to configure. - /// An instance. - public static TAppBuilder Configure() - where TApp : Application, new() - { - return new TAppBuilder() - { - ApplicationType = typeof(TApp), - // Needed for CoreRT compatibility - _appFactory = () => new TApp() - }; - } - - /// - /// Begin configuring an . - /// - /// Factory function for . - /// The subclass of to configure. - /// is useful for passing of dependencies to . - /// An instance. - public static TAppBuilder Configure(Func appFactory) - where TApp : Application - { - return new TAppBuilder() - { - ApplicationType = typeof(TApp), - _appFactory = appFactory - }; - } - - protected TAppBuilder Self => (TAppBuilder)this; - - public TAppBuilder AfterSetup(Action callback) - { - AfterSetupCallback = (Action)Delegate.Combine(AfterSetupCallback, callback); - return Self; - } - - - public TAppBuilder AfterPlatformServicesSetup(Action callback) - { - AfterPlatformServicesSetupCallback = (Action)Delegate.Combine(AfterPlatformServicesSetupCallback, callback); - return Self; - } - - public delegate void AppMainDelegate(Application app, string[] args); - - public void Start(AppMainDelegate main, string[] args) - { - Setup(); - main(Instance!, args); - } - - /// - /// Sets up the platform-specific services for the application, but does not run it. - /// - /// - public TAppBuilder SetupWithoutStarting() - { - Setup(); - return Self; - } - - /// - /// Sets up the platform-specific services for the application and initialized it with a particular lifetime, but does not run it. - /// - /// - /// - public TAppBuilder SetupWithLifetime(IApplicationLifetime lifetime) - { - _lifetime = lifetime; - Setup(); - return Self; - } - - /// - /// Specifies a windowing subsystem to use. - /// - /// The method to call to initialize the windowing subsystem. - /// The name of the windowing subsystem. - /// An instance. - public TAppBuilder UseWindowingSubsystem(Action initializer, string name = "") - { - WindowingSubsystemInitializer = initializer; - WindowingSubsystemName = name; - return Self; - } - - /// - /// Specifies a rendering subsystem to use. - /// - /// The method to call to initialize the rendering subsystem. - /// The name of the rendering subsystem. - /// An instance. - public TAppBuilder UseRenderingSubsystem(Action initializer, string name = "") - { - RenderingSubsystemInitializer = initializer; - RenderingSubsystemName = name; - return Self; - } - - protected virtual bool CheckSetup => true; - - /// - /// Configures platform-specific options - /// - public TAppBuilder With(T options) - { - _optionsInitializers += () => { AvaloniaLocator.CurrentMutable.Bind().ToConstant(options); }; - return Self; - } - - /// - /// Configures platform-specific options - /// - public TAppBuilder With(Func options) - { - _optionsInitializers += () => { AvaloniaLocator.CurrentMutable.Bind().ToFunc(options); }; - return Self; - } - - /// - /// Sets up the platform-specific services for the . - /// - private void Setup() - { - if (RuntimePlatformServicesInitializer == null) - { - throw new InvalidOperationException("No runtime platform services configured."); - } - - if (WindowingSubsystemInitializer == null) - { - throw new InvalidOperationException("No windowing system configured."); - } - - if (RenderingSubsystemInitializer == null) - { - throw new InvalidOperationException("No rendering system configured."); - } - - if (_appFactory == null) - { - throw new InvalidOperationException("No Application factory configured."); - } - - if (s_setupWasAlreadyCalled && CheckSetup) - { - throw new InvalidOperationException("Setup was already called on one of AppBuilder instances"); - } - - s_setupWasAlreadyCalled = true; - _optionsInitializers?.Invoke(); - RuntimePlatformServicesInitializer(); - RenderingSubsystemInitializer(); - WindowingSubsystemInitializer(); - AfterPlatformServicesSetupCallback(Self); - Instance = _appFactory(); - Instance.ApplicationLifetime = _lifetime; - AvaloniaLocator.CurrentMutable.BindToSelf(Instance); - Instance.RegisterServices(); - Instance.Initialize(); - AfterSetupCallback(Self); - Instance.OnFrameworkInitializationCompleted(); - } - } -} diff --git a/src/Avalonia.Controls/ApplicationLifetimes/ClassicDesktopStyleApplicationLifetime.cs b/src/Avalonia.Controls/ApplicationLifetimes/ClassicDesktopStyleApplicationLifetime.cs index 757db96799..fde401fb01 100644 --- a/src/Avalonia.Controls/ApplicationLifetimes/ClassicDesktopStyleApplicationLifetime.cs +++ b/src/Avalonia.Controls/ApplicationLifetimes/ClassicDesktopStyleApplicationLifetime.cs @@ -200,9 +200,8 @@ namespace Avalonia { public static class ClassicDesktopStyleApplicationLifetimeExtensions { - public static int StartWithClassicDesktopLifetime( - this T builder, string[] args, ShutdownMode shutdownMode = ShutdownMode.OnLastWindowClose) - where T : AppBuilderBase, new() + public static int StartWithClassicDesktopLifetime( + this AppBuilder builder, string[] args, ShutdownMode shutdownMode = ShutdownMode.OnLastWindowClose) { var lifetime = new ClassicDesktopStyleApplicationLifetime() { diff --git a/src/Avalonia.Controls/LoggingExtensions.cs b/src/Avalonia.Controls/LoggingExtensions.cs index ef14d0b477..f909ba8714 100644 --- a/src/Avalonia.Controls/LoggingExtensions.cs +++ b/src/Avalonia.Controls/LoggingExtensions.cs @@ -8,16 +8,14 @@ namespace Avalonia /// /// Logs Avalonia events to the sink. /// - /// The application class type. /// The app builder instance. /// The minimum level to log. /// The areas to log. Valid values are listed in . /// The app builder instance. - public static T LogToTrace( - this T builder, + public static AppBuilder LogToTrace( + this AppBuilder builder, LogEventLevel level = LogEventLevel.Warning, params string[] areas) - where T : AppBuilderBase, new() { Logger.Sink = new TraceLogSink(level, areas); return builder; diff --git a/src/Avalonia.DesignerSupport/Remote/RemoteDesignerEntryPoint.cs b/src/Avalonia.DesignerSupport/Remote/RemoteDesignerEntryPoint.cs index 543d07f958..85605ccd9d 100644 --- a/src/Avalonia.DesignerSupport/Remote/RemoteDesignerEntryPoint.cs +++ b/src/Avalonia.DesignerSupport/Remote/RemoteDesignerEntryPoint.cs @@ -134,12 +134,12 @@ namespace Avalonia.DesignerSupport.Remote IAvaloniaRemoteTransportConnection ConfigureApp(IAvaloniaRemoteTransportConnection transport, CommandLineArgs args, object obj); } - class AppInitializer : IAppInitializer where T : AppBuilderBase, new() + class AppInitializer : IAppInitializer { public IAvaloniaRemoteTransportConnection ConfigureApp(IAvaloniaRemoteTransportConnection transport, CommandLineArgs args, object obj) { - var builder = (AppBuilderBase)obj; + var builder = (AppBuilder)obj; if (args.Method == Methods.AvaloniaRemote) builder.UseWindowingSubsystem(() => PreviewerWindowingPlatform.Initialize(transport)); if (args.Method == Methods.Html) @@ -191,7 +191,7 @@ namespace Avalonia.DesignerSupport.Remote Log($"Obtaining AppBuilder instance from {builderMethod.DeclaringType.FullName}.{builderMethod.Name}"); var appBuilder = builderMethod.Invoke(null, null); Log($"Initializing application in design mode"); - var initializer =(IAppInitializer)Activator.CreateInstance(typeof(AppInitializer<>).MakeGenericType(appBuilder.GetType())); + var initializer =(IAppInitializer)Activator.CreateInstance(typeof(AppInitializer)); transport = initializer.ConfigureApp(transport, args, appBuilder); s_transport = transport; transport.OnMessage += OnTransportMessage; diff --git a/src/Avalonia.Desktop/AppBuilderDesktopExtensions.cs b/src/Avalonia.Desktop/AppBuilderDesktopExtensions.cs index 7c4a489328..195af7b46e 100644 --- a/src/Avalonia.Desktop/AppBuilderDesktopExtensions.cs +++ b/src/Avalonia.Desktop/AppBuilderDesktopExtensions.cs @@ -5,8 +5,7 @@ namespace Avalonia { public static class AppBuilderDesktopExtensions { - public static TAppBuilder UsePlatformDetect(this TAppBuilder builder) - where TAppBuilder : AppBuilderBase, new() + public static AppBuilder UsePlatformDetect(this AppBuilder builder) { var os = builder.RuntimePlatform.GetRuntimeInfo().OperatingSystem; @@ -35,19 +34,15 @@ namespace Avalonia return builder; } - static void LoadAvaloniaNative(TAppBuilder builder) - where TAppBuilder : AppBuilderBase, new() + static void LoadAvaloniaNative(AppBuilder builder) => builder.UseAvaloniaNative(); - static void LoadWin32(TAppBuilder builder) - where TAppBuilder : AppBuilderBase, new() + static void LoadWin32(AppBuilder builder) => builder.UseWin32(); - static void LoadX11(TAppBuilder builder) - where TAppBuilder : AppBuilderBase, new() + static void LoadX11(AppBuilder builder) => builder.UseX11(); - static void LoadSkia(TAppBuilder builder) - where TAppBuilder : AppBuilderBase, new() + static void LoadSkia(AppBuilder builder) => builder.UseSkia(); } } diff --git a/src/Avalonia.Dialogs/ManagedFileDialogExtensions.cs b/src/Avalonia.Dialogs/ManagedFileDialogExtensions.cs index fe2dd99100..bc5945441c 100644 --- a/src/Avalonia.Dialogs/ManagedFileDialogExtensions.cs +++ b/src/Avalonia.Dialogs/ManagedFileDialogExtensions.cs @@ -24,16 +24,15 @@ namespace Avalonia.Dialogs } } - public static TAppBuilder UseManagedSystemDialogs(this TAppBuilder builder) - where TAppBuilder : AppBuilderBase, new() + public static AppBuilder UseManagedSystemDialogs(this AppBuilder builder) { builder.AfterSetup(_ => AvaloniaLocator.CurrentMutable.Bind().ToSingleton>()); return builder; } - public static TAppBuilder UseManagedSystemDialogs(this TAppBuilder builder) - where TAppBuilder : AppBuilderBase, new() where TWindow : Window, new() + public static AppBuilder UseManagedSystemDialogs(this AppBuilder builder) + where TWindow : Window, new() { builder.AfterSetup(_ => AvaloniaLocator.CurrentMutable.Bind().ToSingleton>()); diff --git a/src/Avalonia.Headless.Vnc/HeadlessVncPlatformExtensions.cs b/src/Avalonia.Headless.Vnc/HeadlessVncPlatformExtensions.cs index cc7d5ef30d..b6bb69b05d 100644 --- a/src/Avalonia.Headless.Vnc/HeadlessVncPlatformExtensions.cs +++ b/src/Avalonia.Headless.Vnc/HeadlessVncPlatformExtensions.cs @@ -11,11 +11,10 @@ namespace Avalonia { public static class HeadlessVncPlatformExtensions { - public static int StartWithHeadlessVncPlatform( - this T builder, + public static int StartWithHeadlessVncPlatform( + this AppBuilder builder, string host, int port, string[] args, ShutdownMode shutdownMode = ShutdownMode.OnLastWindowClose) - where T : AppBuilderBase, new() { var tcpServer = new TcpListener(host == null ? IPAddress.Loopback : IPAddress.Parse(host), port); tcpServer.Start(); diff --git a/src/Avalonia.Headless/AvaloniaHeadlessPlatform.cs b/src/Avalonia.Headless/AvaloniaHeadlessPlatform.cs index 8da10fa59b..e7cba13ce0 100644 --- a/src/Avalonia.Headless/AvaloniaHeadlessPlatform.cs +++ b/src/Avalonia.Headless/AvaloniaHeadlessPlatform.cs @@ -94,8 +94,7 @@ namespace Avalonia.Headless public static class AvaloniaHeadlessPlatformExtensions { - public static T UseHeadless(this T builder, AvaloniaHeadlessPlatformOptions opts) - where T : AppBuilderBase, new() + public static AppBuilder UseHeadless(this AppBuilder builder, AvaloniaHeadlessPlatformOptions opts) { if(opts.UseHeadlessDrawing) builder.UseRenderingSubsystem(HeadlessPlatformRenderInterface.Initialize, "Headless"); diff --git a/src/Avalonia.Native/AvaloniaNativePlatformExtensions.cs b/src/Avalonia.Native/AvaloniaNativePlatformExtensions.cs index 189f45d7c8..6613fc09be 100644 --- a/src/Avalonia.Native/AvaloniaNativePlatformExtensions.cs +++ b/src/Avalonia.Native/AvaloniaNativePlatformExtensions.cs @@ -6,8 +6,7 @@ namespace Avalonia { public static class AvaloniaNativePlatformExtensions { - public static T UseAvaloniaNative(this T builder) - where T : AppBuilderBase, new() + public static AppBuilder UseAvaloniaNative(this AppBuilder builder) { builder.UseWindowingSubsystem(() => { diff --git a/src/Avalonia.ReactiveUI/AppBuilderExtensions.cs b/src/Avalonia.ReactiveUI/AppBuilderExtensions.cs index 359da3d7c2..3fde580160 100644 --- a/src/Avalonia.ReactiveUI/AppBuilderExtensions.cs +++ b/src/Avalonia.ReactiveUI/AppBuilderExtensions.cs @@ -12,8 +12,7 @@ namespace Avalonia.ReactiveUI /// scheduler, an activation for view fetcher, a template binding hook. Remember /// to call this method if you are using ReactiveUI in your application. /// - public static TAppBuilder UseReactiveUI(this TAppBuilder builder) - where TAppBuilder : AppBuilderBase, new() => + public static AppBuilder UseReactiveUI(this AppBuilder builder) => builder.AfterPlatformServicesSetup(_ => Locator.RegisterResolverCallbackChanged(() => { if (Locator.CurrentMutable is null) diff --git a/src/Avalonia.X11/X11Platform.cs b/src/Avalonia.X11/X11Platform.cs index e44b5ded14..9692e0a384 100644 --- a/src/Avalonia.X11/X11Platform.cs +++ b/src/Avalonia.X11/X11Platform.cs @@ -303,7 +303,7 @@ namespace Avalonia } public static class AvaloniaX11PlatformExtensions { - public static T UseX11(this T builder) where T : AppBuilderBase, new() + public static AppBuilder UseX11(this AppBuilder builder) { builder.UseWindowingSubsystem(() => new AvaloniaX11Platform().Initialize(AvaloniaLocator.Current.GetService() ?? diff --git a/src/Browser/Avalonia.Browser.Blazor/BlazorSingleViewLifetime.cs b/src/Browser/Avalonia.Browser.Blazor/BlazorSingleViewLifetime.cs index 2432dc29a3..5e101f7f17 100644 --- a/src/Browser/Avalonia.Browser.Blazor/BlazorSingleViewLifetime.cs +++ b/src/Browser/Avalonia.Browser.Blazor/BlazorSingleViewLifetime.cs @@ -8,14 +8,13 @@ namespace Avalonia.Browser.Blazor; [SupportedOSPlatform("browser")] public static class WebAppBuilder { - public static T SetupWithSingleViewLifetime( - this T builder) - where T : AppBuilderBase, new() + public static AppBuilder SetupWithSingleViewLifetime( + this AppBuilder builder) { return builder.SetupWithLifetime(new BlazorSingleViewLifetime()); } - public static T UseBlazor(this T builder) where T : AppBuilderBase, new() + public static AppBuilder UseBlazor(this AppBuilder builder) { return builder .UseBrowser() diff --git a/src/Browser/Avalonia.Browser/BrowserSingleViewLifetime.cs b/src/Browser/Avalonia.Browser/BrowserSingleViewLifetime.cs index b6c766b2a5..3adcb8e539 100644 --- a/src/Browser/Avalonia.Browser/BrowserSingleViewLifetime.cs +++ b/src/Browser/Avalonia.Browser/BrowserSingleViewLifetime.cs @@ -2,8 +2,6 @@ using Avalonia.Controls; using Avalonia.Controls.ApplicationLifetimes; using System.Runtime.Versioning; -using Avalonia.Browser.Skia; -using Avalonia.Platform; namespace Avalonia.Browser; @@ -27,9 +25,8 @@ public class BrowserPlatformOptions [SupportedOSPlatform("browser")] public static class WebAppBuilder { - public static T SetupBrowserApp( - this T builder, string mainDivId) - where T : AppBuilderBase, new() + public static AppBuilder SetupBrowserApp( + this AppBuilder builder, string mainDivId) { var lifetime = new BrowserSingleViewLifetime(); @@ -42,9 +39,8 @@ public static class WebAppBuilder .SetupWithLifetime(lifetime); } - public static T UseBrowser( - this T builder) - where T : AppBuilderBase, new() + public static AppBuilder UseBrowser( + this AppBuilder builder) { return builder .UseWindowingSubsystem(BrowserWindowingPlatform.Register) diff --git a/src/Linux/Avalonia.LinuxFramebuffer/LinuxFramebufferPlatform.cs b/src/Linux/Avalonia.LinuxFramebuffer/LinuxFramebufferPlatform.cs index e2e9ec8fb3..4202ba821f 100644 --- a/src/Linux/Avalonia.LinuxFramebuffer/LinuxFramebufferPlatform.cs +++ b/src/Linux/Avalonia.LinuxFramebuffer/LinuxFramebufferPlatform.cs @@ -1,6 +1,7 @@ using System; using System.Diagnostics; using System.Threading; +using Avalonia; using Avalonia.Controls; using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Controls.Embedding; @@ -12,11 +13,9 @@ using Avalonia.LinuxFramebuffer.Input; using Avalonia.LinuxFramebuffer.Input.EvDev; using Avalonia.LinuxFramebuffer.Input.LibInput; using Avalonia.LinuxFramebuffer.Output; -using Avalonia.OpenGL; using Avalonia.Platform; using Avalonia.Rendering; using Avalonia.Rendering.Composition; -using Avalonia.Threading; #nullable enable namespace Avalonia.LinuxFramebuffer @@ -60,7 +59,7 @@ namespace Avalonia.LinuxFramebuffer } - internal static LinuxFramebufferLifetime Initialize(T builder, IOutputBackend outputBackend, IInputBackend? inputBackend) where T : AppBuilderBase, new() + internal static LinuxFramebufferLifetime Initialize(AppBuilder builder, IOutputBackend outputBackend, IInputBackend? inputBackend) { var platform = new LinuxFramebufferPlatform(outputBackend); builder.UseSkia().UseWindowingSubsystem(platform.Initialize, "fbdev"); @@ -140,20 +139,17 @@ namespace Avalonia.LinuxFramebuffer public static class LinuxFramebufferPlatformExtensions { - public static int StartLinuxFbDev(this T builder, string[] args, string? fbdev = null, double scaling = 1, IInputBackend? inputBackend = default) - where T : AppBuilderBase, new() => - StartLinuxDirect(builder, args, new FbdevOutput(fileName: fbdev, format: null) { Scaling = scaling }, inputBackend); - public static int StartLinuxFbDev(this T builder, string[] args, string fbdev, PixelFormat? format, double scaling, IInputBackend? inputBackend = default) - where T : AppBuilderBase, new() => - StartLinuxDirect(builder, args, new FbdevOutput(fileName: fbdev, format: format) { Scaling = scaling }, inputBackend); - - public static int StartLinuxDrm(this T builder, string[] args, string? card = null, double scaling = 1, IInputBackend? inputBackend = default) - where T : AppBuilderBase, new() => StartLinuxDirect(builder, args, new DrmOutput(card) { Scaling = scaling }, inputBackend); - public static int StartLinuxDrm(this T builder, string[] args, string? card = null, bool connectorsForceProbe = false, DrmOutputOptions? options = null, IInputBackend? inputBackend = default) - where T : AppBuilderBase, new() => StartLinuxDirect(builder, args, new DrmOutput(card, connectorsForceProbe, options), inputBackend); - - public static int StartLinuxDirect(this T builder, string[] args, IOutputBackend outputBackend, IInputBackend? inputBackend = default) - where T : AppBuilderBase, new() + public static int StartLinuxFbDev(this AppBuilder builder, string[] args, string? fbdev = null, double scaling = 1, IInputBackend? inputBackend = default) + => StartLinuxDirect(builder, args, new FbdevOutput(fileName: fbdev, format: null) { Scaling = scaling }, inputBackend); + public static int StartLinuxFbDev(this AppBuilder builder, string[] args, string fbdev, PixelFormat? format, double scaling, IInputBackend? inputBackend = default) + => StartLinuxDirect(builder, args, new FbdevOutput(fileName: fbdev, format: format) { Scaling = scaling }, inputBackend); + + public static int StartLinuxDrm(this AppBuilder builder, string[] args, string? card = null, double scaling = 1, IInputBackend? inputBackend = default) + => StartLinuxDirect(builder, args, new DrmOutput(card) { Scaling = scaling }, inputBackend); + public static int StartLinuxDrm(this AppBuilder builder, string[] args, string? card = null, bool connectorsForceProbe = false, DrmOutputOptions? options = null, IInputBackend? inputBackend = default) + => StartLinuxDirect(builder, args, new DrmOutput(card, connectorsForceProbe, options), inputBackend); + + public static int StartLinuxDirect(this AppBuilder builder, string[] args, IOutputBackend outputBackend, IInputBackend? inputBackend = default) { var lifetime = LinuxFramebufferPlatform.Initialize(builder, outputBackend, inputBackend); builder.SetupWithLifetime(lifetime); diff --git a/src/Skia/Avalonia.Skia/SkiaApplicationExtensions.cs b/src/Skia/Avalonia.Skia/SkiaApplicationExtensions.cs index 014298ce83..3e4fd7b385 100644 --- a/src/Skia/Avalonia.Skia/SkiaApplicationExtensions.cs +++ b/src/Skia/Avalonia.Skia/SkiaApplicationExtensions.cs @@ -12,10 +12,9 @@ namespace Avalonia /// /// Enable Skia renderer. /// - /// Builder type. /// Builder. /// Configure builder. - public static T UseSkia(this T builder) where T : AppBuilderBase, new() + public static AppBuilder UseSkia(this AppBuilder builder) { return builder.UseRenderingSubsystem(() => SkiaPlatform.Initialize( AvaloniaLocator.Current.GetService() ?? new SkiaOptions()), diff --git a/src/Windows/Avalonia.Direct2D1/Direct2D1Platform.cs b/src/Windows/Avalonia.Direct2D1/Direct2D1Platform.cs index a5f77230b7..5887ba2172 100644 --- a/src/Windows/Avalonia.Direct2D1/Direct2D1Platform.cs +++ b/src/Windows/Avalonia.Direct2D1/Direct2D1Platform.cs @@ -16,7 +16,7 @@ namespace Avalonia { public static class Direct2DApplicationExtensions { - public static T UseDirect2D1(this T builder) where T : AppBuilderBase, new() + public static AppBuilder UseDirect2D1(this AppBuilder builder) { builder.UseRenderingSubsystem(Direct2D1.Direct2D1Platform.Initialize, "Direct2D1"); return builder; diff --git a/src/Windows/Avalonia.Win32/Win32Platform.cs b/src/Windows/Avalonia.Win32/Win32Platform.cs index 3f16b49772..93d16b5768 100644 --- a/src/Windows/Avalonia.Win32/Win32Platform.cs +++ b/src/Windows/Avalonia.Win32/Win32Platform.cs @@ -26,9 +26,7 @@ namespace Avalonia #nullable enable public static class Win32ApplicationExtensions { - public static T UseWin32( - this T builder) - where T : AppBuilderBase, new() + public static AppBuilder UseWin32(this AppBuilder builder) { return builder.UseWindowingSubsystem( () => Win32.Win32Platform.Initialize( diff --git a/src/iOS/Avalonia.iOS/Platform.cs b/src/iOS/Avalonia.iOS/Platform.cs index eb0a55734a..63025f7f0a 100644 --- a/src/iOS/Avalonia.iOS/Platform.cs +++ b/src/iOS/Avalonia.iOS/Platform.cs @@ -12,7 +12,7 @@ namespace Avalonia { public static class IOSApplicationExtensions { - public static T UseiOS(this T builder) where T : AppBuilderBase, new() + public static AppBuilder UseiOS(this AppBuilder builder) { return builder .UseWindowingSubsystem(iOS.Platform.Register, "iOS") diff --git a/tests/Avalonia.Base.UnitTests/Media/Fonts/FontFamilyLoaderTests.cs b/tests/Avalonia.Base.UnitTests/Media/Fonts/FontFamilyLoaderTests.cs index 1bb2300724..aa042ffec8 100644 --- a/tests/Avalonia.Base.UnitTests/Media/Fonts/FontFamilyLoaderTests.cs +++ b/tests/Avalonia.Base.UnitTests/Media/Fonts/FontFamilyLoaderTests.cs @@ -117,7 +117,7 @@ namespace Avalonia.Base.UnitTests.Media.Fonts private static IDisposable StartWithResources(params (string, string)[] assets) { var assetLoader = new MockAssetLoader(assets); - var services = new TestServices(assetLoader: assetLoader, platform: new AppBuilder().RuntimePlatform); + var services = new TestServices(assetLoader: assetLoader, platform: new StandardRuntimePlatform()); return UnitTestApplication.Start(services); } } diff --git a/tests/Avalonia.UnitTests/TestServices.cs b/tests/Avalonia.UnitTests/TestServices.cs index c421adaf21..8f132433ec 100644 --- a/tests/Avalonia.UnitTests/TestServices.cs +++ b/tests/Avalonia.UnitTests/TestServices.cs @@ -20,7 +20,7 @@ namespace Avalonia.UnitTests { public static readonly TestServices StyledWindow = new TestServices( assetLoader: new AssetLoader(), - platform: new AppBuilder().RuntimePlatform, + platform: new StandardRuntimePlatform(), renderInterface: new MockPlatformRenderInterface(), standardCursorFactory: Mock.Of(), theme: () => CreateSimpleTheme(), @@ -169,16 +169,4 @@ namespace Avalonia.UnitTests y => y.Open() == Mock.Of())); } } - - public class AppBuilder : AppBuilderBase - { - public AppBuilder() - : base(new StandardRuntimePlatform(), - builder => StandardRuntimePlatformServices.Register(builder.Instance?.GetType() - ?.GetTypeInfo().Assembly)) - { - } - - protected override bool CheckSetup => false; - } }