|
1 | 1 | using System;
|
| 2 | +using Microsoft.Win32; |
| 3 | +using Uno.Helpers.Theming; |
| 4 | +using Uno.UI.Runtime.Skia.Wpf.WPF.Extensions.Helper.Theming; |
2 | 5 | using Uno.UI.Xaml;
|
3 | 6 | using Windows.UI.Xaml;
|
4 | 7 |
|
5 | 8 | namespace Uno.UI.Runtime.Skia.Wpf
|
6 | 9 | {
|
7 |
| - public class WpfApplicationExtension : IApplicationExtension |
| 10 | + public class WpfApplicationExtension : IApplicationExtension, IDisposable |
8 | 11 | {
|
9 | 12 | private readonly Application _owner;
|
| 13 | + private WpfSystemThemeHelperExtension _themeHelper; |
| 14 | + private SystemTheme _currentTheme; |
| 15 | + |
| 16 | + private bool _disposedValue; |
10 | 17 |
|
11 | 18 | public WpfApplicationExtension(Application owner)
|
12 | 19 | {
|
13 | 20 | _owner = owner ?? throw new ArgumentNullException(nameof(owner));
|
| 21 | + |
| 22 | + _themeHelper = new WpfSystemThemeHelperExtension(null); |
| 23 | + _currentTheme = _themeHelper.GetSystemTheme(); |
| 24 | + |
| 25 | + SystemEvents.UserPreferenceChanged += SystemEvents_UserPreferenceChanged; |
14 | 26 | }
|
15 | 27 |
|
16 | 28 | public bool CanExit => true;
|
17 | 29 |
|
18 | 30 | public void Exit() => System.Windows.Application.Current.Shutdown();
|
| 31 | + |
| 32 | + public event EventHandler SystemThemeChanged; |
| 33 | + |
| 34 | + private void SystemEvents_UserPreferenceChanged(object sender, UserPreferenceChangedEventArgs e) |
| 35 | + { |
| 36 | + // This event is raised when the theme is changed, |
| 37 | + // but also for various other irrelevant reasons. |
| 38 | + // Check the registry first, before forcing the application |
| 39 | + // to refresh everything. |
| 40 | + var newTheme = _themeHelper.GetSystemTheme(); |
| 41 | + |
| 42 | + if (newTheme != _currentTheme) |
| 43 | + { |
| 44 | + _currentTheme = newTheme; |
| 45 | + SystemThemeChanged?.Invoke(this, EventArgs.Empty); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + #region IDisposable |
| 50 | + protected virtual void Dispose(bool disposing) |
| 51 | + { |
| 52 | + if (!_disposedValue) |
| 53 | + { |
| 54 | + // According to Microsoft, https://docs.microsoft.com/en-us/dotnet/api/microsoft.win32.systemevents.userpreferencechanged, |
| 55 | + // this event must be manully unhooked when the application is disposed. |
| 56 | + SystemEvents.UserPreferenceChanged -= SystemEvents_UserPreferenceChanged; |
| 57 | + _disposedValue = true; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + ~WpfApplicationExtension() |
| 62 | + { |
| 63 | + // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method |
| 64 | + Dispose(disposing: false); |
| 65 | + } |
| 66 | + |
| 67 | + public void Dispose() |
| 68 | + { |
| 69 | + // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method |
| 70 | + Dispose(disposing: true); |
| 71 | + GC.SuppressFinalize(this); |
| 72 | + } |
| 73 | + #endregion |
19 | 74 | }
|
20 | 75 | }
|
0 commit comments