1
1
using System;
2
2
using System.Collections.Concurrent;
3
- using System.ComponentModel;
4
3
using System.IO;
5
4
using System.Linq;
6
5
using System.Windows;
11
10
12
11
namespace Flow.Launcher.Plugin.OneNote.Icons
13
12
{
14
- public class IconProvider : BaseModel, IDisposable
13
+ public class IconProvider : BaseModel
15
14
{
16
15
public const string Logo = IC.ImagesDirectory + IC.Logo;
17
16
public string Sync => GetIconLocal(IC.Sync);
@@ -36,8 +35,6 @@ public class IconProvider : BaseModel, IDisposable
36
35
public int CachedIconCount => iconCache.Keys.Count(k => char.IsDigit(k.Split('.')[1][1]));
37
36
38
37
private readonly PluginInitContext context;
39
-
40
- private readonly WindowsThemeWatcher windowsThemeWatcher = new ();
41
38
42
39
public IconProvider(PluginInitContext context, Settings settings)
43
40
{
@@ -48,44 +45,35 @@ public IconProvider(PluginInitContext context, Settings settings)
48
45
this.context = context;
49
46
this.settings = settings;
50
47
51
- if (settings.IconTheme == IconTheme.System)
52
- {
53
- windowsThemeWatcher.StartWatching();
54
- }
55
-
56
- settings.PropertyChanged += OnIconThemeChanged;
57
-
58
48
foreach (var image in GeneratedImagesDirectoryInfo.EnumerateFiles())
59
49
{
60
50
iconCache.TryAdd(image.Name, BitmapImageFromPath(image.FullName));
61
51
}
62
52
}
63
-
64
- private void OnIconThemeChanged(object sender, PropertyChangedEventArgs args)
65
- {
66
- if (args.PropertyName != nameof(Settings.IconTheme))
67
- return;
68
-
69
- if (settings.IconTheme == IconTheme.System)
70
- {
71
- windowsThemeWatcher.StartWatching();
72
- }
73
- else
74
- {
75
- windowsThemeWatcher.StopWatching();
76
- }
77
- }
78
-
53
+
79
54
private string GetIconLocal(string icon) => $"{IC.ImagesDirectory}{icon}.{GetIconThemeString(settings.IconTheme)}.png";
80
55
81
56
private string GetIconThemeString(IconTheme iconTheme)
82
57
{
83
58
if (iconTheme == IconTheme.System)
84
59
{
85
- iconTheme = windowsThemeWatcher.CurrentWindowsTheme.ToIconTheme ();
60
+ iconTheme = FlowLauncherThemeToIconTheme ();
86
61
}
87
62
return Enum.GetName(iconTheme).ToLower();
88
63
}
64
+
65
+ private static IconTheme FlowLauncherThemeToIconTheme()
66
+ {
67
+ var color05B = (SolidColorBrush)Application.Current.TryFindResource("Color05B"); //Alt key "SystemControlPageTextBaseHighBrush"
68
+ if (color05B == null)
69
+ return IconTheme.Light;
70
+
71
+ var color = color05B.Color;
72
+ return 5 * color.G + 2 * color.R + color.B > 8 * 128 //Is the color light?
73
+ ? IconTheme.Light
74
+ : IconTheme.Dark;
75
+ }
76
+
89
77
private static BitmapImage BitmapImageFromPath(string path) => new BitmapImage(new Uri(path));
90
78
91
79
public Result.IconDelegate GetIcon(IconGeneratorInfo info)
@@ -177,11 +165,5 @@ public void ClearCachedIcons()
177
165
}
178
166
OnPropertyChanged(nameof(CachedIconCount));
179
167
}
180
-
181
- public void Dispose()
182
- {
183
- settings.PropertyChanged -= OnIconThemeChanged;
184
- windowsThemeWatcher.Dispose();
185
- }
186
168
}
187
169
}
0 commit comments