From a0e694930a46a129aa9feaa070b426424ccba507 Mon Sep 17 00:00:00 2001 From: Jaggzh Date: Thu, 9 Jun 2022 15:45:59 -0700 Subject: [PATCH] Add mnemonics to Tools -> Boards and Ports --- app/src/processing/app/Base.java | 49 ++++++++++++++++++++++++++++-- app/src/processing/app/Editor.java | 5 ++- 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/app/src/processing/app/Base.java b/app/src/processing/app/Base.java index cdac3059f6..f0dbc4d13f 100644 --- a/app/src/processing/app/Base.java +++ b/app/src/processing/app/Base.java @@ -1424,15 +1424,45 @@ protected void onIndexesUpdated() throws Exception { onBoardOrPortChange(); } + public static void setMenuItemMnemonicAlphaNum(JMenuItem item, int i, Boolean repeat) { + char c; + // JMenu hotkeys treats lower and upper case the same, + // so we only do lower, then digits, for visibility + if (i>=26+10) { + if (!repeat) return; + i = i%(26+10); + } + if (i>=0 && i<26) { + c = (char)(i+'a'); + } else if (i>=26 && i<(26+10)) { + c = (char)(i-26+'0'); + } else { + return; + } + item.setText(c + ". " + item.getText()); + item.setMnemonic(c); + } + + public static void setMenuItemMnemonicNum10(JMenuItem item, int i, Boolean repeat) { + char c; + if (i>=0 && (repeat || i<10)) { + c = (char)((i%10)+'0'); + item.setText(c + ". " + item.getText()); + item.setMnemonic(c); + } + } + + public void rebuildBoardsMenu() throws Exception { boardsCustomMenus = new LinkedList<>(); // The first custom menu is the "Board" selection submenu JMenu boardMenu = new JMenu(tr("Board")); + boardMenu.setMnemonic('B'); boardMenu.putClientProperty("removeOnWindowDeactivation", true); MenuScroller.setScrollerFor(boardMenu).setTopFixedCount(1); - boardMenu.add(new JMenuItem(new AbstractAction(tr("Boards Manager...")) { + JMenuItem menuItem = new JMenuItem(new AbstractAction(tr("Boards Manager...")) { public void actionPerformed(ActionEvent actionevent) { String filterText = ""; String dropdownItem = ""; @@ -1448,7 +1478,9 @@ public void actionPerformed(ActionEvent actionevent) { e.printStackTrace(); } } - })); + }); + menuItem.setMnemonic('M'); + boardMenu.add(menuItem); boardsCustomMenus.add(boardMenu); // If there are no platforms installed we are done @@ -1497,12 +1529,15 @@ public void actionPerformed(ActionEvent actionevent) { platformMenus.add(platformBoardsMenu); // Cycle through all boards of this platform + int i=0; for (TargetBoard board : targetPlatform.getBoards().values()) { if (board.getPreferences().get("hide") != null) continue; JMenuItem item = createBoardMenusAndCustomMenus(boardsCustomMenus, menuItemsToClickAfterStartup, buttonGroupsMap, board, targetPlatform, targetPackage); + setMenuItemMnemonicAlphaNum(item, i, true); + i++; platformBoardsMenu.add(item); boardsButtonGroup.add(item); } @@ -1515,16 +1550,26 @@ public void actionPerformed(ActionEvent actionevent) { if (platformMenus.size() == 1) { // When just one platform exists, add the board items directly, // rather than using a submenu + int i=0; for (Component boardItem : platformMenus.get(0).getMenuComponents()) { + // For mnemonics, need to test single-platform setups: + // Eg. setMenuItemMnemonicAlphaNum((JMenuItem)boardItem, i, true); i++; boardMenu.add(boardItem); if (firstBoardItem == null) firstBoardItem = (JMenuItem)boardItem; } } else { // For multiple platforms, use submenus + // int i=0; + String keys=""; + int i=0; for (JMenu platformMenu : platformMenus) { if (firstBoardItem == null && platformMenu.getItemCount() > 0) firstBoardItem = platformMenu.getItem(0); + // Ideally we'd exclude the manually-assigned "Board (M)anager" key + // when assigning a mnemonic to this item + setMenuItemMnemonicAlphaNum(platformMenu, i, true); + i++; boardMenu.add(platformMenu); } } diff --git a/app/src/processing/app/Editor.java b/app/src/processing/app/Editor.java index a307bde8fb..d78eea5f61 100644 --- a/app/src/processing/app/Editor.java +++ b/app/src/processing/app/Editor.java @@ -741,6 +741,7 @@ private JMenu buildToolsMenu() { if (portMenu == null) portMenu = new JMenu(tr("Port")); + portMenu.setMnemonic('P'); populatePortMenu(); toolsMenu.add(portMenu); MenuScroller.setScrollerFor(portMenu); @@ -1109,6 +1110,7 @@ private void populatePortMenu() { String lastProtocol = ""; String lastProtocolLabel = ""; + int i=0; for (BoardPort port : ports) { if (!port.getProtocol().equals(lastProtocol) || !port.getProtocolLabel().equals(lastProtocolLabel)) { if (!lastProtocol.isEmpty()) { @@ -1124,13 +1126,14 @@ private void populatePortMenu() { BoardPortJCheckBoxMenuItem item = new BoardPortJCheckBoxMenuItem(port); item.setSelected(address.equals(selectedPort)); + Base.setMenuItemMnemonicNum10(item, i, false); + i++; portMenu.add(item); } portMenu.setEnabled(portMenu.getMenuComponentCount() > 0); } - private JMenu buildHelpMenu() { JMenu menu = new JMenu(tr("Help")); menu.setMnemonic(KeyEvent.VK_H);