Skip to content

Commit 2acc1f7

Browse files
committed
Suggested Changes from PR 10816
- Make number of recent boards adjustable in the preferences.txt file at "recent.num_boards" - Disable feature by entering value '0' for num_boards - Change Recent board menu items to radio buttons - Add "All Installed Boards" header to Boards menu -Remove unnecessary LinkedList creation at line 1599 - Broaden checks to anticipate alterations to preferences
1 parent b7152b2 commit 2acc1f7

File tree

1 file changed

+47
-22
lines changed

1 file changed

+47
-22
lines changed

Diff for: app/src/processing/app/Base.java

+47-22
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ public class Base {
124124

125125
// these variables help rebuild the "recently used boards"
126126
// menu on board selection
127-
private HashMap<String, JMenuItem> recentBoardItems;
128-
private List<JMenuItem> recentBoardsToClear = new LinkedList<>();;
127+
private HashMap<String, JRadioButtonMenuItem> recentBoardItems;
128+
private List<JRadioButtonMenuItem> recentBoardsToClear = new LinkedList<>();;
129129
private JMenu boardMenu;
130130
private int recentBoardsJMenuIndex;
131131

@@ -1376,16 +1376,29 @@ public void onBoardOrPortChange() {
13761376
}
13771377
}
13781378
newRecentBoardIds.add(0, currentBoard);
1379-
if (newRecentBoardIds.size() == 6) {
1380-
newRecentBoardIds.remove(5);
1379+
1380+
int numBoards = 0;
1381+
1382+
if (PreferencesData.has("recent.num_boards")) {
1383+
numBoards = PreferencesData.getInteger("recent.num_boards");
1384+
}
1385+
1386+
while (newRecentBoardIds.size() > numBoards) {
1387+
newRecentBoardIds.remove(newRecentBoardIds.size() - 1);
13811388
}
13821389
PreferencesData.setCollection("recent.boards", newRecentBoardIds);
1383-
try {
1384-
rebuildRecentBoardsList();
1385-
} catch (Exception e) {
1386-
//TODO show error
1387-
e.printStackTrace();
1388-
}
1390+
1391+
// If recent.num_boards is 0, interpret this as the feature
1392+
// being turned off. There's no need to rebuild the menu
1393+
// because it will be hidden
1394+
if (numBoards > 0) {
1395+
try {
1396+
rebuildRecentBoardsList();
1397+
} catch (Exception e) {
1398+
//TODO show error
1399+
e.printStackTrace();
1400+
}
1401+
}
13891402

13901403
// Update editors status bar
13911404
for (Editor editor : editors) {
@@ -1460,7 +1473,7 @@ protected void onIndexesUpdated() throws Exception {
14601473

14611474
public void rebuildBoardsMenu() throws Exception {
14621475
boardsCustomMenus = new LinkedList<>();
1463-
recentBoardItems = new HashMap<String, JMenuItem>();
1476+
recentBoardItems = new HashMap<String, JRadioButtonMenuItem>();
14641477

14651478
// The first custom menu is the "Board" selection submenu
14661479
boardMenu = new JMenu(tr("Board"));
@@ -1487,7 +1500,12 @@ public void actionPerformed(ActionEvent actionevent) {
14871500
boardsCustomMenus.add(boardMenu);
14881501

14891502
// Insert recently used boards menu and remember index for insertion later
1490-
if (PreferencesData.has("recent.boards")) {
1503+
// Check if the field exists, in case preferences got lost
1504+
if (!PreferencesData.has("recent.num_boards")){
1505+
// (default to 5)
1506+
PreferencesData.setInteger("recent.num_boards", 5);
1507+
}
1508+
if (PreferencesData.getInteger("recent.num_boards") > 0) {
14911509
// Insert menu label
14921510
boardMenu.add(new JSeparator());
14931511
JMenuItem label = new JMenuItem(tr("Recently Used Boards"));
@@ -1502,6 +1520,9 @@ public void actionPerformed(ActionEvent actionevent) {
15021520

15031521
// Separate "Install boards..." command from installed boards
15041522
boardMenu.add(new JSeparator());
1523+
JMenuItem label = new JMenuItem(tr("All Installed Boards"));
1524+
label.setEnabled(false);
1525+
boardMenu.add(label);
15051526

15061527
// Generate custom menus for all platforms
15071528
for (TargetPackage targetPackage : BaseNoGui.packages.values()) {
@@ -1596,16 +1617,18 @@ private String getPlatformUniqueId(TargetPlatform platform) {
15961617
// clear the previous menu items from the "recently used boards"
15971618
// menu and repopulate with updated items
15981619
private void rebuildRecentBoardsList() throws Exception {
1599-
Collection<String> recentBoardIds = new LinkedList<>();
1600-
recentBoardIds = PreferencesData.getCollection("recent.boards");
1620+
Collection<String> recentBoardIds = PreferencesData.getCollection("recent.boards");
1621+
String currentBoard = PreferencesData.get("board");
16011622
int idxAdv = 0;
1602-
for (JMenuItem itemToClear : recentBoardsToClear) {
1623+
for (JRadioButtonMenuItem itemToClear : recentBoardsToClear) {
16031624
boardMenu.remove(itemToClear);
16041625
}
16051626
recentBoardsToClear.clear();
16061627
for (String boardId : recentBoardIds) {
1607-
boardMenu.add(recentBoardItems.get(boardId), recentBoardsJMenuIndex+idxAdv);
1608-
recentBoardsToClear.add(recentBoardItems.get(boardId));
1628+
JRadioButtonMenuItem addItem = recentBoardItems.get(boardId);
1629+
boardMenu.add(addItem, recentBoardsJMenuIndex+idxAdv);
1630+
recentBoardsToClear.add(addItem);
1631+
addItem.setSelected(boardId.equals(currentBoard));
16091632
idxAdv++;
16101633
}
16111634
}
@@ -1649,11 +1672,13 @@ public void actionPerformed(ActionEvent actionevent) {
16491672
}
16501673
};
16511674

1652-
// create a menu item for the "recent boards" menu
1653-
JMenuItem itemClone = new JMenuItem(actionClone);
1654-
1655-
// populate list of menuitem copies
1656-
recentBoardItems.put(boardId, itemClone);
1675+
// No need to hog memory if recent boards feature is turned off
1676+
if (PreferencesData.getInteger("recent.num_boards") > 0) {
1677+
// create a menu item for the "recent boards" menu
1678+
JRadioButtonMenuItem itemClone = new JRadioButtonMenuItem(actionClone);
1679+
// populate list of menuitem copies
1680+
recentBoardItems.put(boardId, itemClone);
1681+
}
16571682

16581683
if (selBoard.equals(boardId) && selPackage.equals(packageName)
16591684
&& selPlatform.equals(platformName)) {

0 commit comments

Comments
 (0)