Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/core/contexts/abstractwindowcontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
38 changes: 38 additions & 0 deletions src/core/windowagentbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<WindowAgentBase::SystemButton>(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.
*/
Expand Down
25 changes: 25 additions & 0 deletions src/core/windowagentbase_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
#include <QWKCore/windowagentbase.h>
#include <QWKCore/private/abstractwindowcontext_p.h>

#include <QtCore/QList>
#include <QtCore/QMetaObject>
#include <QtCore/QObject>

namespace QWK {

class QWK_CORE_EXPORT WindowAgentBasePrivate {
Expand All @@ -35,12 +39,33 @@ namespace QWK {

std::unique_ptr<AbstractWindowContext> context;

using SystemButtonList = QList<std::pair<WindowAgentBase::SystemButton, QObject *>>;

// 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)
};

Expand Down
53 changes: 49 additions & 4 deletions src/quick/quickwindowagent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,59 @@ namespace QWK {
return static_cast<QQuickItem *>(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<QQuickItem *>(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 {
Q_D(const QuickWindowAgent);
return static_cast<QQuickItem *>(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)) {
Expand All @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions src/quick/quickwindowagent_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// version without notice, or may even be removed.
//

#include <QtCore/QObject>
#include <QPointer>

#include <QWKCore/qwkconfig.h>
Expand All @@ -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<QQuickItem> systemButtonAreaItem;
std::unique_ptr<QObject> systemButtonAreaItemHandler;
Expand Down
61 changes: 49 additions & 12 deletions src/widgets/widgetwindowagent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<QWidget *>(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);
}

/*!
Expand All @@ -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);
Expand All @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions src/widgets/widgetwindowagent_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <QWKCore/private/windowagentbase_p.h>
#include <QWKWidgets/widgetwindowagent.h>

#include <QtCore/QObject>
#include <QtCore/QPointer>

namespace QWK {
Expand All @@ -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();
Expand Down
Loading