diff --git a/.changes/linux-fix-native-menu-items.md b/.changes/linux-fix-native-menu-items.md new file mode 100644 index 000000000..b673bd380 --- /dev/null +++ b/.changes/linux-fix-native-menu-items.md @@ -0,0 +1,5 @@ +--- +"tao": patch +--- + +Fix linux native menu items not working. \ No newline at end of file diff --git a/examples/custom_menu.rs b/examples/custom_menu.rs index e89b8bf10..5ccf8cb01 100644 --- a/examples/custom_menu.rs +++ b/examples/custom_menu.rs @@ -47,6 +47,7 @@ fn main() { // add `my_sub_menu` children of `first_menu` with `Sub menu` title first_menu.add_submenu("Sub menu", true, my_sub_menu); + first_menu.add_native_item(MenuItem::CloseWindow); first_menu.add_native_item(MenuItem::Quit); // create custom item `Selected and disabled` children of `second_menu` @@ -79,9 +80,6 @@ fn main() { window_id, .. } if window_id == window.id() => *control_flow = ControlFlow::Exit, - Event::MainEventsCleared => { - window.request_redraw(); - } Event::MenuEvent { window_id, menu_id, diff --git a/src/platform_impl/linux/menu.rs b/src/platform_impl/linux/menu.rs index 98aca5819..e30fd83ca 100644 --- a/src/platform_impl/linux/menu.rs +++ b/src/platform_impl/linux/menu.rs @@ -18,10 +18,15 @@ use crate::{ }; macro_rules! menuitem { - ( $description:expr, $key:expr, $accel_group:ident ) => {{ + ( $description:expr, $key:expr, $accel_group:ident, $window_id:expr, $native_menu_item:expr, $tx:ident ) => {{ let item = GtkMenuItem::with_label($description); let (key, mods) = gtk::accelerator_parse($key); item.add_accelerator("activate", $accel_group, key, mods, AccelFlags::VISIBLE); + item.connect_activate(move |_| { + if let Err(e) = $tx.send(($window_id, WindowRequest::Menu(($native_menu_item, None)))) { + log::warn!("Fail to send native menu request: {}", e); + } + }); Some(item) }}; } @@ -246,17 +251,47 @@ impl Menu { menu_type: GtkMenuType::Native, menu_item: Some(MenuItem::Hide), .. - } => menuitem!("Hide", "H", accel_group), + } => { + let tx_clone = tx.clone(); + menuitem!( + "Hide", + "H", + accel_group, + window_id, + Some(MenuItem::Hide), + tx_clone + ) + } GtkMenuInfo { menu_type: GtkMenuType::Native, menu_item: Some(MenuItem::CloseWindow), .. - } => menuitem!("Close Window", "W", accel_group), + } => { + let tx_clone = tx.clone(); + menuitem!( + "Close Window", + "W", + accel_group, + window_id, + Some(MenuItem::CloseWindow), + tx_clone + ) + } GtkMenuInfo { menu_type: GtkMenuType::Native, menu_item: Some(MenuItem::Quit), .. - } => menuitem!("Quit", "Q", accel_group), + } => { + let tx_clone = tx.clone(); + menuitem!( + "Quit", + "Q", + accel_group, + window_id, + Some(MenuItem::Quit), + tx_clone + ) + } // TODO add others _ => None, };