Skip to content

Commit cfe05d6

Browse files
committed
Fix menu positioning to use pointer window coordinates
Menu positioning now uses the window under the pointer to avoid coordinate space mismatches. This ensures the menu appears at the correct location relative to the user's cursor, improving reliability across different GTK versions.
1 parent 798af97 commit cfe05d6

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

src/platform/linux/menu_linux.cpp

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -682,18 +682,31 @@ bool Menu::Open(const PositioningStrategy& strategy, Placement placement) {
682682

683683
// Calculate position based on strategy type
684684
if (strategy.GetType() == PositioningStrategy::Type::CursorPosition) {
685-
// Use mouse position
686-
GdkDevice* mouse_device;
685+
// Position relative to the window under the pointer to avoid coord space mismatches
686+
int x = 0, y = 0;
687+
GdkWindow* pointer_window = nullptr;
687688
#if GTK_CHECK_VERSION(3, 20, 0)
688-
GdkSeat* seat = gdk_display_get_default_seat(gdk_display_get_default());
689-
mouse_device = gdk_seat_get_pointer(seat);
689+
GdkDisplay* display = gdk_display_get_default();
690+
GdkSeat* seat = display ? gdk_display_get_default_seat(display) : nullptr;
691+
GdkDevice* pointer = seat ? gdk_seat_get_pointer(seat) : nullptr;
692+
if (pointer) {
693+
pointer_window = gdk_device_get_window_at_position(pointer, &x, &y);
694+
}
690695
#else
691-
GdkDeviceManager* devman =
692-
gdk_display_get_device_manager(gdk_display_get_default());
693-
mouse_device = gdk_device_manager_get_client_pointer(devman);
696+
GdkDeviceManager* devman = gdk_display_get_device_manager(gdk_display_get_default());
697+
GdkDevice* pointer = gdk_device_manager_get_client_pointer(devman);
698+
if (pointer) {
699+
GdkScreen* screen = nullptr;
700+
gdk_device_get_position(pointer, &screen, &x, &y); // screen coords
701+
pointer_window = gdk_get_default_root_window();
702+
}
694703
#endif
695-
int x, y;
696-
gdk_window_get_device_position(gdk_window, mouse_device, &x, &y, nullptr);
704+
if (pointer_window) {
705+
gdk_window = pointer_window; // ensure rect coords match this window
706+
} else if (!gdk_window) {
707+
gdk_window = gdk_get_default_root_window();
708+
}
709+
697710
rectangle.x = x;
698711
rectangle.y = y;
699712
rectangle.width = 1;

0 commit comments

Comments
 (0)