Skip to content

Commit 295fbe9

Browse files
committed
auto switch for task bar icon color added
1 parent 63b39dd commit 295fbe9

File tree

6 files changed

+33
-55
lines changed

6 files changed

+33
-55
lines changed

src/VolumeScroller/App.xaml.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ namespace VolumeScroller;
1616
public partial class App : Application
1717
{
1818
private AudioController audioController;
19+
private static MainViewModel mainViewModel;
1920

2021
public void App_Startup(object sender, StartupEventArgs e)
2122
{
@@ -49,13 +50,14 @@ private static void ShutdownIfAlreadyRunning()
4950
private static void InitializeTrayIcon()
5051
{
5152
MainModel main = new();
52-
MainViewModel mainViewModel = new(main);
53+
mainViewModel = new(main);
5354
MainWindow mainWindow = new(mainViewModel);
5455
mainWindow.Show();
5556
}
5657

5758
private void Application_Exit(object sender, ExitEventArgs e)
5859
{
60+
mainViewModel.Dispose();
5961
audioController.Dispose();
6062
SystemEvents.DisplaySettingsChanged -= DisplaySettingsChanged;
6163
}

src/VolumeScroller/MainModel.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,6 @@ public MainModel()
77
ResetRunOnStartup();
88
}
99

10-
public bool DarkTaskbar
11-
{
12-
get => Properties.Settings.Default.DarkTaskbar;
13-
set
14-
{
15-
Properties.Settings.Default.DarkTaskbar = value;
16-
Properties.Settings.Default.Save();
17-
}
18-
}
19-
2010
public int Increment
2111
{
2212
get => Properties.Settings.Default.Increment;

src/VolumeScroller/MainViewModel.cs

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,27 @@
1-
namespace VolumeScroller;
1+
using Microsoft.Win32;
22

3-
public class MainViewModel : ViewModelBase
3+
namespace VolumeScroller;
4+
5+
public class MainViewModel : ViewModelBase, IDisposable
46
{
57
private readonly MainModel mainModel;
6-
private bool darkTaskbar;
78
private int increment;
89
private bool runOnStartup;
10+
private string taskBarIconPath;
911

1012
public MainViewModel(MainModel mainModel)
1113
{
1214
this.mainModel = mainModel;
13-
darkTaskbar = mainModel.DarkTaskbar;
1415
increment = mainModel.Increment * 2;
1516
runOnStartup = mainModel.RunOnStartup;
17+
TaskBarIconPath = GetTaskbarIconPath();
18+
SystemEvents.UserPreferenceChanged += UserPreferenceChanged;
1619
}
1720

1821
public string TaskBarIconPath
19-
=> DarkTaskbar
20-
? "/Resources/VolumeScroller_dark.ico"
21-
: "/Resources/VolumeScroller_light.ico";
22-
23-
public bool DarkTaskbar
2422
{
25-
get => darkTaskbar;
26-
set
27-
{
28-
SetProperty(ref darkTaskbar, value);
29-
mainModel.DarkTaskbar = value;
30-
OnPropertyChanged(nameof(TaskBarIconPath));
31-
}
23+
get => taskBarIconPath;
24+
set => SetProperty(ref taskBarIconPath, value);
3225
}
3326

3427
public bool RunOnStartup
@@ -50,4 +43,20 @@ public int Increment
5043
mainModel.Increment = value / 2;
5144
}
5245
}
46+
47+
public void Dispose()
48+
=> SystemEvents.UserPreferenceChanged -= UserPreferenceChanged;
49+
50+
private void UserPreferenceChanged(object sender, EventArgs e)
51+
=> TaskBarIconPath = GetTaskbarIconPath();
52+
53+
private static string GetTaskbarIconPath()
54+
{
55+
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize");
56+
var isLightTheme = (int)key.GetValue("SystemUsesLightTheme") == 1;
57+
58+
return isLightTheme
59+
? "/Resources/VolumeScroller_light.ico"
60+
: "/Resources/VolumeScroller_dark.ico";
61+
}
5362
}

src/VolumeScroller/MainWindow.xaml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,6 @@
5757
AutoToolTipPlacement="TopLeft"
5858
IsSnapToTickEnabled="True"
5959
TickFrequency="2" />
60-
<Label Content="Dark taskbar"
61-
Grid.Row="1"
62-
Grid.Column="0"
63-
HorizontalAlignment="Right" />
64-
<CheckBox Grid.Row="1"
65-
Grid.Column="1"
66-
VerticalAlignment="Center"
67-
IsChecked="{Binding DarkTaskbar, Mode=TwoWay}"/>
6860
<Label Content="Run on startup"
6961
Grid.Row="2"
7062
Grid.Column="0"

src/VolumeScroller/Properties/Settings.Designer.cs

Lines changed: 5 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/VolumeScroller/Properties/Settings.settings

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
<Setting Name="Increment" Type="System.Int32" Scope="User">
66
<Value Profile="(Default)">1</Value>
77
</Setting>
8-
<Setting Name="DarkTaskbar" Type="System.Boolean" Scope="User">
9-
<Value Profile="(Default)">True</Value>
10-
</Setting>
118
<Setting Name="RunOnStartup" Type="System.Boolean" Scope="User">
129
<Value Profile="(Default)">True</Value>
1310
</Setting>

0 commit comments

Comments
 (0)