Skip to content

Commit

Permalink
Decided I prefer having it in this file
Browse files Browse the repository at this point in the history
  • Loading branch information
ScrubN committed Sep 16, 2023
1 parent ac718a6 commit 0a166f5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 34 deletions.
37 changes: 36 additions & 1 deletion TwitchDownloaderWPF/Services/ThemeService.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using HandyControl.Data;
using System;
using System.IO;
using System.Runtime.Versioning;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
using System.Xml.Serialization;
using TwitchDownloaderWPF.Models;
Expand All @@ -11,6 +13,11 @@ namespace TwitchDownloaderWPF.Services
{
public class ThemeService
{
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 @@ -77,7 +84,35 @@ public void ChangeAppTheme()
}
}

public void SetTitleBarTheme(WindowCollection windows) => WindowsThemeService.SetTitleBarTheme(windows, _darkAppTitleBar);
[SupportedOSPlatform("windows")]
public void SetTitleBarTheme(WindowCollection windows)
{
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 WindowInteropHelper(window).Handle;
NativeFunctions.SetWindowAttribute(windowHandle, darkTitleBarAttribute, ref shouldUseDarkTitleBar, sizeof(int));
}

Window wnd = new()
{
SizeToContent = SizeToContent.WidthAndHeight,
Top = int.MinValue + 1,
WindowStyle = WindowStyle.None
};
wnd.Show();
wnd.Close();
// 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
}

private void ChangeThemePath(string newTheme)
{
Expand Down
33 changes: 0 additions & 33 deletions TwitchDownloaderWPF/Services/WindowsThemeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Runtime.Versioning;
using System.Security.Principal;
using System.Windows;
using System.Windows.Interop;

namespace TwitchDownloaderWPF.Services
{
Expand All @@ -19,9 +18,6 @@ public class WindowsThemeService : ManagementEventWatcher
private const string LIGHT_THEME = "Light";
private const string DARK_THEME = "Dark";
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;

public WindowsThemeService()
{
Expand Down Expand Up @@ -67,34 +63,5 @@ public static string GetWindowsTheme()
? LIGHT_THEME
: DARK_THEME;
}

public static void SetTitleBarTheme(WindowCollection windows, bool useDarkTitleBar)
{
if (Environment.OSVersion.Version.Major < 10 || Environment.OSVersion.Version.Build < WINDOWS_1809_BUILD_NUMBER)
return;

var useDarkTitleBarInt = Convert.ToInt32(useDarkTitleBar);
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 WindowInteropHelper(window).Handle;
NativeFunctions.SetWindowAttribute(windowHandle, darkTitleBarAttribute, ref useDarkTitleBarInt, sizeof(int));
}

Window wnd = new()
{
SizeToContent = SizeToContent.WidthAndHeight,
Top = int.MinValue + 1,
WindowStyle = WindowStyle.None
};
wnd.Show();
wnd.Close();
// 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
}
}
}

0 comments on commit 0a166f5

Please sign in to comment.