From 3dea35f7dfd3a9d8ec99c4798eeaca0ca9de14f7 Mon Sep 17 00:00:00 2001 From: hesphoros Date: Tue, 28 Jul 2026 19:22:44 +0800 Subject: [PATCH 1/2] Fix:pinning-induced window position drift on Windows --- examples/mainwindow/mainwindow.cpp | 61 ++++++++++++++++++++++++++++-- examples/mainwindow/mainwindow.h | 3 ++ 2 files changed, 61 insertions(+), 3 deletions(-) diff --git a/examples/mainwindow/mainwindow.cpp b/examples/mainwindow/mainwindow.cpp index 18de806..9ede981 100644 --- a/examples/mainwindow/mainwindow.cpp +++ b/examples/mainwindow/mainwindow.cpp @@ -26,6 +26,10 @@ #include +#ifdef Q_OS_WINDOWS +# include +#endif + #include #include @@ -128,6 +132,14 @@ bool MainWindow::event(QEvent *event) { break; } +#ifdef Q_OS_WINDOWS + case QEvent::WinIdChange: + if (!applyingStayOnTop) { + setStayOnTop(stayOnTop); + } + break; +#endif + default: break; } @@ -432,11 +444,14 @@ void MainWindow::installWindowAgent() { #ifndef Q_OS_MAC connect(windowBar, &QWK::WindowBar::pinRequested, this, [this, pinButton](bool pin) { if (isHidden() || isMinimized() || isMaximized() || isFullScreen()) { + pinButton->setChecked(stayOnTop); return; } - setWindowFlag(Qt::WindowStaysOnTopHint, pin); - show(); - pinButton->setChecked(pin); + if (setStayOnTop(pin)) { + pinButton->setChecked(pin); + } else { + pinButton->setChecked(stayOnTop); + } }); connect(windowBar, &QWK::WindowBar::minimizeRequested, this, &QWidget::showMinimized); connect(windowBar, &QWK::WindowBar::maximizeRequested, this, [this, maxButton](bool max) { @@ -455,6 +470,46 @@ void MainWindow::installWindowAgent() { #endif } +bool MainWindow::setStayOnTop(bool enabled) { +#ifdef Q_OS_WINDOWS + if (applyingStayOnTop) { + return false; + } + + const auto hwnd = reinterpret_cast(internalWinId()); + if (!hwnd) { + return false; + } + + applyingStayOnTop = true; + const bool success = ::SetWindowPos(hwnd, enabled ? HWND_TOPMOST : HWND_NOTOPMOST, + 0, 0, 0, 0, + SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | + SWP_NOOWNERZORDER) != FALSE; + applyingStayOnTop = false; + if (success) { + stayOnTop = enabled; + } + return success; +#else + if (windowFlags().testFlag(Qt::WindowStaysOnTopHint) == enabled) { + stayOnTop = enabled; + return true; + } + + const bool visible = isVisible(); + const QPoint frameTopLeft = frameGeometry().topLeft(); + setWindowFlag(Qt::WindowStaysOnTopHint, enabled); + if (visible) { + show(); + const QPoint clientOffset = geometry().topLeft() - frameGeometry().topLeft(); + move(frameTopLeft + clientOffset); + } + stayOnTop = enabled; + return true; +#endif +} + void MainWindow::loadStyleSheet(Theme theme) { if (!styleSheet().isEmpty() && theme == currentTheme) return; diff --git a/examples/mainwindow/mainwindow.h b/examples/mainwindow/mainwindow.h index 53514a1..ee68ed9 100644 --- a/examples/mainwindow/mainwindow.h +++ b/examples/mainwindow/mainwindow.h @@ -33,8 +33,11 @@ class MainWindow : public QMainWindow { private: void installWindowAgent(); void loadStyleSheet(Theme theme); + bool setStayOnTop(bool stayOnTop); Theme currentTheme{}; + bool stayOnTop{false}; + bool applyingStayOnTop{false}; QWK::WidgetWindowAgent *windowAgent; }; From 6c427a695aec3d1dfc1632c67dd39b00466302fa Mon Sep 17 00:00:00 2001 From: hesphoros Date: Tue, 28 Jul 2026 19:49:53 +0800 Subject: [PATCH 2/2] Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- examples/mainwindow/mainwindow.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/mainwindow/mainwindow.cpp b/examples/mainwindow/mainwindow.cpp index 9ede981..6b89f56 100644 --- a/examples/mainwindow/mainwindow.cpp +++ b/examples/mainwindow/mainwindow.cpp @@ -26,7 +26,7 @@ #include -#ifdef Q_OS_WINDOWS +#ifdef Q_OS_WIN # include #endif