annotate src/sys/win32/dark_theme.cc @ 106:c8c72278f6fd

*: #if -> #ifdef, remove outdated comments in sys/win32/dark_theme.cc
author Paper <mrpapersonic@gmail.com>
date Sun, 05 Nov 2023 04:01:58 -0500
parents 6d8da6e64d61
children bc8d2ccff09c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
1 #include "sys/win32/dark_theme.h"
105
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
2 #include <QApplication>
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
3 #include <QDebug>
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
4 #include <QOperatingSystemVersion>
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
5 #include <QSettings>
105
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
6 #include <QWidget>
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
7 #include <iostream>
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
8 #include <dwmapi.h>
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
9
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
10 /* let's make a class wrapper around HINSTANCE,
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
11 so we don't fuck anything up :). */
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
12 class Library {
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
13 public:
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
14 Library() {}
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
15 ~Library() {
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
16 Unload();
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
17 }
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
18 void Unload() {
106
c8c72278f6fd *: #if -> #ifdef, remove outdated comments in sys/win32/dark_theme.cc
Paper <mrpapersonic@gmail.com>
parents: 105
diff changeset
19 if (hInstance) {
105
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
20 FreeLibrary(hInstance);
106
c8c72278f6fd *: #if -> #ifdef, remove outdated comments in sys/win32/dark_theme.cc
Paper <mrpapersonic@gmail.com>
parents: 105
diff changeset
21 hInstance = nullptr;
c8c72278f6fd *: #if -> #ifdef, remove outdated comments in sys/win32/dark_theme.cc
Paper <mrpapersonic@gmail.com>
parents: 105
diff changeset
22 }
105
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
23 loaded = false;
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
24 }
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
25 void Load(LPCWSTR name) {
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
26 if (loaded)
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
27 Unload();
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
28 hInstance = LoadLibraryW(name);
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
29 if (hInstance)
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
30 loaded = true;
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
31 }
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
32 HINSTANCE GetInstance() {
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
33 return hInstance;
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
34 }
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
35 bool IsLoaded() {
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
36 return loaded;
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
37 }
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
38 private:
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
39 HINSTANCE hInstance = nullptr;
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
40 bool loaded = false;
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
41 };
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
42
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
43 Library dwmapi;
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
44
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
45 namespace win32 {
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
46
105
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
47 #define GET_FUNCTION(f, i) \
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
48 reinterpret_cast<decltype(::f)*>(GetProcAddress(i, #f))
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
49
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
50 static HRESULT SetWindowAttribute(HWND hWnd, DWORD key, LPCVOID data, DWORD sz_data) {
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
51 if (!dwmapi.IsLoaded()) {
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
52 dwmapi.Load(L"dwmapi.dll");
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
53 if (!dwmapi.IsLoaded())
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
54 return false;
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
55 }
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
56
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
57 HINSTANCE hInstance = dwmapi.GetInstance();
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
58 if (!hInstance)
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
59 return false;
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
60
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
61 auto set_wind_attrib = GET_FUNCTION(DwmSetWindowAttribute, hInstance);
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
62 if (!set_wind_attrib)
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
63 return false;
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
64
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
65 return set_wind_attrib(hWnd, key, data, sz_data);
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
66 }
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
67
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
68 bool SetTitleBarToBlack(QWidget* win, bool enabled) {
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
69 BOOL b = enabled;
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
70
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
71 /* MAGIC NUMBERS: 19 and 20 are both DWMWA_USE_IMMERSIVE_DARK_MODE.
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
72 clarification: it's 20 on newer versions of windows (i.e. win11 and late win10),
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
73 but it's 19 on very old versions of win10 nobody ought to be using. */
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
74 {
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
75 HRESULT result = SetWindowAttribute(reinterpret_cast<HWND>(win->winId()), 20, &b, sizeof(b));
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
76 if (result == S_OK)
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
77 return b;
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
78 }
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
79
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
80 {
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
81 HRESULT result = SetWindowAttribute(reinterpret_cast<HWND>(win->winId()), 19, &b, sizeof(b));
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
82 if (result == S_OK)
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
83 return b;
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
84 }
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
85
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
86 return b;
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
87 }
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
88
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
89 void SetTitleBarsToBlack(bool enabled) {
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
90 for (QWidget* widget : qApp->topLevelWidgets()) {
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
91 SetTitleBarToBlack(widget, enabled);
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
92 }
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
93 }
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
94
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
95 bool DarkThemeAvailable() {
66
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents: 15
diff changeset
96 const auto& ver = QOperatingSystemVersion::current();
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents: 15
diff changeset
97 return (ver.majorVersion() > 10) ? true : (ver.majorVersion() == 10 && ver.microVersion() >= 17763);
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
98 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
99
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
100 bool IsInDarkTheme() {
15
cde8f67a7c7d *: update, megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
101 QSettings settings("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize",
cde8f67a7c7d *: update, megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
102 QSettings::NativeFormat);
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
103 return settings.value("AppsUseLightTheme", 1).toInt() == 0;
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
104 }
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
105
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
106 } // namespace win32