From 96833537013e55ee42ef86ea75afca373bdced29 Mon Sep 17 00:00:00 2001 From: Zhao Yuhang <2546789017@qq.com> Date: ćšć, 28 12æ 2023 20:23:50 +0800 Subject: [PATCH] add quick mac button API --- src/quick/quickwindowagent.h | 5 ++ src/quick/quickwindowagent_mac.cpp | 46 +++++++++++++++++++++++ src/quick/CMakeLists.txt | 4 + src/quick/quickwindowagent_p.h | 10 ++++ 4 files changed, 63 insertions(+), 2 deletions(-) diff --git a/src/quick/CMakeLists.txt b/src/quick/CMakeLists.txt index 5e4978c..31c603c 100644 --- a/src/quick/CMakeLists.txt +++ b/src/quick/CMakeLists.txt @@ -15,6 +15,8 @@ if(WIN32) list(APPEND _src quickwindowagent_win.cpp) +elseif(APPLE) + list(APPEND _src quickwindowagent_mac.cpp) endif() qwk_add_library(${PROJECT_NAME} AUTOGEN @@ -32,4 +34,4 @@ ) set(QWINDOWKIT_ENABLED_TARGETS ${QWINDOWKIT_ENABLED_TARGETS} ${PROJECT_NAME} PARENT_SCOPE) -set(QWINDOWKIT_ENABLED_SUBDIRECTORIES ${QWINDOWKIT_ENABLED_SUBDIRECTORIES} quick PARENT_SCOPE) \ No newline at end of file +set(QWINDOWKIT_ENABLED_SUBDIRECTORIES ${QWINDOWKIT_ENABLED_SUBDIRECTORIES} quick PARENT_SCOPE) diff --git a/src/quick/quickwindowagent.h b/src/quick/quickwindowagent.h index 4d6160b..58703a8 100644 --- a/src/quick/quickwindowagent.h +++ b/src/quick/quickwindowagent.h @@ -30,6 +30,11 @@ Q_INVOKABLE bool isHitTestVisible(const QQuickItem *item) const; Q_INVOKABLE void setHitTestVisible(const QQuickItem *item, bool visible = true); +#ifdef Q_OS_MAC + QQuickItem *systemButtonArea() const; + void setSystemButtonArea(QQuickItem *item); +#endif + Q_SIGNALS: void titleBarWidgetChanged(const QQuickItem *item); void systemButtonChanged(SystemButton button, const QQuickItem *item); diff --git a/src/quick/quickwindowagent_mac.cpp b/src/quick/quickwindowagent_mac.cpp new file mode 100644 index 0000000..3f6b3f8 --- /dev/null +++ b/src/quick/quickwindowagent_mac.cpp @@ -0,0 +1,46 @@ +#include "quickwindowagent_p.h" + +namespace QWK { + + /*! + Returns the item that acts as the system button area. + */ + QQuickItem *QuickWindowAgent::systemButtonArea() const { + Q_D(const QuickWindowAgent); + return d->systemButtonAreaItem; + } + + /*! + Sets the item that acts as the system button area. The system button will be centered in + its area, it is recommended to place the item in a layout and set a fixed size policy. + */ + void QuickWindowAgent::setSystemButtonArea(QQuickItem *item) { + Q_D(QuickWindowAgent); + if (d->systemButtonAreaItem == item) + return; + + auto ctx = d->context.get(); + d->systemButtonAreaItem = item; + if (!item) { + disconnect(systemButtonAreaItemXChangedConnection); + disconnect(systemButtonAreaItemYChangedConnection); + disconnect(systemButtonAreaItemWidthChangedConnection); + disconnect(systemButtonAreaItemHeightChangedConnection); + systemButtonAreaItemXChangedConnection = {}; + systemButtonAreaItemYChangedConnection = {}; + systemButtonAreaItemWidthChangedConnection = {}; + systemButtonAreaItemHeightChangedConnection = {}; + ctx->setSystemButtonArea({}); + return; + } + const auto updateSystemButtonArea = [ctx, item]() -> void { + ctx->setSystemButtonArea(QRectF(item->mapToScene(QPointF(0, 0)), item->size()).toRect()); + }; + systemButtonAreaItemXChangedConnection = connect(item, &QQuickItem::xChanged, this, updateSystemButtonArea); + systemButtonAreaItemYChangedConnection = connect(item, &QQuickItem::yChanged, this, updateSystemButtonArea); + systemButtonAreaItemWidthChangedConnection = connect(item, &QQuickItem::widthChanged, this, updateSystemButtonArea); + systemButtonAreaItemHeightChangedConnection = connect(item, &QQuickItem::heightChanged, this, updateSystemButtonArea); + updateSystemButtonArea(); + } + +} diff --git a/src/quick/quickwindowagent_p.h b/src/quick/quickwindowagent_p.h index e63fbfe..f2f1bb1 100644 --- a/src/quick/quickwindowagent_p.h +++ b/src/quick/quickwindowagent_p.h @@ -27,6 +27,14 @@ // Host QQuickWindow *hostWindow{}; +#ifdef Q_OS_MAC + QQuickItem *systemButtonAreaItem{}; + QMetaObject::Connection systemButtonAreaItemXChangedConnection{}; + QMetaObject::Connection systemButtonAreaItemYChangedConnection{}; + QMetaObject::Connection systemButtonAreaItemWidthChangedConnection{}; + QMetaObject::Connection systemButtonAreaItemHeightChangedConnection{}; +#endif + #if defined(Q_OS_WINDOWS) && QWINDOWKIT_CONFIG(ENABLE_WINDOWS_SYSTEM_BORDERS) void setupWindows10BorderWorkaround(); #endif @@ -34,4 +42,4 @@ } -#endif // QUICKWINDOWAGENTPRIVATE_H \ No newline at end of file +#endif // QUICKWINDOWAGENTPRIVATE_H -- Gitblit v1.9.1