| 1 | // Copyright 2023 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #if defined(_WIN32) |
| 5 | |
| 6 | #include "DolphinQt/QtUtils/SetWindowDecorations.h" |
| 7 | |
| 8 | #include <QApplication> |
| 9 | #include <QEvent> |
| 10 | #include <QWidget> |
| 11 | |
| 12 | #include <dwmapi.h> |
| 13 | |
| 14 | #include "DolphinQt/Settings.h" |
| 15 | |
| 16 | namespace |
| 17 | { |
| 18 | |
| 19 | void SetQWidgetWindowDecorations(QWidget* widget) |
| 20 | { |
| 21 | if (!Settings::Instance().IsThemeDark()) |
| 22 | return; |
| 23 | |
| 24 | constexpr DWORD attribute = 20; // DWMWINDOWATTRIBUTE::DWMWA_USE_IMMERSIVE_DARK_MODE |
| 25 | constexpr BOOL use_dark_title_bar = TRUE; |
| 26 | |
| 27 | DwmSetWindowAttribute(HWND(widget->winId()), attribute, &use_dark_title_bar, |
| 28 | DWORD(sizeof(use_dark_title_bar))); |
| 29 | } |
| 30 | |
| 31 | class WindowDecorationFilter final : public QObject |
| 32 | { |
| 33 | public: |
| 34 | using QObject::QObject; |
| 35 | |
| 36 | bool eventFilter(QObject* obj, QEvent* event) override |
| 37 | { |
| 38 | if (event->type() == QEvent::Show || event->type() == QEvent::ApplicationPaletteChange) |
| 39 | { |
| 40 | SetQWidgetWindowDecorations(static_cast<QWidget*>(obj)); |
| 41 | } |
| 42 | return QObject::eventFilter(obj, event); |
| 43 | } |
| 44 | }; |
| 45 | |
| 46 | class WindowDecorationFilterInstaller final : public QObject |
| 47 | { |
| 48 | public: |
| 49 | using QObject::QObject; |
| 50 | |
| 51 | bool eventFilter(QObject* obj, QEvent* event) override |
| 52 | { |
| 53 | if (event->type() == QEvent::WinIdChange) |
| 54 | { |
| 55 | auto* const widget = qobject_cast<QWidget*>(obj); |
| 56 | |
| 57 | // Install the real filter on actual Window QWidgets. |
| 58 | // This avoids the need for a qobject_cast on literally every QEvent::Show. |
| 59 | if (widget && widget->isWindow()) |
| 60 | widget->installEventFilter(m_decoration_filter); |
| 61 | } |
| 62 | return QObject::eventFilter(obj, event); |
| 63 | } |
| 64 | |
| 65 | private: |
| 66 | QObject* const m_decoration_filter = new WindowDecorationFilter{this}; |
| 67 | }; |
| 68 | |
| 69 | } // namespace |
| 70 | |
| 71 | namespace QtUtils |
| 72 | { |
| 73 | |
| 74 | void InstallWindowDecorationFilter(QApplication* app) |
| 75 | { |
| 76 | app->installEventFilter(new WindowDecorationFilterInstaller{app}); |
| 77 | } |
| 78 | |
| 79 | } // namespace QtUtils |
| 80 | |
| 81 | #endif |