Sine Striker
2023-12-07 09287f3d9e9e88271de2bfd5388dae5a53e8c6f5
Add host event filter
8个文件已修改
4个文件已添加
160 ■■■■ 已修改文件
src/core/contexts/abstractwindowcontext.cpp 38 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/core/contexts/abstractwindowcontext_p.h 6 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/core/contexts/qtwindowcontext.cpp 9 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/core/contexts/qtwindowcontext_p.h 6 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/core/contexts/win32windowcontext.cpp 6 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/core/contexts/win32windowcontext_p.h 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/quick/CMakeLists.txt 2 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/quick/contexts/quickwindowcontext.cpp 9 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/quick/contexts/quickwindowcontext_p.h 34 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/widgets/CMakeLists.txt 2 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/widgets/contexts/widgetwindowcontext.cpp 9 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/widgets/contexts/widgetwindowcontext_p.h 34 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/core/contexts/abstractwindowcontext.cpp
@@ -8,6 +8,24 @@
    AbstractWindowContext::~AbstractWindowContext() = default;
    class EventFilterForwarder : public QObject {
    public:
        using EventProc = bool (*)(QEvent *, void *);
        EventFilterForwarder(EventProc proc, void *user, QObject *parent = nullptr)
            : QObject(parent), proc(proc), user(user) {
        }
        bool eventFilter(QObject *obj, QEvent *event) override {
            Q_UNUSED(obj)
            return proc(event, user);
        }
    protected:
        EventProc proc;
        void *user;
    };
    bool AbstractWindowContext::setup(QObject *host, WindowItemDelegate *delegate) {
        if (!host || !delegate) {
            return false;
@@ -21,6 +39,21 @@
        m_host = host;
        m_delegate.reset(delegate);
        m_windowHandle = windowHandle;
        if (!setupHost()) {
            m_host = nullptr;
            m_delegate = nullptr;
            m_windowHandle = nullptr;
            return false;
        }
        // Install specific event filter
        host->installEventFilter(new EventFilterForwarder(
            [](QEvent *event, void *user) {
                return reinterpret_cast<AbstractWindowContext *>(user)->hostEventFilter(event);
            },
            this, this));
        return true;
    }
@@ -157,4 +190,9 @@
        return true;
    }
    bool AbstractWindowContext::hostEventFilter(QEvent *event) {
        Q_UNUSED(event)
        return false;
    }
}
src/core/contexts/abstractwindowcontext_p.h
@@ -20,7 +20,7 @@
        ~AbstractWindowContext() override;
    public:
        virtual bool setup(QObject *host, WindowItemDelegate *delegate);
        bool setup(QObject *host, WindowItemDelegate *delegate);
        inline QObject *host() const;
        inline QWindow *window() const;
@@ -42,6 +42,10 @@
        bool isInTitleBarDraggableArea(const QPoint &pos) const;
    protected:
        virtual bool setupHost() = 0;
        virtual bool hostEventFilter(QEvent *event);
    protected:
        QObject *m_host;
        std::unique_ptr<WindowItemDelegate> m_delegate;
        QWindow *m_windowHandle;
