Skip to content

Commit ec22f59

Browse files
committed
Fix Non Resource situation when change theme
1 parent dda008f commit ec22f59

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

Flow.Launcher.Core/Resource/Theme.cs

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using Flow.Launcher.Plugin;
1818
using Microsoft.Win32;
1919
using TextBox = System.Windows.Controls.TextBox;
20+
using System.Diagnostics;
2021

2122
namespace Flow.Launcher.Core.Resource
2223
{
@@ -56,19 +57,24 @@ public Theme(IPublicAPI publicAPI, Settings settings)
5657
MakeSureThemeDirectoriesExist();
5758

5859
var dicts = Application.Current.Resources.MergedDictionaries;
59-
_oldResource = dicts.First(d =>
60+
_oldResource = dicts.FirstOrDefault(d =>
6061
{
6162
if (d.Source == null)
6263
return false;
6364

6465
var p = d.Source.AbsolutePath;
65-
var dir = Path.GetDirectoryName(p).NonNull();
66-
var info = new DirectoryInfo(dir);
67-
var f = info.Name;
68-
var e = Path.GetExtension(p);
69-
var found = f == Folder && e == Extension;
70-
return found;
66+
return p.Contains(Folder) && Path.GetExtension(p) == Extension;
7167
});
68+
69+
if (_oldResource != null)
70+
{
71+
_oldTheme = Path.GetFileNameWithoutExtension(_oldResource.Source.AbsolutePath);
72+
}
73+
else
74+
{
75+
Log.Error("현재 테마 리소스를 찾을 수 없습니다. 기본 테마로 초기화합니다.");
76+
_oldTheme = Constant.DefaultTheme;
77+
};
7278
_oldTheme = Path.GetFileNameWithoutExtension(_oldResource.Source.AbsolutePath);
7379
}
7480

@@ -98,11 +104,24 @@ private void MakeSureThemeDirectoriesExist()
98104

99105
private void UpdateResourceDictionary(ResourceDictionary dictionaryToUpdate)
100106
{
101-
var dicts = Application.Current.Resources.MergedDictionaries;
102-
103-
dicts.Remove(_oldResource);
104-
dicts.Add(dictionaryToUpdate);
107+
// 새 테마 리소스를 먼저 추가하고
108+
if (!Application.Current.Resources.MergedDictionaries.Contains(dictionaryToUpdate))
109+
{
110+
Application.Current.Resources.MergedDictionaries.Add(dictionaryToUpdate);
111+
}
112+
113+
// 그 다음 이전 테마 리소스 제거
114+
if (_oldResource != null &&
115+
_oldResource != dictionaryToUpdate &&
116+
Application.Current.Resources.MergedDictionaries.Contains(_oldResource))
117+
{
118+
Application.Current.Resources.MergedDictionaries.Remove(_oldResource);
119+
}
120+
105121
_oldResource = dictionaryToUpdate;
122+
123+
// 리소스 변경 후 문제가 없는지 검증
124+
Debug.WriteLine($"테마 변경 후 리소스 딕셔너리 수: {Application.Current.Resources.MergedDictionaries.Count}");
106125
}
107126

108127
private ResourceDictionary GetThemeResourceDictionary(string theme)

0 commit comments

Comments
 (0)