Skip to content

Commit e7fb964

Browse files
committed
Fix menu positioning for DPI-aware windows
Adjusts menu positioning logic to account for DPI scaling when positioning relative to a window. Uses logical pixels and applies the window's scale factor to ensure correct placement on high-DPI displays.
1 parent 5407126 commit e7fb964

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/platform/windows/menu_windows.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include "../../image.h"
1010
#include "../../menu.h"
1111
#include "../../menu_event.h"
12+
#include "../../window.h"
13+
#include "dpi_utils_windows.h"
1214
#include "string_utils_windows.h"
1315
#include "window_message_dispatcher.h"
1416

@@ -709,9 +711,18 @@ bool Menu::Open(const PositioningStrategy& strategy, Placement placement) {
709711
case PositioningStrategy::Type::Relative: {
710712
Rectangle rect = strategy.GetRelativeRectangle();
711713
Point offset = strategy.GetRelativeOffset();
712-
// Position at top-left corner of rectangle plus offset
713-
pt.x = static_cast<int>(rect.x + offset.x);
714-
pt.y = static_cast<int>(rect.y + offset.y);
714+
if (strategy.GetRelativeWindow() != nullptr) {
715+
// rect and offset are in logical pixels (DIP) for Window-relative
716+
HWND rel_hwnd = static_cast<HWND>(strategy.GetRelativeWindow()->GetNativeObject());
717+
double scale = GetScaleFactorForWindow(rel_hwnd);
718+
if (scale <= 0.0) scale = 1.0;
719+
pt.x = static_cast<int>(std::lround((rect.x + offset.x) * scale));
720+
pt.y = static_cast<int>(std::lround((rect.y + offset.y) * scale));
721+
} else {
722+
// For plain rectangles, assume inputs already in screen pixels
723+
pt.x = static_cast<int>(rect.x + offset.x);
724+
pt.y = static_cast<int>(rect.y + offset.y);
725+
}
715726
break;
716727
}
717728
}

0 commit comments

Comments
 (0)