src/core/contexts/qtwindowcontext.cpp
@@ -46,15 +46,12 @@
    QtWindowContext::~QtWindowContext() {
    }
    bool QtWindowContext::setup(QObject *host, WindowItemDelegate *delegate) {
        if (!AbstractWindowContext::setup(host, delegate)) {
            return false;
        }
    bool QtWindowContext::setupHost() {
        return false;
    }
    bool QtWindowContext::eventFilter(QObject *obj, QEvent *event) {
        return AbstractWindowContext::eventFilter(obj, event);
    bool QtWindowContext::hostEventFilter(QEvent *event) {
        return false;
    }
}
src/core/contexts/qtwindowcontext_p.h
@@ -11,11 +11,9 @@
        QtWindowContext();
        ~QtWindowContext();
    public:
        bool setup(QObject *host, WindowItemDelegate *delegate) override;
    protected:
        bool eventFilter(QObject *obj, QEvent *event) override;
        bool setupHost() override;
        bool hostEventFilter(QEvent *event) override;
    };
}
src/core/contexts/win32windowcontext.cpp
@@ -525,11 +525,7 @@
        }
    }
    bool Win32WindowContext::setup(QObject *host, WindowItemDelegate *delegate) {
        if (!AbstractWindowContext::setup(host, delegate)) {
            return false;
        }
    bool Win32WindowContext::setupHost() {
        // Install window hook
        auto winId = m_windowHandle->winId();
        auto hWnd = reinterpret_cast<HWND>(winId);
src/core/contexts/win32windowcontext_p.h
@@ -21,9 +21,10 @@
            TitleBar,
        };
    public:
        bool setup(QObject *host, WindowItemDelegate *delegate) override;
    protected:
        bool setupHost() override;
    public:
        bool windowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam, LRESULT *result);
        // In order to perfectly apply Windows 11 Snap Layout into the Qt window, we need to
src/quick/CMakeLists.txt
@@ -10,6 +10,8 @@
    quickwindowagent.h
    quickwindowagent_p.h
    quickwindowagent.cpp
    contexts/quickwindowcontext_p.h
    contexts/quickwindowcontext.cpp
)
qwk_add_library(${PROJECT_NAME} AUTOGEN
src/quick/contexts/quickwindowcontext.cpp
New file
@@ -0,0 +1,9 @@
#include "quickwindowcontext_p.h"
namespace QWK {
    bool QuickWindowContext::hostEventFilter(QEvent *event) {
        return false;
    }
}
src/quick/contexts/quickwindowcontext_p.h
New file
@@ -0,0 +1,34 @@
#ifndef QUICKWINDOWCONTEXT_P_H
#define QUICKWINDOWCONTEXT_P_H
#include <QtGlobal>
#ifdef Q_OS_WINDOWS
#  include <QWKCore/private/win32windowcontext_p.h>
#else
#  include <QWKCore/private/qtwindowcontext_p.h>
#endif
namespace QWK {
    using CoreWindowContext =
#ifdef Q_OS_WINDOWS
        Win32WindowContext
#else
        QtWindowContext
#endif
        ;
    class QuickWindowContext : public CoreWindowContext {
        Q_OBJECT
    public:
        QuickWindowContext() = default;
        ~QuickWindowContext() override = default;
    protected:
        bool hostEventFilter(QEvent *event) override;
    };
}
#endif // QUICKWINDOWCONTEXT_P_H
src/widgets/CMakeLists.txt
@@ -10,6 +10,8 @@
    widgetwindowagent.h
    widgetwindowagent_p.h
    widgetwindowagent.cpp
    contexts/widgetwindowcontext_p.h
    contexts/widgetwindowcontext.cpp
)
qwk_add_library(${PROJECT_NAME} AUTOGEN
src/widgets/contexts/widgetwindowcontext.cpp
New file
@@ -0,0 +1,9 @@
#include "widgetwindowcontext_p.h"
namespace QWK {
    bool WidgetWindowContext::hostEventFilter(QEvent *event) {
        return false;
    }
}
src/widgets/contexts/widgetwindowcontext_p.h
New file
@@ -0,0 +1,34 @@
#ifndef WIDGETWINDOWCONTEXT_P_H
#define WIDGETWINDOWCONTEXT_P_H
#include <QtGlobal>
#ifdef Q_OS_WINDOWS
#  include <QWKCore/private/win32windowcontext_p.h>
#else
#  include <QWKCore/private/qtwindowcontext_p.h>
#endif
namespace QWK {
    using CoreWindowContext =
#ifdef Q_OS_WINDOWS
        Win32WindowContext
#else
        QtWindowContext
#endif
        ;
    class WidgetWindowContext : public CoreWindowContext {
        Q_OBJECT
    public:
        WidgetWindowContext() = default;
        ~WidgetWindowContext() override = default;
    protected:
        bool hostEventFilter(QEvent *event) override;
    };
}
#endif // WIDGETWINDOWCONTEXT_P_H