Skip to content
This repository has been archived by the owner on Dec 19, 2023. It is now read-only.

Emulate leave event for max button #278

Merged
merged 1 commit into from
Aug 31, 2023
Merged
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
37 changes: 36 additions & 1 deletion src/widgets/standardtitlebar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <QtGui/qpainter.h>
#include <QtGui/qevent.h>
#include <QtGui/qfontmetrics.h>
#include <QtWidgets/qapplication.h>
#include <QtWidgets/qboxlayout.h>

FRAMELESSHELPER_BEGIN_NAMESPACE
Expand All @@ -57,6 +58,33 @@ FRAMELESSHELPER_BEGIN_NAMESPACE

using namespace Global;

static void emulateLeaveEvent(QAbstractButton *button) {
QTimer::singleShot(0, button, [button](){
if (!QRect(button->mapToGlobal({}), button->size()).contains(QCursor::pos())) {
QApplication::postEvent(button, new QEvent(QEvent::Leave));
if (button->testAttribute(Qt::WA_Hover)) {
QPoint localPos = button->mapFromGlobal(QCursor::pos());
QPoint scenePos = button->window()->mapFromGlobal(QCursor::pos());
QPoint globalPos = QCursor::pos();
QPoint oldPos = {};
Qt::KeyboardModifiers modifiers = QApplication::keyboardModifiers();
#if (QT_VERSION >= QT_VERSION_CHECK(6, 4, 0))
auto e = new QHoverEvent(QEvent::HoverLeave, scenePos, globalPos, oldPos, modifiers);
Q_UNUSED(localPos);
#elif (QT_VERSION >= QT_VERSION_CHECK(6, 3, 0))
auto e = new QHoverEvent(QEvent::HoverLeave, localPos, globalPos, oldPos, modifiers);
Q_UNUSED(scenePos);
#else
auto e = new QHoverEvent(QEvent::HoverLeave, localPos, oldPos, modifiers);
Q_UNUSED(scenePos);
Q_UNUSED(globalPos);
#endif
QApplication::postEvent(button, e);
}
}
});
}

StandardTitleBarPrivate::StandardTitleBarPrivate(StandardTitleBar *q) : QObject(q)
{
Q_ASSERT(q);
Expand Down Expand Up @@ -341,7 +369,9 @@ void StandardTitleBarPrivate::initialize()
titleBarLayout->setContentsMargins(0, 0, 0, 0);
#elif FRAMELESSHELPER_CONFIG(system_button)
minimizeButton = new StandardSystemButton(SystemButtonType::Minimize, q);
connect(minimizeButton, &StandardSystemButton::clicked, window, &QWidget::showMinimized);
connect(minimizeButton, &StandardSystemButton::clicked, this, [this](){
window->showMinimized();
});
maximizeButton = new StandardSystemButton(SystemButtonType::Maximize, q);
updateMaximizeButton();
connect(maximizeButton, &StandardSystemButton::clicked, this, [this](){
Expand All @@ -350,6 +380,11 @@ void StandardTitleBarPrivate::initialize()
} else {
window->showMaximized();
}

// It's a Qt issue that if a QAbstractButton::clicked triggers a window's maximization,
// the button remains to be hovered until the mouse move. As a result, we need to
// manully send leave events to the button.
emulateLeaveEvent(maximizeButton);
});
closeButton = new StandardSystemButton(SystemButtonType::Close, q);
connect(closeButton, &StandardSystemButton::clicked, this, [this](){
Expand Down
Loading