Skip to content

Commit b21d5f0

Browse files
committed
Improve menu popup positioning and error handling
Refines menu positioning logic on Linux by ensuring explicit coordinates are only used when a root window is available, and pointer-based positioning requires a current event. Returns false when neither anchor is available, improving reliability on platforms like Wayland.
1 parent 367b7db commit b21d5f0

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

src/platform/linux/menu_linux.cpp

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -565,23 +565,29 @@ bool Menu::Open(const PositioningStrategy& strategy, Placement placement) {
565565
// Try to position at explicit coordinates if available
566566
if (use_explicit_position) {
567567
GdkWindow* root_window = gdk_get_default_root_window();
568-
if (root_window) {
569-
GdkRectangle rect;
570-
rect.x = static_cast<int>(x);
571-
rect.y = static_cast<int>(y);
572-
rect.width = 1;
573-
rect.height = 1;
574-
gtk_menu_popup_at_rect(GTK_MENU(pimpl_->gtk_menu_), root_window, &rect,
575-
anchor_gravity, menu_gravity, nullptr);
576-
} else {
577-
// Fallback to pointer if root window not available
578-
gtk_menu_popup_at_pointer(GTK_MENU(pimpl_->gtk_menu_), nullptr);
568+
if (!root_window) {
569+
// No root window (e.g., Wayland) and no parent to anchor to → cannot show
570+
return false;
579571
}
572+
573+
GdkRectangle rect;
574+
rect.x = static_cast<int>(x);
575+
rect.y = static_cast<int>(y);
576+
rect.width = 1;
577+
rect.height = 1;
578+
gtk_menu_popup_at_rect(GTK_MENU(pimpl_->gtk_menu_), root_window, &rect,
579+
anchor_gravity, menu_gravity, nullptr);
580+
return true;
580581
} else {
581-
// Use pointer position
582-
gtk_menu_popup_at_pointer(GTK_MENU(pimpl_->gtk_menu_), nullptr);
582+
// Use pointer position only if we have a current event to anchor to
583+
GdkEvent* current_event = gtk_get_current_event();
584+
if (!current_event) {
585+
return false;
586+
}
587+
gtk_menu_popup_at_pointer(GTK_MENU(pimpl_->gtk_menu_), current_event);
588+
gdk_event_free(current_event);
589+
return true;
583590
}
584-
return true;
585591
}
586592
return false;
587593
}

0 commit comments

Comments
 (0)