Skip to content

Commit 33ef136

Browse files
authored
Fix dark theme rendering as pure black when Mica can't paint (#5130)
1 parent 098eeae commit 33ef136

2 files changed

Lines changed: 40 additions & 3 deletions

File tree

‎src/UniGetUI.Avalonia/Infrastructure/MicaWindowHelper.cs‎

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Avalonia.Controls.Primitives;
66
using Avalonia.Media;
77
using Avalonia.Platform;
8+
using UniGetUI.Avalonia.Assets.Styles;
89

910
namespace UniGetUI.Avalonia.Infrastructure;
1011

@@ -24,6 +25,7 @@ internal static class MicaWindowHelper
2425
private const int DWMSBT_TRANSIENTWINDOW = 3; // Acrylic — for transient surfaces (menus/flyouts); Mica won't paint on these
2526

2627
private static bool _acrylicPopupsHooked;
28+
private static bool _micaConfirmedUnavailable;
2729

2830
public static void Apply(Window window)
2931
{
@@ -71,6 +73,9 @@ public static void EnableAcrylicPopups()
7173

7274
private static void ApplyAcrylicToPopup(TopLevel root)
7375
{
76+
if (!IsMicaEnabled())
77+
return;
78+
7479
// Request acrylic (not Transparent): the Transparent level makes a layered window, and DWM
7580
// system backdrops never paint on those — so the popup ended up fully see-through with only
7681
// the presenter tint, unreadable when shown over the desktop (e.g. the tray menu). AcrylicBlur
@@ -89,15 +94,37 @@ private static void ApplyAcrylicToPopup(TopLevel root)
8994
}
9095

9196
/// <summary>
92-
/// True when the native Mica look should be used: Windows 11+ AND the user has
93-
/// "Transparency effects" enabled. When transparency is off we keep the solid look,
94-
/// since the Mica backdrop otherwise lingers (DWM keeps painting it).
97+
/// True when the native Mica look should be used: Windows 11+, "Transparency effects" on,
98+
/// GPU-accelerated (software rendering can't be transparent, so Mica shows as black — #5111),
99+
/// and no window has since found the backdrop didn't engage. Otherwise keep the solid look.
95100
/// </summary>
96101
public static bool IsMicaEnabled()
97102
=> OperatingSystem.IsWindows()
98103
&& Environment.OSVersion.Version.Build >= 22000
104+
&& !_micaConfirmedUnavailable
105+
&& !WindowsAvaloniaRenderingPolicy.ShouldUseSoftwareRendering
99106
&& IsOsTransparencyEnabled();
100107

108+
/// <summary>
109+
/// Latch Mica off for the session and drop the translucent surface overrides so every surface
110+
/// falls back to the solid look, once a window confirms the backdrop never engaged (#5111).
111+
/// </summary>
112+
public static void NotifyMicaUnavailable()
113+
{
114+
if (_micaConfirmedUnavailable)
115+
return;
116+
_micaConfirmedUnavailable = true;
117+
118+
if (Application.Current is { } app)
119+
{
120+
for (int i = app.Resources.MergedDictionaries.Count - 1; i >= 0; i--)
121+
{
122+
if (app.Resources.MergedDictionaries[i] is WindowsMicaStyles)
123+
app.Resources.MergedDictionaries.RemoveAt(i);
124+
}
125+
}
126+
}
127+
101128
private static bool IsOsTransparencyEnabled()
102129
{
103130
if (!OperatingSystem.IsWindows())

‎src/UniGetUI.Avalonia/Views/MainWindow.axaml.cs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,16 @@ private void SetupMicaAndAccentBorder()
851851
return;
852852
}
853853

854+
// Mica was requested; None means DWM granted no backdrop, so the transparent window would
855+
// render pure black (#5111). Latch Mica off and keep a solid background instead.
856+
if (ActualTransparencyLevel == WindowTransparencyLevel.None)
857+
{
858+
MicaWindowHelper.NotifyMicaUnavailable();
859+
if (this.TryFindResource("AppWindowBackground", ActualThemeVariant, out var solidBg) && solidBg is IBrush solidBrush)
860+
Background = solidBrush;
861+
return;
862+
}
863+
854864
// The custom NCCALCSIZE frame keeps WS_THICKFRAME, so DWM still has a frame to round.
855865
int corner = DWMWCP_ROUND;
856866
NativeMethods.DwmSetWindowAttribute(handle, DWMWA_WINDOW_CORNER_PREFERENCE, ref corner, sizeof(int));

0 commit comments

Comments
 (0)