mirror of
https://github.com/babalae/better-genshin-impact.git
synced 2026-05-09 00:34:14 +08:00
fix(ui): 处理桌面组合禁用时的主题应用异常 #2678
当系统桌面组合功能被禁用时,应用 Mica/Acrylic 主题会抛出 COM 异常(HRESULT: 0x80263001)。现通过异常捕获机制,在检测到此特定错误时回退到纯色背景方案,确保程序在受限环境下仍能正常运行。 - 在 ApplyThemeToWindow 方法中添加异常处理逻辑 - 引入 ApplyFallbackTheme 方法应用纯色后备主题 - 根据主题类型提供对应的后备背景色
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
using System.Diagnostics;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Windows.Media;
|
||||
using BetterGenshinImpact.Core.Config;
|
||||
using BetterGenshinImpact.GameTask;
|
||||
@@ -9,6 +11,8 @@ namespace BetterGenshinImpact.Helpers.Ui;
|
||||
|
||||
public class WindowHelper
|
||||
{
|
||||
private const uint DesktopCompositionDisabledHResult = 0x80263001;
|
||||
|
||||
public static void TryApplySystemBackdrop(System.Windows.Window window)
|
||||
{
|
||||
var themeType = TaskContext.Instance().Config.CommonConfig.CurrentThemeType;
|
||||
@@ -37,6 +41,22 @@ public class WindowHelper
|
||||
/// <param name="window">要应用主题的窗口</param>
|
||||
/// <param name="themeType">主题类型</param>
|
||||
public static void ApplyThemeToWindow(System.Windows.Window window, ThemeType themeType)
|
||||
{
|
||||
try
|
||||
{
|
||||
ApplyThemeCore(window, themeType);
|
||||
}
|
||||
catch (COMException ex) when ((uint)ex.HResult == DesktopCompositionDisabledHResult)
|
||||
{
|
||||
ApplyFallbackTheme(window, themeType);
|
||||
}
|
||||
catch
|
||||
{
|
||||
ApplyFallbackTheme(window, themeType);
|
||||
}
|
||||
}
|
||||
|
||||
private static void ApplyThemeCore(System.Windows.Window window, ThemeType themeType)
|
||||
{
|
||||
switch (themeType)
|
||||
{
|
||||
@@ -76,4 +96,21 @@ public class WindowHelper
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void ApplyFallbackTheme(System.Windows.Window window, ThemeType themeType)
|
||||
{
|
||||
window.Background = new SolidColorBrush(GetFallbackBackgroundColor(themeType));
|
||||
WindowBackdrop.ApplyBackdrop(window, WindowBackdropType.None);
|
||||
}
|
||||
|
||||
private static Color GetFallbackBackgroundColor(ThemeType themeType)
|
||||
{
|
||||
return themeType switch
|
||||
{
|
||||
ThemeType.LightNone => Color.FromArgb(255, 243, 243, 243),
|
||||
ThemeType.LightMica => Color.FromArgb(255, 243, 243, 243),
|
||||
ThemeType.LightAcrylic => Color.FromArgb(255, 243, 243, 243),
|
||||
_ => Color.FromArgb(255, 32, 32, 32)
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user