Skip to content

Commit 1521b47

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+number (starting from 1)
1 parent e3e04a5 commit 1521b47

File tree

2 files changed

+71
-9
lines changed

2 files changed

+71
-9
lines changed

app/src/processing/app/Base.java

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ public class Base {
117117
Editor activeEditor;
118118

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

121126
// these menus are shared so that the board and serial port selections
122127
// are the same for all windows (since the board and serial port that are
@@ -1350,6 +1355,41 @@ public void selectTargetBoard(TargetBoard targetBoard) {
13501355
onBoardOrPortChange();
13511356
rebuildImportMenu(Editor.importMenu);
13521357
rebuildExamplesMenu(Editor.examplesMenu);
1358+
try {
1359+
rebuildRecentBoardsMenu();
1360+
} catch (Exception e) {
1361+
// fail silently
1362+
}
1363+
}
1364+
1365+
public void rebuildRecentBoardsMenu() throws Exception {
1366+
1367+
Enumeration<AbstractButton> btns = recentBoardsButtonGroup.getElements();
1368+
while (btns.hasMoreElements()) {
1369+
AbstractButton x = btns.nextElement();
1370+
if (x.isSelected()) {
1371+
return;
1372+
}
1373+
}
1374+
btns = recentBoardsButtonGroup.getElements();
1375+
while (btns.hasMoreElements()) {
1376+
AbstractButton x = btns.nextElement();
1377+
boardMenu.remove(x);
1378+
}
1379+
int index = 0;
1380+
for (TargetBoard board : BaseNoGui.getRecentlyUsedBoards()) {
1381+
JMenuItem item = createBoardMenusAndCustomMenus(boardsCustomMenus, menuItemsToClickAfterStartup,
1382+
buttonGroupsMap,
1383+
board, board.getContainerPlatform(), board.getContainerPlatform().getContainerPackage());
1384+
boardMenu.insert(item, 3);
1385+
item.setAccelerator(KeyStroke.getKeyStroke('0' + index,
1386+
Toolkit.getDefaultToolkit().getMenuShortcutKeyMask() |
1387+
ActionEvent.SHIFT_MASK));
1388+
recentBoardsButtonGroup.add(item);
1389+
boardsButtonGroup.add(item);
1390+
index ++;
1391+
}
1392+
boardMenuScroller.setTopFixedCount(3 + index);
13531393
}
13541394

13551395
public void onBoardOrPortChange() {
@@ -1447,7 +1487,8 @@ public void rebuildBoardsMenu() throws Exception {
14471487
// The first custom menu is the "Board" selection submenu
14481488
boardMenu = new JMenu(tr("Board"));
14491489
boardMenu.putClientProperty("removeOnWindowDeactivation", true);
1450-
MenuScroller.setScrollerFor(boardMenu).setTopFixedCount(1);
1490+
boardMenuScroller = MenuScroller.setScrollerFor(boardMenu);
1491+
boardMenuScroller.setTopFixedCount(1);
14511492

14521493
boardMenu.add(new JMenuItem(new AbstractAction(tr("Boards Manager...")) {
14531494
public void actionPerformed(ActionEvent actionevent) {
@@ -1486,21 +1527,26 @@ public void actionPerformed(ActionEvent actionevent) {
14861527
}
14871528
}
14881529

1489-
List<JMenuItem> menuItemsToClickAfterStartup = new LinkedList<>();
1530+
menuItemsToClickAfterStartup = new LinkedList<>();
1531+
boardsButtonGroup = new ButtonGroup();
1532+
recentBoardsButtonGroup = new ButtonGroup();
1533+
buttonGroupsMap = new HashMap<>();
14901534

1491-
ButtonGroup boardsButtonGroup = new ButtonGroup();
1492-
Map<String, ButtonGroup> buttonGroupsMap = new HashMap<>();
1535+
if (BaseNoGui.getRecentlyUsedBoards() != null) {
1536+
JMenuItem recentLabel = new JMenuItem(tr("Recently used boards"));
1537+
recentLabel.setEnabled(false);
1538+
boardMenu.add(recentLabel);
1539+
rebuildRecentBoardsMenu();
1540+
//rebuildRecentBoardsMenu(null);
1541+
}
14931542

14941543
// Cycle through all packages
1495-
boolean first = true;
14961544
for (TargetPackage targetPackage : BaseNoGui.packages.values()) {
14971545
// For every package cycle through all platform
14981546
for (TargetPlatform targetPlatform : targetPackage.platforms()) {
14991547

15001548
// Add a separator from the previous platform
1501-
if (!first)
1502-
boardMenu.add(new JSeparator());
1503-
first = false;
1549+
boardMenu.add(new JSeparator());
15041550

15051551
// Add a title for each platform
15061552
String platformLabel = targetPlatform.getPreferences().get("name");
@@ -1570,6 +1616,9 @@ public void actionPerformed(ActionEvent actionevent) {
15701616
for (final String menuId : customMenus.keySet()) {
15711617
String title = customMenus.get(menuId);
15721618
JMenu menu = getBoardCustomMenu(tr(title), getPlatformUniqueId(targetPlatform));
1619+
if (menu == null) {
1620+
continue;
1621+
}
15731622

15741623
if (board.hasMenu(menuId)) {
15751624
PreferencesMap boardCustomMenu = board.getMenuLabels(menuId);
@@ -1645,7 +1694,7 @@ private JMenu getBoardCustomMenu(String label, String platformUniqueId) throws E
16451694
return menu;
16461695
}
16471696
}
1648-
throw new Exception("Custom menu not found!");
1697+
return null;
16491698
}
16501699

16511700
public List<JMenuItem> getProgrammerMenus() {

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,12 @@ static public void saveFile(String str, File file) throws IOException {
917917
}
918918
}
919919

920+
static private LinkedList<TargetBoard> recentlyUsedBoards = new LinkedList<TargetBoard>();
921+
922+
static public LinkedList<TargetBoard> getRecentlyUsedBoards() {
923+
return recentlyUsedBoards;
924+
}
925+
920926
static public void selectBoard(TargetBoard targetBoard) {
921927
TargetPlatform targetPlatform = targetBoard.getContainerPlatform();
922928
TargetPackage targetPackage = targetPlatform.getContainerPackage();
@@ -928,6 +934,13 @@ static public void selectBoard(TargetBoard targetBoard) {
928934
File platformFolder = targetPlatform.getFolder();
929935
PreferencesData.set("runtime.platform.path", platformFolder.getAbsolutePath());
930936
PreferencesData.set("runtime.hardware.path", platformFolder.getParentFile().getAbsolutePath());
937+
938+
if (!recentlyUsedBoards.contains(targetBoard)) {
939+
recentlyUsedBoards.add(targetBoard);
940+
}
941+
if (recentlyUsedBoards.size() > 4) {
942+
recentlyUsedBoards.remove();
943+
}
931944
}
932945

933946
public static void selectSerialPort(String port) {

0 commit comments

Comments
 (0)