From c73a6c86c0d0fba2b969a81dd9459ce0d0d02125 Mon Sep 17 00:00:00 2001 From: odalet Date: Tue, 4 Jan 2022 17:35:59 +0100 Subject: [PATCH] Fixes #7309 - Each time we retrieve a null *PlatformOptions from AvaloniaLocator, return a default instance --- src/Avalonia.Native/AvaloniaNativeMenuExporter.cs | 8 ++++---- src/Avalonia.Native/AvaloniaNativePlatform.cs | 4 ++-- src/Avalonia.X11/Glx/GlxDisplay.cs | 4 ++-- .../LinuxFramebufferPlatform.cs | 4 ++-- src/Windows/Avalonia.Win32/Win32GlManager.cs | 9 ++++----- 5 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/Avalonia.Native/AvaloniaNativeMenuExporter.cs b/src/Avalonia.Native/AvaloniaNativeMenuExporter.cs index 57a87012020..40ffc317284 100644 --- a/src/Avalonia.Native/AvaloniaNativeMenuExporter.cs +++ b/src/Avalonia.Native/AvaloniaNativeMenuExporter.cs @@ -80,8 +80,8 @@ private NativeMenu CreateDefaultAppMenu() }; result.Add(aboutItem); - var macOpts = AvaloniaLocator.Current.GetService(); - if (macOpts == null || !macOpts.DisableDefaultApplicationMenuItems) + var macOpts = AvaloniaLocator.Current.GetService() ?? new MacOSPlatformOptions(); + if (!macOpts.DisableDefaultApplicationMenuItems) { result.Add(new NativeMenuItemSeparator()); @@ -142,9 +142,9 @@ private NativeMenu CreateDefaultAppMenu() private void DoLayoutReset(bool forceUpdate = false) { - var macOpts = AvaloniaLocator.Current.GetService(); + var macOpts = AvaloniaLocator.Current.GetService() ?? new MacOSPlatformOptions(); - if (macOpts != null && macOpts.DisableNativeMenus) + if (macOpts.DisableNativeMenus) { return; } diff --git a/src/Avalonia.Native/AvaloniaNativePlatform.cs b/src/Avalonia.Native/AvaloniaNativePlatform.cs index 5fa50f0e7f6..1eadf70b13a 100644 --- a/src/Avalonia.Native/AvaloniaNativePlatform.cs +++ b/src/Avalonia.Native/AvaloniaNativePlatform.cs @@ -96,9 +96,9 @@ void DoInitialize(AvaloniaNativePlatformOptions options) _factory.Initialize(new GCHandleDeallocator(), applicationPlatform); if (_factory.MacOptions != null) { - var macOpts = AvaloniaLocator.Current.GetService(); + var macOpts = AvaloniaLocator.Current.GetService() ?? new MacOSPlatformOptions(); - _factory.MacOptions.SetShowInDock(macOpts?.ShowInDock != false ? 1 : 0); + _factory.MacOptions.SetShowInDock(macOpts.ShowInDock ? 1 : 0); } AvaloniaLocator.CurrentMutable diff --git a/src/Avalonia.X11/Glx/GlxDisplay.cs b/src/Avalonia.X11/Glx/GlxDisplay.cs index fa8c866c092..fcdc10e9991 100644 --- a/src/Avalonia.X11/Glx/GlxDisplay.cs +++ b/src/Avalonia.X11/Glx/GlxDisplay.cs @@ -95,8 +95,8 @@ public GlxDisplay(X11Info x11, IList probeProfiles) if (Environment.GetEnvironmentVariable("AVALONIA_GLX_IGNORE_RENDERER_BLACKLIST") != "1") { - var blacklist = AvaloniaLocator.Current.GetService() - ?.GlxRendererBlacklist; + var opts = AvaloniaLocator.Current.GetService() ?? new X11PlatformOptions(); + var blacklist = opts.GlxRendererBlacklist; if (blacklist != null) foreach (var item in blacklist) if (glInterface.Renderer.Contains(item)) diff --git a/src/Linux/Avalonia.LinuxFramebuffer/LinuxFramebufferPlatform.cs b/src/Linux/Avalonia.LinuxFramebuffer/LinuxFramebufferPlatform.cs index f4db6bf48a1..4add4c423b0 100644 --- a/src/Linux/Avalonia.LinuxFramebuffer/LinuxFramebufferPlatform.cs +++ b/src/Linux/Avalonia.LinuxFramebuffer/LinuxFramebufferPlatform.cs @@ -38,11 +38,11 @@ void Initialize() if (_fb is IGlOutputBackend gl) AvaloniaLocator.CurrentMutable.Bind().ToConstant(gl.PlatformOpenGlInterface); - var opts = AvaloniaLocator.Current.GetService(); + var opts = AvaloniaLocator.Current.GetService() ?? new LinuxFramebufferPlatformOptions(); AvaloniaLocator.CurrentMutable .Bind().ToConstant(Threading) - .Bind().ToConstant(new DefaultRenderTimer(opts?.Fps ?? 60)) + .Bind().ToConstant(new DefaultRenderTimer(opts.Fps)) .Bind().ToConstant(new RenderLoop()) .Bind().ToTransient() .Bind().ToConstant(new KeyboardDevice()) diff --git a/src/Windows/Avalonia.Win32/Win32GlManager.cs b/src/Windows/Avalonia.Win32/Win32GlManager.cs index 289c100d51a..0376a41f8cd 100644 --- a/src/Windows/Avalonia.Win32/Win32GlManager.cs +++ b/src/Windows/Avalonia.Win32/Win32GlManager.cs @@ -13,19 +13,18 @@ public static void Initialize() { AvaloniaLocator.CurrentMutable.Bind().ToLazy(() => { - var opts = AvaloniaLocator.Current.GetService(); - if (opts?.UseWgl == true) + var opts = AvaloniaLocator.Current.GetService() ?? new Win32PlatformOptions(); + if (opts.UseWgl) { var wgl = WglPlatformOpenGlInterface.TryCreate(); return wgl; } - if (opts?.AllowEglInitialization ?? Win32Platform.WindowsVersion > PlatformConstants.Windows7) + if (opts.AllowEglInitialization ?? Win32Platform.WindowsVersion > PlatformConstants.Windows7) { var egl = EglPlatformOpenGlInterface.TryCreate(() => new AngleWin32EglDisplay()); - if (egl != null && - opts?.UseWindowsUIComposition == true) + if (egl != null && opts.UseWindowsUIComposition) { WinUICompositorConnection.TryCreateAndRegister(egl, opts.CompositionBackdropCornerRadius); }