-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindowChrome.cs
More file actions
62 lines (54 loc) · 1.88 KB
/
Copy pathWindowChrome.cs
File metadata and controls
62 lines (54 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System.Runtime.InteropServices;
using Avalonia.Controls;
using Avalonia.Threading;
namespace Jesnote;
/// <summary>
/// On Windows the title bar is drawn by DWM (non-client area), so Avalonia's
/// ThemeVariant alone does not affect it. Tell DWM to render the caption in
/// dark mode so the chrome matches the rest of the UI.
/// </summary>
internal static class WindowChrome
{
const int DWMWA_USE_IMMERSIVE_DARK_MODE = 20; // Win10 20H1+, Win11
const int DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1 = 19; // older Win10 builds
[DllImport("dwmapi.dll")]
static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int value, int size);
/// <summary>
/// Apply the title-bar color once the native handle exists. Theme changes
/// are pushed explicitly by the caller through Apply.
/// </summary>
public static void AttachTo(Window window, Func<bool> isDark)
{
if (!OperatingSystem.IsWindows())
return;
window.Opened += (_, _) =>
{
Apply(window, isDark());
Dispatcher.UIThread.Post(() => Apply(window, isDark()), DispatcherPriority.Loaded);
};
}
/// <summary>
/// Push the current dark/light state to the native Windows title bar.
/// </summary>
public static void Apply(Window window, bool dark)
{
if (!OperatingSystem.IsWindows())
return;
var hwnd = window.TryGetPlatformHandle()?.Handle ?? IntPtr.Zero;
if (hwnd == IntPtr.Zero)
return;
int useDark = dark ? 1 : 0;
if (
DwmSetWindowAttribute(hwnd, DWMWA_USE_IMMERSIVE_DARK_MODE, ref useDark, sizeof(int))
!= 0
)
{
_ = DwmSetWindowAttribute(
hwnd,
DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1,
ref useDark,
sizeof(int)
);
}
}
}