Skip to content

Commit 8892d5c

Browse files
committed
Simplify Windows theme watcher to easy resource check
Much simpler, but probs not faster.
1 parent 98d3289 commit 8892d5c

File tree

4 files changed

+16
-132
lines changed

4 files changed

+16
-132
lines changed

Flow.Launcher.Plugin.OneNote/Icons/IconProvider.cs

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Collections.Concurrent;
3-
using System.ComponentModel;
43
using System.IO;
54
using System.Linq;
65
using System.Windows;
@@ -11,7 +10,7 @@
1110

1211
namespace Flow.Launcher.Plugin.OneNote.Icons
1312
{
14-
public class IconProvider : BaseModel, IDisposable
13+
public class IconProvider : BaseModel
1514
{
1615
public const string Logo = IC.ImagesDirectory + IC.Logo;
1716
public string Sync => GetIconLocal(IC.Sync);
@@ -36,8 +35,6 @@ public class IconProvider : BaseModel, IDisposable
3635
public int CachedIconCount => iconCache.Keys.Count(k => char.IsDigit(k.Split('.')[1][1]));
3736

3837
private readonly PluginInitContext context;
39-
40-
private readonly WindowsThemeWatcher windowsThemeWatcher = new ();
4138

4239
public IconProvider(PluginInitContext context, Settings settings)
4340
{
@@ -48,44 +45,35 @@ public IconProvider(PluginInitContext context, Settings settings)
4845
this.context = context;
4946
this.settings = settings;
5047

51-
if (settings.IconTheme == IconTheme.System)
52-
{
53-
windowsThemeWatcher.StartWatching();
54-
}
55-
56-
settings.PropertyChanged += OnIconThemeChanged;
57-
5848
foreach (var image in GeneratedImagesDirectoryInfo.EnumerateFiles())
5949
{
6050
iconCache.TryAdd(image.Name, BitmapImageFromPath(image.FullName));
6151
}
6252
}
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+
7954
private string GetIconLocal(string icon) => $"{IC.ImagesDirectory}{icon}.{GetIconThemeString(settings.IconTheme)}.png";
8055

8156
private string GetIconThemeString(IconTheme iconTheme)
8257
{
8358
if (iconTheme == IconTheme.System)
8459
{
85-
iconTheme = windowsThemeWatcher.CurrentWindowsTheme.ToIconTheme();
60+
iconTheme = FlowLauncherThemeToIconTheme();
8661
}
8762
return Enum.GetName(iconTheme).ToLower();
8863
}
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+
8977
private static BitmapImage BitmapImageFromPath(string path) => new BitmapImage(new Uri(path));
9078

9179
public Result.IconDelegate GetIcon(IconGeneratorInfo info)
@@ -177,11 +165,5 @@ public void ClearCachedIcons()
177165
}
178166
OnPropertyChanged(nameof(CachedIconCount));
179167
}
180-
181-
public void Dispose()
182-
{
183-
settings.PropertyChanged -= OnIconThemeChanged;
184-
windowsThemeWatcher.Dispose();
185-
}
186168
}
187169
}
Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
using System;
2-
using Flow.Launcher.Plugin.OneNote.UI;
3-
41
namespace Flow.Launcher.Plugin.OneNote.Icons
52
{
63
public enum IconTheme
@@ -10,18 +7,5 @@ public enum IconTheme
107
Dark,
118
Color
129
}
13-
14-
public static class IconThemeExtensions
15-
{
16-
public static IconTheme ToIconTheme(this WindowsThemeWatcher.WindowsTheme theme)
17-
{
18-
return theme switch
19-
{
20-
WindowsThemeWatcher.WindowsTheme.Light => IconTheme.Dark,
21-
WindowsThemeWatcher.WindowsTheme.Dark => IconTheme.Light,
22-
_ => throw new ArgumentOutOfRangeException(nameof(theme))
23-
};
24-
}
25-
}
2610
}
2711

Flow.Launcher.Plugin.OneNote/Icons/WindowsThemeWatcher.cs

Lines changed: 0 additions & 81 deletions
This file was deleted.

Flow.Launcher.Plugin.OneNote/Main.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ public void Dispose()
7373
{
7474
context.API.VisibilityChanged -= OnVisibilityChanged;
7575
semaphore.Dispose();
76-
iconProvider.Dispose();
7776
OneNoteApplication.ReleaseComObject();
7877
}
7978
}

0 commit comments

Comments
 (0)