Skip to content

Commit

Permalink
Add support for dark title bars on Windows 10 1809-2004 (#815)
Browse files Browse the repository at this point in the history
* Support dark title bar on Windows 10 versions between 1809 and 2004 (20H1)

* Decided I prefer having it in this file
  • Loading branch information
ScrubN authored Sep 16, 2023
1 parent d8786b2 commit c1ccdfe
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
2 changes: 1 addition & 1 deletion TwitchDownloaderWPF/Services/NativeFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
24 changes: 15 additions & 9 deletions TwitchDownloaderWPF/Services/ThemeService.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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()
Expand All @@ -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
}

Expand Down
4 changes: 1 addition & 3 deletions TwitchDownloaderWPF/Services/WindowsThemeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit c1ccdfe

Please sign in to comment.