From c87f3d3447a186e12450c9565f7c1f33d1e264ed Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Sun, 1 Mar 2026 14:05:36 +0800 Subject: [PATCH] Improve theme resource access robustness Refactored theme resource dictionary lookups to safely check for "WindowBorderStyle" before accessing, preventing potential exceptions. Updated three methods to handle missing styles gracefully and standardized resource key checks. GetWindowBorderStyleBackground now returns transparent if style is missing. --- Flow.Launcher.Core/Resource/Theme.cs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/Flow.Launcher.Core/Resource/Theme.cs b/Flow.Launcher.Core/Resource/Theme.cs index b7b137d91..1e891745d 100644 --- a/Flow.Launcher.Core/Resource/Theme.cs +++ b/Flow.Launcher.Core/Resource/Theme.cs @@ -526,7 +526,8 @@ namespace Flow.Launcher.Core.Resource // Get current theme's WindowBorderStyle var theme = _settings.Theme; var dict = GetThemeResourceDictionary(theme); - if (dict["WindowBorderStyle"] is not Style windowBorderStyle) return; + var windowBorderStyle = dict.Contains("WindowBorderStyle") ? dict["WindowBorderStyle"] as Style : null; + if (windowBorderStyle == null) return; // Get a new unsealed style based on the old one, and copy Resources and Triggers var newWindowBorderStyle = GetNewWindowBorderStyle(windowBorderStyle); @@ -594,7 +595,8 @@ namespace Flow.Launcher.Core.Resource // Get current theme's WindowBorderStyle var theme = _settings.Theme; var dict = GetThemeResourceDictionary(theme); - if (dict["WindowBorderStyle"] is not Style windowBorderStyle) return; + var windowBorderStyle = dict.Contains("WindowBorderStyle") ? dict["WindowBorderStyle"] as Style : null; + if (windowBorderStyle == null) return; // Get a new unsealed style based on the old one, and copy Resources and Triggers var newWindowBorderStyle = GetNewWindowBorderStyle(windowBorderStyle); @@ -855,8 +857,9 @@ namespace Flow.Launcher.Core.Resource // for theme has not "LightBG" or "DarkBG" case. private Color GetWindowBorderStyleBackground(string theme) { - var Resources = GetThemeResourceDictionary(theme); - var windowBorderStyle = (Style)Resources["WindowBorderStyle"]; + var dict = GetThemeResourceDictionary(theme); + var windowBorderStyle = dict.Contains("WindowBorderStyle") ? dict["WindowBorderStyle"] as Style : null; + if (windowBorderStyle == null) return Colors.Transparent; // Default is transparent var backgroundSetter = windowBorderStyle.Setters .OfType() @@ -877,9 +880,9 @@ namespace Flow.Launcher.Core.Resource var resourceKey = dynamicResource.ResourceKey.ToString(); // find key in resource and return color. - if (Resources.Contains(resourceKey)) + if (dict.Contains(resourceKey)) { - var colorResource = Resources[resourceKey]; + var colorResource = dict[resourceKey]; if (colorResource is SolidColorBrush colorBrush) { return colorBrush.Color;