annotate src/sys/win32/dark_theme.cc @ 318:3b355fa948c7

config: use TOML instead of INI unfortunately, INI is not enough, and causes some paths including semicolons to break with our current storage of the library folders. so, I decided to switch to TOML which does support real arrays...
author Paper <paper@paper.us.eu.org>
date Wed, 12 Jun 2024 05:25:41 -0400
parents 862d0d8619f6
children
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"
178
bc8d2ccff09c win32/dark: use existing STL classes for dwmapi
Paper <mrpapersonic@gmail.com>
parents: 106
diff changeset
2
105
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
3 #include <QApplication>
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>
178
bc8d2ccff09c win32/dark: use existing STL classes for dwmapi
Paper <mrpapersonic@gmail.com>
parents: 106
diff changeset
7
105
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
8 #include <iostream>
178
bc8d2ccff09c win32/dark: use existing STL classes for dwmapi
Paper <mrpapersonic@gmail.com>
parents: 106
diff changeset
9 #include <memory>
bc8d2ccff09c win32/dark: use existing STL classes for dwmapi
Paper <mrpapersonic@gmail.com>
parents: 106
diff changeset
10
105
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
11 #include <dwmapi.h>
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
12
178
bc8d2ccff09c win32/dark: use existing STL classes for dwmapi
Paper <mrpapersonic@gmail.com>
parents: 106
diff changeset
13 struct LibraryDeconstructor {
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 196
diff changeset
14 using pointer = HINSTANCE;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 196
diff changeset
15 void operator()(pointer t) const { ::FreeLibrary(t); };
105
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
16 };
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
17
178
bc8d2ccff09c win32/dark: use existing STL classes for dwmapi
Paper <mrpapersonic@gmail.com>
parents: 106
diff changeset
18 using Library = std::unique_ptr<HINSTANCE, LibraryDeconstructor>;
bc8d2ccff09c win32/dark: use existing STL classes for dwmapi
Paper <mrpapersonic@gmail.com>
parents: 106
diff changeset
19
bc8d2ccff09c win32/dark: use existing STL classes for dwmapi
Paper <mrpapersonic@gmail.com>
parents: 106
diff changeset
20 class Dwmapi {
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 196
diff changeset
21 public:
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 196
diff changeset
22 Dwmapi() {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 196
diff changeset
23 /* load functions */
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 196
diff changeset
24 library.reset(::LoadLibraryW(L"dwmapi.dll"));
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 196
diff changeset
25 set_wind_attrib = reinterpret_cast<decltype(::DwmSetWindowAttribute)*>(
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 196
diff changeset
26 GetProcAddress(library.get(), "DwmSetWindowAttribute"));
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 196
diff changeset
27 }
178
bc8d2ccff09c win32/dark: use existing STL classes for dwmapi
Paper <mrpapersonic@gmail.com>
parents: 106
diff changeset
28
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 196
diff changeset
29 HRESULT SetWindowAttribute(HWND hWnd, DWORD key, LPCVOID data, DWORD sz_data) {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 196
diff changeset
30 if (!library.get())
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 196
diff changeset
31 return E_POINTER;
178
bc8d2ccff09c win32/dark: use existing STL classes for dwmapi
Paper <mrpapersonic@gmail.com>
parents: 106
diff changeset
32
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 196
diff changeset
33 /* GCC throws a fit here because C++ lacks a "generic" function pointer type.
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 196
diff changeset
34 * Ignore. */
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 196
diff changeset
35 if (!set_wind_attrib)
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 196
diff changeset
36 return E_POINTER;
178
bc8d2ccff09c win32/dark: use existing STL classes for dwmapi
Paper <mrpapersonic@gmail.com>
parents: 106
diff changeset
37
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 196
diff changeset
38 return set_wind_attrib(hWnd, key, data, sz_data);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 196
diff changeset
39 }
178
bc8d2ccff09c win32/dark: use existing STL classes for dwmapi
Paper <mrpapersonic@gmail.com>
parents: 106
diff changeset
40
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 196
diff changeset
41 protected:
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 196
diff changeset
42 Library library = nullptr;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 196
diff changeset
43 decltype(::DwmSetWindowAttribute)* set_wind_attrib;
178
bc8d2ccff09c win32/dark: use existing STL classes for dwmapi
Paper <mrpapersonic@gmail.com>
parents: 106
diff changeset
44 };
bc8d2ccff09c win32/dark: use existing STL classes for dwmapi
Paper <mrpapersonic@gmail.com>
parents: 106
diff changeset
45
bc8d2ccff09c win32/dark: use existing STL classes for dwmapi
Paper <mrpapersonic@gmail.com>
parents: 106
diff changeset
46 Dwmapi dwmapi;
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
47
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
48 namespace win32 {
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
49
105
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
50 bool SetTitleBarToBlack(QWidget* win, bool enabled) {
196
f0ff06a45c42 date: use std::optional for values
Paper <mrpapersonic@gmail.com>
parents: 178
diff changeset
51 /* 19 and 20 are *both* DWMWA_USE_IMMERSIVE_DARK_MODE.
f0ff06a45c42 date: use std::optional for values
Paper <mrpapersonic@gmail.com>
parents: 178
diff changeset
52 *
f0ff06a45c42 date: use std::optional for values
Paper <mrpapersonic@gmail.com>
parents: 178
diff changeset
53 * It's 20 on newer versions of windows (i.e. win11 and late win10),
f0ff06a45c42 date: use std::optional for values
Paper <mrpapersonic@gmail.com>
parents: 178
diff changeset
54 * but it's 19 on very old versions of win10 nobody ought to be using anymore.
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 196
diff changeset
55 */
196
f0ff06a45c42 date: use std::optional for values
Paper <mrpapersonic@gmail.com>
parents: 178
diff changeset
56 static constexpr DWORD DWMWA_USE_IMMERSIVE_DARK_MODE_OLD = 19;
f0ff06a45c42 date: use std::optional for values
Paper <mrpapersonic@gmail.com>
parents: 178
diff changeset
57 static constexpr DWORD DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
f0ff06a45c42 date: use std::optional for values
Paper <mrpapersonic@gmail.com>
parents: 178
diff changeset
58
105
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
59 BOOL b = enabled;
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 {
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 196
diff changeset
62 HRESULT result = dwmapi.SetWindowAttribute(reinterpret_cast<HWND>(win->winId()), DWMWA_USE_IMMERSIVE_DARK_MODE,
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 196
diff changeset
63 &b, sizeof(b));
105
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
64 if (result == S_OK)
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
65 return b;
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 {
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 196
diff changeset
69 HRESULT result = dwmapi.SetWindowAttribute(reinterpret_cast<HWND>(win->winId()),
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 196
diff changeset
70 DWMWA_USE_IMMERSIVE_DARK_MODE_OLD, &b, sizeof(b));
105
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
71 if (result == S_OK)
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
72 return b;
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
73 }
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 return b;
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
76 }
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
77
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
78 void SetTitleBarsToBlack(bool enabled) {
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
79 for (QWidget* widget : qApp->topLevelWidgets()) {
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
80 SetTitleBarToBlack(widget, enabled);
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
81 }
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
82 }
6d8da6e64d61 theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
83
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
84 bool DarkThemeAvailable() {
66
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents: 15
diff changeset
85 const auto& ver = QOperatingSystemVersion::current();
6481c5aed3e1 posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents: 15
diff changeset
86 return (ver.majorVersion() > 10) ? true : (ver.majorVersion() == 10 && ver.microVersion() >= 17763);
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
87 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
88
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
89 bool IsInDarkTheme() {
15
cde8f67a7c7d *: update, megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
90 QSettings settings("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize",
cde8f67a7c7d *: update, megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
91 QSettings::NativeFormat);
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
92 return settings.value("AppsUseLightTheme", 1).toInt() == 0;
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
93 }
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
94
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
95 } // namespace win32