|
17 | 17 | using Flow.Launcher.Plugin;
|
18 | 18 | using Microsoft.Win32;
|
19 | 19 | using TextBox = System.Windows.Controls.TextBox;
|
| 20 | +using System.Diagnostics; |
20 | 21 |
|
21 | 22 | namespace Flow.Launcher.Core.Resource
|
22 | 23 | {
|
@@ -56,19 +57,24 @@ public Theme(IPublicAPI publicAPI, Settings settings)
|
56 | 57 | MakeSureThemeDirectoriesExist();
|
57 | 58 |
|
58 | 59 | var dicts = Application.Current.Resources.MergedDictionaries;
|
59 |
| - _oldResource = dicts.First(d => |
| 60 | + _oldResource = dicts.FirstOrDefault(d => |
60 | 61 | {
|
61 | 62 | if (d.Source == null)
|
62 | 63 | return false;
|
63 | 64 |
|
64 | 65 | 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; |
71 | 67 | });
|
| 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 | + }; |
72 | 78 | _oldTheme = Path.GetFileNameWithoutExtension(_oldResource.Source.AbsolutePath);
|
73 | 79 | }
|
74 | 80 |
|
@@ -98,11 +104,24 @@ private void MakeSureThemeDirectoriesExist()
|
98 | 104 |
|
99 | 105 | private void UpdateResourceDictionary(ResourceDictionary dictionaryToUpdate)
|
100 | 106 | {
|
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 | + |
105 | 121 | _oldResource = dictionaryToUpdate;
|
| 122 | + |
| 123 | + // 리소스 변경 후 문제가 없는지 검증 |
| 124 | + Debug.WriteLine($"테마 변경 후 리소스 딕셔너리 수: {Application.Current.Resources.MergedDictionaries.Count}"); |
106 | 125 | }
|
107 | 126 |
|
108 | 127 | private ResourceDictionary GetThemeResourceDictionary(string theme)
|
|
0 commit comments