diff --git a/.changes/gtk-context-menu.md b/.changes/gtk-context-menu.md new file mode 100644 index 00000000..5e8029c6 --- /dev/null +++ b/.changes/gtk-context-menu.md @@ -0,0 +1,5 @@ +--- +"muda": patch +--- + +On Linux, fix context menu closing immediately when right click is released. \ No newline at end of file diff --git a/src/platform_impl/gtk/mod.rs b/src/platform_impl/gtk/mod.rs index 698d1d1d..c910ce41 100644 --- a/src/platform_impl/gtk/mod.rs +++ b/src/platform_impl/gtk/mod.rs @@ -16,7 +16,8 @@ use crate::{ IsMenuItem, MenuEvent, MenuId, MenuItemKind, MenuItemType, }; use accelerator::{from_gtk_mnemonic, parse_accelerator, to_gtk_mnemonic}; -use gtk::{gdk, prelude::*, AboutDialog, Container, Orientation}; +use glib::translate::ToGlibPtr; +use gtk::{gdk, glib, prelude::*, AboutDialog, Container, Orientation}; use std::{ cell::RefCell, collections::{hash_map::Entry, HashMap}, @@ -1388,6 +1389,17 @@ fn show_context_menu( .and_then(|d| d.pointer()) .as_ref(), ); + + // Set the time of the event otherwise GTK will close the menu + // when right click is released + let event_ffi: *mut gdk::ffi::GdkEvent = event.to_glib_none().0; + if !event_ffi.is_null() { + let time = glib::monotonic_time() / 1000; + unsafe { + (*event_ffi).button.time = time as _; + } + } + gtk_menu.popup_at_rect( &window, &gdk::Rectangle::new(pos.0, pos.1, 0, 0),