diff --git a/TwitchDownloaderWPF/Services/NativeFunctions.cs b/TwitchDownloaderWPF/Services/NativeFunctions.cs index 4a33f7dc..332fe2a5 100644 --- a/TwitchDownloaderWPF/Services/NativeFunctions.cs +++ b/TwitchDownloaderWPF/Services/NativeFunctions.cs @@ -8,6 +8,6 @@ namespace TwitchDownloaderWPF.Services public static class NativeFunctions { [DllImport("dwmapi.dll", EntryPoint = "DwmSetWindowAttribute", PreserveSig = true)] - public static extern int SetWindowAttribute(IntPtr handle, int attribute, ref bool attributeValue, int attributeSize); + public static extern int SetWindowAttribute(IntPtr handle, int attribute, [In] ref int attributeValue, int attributeSize); } } \ No newline at end of file diff --git a/TwitchDownloaderWPF/Services/ThemeService.cs b/TwitchDownloaderWPF/Services/ThemeService.cs index 8c633ca2..477f3a2e 100644 --- a/TwitchDownloaderWPF/Services/ThemeService.cs +++ b/TwitchDownloaderWPF/Services/ThemeService.cs @@ -1,9 +1,9 @@ using HandyControl.Data; using System; using System.IO; -using System.Runtime.InteropServices; using System.Runtime.Versioning; using System.Windows; +using System.Windows.Interop; using System.Windows.Media; using System.Xml.Serialization; using TwitchDownloaderWPF.Models; @@ -13,7 +13,10 @@ namespace TwitchDownloaderWPF.Services { public class ThemeService { - private const int TITLEBAR_THEME_ATTRIBUTE = 20; + private const int WINDOWS_1809_BUILD_NUMBER = 17763; + private const int WINDOWS_2004_INSIDER_BUILD_NUMBER = 18985; + private const int USE_IMMERSIVE_DARK_MODE_ATTRIBUTE_BEFORE_2004 = 19; + private const int USE_IMMERSIVE_DARK_MODE_ATTRIBUTE = 20; private bool _darkAppTitleBar = false; private bool _darkHandyControl = false; @@ -84,16 +87,18 @@ public void ChangeAppTheme() [SupportedOSPlatform("windows")] public void SetTitleBarTheme(WindowCollection windows) { - // If windows 10 build is before 1903, it doesn't support dark title bars - if (Environment.OSVersion.Version.Build < 18362) - { + if (Environment.OSVersion.Version.Major < 10 || Environment.OSVersion.Version.Build < WINDOWS_1809_BUILD_NUMBER) return; - } + + var shouldUseDarkTitleBar = Convert.ToInt32(_darkAppTitleBar); + var darkTitleBarAttribute = Environment.OSVersion.Version.Build < WINDOWS_2004_INSIDER_BUILD_NUMBER + ? USE_IMMERSIVE_DARK_MODE_ATTRIBUTE_BEFORE_2004 + : USE_IMMERSIVE_DARK_MODE_ATTRIBUTE; foreach (Window window in windows) { - var windowHandle = new System.Windows.Interop.WindowInteropHelper(window).Handle; - NativeFunctions.SetWindowAttribute(windowHandle, TITLEBAR_THEME_ATTRIBUTE, ref _darkAppTitleBar, Marshal.SizeOf(_darkAppTitleBar)); + var windowHandle = new WindowInteropHelper(window).Handle; + NativeFunctions.SetWindowAttribute(windowHandle, darkTitleBarAttribute, ref shouldUseDarkTitleBar, sizeof(int)); } Window wnd = new() @@ -104,7 +109,8 @@ public void SetTitleBarTheme(WindowCollection windows) }; wnd.Show(); wnd.Close(); - // Dark title bar is a bit buggy, requires window resize or focus change to fully apply + // Dark title bar is a bit buggy, requires window redraw (focus change, resize, transparency change) to fully apply. + // We *could* send a repaint message to win32.dll, but this solution works and is way easier. // Win11 might not have this issue but Win10 does so please leave this } diff --git a/TwitchDownloaderWPF/Services/WindowsThemeService.cs b/TwitchDownloaderWPF/Services/WindowsThemeService.cs index 04bea753..aebcbd54 100644 --- a/TwitchDownloaderWPF/Services/WindowsThemeService.cs +++ b/TwitchDownloaderWPF/Services/WindowsThemeService.cs @@ -17,15 +17,13 @@ public class WindowsThemeService : ManagementEventWatcher private const string REGISTRY_KEY_NAME = "AppsUseLightTheme"; private const string LIGHT_THEME = "Light"; private const string DARK_THEME = "Dark"; + private const int WINDOWS_1809_BUILD_NUMBER = 17763; public WindowsThemeService() { // If the OS is older than Windows 10 1809 then it doesn't have the app theme registry key - const int WINDOWS_1809_BUILD_NUMBER = 17763; if (Environment.OSVersion.Version.Major < 10 || Environment.OSVersion.Version.Build < WINDOWS_1809_BUILD_NUMBER) - { return; - } var currentUser = WindowsIdentity.GetCurrent().User;