Sine Striker
2023-12-17 52a662ed17db6c3a3ff2050d61bc2e06ea21b056
Add Mac system button area interfaces
6个文件已修改
1个文件已添加
101 ■■■■■ 已修改文件
src/core/contexts/abstractwindowcontext.cpp 7 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/core/contexts/abstractwindowcontext_p.h 17 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/core/contexts/cocoawindowcontext.mm 13 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/widgets/CMakeLists.txt 2 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/widgets/widgetwindowagent.h 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/widgets/widgetwindowagent_mac.cpp 53 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/widgets/widgetwindowagent_p.h 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/core/contexts/abstractwindowcontext.cpp
@@ -76,6 +76,13 @@
        return true;
    }
#ifdef Q_OS_MAC
    void AbstractWindowContext::setSystemButtonArea(const QRect &rect) {
        m_systemButtonArea = rect;
        virtual_hook(SystemButtonAreaChangedHook, nullptr);
    }
#endif
    bool AbstractWindowContext::isInSystemButtons(const QPoint &pos,
                                                  WindowAgentBase::SystemButton *button) const {
        *button = WindowAgentBase::Unknown;
src/core/contexts/abstractwindowcontext_p.h
@@ -36,6 +36,11 @@
        inline QObject *titleBar() const;
        bool setTitleBar(QObject *obj);
#ifdef Q_OS_MAC
        inline QRect systemButtonArea() const;
        void setSystemButtonArea(const QRect &rect);
#endif
        bool isInSystemButtons(const QPoint &pos, WindowAgentBase::SystemButton *button) const;
        bool isInTitleBarDraggableArea(const QPoint &pos) const;
@@ -45,7 +50,8 @@
            CentralizeHook = 1,
            ShowSystemMenuHook,
            DefaultColorsHook,
            DrawWindows10BorderHook, // Only works on Windows 10
            DrawWindows10BorderHook,     // Only works on Windows 10
            SystemButtonAreaChangedHook, // Only works on Mac
        };
        virtual void virtual_hook(int id, void *data);
@@ -60,6 +66,9 @@
        QWindow *m_windowHandle{};
        QSet<const QObject *> m_hitTestVisibleItems;
#ifdef Q_OS_MAC
        QRect m_systemButtonArea;
#endif
        QObject *m_titleBar{};
        std::array<QObject *, WindowAgentBase::NumSystemButton> m_systemButtons{};
@@ -90,6 +99,12 @@
        return m_titleBar;
    }
#ifdef Q_OS_MAC
    inline QRect AbstractWindowContext::systemButtonArea() const {
        return m_systemButtonArea;
    }
#endif
}
#endif // ABSTRACTWINDOWCONTEXT_P_H
src/core/contexts/cocoawindowcontext.mm
@@ -105,7 +105,7 @@
            nswindow.showsToolbarButton = NO;
            nswindow.movableByWindowBackground = NO;
            nswindow.movable = NO; // This line causes the window in the wrong position when
            // become fullscreen.
                                   // become fullscreen.
            //  For some unknown reason, we don't need the following hack in Qt versions below or
            //  equal to 6.2.4.
#if (QT_VERSION > QT_VERSION_CHECK(6, 2, 4))
@@ -384,6 +384,17 @@
    }
    void CocoaWindowContext::virtual_hook(int id, void *data) {
        switch (id) {
            case ShowSystemMenuHook:
                // TODO: mac system menu
                return;
            case SystemButtonAreaChangedHook:
                // TODO: mac system button rect updated
                return;
            default:
                break;
        }
        AbstractWindowContext::virtual_hook(id, data);
    }
    bool CocoaWindowContext::setupHost() {
src/widgets/CMakeLists.txt
@@ -14,6 +14,8 @@
if(WIN32)
    list(APPEND _src widgetwindowagent_win.cpp)
elseif(APPLE)
    list(APPEND _src widgetwindowagent_mac.cpp)
endif()
qwk_add_library(${PROJECT_NAME} AUTOGEN
src/widgets/widgetwindowagent.h
@@ -26,6 +26,11 @@
        QWidget *systemButton(SystemButton button) const;
        void setSystemButton(SystemButton button, QWidget *w);
#ifdef Q_OS_MAC
        QWidget *systemButtonArea() const;
        void setSystemButtonArea(QWidget *widget);
#endif
        bool isHitTestVisible(const QWidget *w) const;
        void setHitTestVisible(const QWidget *w, bool visible = true);
src/widgets/widgetwindowagent_mac.cpp
New file
@@ -0,0 +1,53 @@
#include "widgetwindowagent_p.h"
#include <QtGui/QtEvents>
namespace QWK {
    class SystemButtonAreaWidgetEventFilter : public QObject {
    public:
        SystemButtonAreaWidgetEventFilter(QWidget *widget, AbstractWindowContext *ctx,
                                          QObject *parent = nullptr)
            : QObject(parent), widget(widget), ctx(ctx) {
            widget->installEventFilter(this);
        }
        ~SystemButtonAreaWidgetEventFilter() = default;
    protected:
        bool eventFilter(QObject *obj, QEvent *event) override {
            switch (event->type()) {
                case QEvent::Move:
                case QEvent::Resize: {
                    ctx->setSystemButtonArea(widget->geometry());
                    break;
                }
                default:
                    break;
            }
            return QObject::eventFilter(obj, event);
        }
    protected:
        QWidget *widget;
        AbstractWindowContext *ctx;
    };
    QWidget *WidgetWindowAgent::systemButtonArea() const {
        Q_D(const WidgetWindowAgent);
        return d->systemButtonAreaWidget;
    }
    void WidgetWindowAgent::setSystemButtonArea(QWidget *widget) {
        Q_D(WidgetWindowAgent);
        d->systemButtonAreaWidget = widget;
        if (!widget) {
            systemButtonAreaWidgetEventFilter.reset();
            d->context->setSystemButtonArea({});
            return;
        }
        systemButtonAreaWidgetEventFilter =
            std::make_unique<SystemButtonAreaWidgetEventFilter>(widget, d->context);
    }
}
src/widgets/widgetwindowagent_p.h
@@ -16,6 +16,10 @@
        // Host
        QWidget *hostWidget{};
#ifdef Q_OS_MAC
        QWidget *systemButtonAreaWidget{};
        std::unique_ptr<QObject> systemButtonAreaWidgetEventFilter;
#endif
#ifdef Q_OS_WINDOWS
        void setupWindows10BorderWorkaround();