Skip to content

Commit a83da2b

Browse files
committed
Improve Theme resource handling and suppress async warnings
Add try-catch to UpdateResourceDictionary to prevent crashes from InvalidCastException when updating resources. Set _oldResource to null on error. Suppress VSTHRD103 warnings around SetBlurForWindow to clarify intentional async usage and avoid build warnings.
1 parent 7b85574 commit a83da2b

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

Flow.Launcher.Core/Resource/Theme.cs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,20 +99,27 @@ private void MakeSureThemeDirectoriesExist()
9999

100100
private void UpdateResourceDictionary(ResourceDictionary dictionaryToUpdate)
101101
{
102-
// Add new resources
103-
if (!Application.Current.Resources.MergedDictionaries.Contains(dictionaryToUpdate))
104-
{
105-
Application.Current.Resources.MergedDictionaries.Add(dictionaryToUpdate);
106-
}
107-
108102
// Remove old resources
109103
if (_oldResource != null && _oldResource != dictionaryToUpdate &&
110104
Application.Current.Resources.MergedDictionaries.Contains(_oldResource))
111105
{
112106
Application.Current.Resources.MergedDictionaries.Remove(_oldResource);
113107
}
114108

115-
_oldResource = dictionaryToUpdate;
109+
// Add new resources
110+
try
111+
{
112+
if (!Application.Current.Resources.MergedDictionaries.Contains(dictionaryToUpdate))
113+
{
114+
Application.Current.Resources.MergedDictionaries.Add(dictionaryToUpdate);
115+
}
116+
_oldResource = dictionaryToUpdate;
117+
}
118+
catch (InvalidCastException)
119+
{
120+
// System.InvalidCastException: Unable to cast object of type 'System.Windows.Media.Color' to type 'System.Windows.Expression'.
121+
_oldResource = null;
122+
}
116123
}
117124

118125
/// <summary>
@@ -654,7 +661,9 @@ public async Task RefreshFrameAsync()
654661
{
655662
AutoDropShadow(useDropShadowEffect);
656663
}
664+
#pragma warning disable VSTHRD103 // Call async methods when in an async method
657665
SetBlurForWindow(_settings.Theme, backdropType);
666+
#pragma warning restore VSTHRD103 // Call async methods when in an async method
658667

659668
if (!BlurEnabled)
660669
{

0 commit comments

Comments
 (0)