|
| 1 | +/* |
| 2 | +MIT License |
| 3 | +
|
| 4 | +Copyright (c) Léo Corporation |
| 5 | +
|
| 6 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +of this software and associated documentation files (the "Software"), to deal |
| 8 | +in the Software without restriction, including without limitation the rights |
| 9 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +copies of the Software, and to permit persons to whom the Software is |
| 11 | +furnished to do so, subject to the following conditions: |
| 12 | +
|
| 13 | +The above copyright notice and this permission notice shall be included in all |
| 14 | +copies or substantial portions of the Software. |
| 15 | +
|
| 16 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | +SOFTWARE. |
| 23 | +*/ |
| 24 | +using InternetTest.Enums; |
| 25 | +using MicaWPF.Core.Enums; |
| 26 | +using MicaWPF.Core.Services; |
| 27 | +using Microsoft.Win32; |
| 28 | +using PeyrSharp.Enums; |
| 29 | +using PeyrSharp.Env; |
| 30 | +using System.Windows; |
| 31 | +using System.Windows.Media; |
| 32 | + |
| 33 | +namespace InternetTest.Helpers; |
| 34 | + |
| 35 | +public class ThemeHelper |
| 36 | +{ |
| 37 | + public static SolidColorBrush GetSolidColorBrush(string resource) => (SolidColorBrush)Application.Current.Resources[resource]; |
| 38 | + |
| 39 | + public static void ChangeTheme(Theme theme) |
| 40 | + { |
| 41 | + bool isDark = theme switch |
| 42 | + { |
| 43 | + Theme.Dark => true, |
| 44 | + Theme.Light => false, |
| 45 | + Theme.System => IsSystemThemeDark(), |
| 46 | + _ => false |
| 47 | + }; |
| 48 | + |
| 49 | + var dictionary = new ResourceDictionary |
| 50 | + { |
| 51 | + Source = new Uri($"..\\Themes\\{(isDark ? "Dark" : "Light")}.xaml", UriKind.Relative) |
| 52 | + }; |
| 53 | + |
| 54 | + MicaWPFServiceUtility.ThemeService.ChangeTheme((WindowsTheme)theme); |
| 55 | + |
| 56 | + Application.Current.Resources.MergedDictionaries.Clear(); |
| 57 | + Application.Current.Resources.MergedDictionaries.Add(dictionary); |
| 58 | + } |
| 59 | + |
| 60 | + public static bool IsSystemThemeDark() |
| 61 | + { |
| 62 | + if (Sys.CurrentWindowsVersion is not WindowsVersion.Windows10 and not WindowsVersion.Windows11) |
| 63 | + { |
| 64 | + return false; // Avoid errors on older OSs |
| 65 | + } |
| 66 | + |
| 67 | + var t = Registry.GetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize", "SystemUsesLightTheme", "1"); |
| 68 | + return t switch |
| 69 | + { |
| 70 | + 0 => true, |
| 71 | + 1 => false, |
| 72 | + _ => false |
| 73 | + }; |
| 74 | + } |
| 75 | +} |
0 commit comments