Skip to content

Commit cf4c46b

Browse files
committed
Add per-session recently used boards list
The list appears at the top of Board submenu Boards are also reachable via a CTRL+SHIFT+INDEX
1 parent caa2b16 commit cf4c46b

File tree

2 files changed

+73
-18
lines changed

2 files changed

+73
-18
lines changed

app/src/processing/app/Base.java

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ public class Base {
118118
Editor activeEditor;
119119

120120
private static JMenu boardMenu;
121+
private static ButtonGroup boardsButtonGroup;
122+
private static ButtonGroup recentBoardsButtonGroup;
123+
private static Map<String, ButtonGroup> buttonGroupsMap;
124+
private static List<JMenuItem> menuItemsToClickAfterStartup;
125+
private static MenuScroller boardMenuScroller;
121126

122127
// these menus are shared so that the board and serial port selections
123128
// are the same for all windows (since the board and serial port that are
@@ -1324,6 +1329,42 @@ public void selectTargetBoard(TargetBoard targetBoard) {
13241329
onBoardOrPortChange();
13251330
rebuildImportMenu(Editor.importMenu);
13261331
rebuildExamplesMenu(Editor.examplesMenu);
1332+
try {
1333+
rebuildRecentBoardsMenu();
1334+
} catch (Exception e) {
1335+
// TODO Auto-generated catch block
1336+
e.printStackTrace();
1337+
}
1338+
}
1339+
1340+
public void rebuildRecentBoardsMenu() throws Exception {
1341+
1342+
Enumeration<AbstractButton> btns = recentBoardsButtonGroup.getElements();
1343+
while (btns.hasMoreElements()) {
1344+
AbstractButton x = btns.nextElement();
1345+
if (x.isSelected()) {
1346+
return;
1347+
}
1348+
}
1349+
btns = recentBoardsButtonGroup.getElements();
1350+
while (btns.hasMoreElements()) {
1351+
AbstractButton x = btns.nextElement();
1352+
boardMenu.remove(x);
1353+
}
1354+
int index = 0;
1355+
for (TargetBoard board : BaseNoGui.getRecentlyUsedBoards()) {
1356+
JMenuItem item = createBoardMenusAndCustomMenus(boardsCustomMenus, menuItemsToClickAfterStartup,
1357+
buttonGroupsMap,
1358+
board, board.getContainerPlatform(), board.getContainerPlatform().getContainerPackage());
1359+
boardMenu.insert(item, 3);
1360+
item.setAccelerator(KeyStroke.getKeyStroke('0' + index,
1361+
Toolkit.getDefaultToolkit().getMenuShortcutKeyMask() |
1362+
ActionEvent.SHIFT_MASK));
1363+
recentBoardsButtonGroup.add(item);
1364+
boardsButtonGroup.add(item);
1365+
index ++;
1366+
}
1367+
boardMenuScroller.setTopFixedCount(3 + index);
13271368
}
13281369

13291370
public synchronized void onBoardOrPortChange() {
@@ -1421,7 +1462,8 @@ public void rebuildBoardsMenu() throws Exception {
14211462
// The first custom menu is the "Board" selection submenu
14221463
boardMenu = new JMenu(tr("Board"));
14231464
boardMenu.putClientProperty("removeOnWindowDeactivation", true);
1424-
MenuScroller.setScrollerFor(boardMenu).setTopFixedCount(1);
1465+
boardMenuScroller = MenuScroller.setScrollerFor(boardMenu);
1466+
boardMenuScroller.setTopFixedCount(1);
14251467

14261468
boardMenu.add(new JMenuItem(new AbstractAction(tr("Boards Manager...")) {
14271469
public void actionPerformed(ActionEvent actionevent) {
@@ -1461,21 +1503,26 @@ public void actionPerformed(ActionEvent actionevent) {
14611503
boardsCustomMenus.add(customMenu);
14621504
}
14631505

1464-
List<JMenuItem> menuItemsToClickAfterStartup = new LinkedList<>();
1506+
menuItemsToClickAfterStartup = new LinkedList<>();
1507+
boardsButtonGroup = new ButtonGroup();
1508+
recentBoardsButtonGroup = new ButtonGroup();
1509+
buttonGroupsMap = new HashMap<>();
14651510

1466-
ButtonGroup boardsButtonGroup = new ButtonGroup();
1467-
Map<String, ButtonGroup> buttonGroupsMap = new HashMap<>();
1511+
if (BaseNoGui.getRecentlyUsedBoards() != null) {
1512+
JMenuItem recentLabel = new JMenuItem(tr("Recently used boards"));
1513+
recentLabel.setEnabled(false);
1514+
boardMenu.add(recentLabel);
1515+
rebuildRecentBoardsMenu();
1516+
//rebuildRecentBoardsMenu(null);
1517+
}
14681518

14691519
// Cycle through all packages
1470-
boolean first = true;
14711520
for (TargetPackage targetPackage : BaseNoGui.packages.values()) {
14721521
// For every package cycle through all platform
14731522
for (TargetPlatform targetPlatform : targetPackage.platforms()) {
14741523

14751524
// Add a separator from the previous platform
1476-
if (!first)
1477-
boardMenu.add(new JSeparator());
1478-
first = false;
1525+
boardMenu.add(new JSeparator());
14791526

14801527
// Add a title for each platform
14811528
String platformLabel = targetPlatform.getPreferences().get("name");
@@ -1555,6 +1602,9 @@ public void run() {
15551602
for (final String menuId : customMenus.keySet()) {
15561603
String title = customMenus.get(menuId);
15571604
JMenu menu = getBoardCustomMenu(tr(title));
1605+
if (menu == null) {
1606+
continue;
1607+
}
15581608

15591609
if (board.hasMenu(menuId)) {
15601610
PreferencesMap boardCustomMenu = board.getMenuLabels(menuId);
@@ -1617,13 +1667,13 @@ private static boolean ifThereAreVisibleItemsOn(JMenu menu) {
16171667
return false;
16181668
}
16191669

1620-
private JMenu getBoardCustomMenu(String label) throws Exception {
1670+
private JMenu getBoardCustomMenu(String label) {
16211671
for (JMenu menu : boardsCustomMenus) {
16221672
if (label.equals(menu.getText())) {
16231673
return menu;
16241674
}
16251675
}
1626-
throw new Exception("Custom menu not found!");
1676+
return null;
16271677
}
16281678

16291679
public List<JMenuItem> getProgrammerMenus() {

arduino-core/src/processing/app/BaseNoGui.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,6 @@
88
import java.io.File;
99
import java.io.FileWriter;
1010
import java.io.IOException;
11-
import java.util.ArrayList;
12-
import java.util.Arrays;
13-
import java.util.Collection;
14-
import java.util.Date;
15-
import java.util.HashMap;
16-
import java.util.LinkedHashMap;
17-
import java.util.List;
18-
import java.util.Map;
1911
import java.util.logging.Level;
2012
import java.util.logging.Logger;
2113
import java.util.stream.Collectors;
@@ -968,6 +960,12 @@ static public void saveFile(String str, File file) throws IOException {
968960
}
969961
}
970962

963+
static private LinkedList<TargetBoard> recentlyUsedBoards = new LinkedList<TargetBoard>();
964+
965+
static public LinkedList<TargetBoard> getRecentlyUsedBoards() {
966+
return recentlyUsedBoards;
967+
}
968+
971969
static public void selectBoard(TargetBoard targetBoard) {
972970
TargetPlatform targetPlatform = targetBoard.getContainerPlatform();
973971
TargetPackage targetPackage = targetPlatform.getContainerPackage();
@@ -979,6 +977,13 @@ static public void selectBoard(TargetBoard targetBoard) {
979977
File platformFolder = targetPlatform.getFolder();
980978
PreferencesData.set("runtime.platform.path", platformFolder.getAbsolutePath());
981979
PreferencesData.set("runtime.hardware.path", platformFolder.getParentFile().getAbsolutePath());
980+
981+
if (!recentlyUsedBoards.contains(targetBoard)) {
982+
recentlyUsedBoards.add(targetBoard);
983+
}
984+
if (recentlyUsedBoards.size() > 4) {
985+
recentlyUsedBoards.remove();
986+
}
982987
}
983988

984989
public static void selectSerialPort(String port) {

0 commit comments

Comments
 (0)