Skip to content

Commit

Permalink
Add mnemonics to Tools -> Boards and Ports
Browse files Browse the repository at this point in the history
  • Loading branch information
jaggzh committed Jun 10, 2022
1 parent 43b0818 commit a0e6949
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
49 changes: 47 additions & 2 deletions app/src/processing/app/Base.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
Expand All @@ -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
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
}
Expand Down
5 changes: 4 additions & 1 deletion app/src/processing/app/Editor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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()) {
Expand All @@ -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);
Expand Down

0 comments on commit a0e6949

Please sign in to comment.