From 52a662ed17db6c3a3ff2050d61bc2e06ea21b056 Mon Sep 17 00:00:00 2001 From: Sine Striker <trueful@163.com> Date: 周日, 17 12月 2023 01:07:13 +0800 Subject: [PATCH] Add Mac system button area interfaces --- src/widgets/widgetwindowagent_mac.cpp | 53 ++++++++++++++++++++++++++ src/core/contexts/abstractwindowcontext.cpp | 7 +++ src/core/contexts/cocoawindowcontext.mm | 13 ++++++ src/widgets/CMakeLists.txt | 2 + src/core/contexts/abstractwindowcontext_p.h | 17 ++++++++ src/widgets/widgetwindowagent.h | 5 ++ src/widgets/widgetwindowagent_p.h | 4 ++ 7 files changed, 99 insertions(+), 2 deletions(-) diff --git a/src/core/contexts/abstractwindowcontext.cpp b/src/core/contexts/abstractwindowcontext.cpp index 96f2935..7c40c89 100644 --- a/src/core/contexts/abstractwindowcontext.cpp +++ b/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; diff --git a/src/core/contexts/abstractwindowcontext_p.h b/src/core/contexts/abstractwindowcontext_p.h index be5ab36..a86ddd8 100644 --- a/src/core/contexts/abstractwindowcontext_p.h +++ b/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 diff --git a/src/core/contexts/cocoawindowcontext.mm b/src/core/contexts/cocoawindowcontext.mm index f8a45d5..67ca980 100644 --- a/src/core/contexts/cocoawindowcontext.mm +++ b/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() { diff --git a/src/widgets/CMakeLists.txt b/src/widgets/CMakeLists.txt index 5cdb2c2..3b8db9a 100644 --- a/src/widgets/CMakeLists.txt +++ b/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 diff --git a/src/widgets/widgetwindowagent.h b/src/widgets/widgetwindowagent.h index 20347a9..ea20025 100644 --- a/src/widgets/widgetwindowagent.h +++ b/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); diff --git a/src/widgets/widgetwindowagent_mac.cpp b/src/widgets/widgetwindowagent_mac.cpp new file mode 100644 index 0000000..d09f7b3 --- /dev/null +++ b/src/widgets/widgetwindowagent_mac.cpp @@ -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); + } + +} \ No newline at end of file diff --git a/src/widgets/widgetwindowagent_p.h b/src/widgets/widgetwindowagent_p.h index 8fee638..cb6d216 100644 --- a/src/widgets/widgetwindowagent_p.h +++ b/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(); -- Gitblit v1.9.1