Skip to content

Commit

Permalink
feat(menus): add support for Plus key in accelerators, closes #227 (#573
Browse files Browse the repository at this point in the history
)

* feat(menus): add support for Plus key in accelerators

* feat(menus) add support for Plus key in accelerators on Windows

* feat(menus) add support for Plus key in accelerators on Linux

* chore: add changefile and docs for `KeyCode::Plus`

* lint: run cargo fmt

* docs: clarify wording
  • Loading branch information
caesar committed Sep 27, 2022
1 parent 65f768e commit 937aba7
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changes/macos-menus-accel-plus.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"tao": patch
---

Add support for the "+" key in menu accelerators using `KeyCode::Plus` or the "Plus" keyword.
See documentation for `KeyCode::Plus` for notes on platform-dependent behaviour.
12 changes: 12 additions & 0 deletions examples/custom_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ fn main() {
// create custom item `Change menu` children of `second_menu`
let change_menu = second_menu.add_item(MenuItemAttributes::new("Change menu"));

second_menu.add_native_item(MenuItem::Separator);
let mut zoom_in_item = second_menu.add_item(
MenuItemAttributes::new("Zoom in").with_accelerators(&"CmdOrCtrl+Plus".parse().unwrap()),
);

// add all our childs to menu_bar_menu (order is how they'll appear)
menu_bar_menu.add_submenu("My app", true, first_menu);
menu_bar_menu.add_submenu("Other menu", true, second_menu);
Expand Down Expand Up @@ -136,6 +141,13 @@ fn main() {
} if menu_id == custom_read_clipboard.clone().id() => {
println!("Clipboard content: {:?}", cliboard.read_text());
}
Event::MenuEvent {
menu_id,
origin: MenuType::MenuBar,
..
} if menu_id == zoom_in_item.clone().id() => {
println!("Zoom in!");
}
_ => (),
}
});
Expand Down
9 changes: 9 additions & 0 deletions src/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,14 @@ pub enum KeyCode {
KeyZ,
/// <kbd>-</kbd> on a US keyboard.
Minus,
/// <kbd>Shift</kbd>+<kbd>=</kbd> on a US keyboard.
///
/// When used as a menu accelerator this is displayed as <kbd>+</kbd>, and on macOS
/// and Windows the accelerator can be used without pressing the <kbd>Shift</kbd> key.
/// On Linux the <kbd>Shift</kbd> key is still required with a US keyboard layout.
/// `Plus` does not work as a value for keyboard accelerators outside menus on
/// keyboards where <kbd>+</kbd> requires a modifier key.
Plus,
/// <kbd>.</kbd> on a US keyboard.
Period,
/// <kbd>'</kbd> on a US keyboard.
Expand Down Expand Up @@ -698,6 +706,7 @@ impl FromStr for KeyCode {
"NUM9" | "NUMPAD9" => KeyCode::Numpad9,
"=" => KeyCode::Equal,
"-" => KeyCode::Minus,
"PLUS" => KeyCode::Plus,
"." | "PERIOD" => KeyCode::Period,
"'" | "QUOTE" => KeyCode::Quote,
"\\" => KeyCode::IntlBackslash,
Expand Down
1 change: 1 addition & 0 deletions src/platform_impl/linux/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ pub fn key_to_raw_key(src: &KeyCode) -> Option<RawKey> {
KeyCode::PageDown => Page_Down,

KeyCode::NumLock => Num_Lock,
KeyCode::Plus => plus,

KeyCode::ArrowUp => Up,
KeyCode::ArrowDown => Down,
Expand Down
1 change: 1 addition & 0 deletions src/platform_impl/macos/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,7 @@ impl Accelerator {
KeyCode::Digit9 => "9".into(),
KeyCode::Comma => ",".into(),
KeyCode::Minus => "-".into(),
KeyCode::Plus => "+".into(),
KeyCode::Period => ".".into(),
KeyCode::Space => "\u{0020}".into(),
KeyCode::Equal => "=".into(),
Expand Down
1 change: 1 addition & 0 deletions src/platform_impl/windows/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,7 @@ pub(crate) fn key_to_vk(key: &KeyCode) -> Option<VIRTUAL_KEY> {
KeyCode::Digit9 => unsafe { VIRTUAL_KEY(VkKeyScanW('9' as u16) as u16) },
KeyCode::Comma => VK_OEM_COMMA,
KeyCode::Minus => VK_OEM_MINUS,
KeyCode::Plus => VK_OEM_PLUS,
KeyCode::Period => VK_OEM_PERIOD,
KeyCode::Equal => unsafe { VIRTUAL_KEY(VkKeyScanW('=' as u16) as u16) },
KeyCode::Semicolon => unsafe { VIRTUAL_KEY(VkKeyScanW(';' as u16) as u16) },
Expand Down
1 change: 1 addition & 0 deletions src/platform_impl/windows/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,7 @@ impl fmt::Display for Accelerator {
KeyCode::Digit9 => write!(f, "9"),
KeyCode::Comma => write!(f, ","),
KeyCode::Minus => write!(f, "-"),
KeyCode::Plus => write!(f, "+"),
KeyCode::Period => write!(f, "."),
KeyCode::Space => write!(f, "Space"),
KeyCode::Equal => write!(f, "="),
Expand Down

0 comments on commit 937aba7

Please sign in to comment.