diff --git a/src/core/contexts/abstractwindowcontext.cpp b/src/core/contexts/abstractwindowcontext.cpp index ded9614..a87e912 100644 --- a/src/core/contexts/abstractwindowcontext.cpp +++ b/src/core/contexts/abstractwindowcontext.cpp @@ -62,16 +62,15 @@ namespace QWK { } bool AbstractWindowContext::setTitleBar(QObject *item) { - Q_ASSERT(item); auto org = m_titleBar; if (org == item) { return false; } - if (org) { - // Since the title bar is changed, all items inside it should be dereferenced right away - removeSystemButtonsAndHitTestItems(); - } + // All system buttons and hit-test visible items are dereferenced, even if the previous + // title bar has been destroyed or never existed. Setting a null title bar also dereferences + // them. + removeSystemButtonsAndHitTestItems(); m_titleBar = item; return true; } diff --git a/src/core/windowagentbase.cpp b/src/core/windowagentbase.cpp index 1ed4e32..b0ba82c 100644 --- a/src/core/windowagentbase.cpp +++ b/src/core/windowagentbase.cpp @@ -78,6 +78,44 @@ namespace QWK { context.reset(ctx); } + WindowAgentBasePrivate::SystemButtonList WindowAgentBasePrivate::systemButtons() const { + SystemButtonList result; + for (int i = WindowAgentBase::WindowIcon; i <= WindowAgentBase::Close; ++i) { + const auto role = static_cast(i); + if (auto button = context->systemButton(role)) { + result.append({role, button}); + } + } + return result; + } + + bool WindowAgentBasePrivate::replaceTitleBar(QObject *item) { + const auto cleared = systemButtons(); + if (!context->setTitleBar(item)) { + return false; + } + + QObject::disconnect(titleBarDestroyedConnection); + titleBarDestroyedConnection = {}; + if (item) { + titleBarDestroyedConnection = QObject::connect(item, &QObject::destroyed, q_ptr, + [this] { handleTitleBarDestroyed(); }); + } + + notifyTitleBarReplaced(item, cleared); + return true; + } + + // The title bar is destroyed while this function runs. Its derived part, such as the QWidget + // or the QQuickItem, is already destroyed at this point, so only the QObject-level state and + // the pointers stored in the context are accessed. + void WindowAgentBasePrivate::handleTitleBarDestroyed() { + titleBarDestroyedConnection = {}; + const auto cleared = systemButtons(); + context->setTitleBar(nullptr); + notifyTitleBarDestroyed(cleared); + } + /*! Destructor. */ diff --git a/src/core/windowagentbase_p.h b/src/core/windowagentbase_p.h index e1e9ab6..fed899e 100644 --- a/src/core/windowagentbase_p.h +++ b/src/core/windowagentbase_p.h @@ -17,6 +17,10 @@ #include #include +#include +#include +#include + namespace QWK { class QWK_CORE_EXPORT WindowAgentBasePrivate { @@ -35,12 +39,33 @@ namespace QWK { std::unique_ptr context; + using SystemButtonList = QList>; + + // Returns the system buttons set on the context, in the order of their roles. + SystemButtonList systemButtons() const; + + // Sets \a item as the title bar of the context and connects its destroyed signal. + // Returns \c false if the context rejects the change. No notification is emitted then. + bool replaceTitleBar(QObject *item); + + // Called after the title bar is replaced with \a item. \a clearedButtons lists the system + // buttons that were unset by the replacement. Implementations emit the public signals. + virtual void notifyTitleBarReplaced(QObject *item, + const SystemButtonList &clearedButtons) = 0; + + // Called after the title bar is destroyed. \a clearedButtons lists the system buttons that + // were unset by the destruction. Implementations emit the public signals. + virtual void notifyTitleBarDestroyed(const SystemButtonList &clearedButtons) = 0; + public: using WindowContextFactoryMethod = AbstractWindowContext *(*) (); static WindowContextFactoryMethod windowContextFactoryMethod; private: + QMetaObject::Connection titleBarDestroyedConnection; + void handleTitleBarDestroyed(); + Q_DISABLE_COPY(WindowAgentBasePrivate) }; diff --git a/src/quick/quickwindowagent.cpp b/src/quick/quickwindowagent.cpp index ccdc7d9..d528242 100644 --- a/src/quick/quickwindowagent.cpp +++ b/src/quick/quickwindowagent.cpp @@ -66,15 +66,43 @@ namespace QWK { return static_cast(d->context->titleBar()); } + /*! + Sets the title bar item to \a item. Passing \c nullptr removes the title bar. All system + buttons and hit-test visible items are unset when the title bar changes. The title bar + is unset in the same way when it is destroyed. + + A system button or hit-test visible item must be unset before it is moved out of the + title bar, see \sa setSystemButton and \sa setHitTestVisible. The title bar must be set to + \c nullptr before it is moved out of the window. + */ void QuickWindowAgent::setTitleBar(QQuickItem *item) { Q_D(QuickWindowAgent); - if (!d->context->setTitleBar(item)) { - return; + d->replaceTitleBar(item); + } + + void QuickWindowAgentPrivate::notifyTitleBarReplaced(QObject *item, + const SystemButtonList &clearedButtons) { + Q_Q(QuickWindowAgent); + for (const auto &entry : clearedButtons) { + Q_EMIT q->systemButtonChanged(entry.first, nullptr); } #ifdef Q_OS_MAC - setSystemButtonArea(nullptr); + q->setSystemButtonArea(nullptr); +#endif + Q_EMIT q->titleBarWidgetChanged(static_cast(item)); + } + + // The macOS updates are queued, so that they run after the destruction finishes. + void QuickWindowAgentPrivate::notifyTitleBarDestroyed(const SystemButtonList &clearedButtons) { + Q_Q(QuickWindowAgent); + for (const auto &entry : clearedButtons) { + Q_EMIT q->systemButtonChanged(entry.first, nullptr); + } + Q_EMIT q->titleBarWidgetChanged(nullptr); +#ifdef Q_OS_MAC + QMetaObject::invokeMethod( + q, [q] { q->setSystemButtonArea(nullptr); }, Qt::QueuedConnection); #endif - Q_EMIT titleBarWidgetChanged(item); } QQuickItem *QuickWindowAgent::systemButton(SystemButton button) const { @@ -82,6 +110,15 @@ namespace QWK { return static_cast(d->context->systemButton(button)); } + /*! + Sets \a item as the system button of the given type. Passing \c nullptr unsets the system + button of this type. A system button receives mouse events on the title bar, so calling + \sa setHitTestVisible for \a item is not required. + + While \a item is the system button, it must be a child or descendant of the title bar item. + To move \a item out of the title bar, first call this function with \c nullptr for the same + button type. Moving a system button out of the title bar is not supported. + */ void QuickWindowAgent::setSystemButton(SystemButton button, QQuickItem *item) { Q_D(QuickWindowAgent); if (!d->context->setSystemButton(button, item)) { @@ -100,6 +137,14 @@ namespace QWK { return d->context->isHitTestVisible(item); } + /*! + Makes \a item able to receive mouse events on the title bar if \a visible is \c true. + Otherwise, \a item no longer receives them. + + While \a item is hit-test visible, it must be a child or descendant of the title bar item. + To move \a item out of the title bar, first call this function with \a visible set to + \c false. + */ void QuickWindowAgent::setHitTestVisible(QQuickItem *item, bool visible) { Q_D(QuickWindowAgent); d->context->setHitTestVisible(item, visible); diff --git a/src/quick/quickwindowagent_p.h b/src/quick/quickwindowagent_p.h index 887e901..6078784 100644 --- a/src/quick/quickwindowagent_p.h +++ b/src/quick/quickwindowagent_p.h @@ -14,6 +14,7 @@ // version without notice, or may even be removed. // +#include #include #include @@ -33,6 +34,9 @@ namespace QWK { // Host QQuickWindow *hostWindow{}; + void notifyTitleBarReplaced(QObject *item, const SystemButtonList &clearedButtons) override; + void notifyTitleBarDestroyed(const SystemButtonList &clearedButtons) override; + #ifdef Q_OS_MAC QPointer systemButtonAreaItem; std::unique_ptr systemButtonAreaItemHandler; diff --git a/src/widgets/widgetwindowagent.cpp b/src/widgets/widgetwindowagent.cpp index 0ae4fed..da3af22 100644 --- a/src/widgets/widgetwindowagent.cpp +++ b/src/widgets/widgetwindowagent.cpp @@ -91,19 +91,48 @@ namespace QWK { } /*! - Sets the title bar widget, all system button and hit-test visible widget references that - have been set will be removed. + Sets the title bar widget to \a w. Passing \c nullptr removes the title bar. All system + buttons and hit-test visible widgets are unset when the title bar changes. The title bar + is unset in the same way when it is destroyed. + + A system button or hit-test visible widget must be unset before it is moved out of the + title bar, see \sa setSystemButton and \sa setHitTestVisible. The title bar must be set to + \c nullptr before it is moved out of the window. */ void WidgetWindowAgent::setTitleBar(QWidget *w) { Q_D(WidgetWindowAgent); - if (!d->context->setTitleBar(w)) { - return; + d->replaceTitleBar(w); + } + + void WidgetWindowAgentPrivate::notifyTitleBarReplaced(QObject *item, + const SystemButtonList &clearedButtons) { + Q_Q(WidgetWindowAgent); + for (const auto &entry : clearedButtons) { + Q_EMIT q->systemButtonChanged(entry.first, nullptr); } #ifdef Q_OS_MAC - setSystemButtonArea(nullptr); - d->updateCocoaTitleBar(); + q->setSystemButtonArea(nullptr); + updateCocoaTitleBar(); +#endif + Q_EMIT q->titleBarChanged(static_cast(item)); + } + + // The macOS updates are queued, so that they run after the destruction finishes. + void WidgetWindowAgentPrivate::notifyTitleBarDestroyed(const SystemButtonList &clearedButtons) { + Q_Q(WidgetWindowAgent); + for (const auto &entry : clearedButtons) { + Q_EMIT q->systemButtonChanged(entry.first, nullptr); + } + Q_EMIT q->titleBarChanged(nullptr); +#ifdef Q_OS_MAC + QMetaObject::invokeMethod( + q, + [this, q] { + q->setSystemButtonArea(nullptr); + updateCocoaTitleBar(); + }, + Qt::QueuedConnection); #endif - Q_EMIT titleBarChanged(w); } /*! @@ -115,8 +144,13 @@ namespace QWK { } /*! - Sets the system button of the given type, the system buttons always receive mouse events so - you don't need to call \c setHitTestVisible for them. + Sets \a w as the system button of the given type. Passing \c nullptr unsets the system + button of this type. A system button receives mouse events on the title bar, so calling + \sa setHitTestVisible for \a w is not required. + + While \a w is the system button, it must be a child or descendant of the title bar widget. + To move \a w out of the title bar, first call this function with \c nullptr for the same + button type. Moving a system button out of the title bar is not supported. */ void WidgetWindowAgent::setSystemButton(SystemButton button, QWidget *w) { Q_D(WidgetWindowAgent); @@ -135,9 +169,12 @@ namespace QWK { } /*! - Makes the widget able to receive mouse events on title bar if \a visible is \c true. - You're supposed to make sure that the specified widget \a w is a child or descendant - of the title bar widget. + Makes \a w able to receive mouse events on the title bar if \a visible is \c true. + Otherwise, \a w no longer receives them. + + While \a w is hit-test visible, it must be a child or descendant of the title bar widget. + To move \a w out of the title bar, you should call this function with \a visible set to + \c false first. */ void WidgetWindowAgent::setHitTestVisible(QWidget *w, bool visible) { Q_D(WidgetWindowAgent); diff --git a/src/widgets/widgetwindowagent_p.h b/src/widgets/widgetwindowagent_p.h index 1073002..41964fb 100644 --- a/src/widgets/widgetwindowagent_p.h +++ b/src/widgets/widgetwindowagent_p.h @@ -18,6 +18,7 @@ #include #include +#include #include namespace QWK { @@ -33,6 +34,9 @@ namespace QWK { // Host QWidget *hostWidget{}; + void notifyTitleBarReplaced(QObject *item, const SystemButtonList &clearedButtons) override; + void notifyTitleBarDestroyed(const SystemButtonList &clearedButtons) override; + #ifdef Q_OS_MAC void setupCocoaWindow(); void updateCocoaTitleBar();