Sine Striker
2023-12-19 245ddfa1f01b4c9fa10337246b8045755e5bcce5
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
82
83
84
85
86
87
88
#include "nativeeventfilter_p.h"
 
#include <QtCore/QAbstractNativeEventFilter>
#include <QtCore/QCoreApplication>
 
namespace QWK {
 
    NativeEventFilter::NativeEventFilter() : m_dispatcher(nullptr) {
    }
 
    NativeEventFilter::~NativeEventFilter() {
        if (m_dispatcher)
            m_dispatcher->removeNativeEventFilter(this);
    }
 
    NativeEventDispatcher::NativeEventDispatcher() = default;
 
    NativeEventDispatcher::~NativeEventDispatcher() {
        for (const auto &observer : std::as_const(m_nativeEventFilters)) {
            observer->m_dispatcher = nullptr;
        }
    }
 
    bool NativeEventDispatcher::dispatch(const QByteArray &eventType, void *message,
                                         QT_NATIVE_EVENT_RESULT_TYPE *result) {
        for (const auto &ef : std::as_const(m_nativeEventFilters)) {
            if (ef->nativeEventFilter(eventType, message, result))
                return true;
        }
        return false;
    }
 
    void NativeEventDispatcher::installNativeEventFilter(NativeEventFilter *filter) {
        if (!filter || filter->m_dispatcher)
            return;
 
        m_nativeEventFilters.append(filter);
        filter->m_dispatcher = this;
    }
 
    void NativeEventDispatcher::removeNativeEventFilter(NativeEventFilter *filter) {
        if (!m_nativeEventFilters.removeOne(filter)) {
            return;
        }
        filter->m_dispatcher = nullptr;
    }
 
 
    // Avoid adding multiple global native event filters to QGuiApplication
    // in this library.
    class AppMasterNativeEventFilter : public QAbstractNativeEventFilter,
                                       public NativeEventDispatcher {
    public:
        AppMasterNativeEventFilter() {
            qApp->installNativeEventFilter(this);
        }
 
        // The base class removes automatically
        ~AppMasterNativeEventFilter() override = default;
 
        bool nativeEventFilter(const QByteArray &eventType, void *message,
                               QT_NATIVE_EVENT_RESULT_TYPE *result) override {
            return dispatch(eventType, message, result);
        }
 
        static AppMasterNativeEventFilter *instance;
 
        friend class AppNativeEventFilter;
    };
 
    AppMasterNativeEventFilter *AppMasterNativeEventFilter::instance = nullptr;
 
    AppNativeEventFilter::AppNativeEventFilter() {
        if (!AppMasterNativeEventFilter::instance) {
            AppMasterNativeEventFilter::instance = new AppMasterNativeEventFilter();
        }
        AppMasterNativeEventFilter::instance->installNativeEventFilter(this);
    }
 
    AppNativeEventFilter::~AppNativeEventFilter() {
        AppMasterNativeEventFilter::instance->removeNativeEventFilter(this);
        if (AppMasterNativeEventFilter::instance->m_nativeEventFilters.isEmpty()) {
            delete AppMasterNativeEventFilter::instance;
            AppMasterNativeEventFilter::instance = nullptr;
        }
    }
 
}