summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorYuhang Zhao <2546789017@qq.com>2022-02-12 16:46:26 +0800
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2022-03-03 02:08:58 +0000
commit7e9535f8c71dbc956f0f1b46817a86d5ab937891 (patch)
tree1be583aa96b6b585d720e68d98eaec463e025ac4 /src
parent591ba65f2a520a4cdc837688438f9c0a52b40fa4 (diff)
Windows QPA: Add some offset to the system menu when appropriate
Before this patch, the system menu will always appear on the top-left corner of the window if the window doesn't have the standard window frame. However, this doesn't look very good on most situations, especially when the window has a homemade title bar. This patch adds an extra check for this kind of situations. This patch will automatically apply an appropriate offset for the system menu if the user is trying to use a self-made title bar for their frameless or customized windows. Change-Id: I55e1c4ac26a4051ca48928d4a2ac3456dce117d1 Reviewed-by: André de la Rocha <andre.rocha@qt.io> (cherry picked from commit 77d0ad6657997b24e760f9a510676c08fc952eca) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'src')
-rw-r--r--src/plugins/platforms/windows/qwindowskeymapper.cpp24
1 files changed, 23 insertions, 1 deletions
diff --git a/src/plugins/platforms/windows/qwindowskeymapper.cpp b/src/plugins/platforms/windows/qwindowskeymapper.cpp
index 5954f35b07..7ee1d1e7d8 100644
--- a/src/plugins/platforms/windows/qwindowskeymapper.cpp
+++ b/src/plugins/platforms/windows/qwindowskeymapper.cpp
@@ -785,6 +785,27 @@ static inline QString messageKeyText(const MSG &msg)
return ch.isNull() ? QString() : QString(ch);
}
+[[nodiscard]] static inline int getTitleBarHeight(const HWND hwnd)
+{
+ const UINT dpi = GetDpiForWindow(hwnd);
+ const int captionHeight = GetSystemMetricsForDpi(SM_CYCAPTION, dpi);
+ if (IsZoomed(hwnd))
+ return captionHeight;
+ // The frame height should also be taken into account if the window
+ // is not maximized.
+ const int frameHeight = GetSystemMetricsForDpi(SM_CYSIZEFRAME, dpi)
+ + GetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi);
+ return captionHeight + frameHeight;
+}
+
+[[nodiscard]] static inline bool isSystemMenuOffsetNeeded(const Qt::WindowFlags flags)
+{
+ static constexpr const Qt::WindowFlags titleBarHints =
+ Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint | Qt::WindowContextHelpButtonHint;
+ return (flags & Qt::WindowSystemMenuHint) && (flags & Qt::WindowTitleHint) && !(flags & titleBarHints)
+ && (flags & (Qt::FramelessWindowHint | Qt::CustomizeWindowHint));
+}
+
static void showSystemMenu(QWindow* w)
{
QWindow *topLevel = QWindowsWindow::topLevelOf(w);
@@ -826,9 +847,10 @@ static void showSystemMenu(QWindow* w)
#undef enabled
#undef disabled
const QPoint pos = QHighDpi::toNativePixels(topLevel->geometry().topLeft(), topLevel);
+ const int titleBarOffset = isSystemMenuOffsetNeeded(topLevel->flags()) ? getTitleBarHeight(topLevelHwnd) : 0;
const int ret = TrackPopupMenuEx(menu,
TPM_LEFTALIGN | TPM_TOPALIGN | TPM_NONOTIFY | TPM_RETURNCMD,
- pos.x(), pos.y(),
+ pos.x(), pos.y() + titleBarOffset,
topLevelHwnd,
nullptr);
if (ret)