Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(linux): fix native menu items #256

Merged
merged 1 commit into from
Dec 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/linux-fix-native-menu-items.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tao": patch
---

Fix linux native menu items not working.
4 changes: 1 addition & 3 deletions examples/custom_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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,
Expand Down
43 changes: 39 additions & 4 deletions src/platform_impl/linux/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}};
}
Expand Down Expand Up @@ -246,17 +251,47 @@ impl Menu {
menu_type: GtkMenuType::Native,
menu_item: Some(MenuItem::Hide),
..
} => menuitem!("Hide", "<Ctrl>H", accel_group),
} => {
let tx_clone = tx.clone();
menuitem!(
"Hide",
"<Ctrl>H",
accel_group,
window_id,
Some(MenuItem::Hide),
tx_clone
)
}
GtkMenuInfo {
menu_type: GtkMenuType::Native,
menu_item: Some(MenuItem::CloseWindow),
..
} => menuitem!("Close Window", "<Ctrl>W", accel_group),
} => {
let tx_clone = tx.clone();
menuitem!(
"Close Window",
"<Ctrl>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",
"<Ctrl>Q",
accel_group,
window_id,
Some(MenuItem::Quit),
tx_clone
)
}
// TODO add others
_ => None,
};
Expand Down