Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for dark title bars on Windows 10 1809-2004 #815

Merged
merged 2 commits into from
Sep 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading