From e95e2bfd29b64d3144515c30fb9cf76adb75c6c3 Mon Sep 17 00:00:00 2001 From: Sine Striker <trueful@163.com> Date: 周二, 07 5月 2024 21:44:21 +0800 Subject: [PATCH] minor tweaks --- src/quick/quickwindowagent_mac.cpp | 77 ++++++++++++++++++++++++++------------ 1 files changed, 53 insertions(+), 24 deletions(-) diff --git a/src/quick/quickwindowagent_mac.cpp b/src/quick/quickwindowagent_mac.cpp index 3f6b3f8..c7a822e 100644 --- a/src/quick/quickwindowagent_mac.cpp +++ b/src/quick/quickwindowagent_mac.cpp @@ -1,19 +1,51 @@ +// Copyright (C) 2023-2024 Stdware Collections (https://www.github.com/stdware) +// Copyright (C) 2021-2023 wangwenx190 (Yuhang Zhao) +// SPDX-License-Identifier: Apache-2.0 + #include "quickwindowagent_p.h" namespace QWK { - /*! - Returns the item that acts as the system button area. - */ + class SystemButtonAreaItemHandler : public QObject { + public: + SystemButtonAreaItemHandler(QQuickItem *item, AbstractWindowContext *ctx, + QObject *parent = nullptr); + ~SystemButtonAreaItemHandler() override = default; + + void updateSystemButtonArea(); + + protected: + QQuickItem *item; + AbstractWindowContext *ctx; + }; + + SystemButtonAreaItemHandler::SystemButtonAreaItemHandler(QQuickItem *item, + AbstractWindowContext *ctx, + QObject *parent) + : QObject(parent), item(item), ctx(ctx) { + connect(item, &QQuickItem::xChanged, this, + &SystemButtonAreaItemHandler::updateSystemButtonArea); + connect(item, &QQuickItem::yChanged, this, + &SystemButtonAreaItemHandler::updateSystemButtonArea); + connect(item, &QQuickItem::widthChanged, this, + &SystemButtonAreaItemHandler::updateSystemButtonArea); + connect(item, &QQuickItem::heightChanged, this, + &SystemButtonAreaItemHandler::updateSystemButtonArea); + + ctx->setSystemButtonAreaCallback([item](const QSize &) { + return QRectF(item->mapToScene(QPointF(0, 0)), item->size()).toRect(); // + }); + } + + void SystemButtonAreaItemHandler::updateSystemButtonArea() { + ctx->virtual_hook(AbstractWindowContext::SystemButtonAreaChangedHook, nullptr); + } + 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) @@ -22,25 +54,22 @@ auto ctx = d->context.get(); d->systemButtonAreaItem = item; if (!item) { - disconnect(systemButtonAreaItemXChangedConnection); - disconnect(systemButtonAreaItemYChangedConnection); - disconnect(systemButtonAreaItemWidthChangedConnection); - disconnect(systemButtonAreaItemHeightChangedConnection); - systemButtonAreaItemXChangedConnection = {}; - systemButtonAreaItemYChangedConnection = {}; - systemButtonAreaItemWidthChangedConnection = {}; - systemButtonAreaItemHeightChangedConnection = {}; - ctx->setSystemButtonArea({}); + d->systemButtonAreaItemHandler.reset(); + ctx->setSystemButtonAreaCallback({}); 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(); + d->systemButtonAreaItemHandler = std::make_unique<SystemButtonAreaItemHandler>(item, ctx); + } + + ScreenRectCallback QuickWindowAgent::systemButtonAreaCallback() const { + Q_D(const QuickWindowAgent); + return d->systemButtonAreaItem ? nullptr : d->context->systemButtonAreaCallback(); + } + + void QuickWindowAgent::setSystemButtonAreaCallback(const ScreenRectCallback &callback) { + Q_D(QuickWindowAgent); + setSystemButtonArea(nullptr); + d->context->setSystemButtonAreaCallback(callback); } } -- Gitblit v1.9.1