-
Notifications
You must be signed in to change notification settings - Fork 6.9k
/
Copy pathThemeListener.cs
62 lines (51 loc) · 1.9 KB
/
ThemeListener.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Management;
using System.Security.Principal;
namespace ManagedCommon
{
/// <summary>
/// The Delegate for a ThemeChanged Event.
/// </summary>
/// <param name="sender">Sender ThemeListener</param>
public delegate void ThemeChangedEvent(ThemeListener sender);
public partial class ThemeListener : IDisposable
{
/// <summary>
/// Gets the App Theme.
/// </summary>
public AppTheme AppTheme { get; private set; }
/// <summary>
/// An event that fires if the Theme changes.
/// </summary>
public event ThemeChangedEvent ThemeChanged;
private readonly ManagementEventWatcher watcher;
public ThemeListener()
{
var currentUser = WindowsIdentity.GetCurrent();
var query = new WqlEventQuery(
$"SELECT * FROM RegistryValueChangeEvent WHERE Hive='HKEY_USERS' AND " +
$"KeyPath='{currentUser.User.Value}\\\\{ThemeHelpers.HkeyWindowsPersonalizeTheme.Replace("\\", "\\\\")}' AND ValueName='{ThemeHelpers.HValueAppTheme}'");
watcher = new ManagementEventWatcher(query);
watcher.EventArrived += Watcher_EventArrived;
watcher.Start();
AppTheme = ThemeHelpers.GetAppTheme();
}
private void Watcher_EventArrived(object sender, EventArrivedEventArgs e)
{
var appTheme = ThemeHelpers.GetAppTheme();
if (appTheme != AppTheme)
{
AppTheme = appTheme;
ThemeChanged?.Invoke(this);
}
}
public void Dispose()
{
watcher.Dispose();
GC.SuppressFinalize(this);
}
}
}