Mercurial > minori
annotate src/sys/win32/dark_theme.cc @ 334:948955c3ba81
services: use fmt for setting the status bar
this should make localization easier
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Mon, 17 Jun 2024 20:35:31 -0400 |
parents | 862d0d8619f6 |
children |
rev | line source |
---|---|
9 | 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 | 4 #include <QOperatingSystemVersion> |
9 | 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 | 14 using pointer = HINSTANCE; |
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 | 21 public: |
22 Dwmapi() { | |
23 /* load functions */ | |
24 library.reset(::LoadLibraryW(L"dwmapi.dll")); | |
25 set_wind_attrib = reinterpret_cast<decltype(::DwmSetWindowAttribute)*>( | |
26 GetProcAddress(library.get(), "DwmSetWindowAttribute")); | |
27 } | |
178
bc8d2ccff09c
win32/dark: use existing STL classes for dwmapi
Paper <mrpapersonic@gmail.com>
parents:
106
diff
changeset
|
28 |
258 | 29 HRESULT SetWindowAttribute(HWND hWnd, DWORD key, LPCVOID data, DWORD sz_data) { |
30 if (!library.get()) | |
31 return E_POINTER; | |
178
bc8d2ccff09c
win32/dark: use existing STL classes for dwmapi
Paper <mrpapersonic@gmail.com>
parents:
106
diff
changeset
|
32 |
258 | 33 /* GCC throws a fit here because C++ lacks a "generic" function pointer type. |
34 * Ignore. */ | |
35 if (!set_wind_attrib) | |
36 return E_POINTER; | |
178
bc8d2ccff09c
win32/dark: use existing STL classes for dwmapi
Paper <mrpapersonic@gmail.com>
parents:
106
diff
changeset
|
37 |
258 | 38 return set_wind_attrib(hWnd, key, data, sz_data); |
39 } | |
178
bc8d2ccff09c
win32/dark: use existing STL classes for dwmapi
Paper <mrpapersonic@gmail.com>
parents:
106
diff
changeset
|
40 |
258 | 41 protected: |
42 Library library = nullptr; | |
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 | 47 |
48 namespace win32 { | |
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 | 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 | 62 HRESULT result = dwmapi.SetWindowAttribute(reinterpret_cast<HWND>(win->winId()), DWMWA_USE_IMMERSIVE_DARK_MODE, |
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 | 69 HRESULT result = dwmapi.SetWindowAttribute(reinterpret_cast<HWND>(win->winId()), |
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 | 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 | 87 } |
88 | |
9 | 89 bool IsInDarkTheme() { |
15 | 90 QSettings settings("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize", |
91 QSettings::NativeFormat); | |
9 | 92 return settings.value("AppsUseLightTheme", 1).toInt() == 0; |
2 | 93 } |
9 | 94 |
95 } // namespace win32 |