-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
132 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using MicaSetup.Helper; | ||
using MicaSetup.Natives; | ||
using Microsoft.Win32; | ||
using System; | ||
|
||
namespace MicaSetup.Design.Themes; | ||
|
||
internal static class SystemMenuThemeManager | ||
{ | ||
public static void Apply(SystemMenuTheme theme = SystemMenuTheme.Auto) | ||
{ | ||
// Enable dark mode for context menus if using dark theme | ||
if (Environment.OSVersion.Version.Build >= 18362) // Windows 10 1903 | ||
{ | ||
if (theme == SystemMenuTheme.Auto) | ||
{ | ||
// UxTheme methods will apply all of menus. | ||
// However, the Windows style system prefers that | ||
// Windows System Menu is based on `Apps Theme`, | ||
// and Tray Context Menu is based on `System Theme` when using a custom theme. | ||
// But actually we can't have our cake and eat it too. | ||
// Finally, we synchronize the theme styles of tray with higher usage rates. | ||
if (OSThemeHelper.SystemUsesDarkTheme()) | ||
{ | ||
_ = UxTheme.SetPreferredAppMode(UxTheme.PreferredAppMode.ForceDark); | ||
UxTheme.FlushMenuThemes(); | ||
} | ||
|
||
// Synchronize the theme with system settings | ||
SystemEvents.UserPreferenceChanged -= OnUserPreferenceChangedEventHandler; | ||
SystemEvents.UserPreferenceChanged += OnUserPreferenceChangedEventHandler; | ||
} | ||
else if (theme == SystemMenuTheme.Dark) | ||
{ | ||
SystemEvents.UserPreferenceChanged -= OnUserPreferenceChangedEventHandler; | ||
_ = UxTheme.SetPreferredAppMode(UxTheme.PreferredAppMode.ForceDark); | ||
UxTheme.FlushMenuThemes(); | ||
} | ||
else if (theme == SystemMenuTheme.Light) | ||
{ | ||
SystemEvents.UserPreferenceChanged -= OnUserPreferenceChangedEventHandler; | ||
_ = UxTheme.SetPreferredAppMode(UxTheme.PreferredAppMode.ForceLight); | ||
UxTheme.FlushMenuThemes(); | ||
} | ||
} | ||
} | ||
|
||
private static void OnUserPreferenceChangedEventHandler(object sender, UserPreferenceChangedEventArgs e) | ||
{ | ||
if (OSThemeHelper.SystemUsesDarkTheme()) | ||
{ | ||
_ = UxTheme.SetPreferredAppMode(UxTheme.PreferredAppMode.ForceDark); | ||
UxTheme.FlushMenuThemes(); | ||
} | ||
else | ||
{ | ||
_ = UxTheme.SetPreferredAppMode(UxTheme.PreferredAppMode.ForceLight); | ||
UxTheme.FlushMenuThemes(); | ||
} | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Theme in which an system menu is displayed. | ||
/// </summary> | ||
public enum SystemMenuTheme | ||
{ | ||
/// <summary> | ||
/// Auto system theme. | ||
/// </summary> | ||
Auto = ApplicationTheme.Unknown, | ||
|
||
/// <summary> | ||
/// Dark system theme. | ||
/// </summary> | ||
Dark = ApplicationTheme.Dark, | ||
|
||
/// <summary> | ||
/// Light system theme. | ||
/// </summary> | ||
Light = ApplicationTheme.Light, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using Microsoft.Win32; | ||
|
||
namespace MicaSetup.Helper; | ||
|
||
public static class OSThemeHelper | ||
{ | ||
public static bool AppsUseDarkTheme() | ||
{ | ||
object? value = Registry.GetValue( | ||
@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize", | ||
"AppsUseLightTheme", 1); | ||
|
||
return value != null && (int)value == 0; | ||
} | ||
|
||
public static bool SystemUsesDarkTheme() | ||
{ | ||
object? value = Registry.GetValue( | ||
@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize", | ||
"SystemUsesLightTheme", 0); | ||
|
||
return value == null || (int)value == 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters