summaryrefslogtreecommitdiff
path: root/Source/Core/DolphinQt/QtUtils/SetWindowDecorations.cpp
blob: b6c985a61e05a4100c58e7f6ceb6a60a37f1222b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright 2023 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#if defined(_WIN32)

#include "DolphinQt/QtUtils/SetWindowDecorations.h"

#include <QApplication>
#include <QEvent>
#include <QWidget>

#include <dwmapi.h>

#include "DolphinQt/Settings.h"

namespace
{

void SetQWidgetWindowDecorations(QWidget* widget)
{
  if (!Settings::Instance().IsThemeDark())
    return;

  constexpr DWORD attribute = 20;  // DWMWINDOWATTRIBUTE::DWMWA_USE_IMMERSIVE_DARK_MODE
  constexpr BOOL use_dark_title_bar = TRUE;

  DwmSetWindowAttribute(HWND(widget->winId()), attribute, &use_dark_title_bar,
                        DWORD(sizeof(use_dark_title_bar)));
}

class WindowDecorationFilter final : public QObject
{
public:
  using QObject::QObject;

  bool eventFilter(QObject* obj, QEvent* event) override
  {
    if (event->type() == QEvent::Show || event->type() == QEvent::ApplicationPaletteChange)
    {
      SetQWidgetWindowDecorations(static_cast<QWidget*>(obj));
    }
    return QObject::eventFilter(obj, event);
  }
};

class WindowDecorationFilterInstaller final : public QObject
{
public:
  using QObject::QObject;

  bool eventFilter(QObject* obj, QEvent* event) override
  {
    if (event->type() == QEvent::WinIdChange)
    {
      auto* const widget = qobject_cast<QWidget*>(obj);

      // Install the real filter on actual Window QWidgets.
      // This avoids the need for a qobject_cast on literally every QEvent::Show.
      if (widget && widget->isWindow())
        widget->installEventFilter(m_decoration_filter);
    }
    return QObject::eventFilter(obj, event);
  }

private:
  QObject* const m_decoration_filter = new WindowDecorationFilter{this};
};

}  // namespace

namespace QtUtils
{

void InstallWindowDecorationFilter(QApplication* app)
{
  app->installEventFilter(new WindowDecorationFilterInstaller{app});
}

}  // namespace QtUtils

#endif