From 1e48f8bbfdb0f1ce2f9132d604405fb72d160d9d Mon Sep 17 00:00:00 2001
From: David Robillard <d@drobilla.net>
Date: Thu, 12 Jan 2023 15:30:44 -0500
Subject: Windows: Add PUGL_DARK_FRAME hint

This allows dark applications to visually integrate more nicely in Windows 10.
A little thing, but it really goes a long way to make programs not look out of
place and half-baked.
---
 src/win.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

(limited to 'src')

diff --git a/src/win.c b/src/win.c
index db3f98c..e9c621e 100644
--- a/src/win.c
+++ b/src/win.c
@@ -8,6 +8,7 @@
 
 #include "pugl/pugl.h"
 
+#include <dwmapi.h>
 #include <windows.h>
 #include <windowsx.h>
 
@@ -30,6 +31,11 @@
 #  define GWLP_USERDATA (-21)
 #endif
 
+#define PRE_20H1_DWMWA_USE_IMMERSIVE_DARK_MODE 19
+#ifndef DWMWA_USE_IMMERSIVE_DARK_MODE
+#  define DWMWA_USE_IMMERSIVE_DARK_MODE 20
+#endif
+
 #define PUGL_LOCAL_CLOSE_MSG (WM_USER + 50)
 #define PUGL_LOCAL_MARK_MSG (WM_USER + 51)
 #define PUGL_LOCAL_CLIENT_MSG (WM_USER + 52)
@@ -280,12 +286,26 @@ puglRealize(PuglView* view)
   }
 
   // Set basic window hints and attributes
+
   puglSetViewString(view, PUGL_WINDOW_TITLE, view->strings[PUGL_WINDOW_TITLE]);
   puglSetTransientParent(view, view->transientParent);
 
   view->impl->scaleFactor = puglWinGetViewScaleFactor(view);
   view->impl->cursor      = LoadCursor(NULL, IDC_ARROW);
 
+  if (view->hints[PUGL_DARK_FRAME]) {
+    const BOOL useDarkMode = TRUE;
+    if ((DwmSetWindowAttribute(view->impl->hwnd,
+                               DWMWA_USE_IMMERSIVE_DARK_MODE,
+                               &useDarkMode,
+                               sizeof(useDarkMode)) != S_OK)) {
+      DwmSetWindowAttribute(view->impl->hwnd,
+                            PRE_20H1_DWMWA_USE_IMMERSIVE_DARK_MODE,
+                            &useDarkMode,
+                            sizeof(useDarkMode));
+    }
+  }
+
   SetWindowLongPtr(impl->hwnd, GWLP_USERDATA, (LONG_PTR)view);
 
   return puglDispatchSimpleEvent(view, PUGL_REALIZE);
-- 
cgit v1.2.1