From bdb1b5d68fd9fd59b55d07d32dba28e8ad08f5cf Mon Sep 17 00:00:00 2001 From: Yuhang Zhao Date: Fri, 25 Aug 2023 18:19:32 +0800 Subject: [PATCH] snap layout: improve --- src/core/framelesshelper_win.cpp | 21 ++++++++++++++++----- src/widgets/standardtitlebar.cpp | 11 +++++++---- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/core/framelesshelper_win.cpp b/src/core/framelesshelper_win.cpp index ffcb138e..85b3b9c0 100644 --- a/src/core/framelesshelper_win.cpp +++ b/src/core/framelesshelper_win.cpp @@ -58,6 +58,8 @@ FRAMELESSHELPER_BEGIN_NAMESPACE using namespace Global; +static constexpr const auto kMessageTag = WPARAM(2546789017); + FRAMELESSHELPER_STRING_CONSTANT(MonitorFromWindow) FRAMELESSHELPER_STRING_CONSTANT(GetMonitorInfoW) FRAMELESSHELPER_STRING_CONSTANT(ScreenToClient) @@ -173,6 +175,11 @@ Q_GLOBAL_STATIC(FramelessWin32HelperInternal, g_framelessWin32HelperData) return WindowPart::NotInterested; } +[[nodiscard]] static inline constexpr bool isTaggedMessage(const WPARAM wParam) +{ + return (wParam == kMessageTag); +} + [[nodiscard]] static inline bool listenForMouseLeave(const HWND hWnd, const bool nonClient) { Q_ASSERT(hWnd); @@ -340,7 +347,7 @@ bool FramelessHelperWin::nativeEventFilter(const QByteArray &eventType, void *me const auto emulateClientAreaMessage = [hWnd, uMsg, wParam, lParam](const std::optional overrideMessage = std::nullopt) -> void { const auto wparam = [uMsg, wParam]() -> WPARAM { if (uMsg == WM_NCMOUSELEAVE) { - return 0; + return kMessageTag; } const quint64 keyState = Utils::getKeyState(); if ((uMsg >= WM_NCXBUTTONDOWN) && (uMsg <= WM_NCXBUTTONDBLCLK)) { @@ -424,7 +431,7 @@ bool FramelessHelperWin::nativeEventFilter(const QByteArray &eventType, void *me } }; - if (uMsg == WM_MOUSELEAVE) { + if ((uMsg == WM_MOUSELEAVE) && !isTaggedMessage(wParam)) { // Qt will call TrackMouseEvent() to get the WM_MOUSELEAVE message when it receives // WM_MOUSEMOVE messages, and since we are converting every WM_NCMOUSEMOVE message // to WM_MOUSEMOVE message and send it back to the window to be able to hover our @@ -998,9 +1005,13 @@ bool FramelessHelperWin::nativeEventFilter(const QByteArray &eventType, void *me if (nowWindowPart == WindowPart::NotInterested) { std::ignore = listenForMouseLeave(hWnd, false); } - if ((previousWindowPart == WindowPart::ChromeButton) && (nowWindowPart == WindowPart::ClientArea)) { - *result = FALSE; - return true; + if (previousWindowPart == WindowPart::ChromeButton) { + if (nowWindowPart == WindowPart::ClientArea) { + *result = FALSE; + return true; + } else if (nowWindowPart == WindowPart::NotInterested) { + emulateClientAreaMessage(WM_NCMOUSELEAVE); + } } } else { if (uMsg == WM_NCMOUSEMOVE) { diff --git a/src/widgets/standardtitlebar.cpp b/src/widgets/standardtitlebar.cpp index e11c75aa..7df5b5f9 100644 --- a/src/widgets/standardtitlebar.cpp +++ b/src/widgets/standardtitlebar.cpp @@ -323,8 +323,6 @@ void StandardTitleBarPrivate::initialize() this, &StandardTitleBarPrivate::updateTitleBarColor); connect(chromePalette, &ChromePalette::chromeButtonColorChanged, this, &StandardTitleBarPrivate::updateChromeButtonColor); - q->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); - q->setFixedHeight(kDefaultTitleBarHeight); connect(window, &QWidget::windowIconChanged, this, [q](const QIcon &icon){ Q_UNUSED(icon); q->update(); @@ -337,7 +335,6 @@ void StandardTitleBarPrivate::initialize() const auto titleBarLayout = new QHBoxLayout(q); titleBarLayout->setSpacing(0); titleBarLayout->setContentsMargins(0, 0, 0, 0); - q->setTitleLabelAlignment(Qt::AlignCenter); #else // !Q_OS_MACOS minimizeButton = new StandardSystemButton(SystemButtonType::Minimize, q); connect(minimizeButton, &StandardSystemButton::clicked, window, &QWidget::showMinimized); @@ -377,7 +374,6 @@ void StandardTitleBarPrivate::initialize() titleBarLayout->setContentsMargins(0, 0, 0, 0); titleBarLayout->addStretch(); titleBarLayout->addLayout(systemButtonsOuterLayout); - q->setTitleLabelAlignment(Qt::AlignLeft | Qt::AlignVCenter); #endif // Q_OS_MACOS retranslateUi(); updateTitleBarColor(); @@ -388,6 +384,13 @@ void StandardTitleBarPrivate::initialize() StandardTitleBar::StandardTitleBar(QWidget *parent) : QWidget(parent), d_ptr(new StandardTitleBarPrivate(this)) { + setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); + setFixedHeight(kDefaultTitleBarHeight); +#ifdef Q_OS_MACOS + setTitleLabelAlignment(Qt::AlignCenter); +#else + setTitleLabelAlignment(Qt::AlignLeft | Qt::AlignVCenter); +#endif } StandardTitleBar::~StandardTitleBar() = default;