Skip to content

Commit cd41ab5

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 0096b38 commit cd41ab5

File tree

2 files changed

+78
-14
lines changed

2 files changed

+78
-14
lines changed

app/src/processing/app/Base.java

+65-14
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
@@ -1335,6 +1340,41 @@ public void selectTargetBoard(TargetBoard targetBoard) {
13351340
onBoardOrPortChange();
13361341
rebuildImportMenu(Editor.importMenu);
13371342
rebuildExamplesMenu(Editor.examplesMenu);
1343+
try {
1344+
rebuildRecentBoardsMenu();
1345+
} catch (Exception e) {
1346+
// fail silently
1347+
}
1348+
}
1349+
1350+
public void rebuildRecentBoardsMenu() throws Exception {
1351+
1352+
Enumeration<AbstractButton> btns = recentBoardsButtonGroup.getElements();
1353+
while (btns.hasMoreElements()) {
1354+
AbstractButton x = btns.nextElement();
1355+
if (x.isSelected()) {
1356+
return;
1357+
}
1358+
}
1359+
btns = recentBoardsButtonGroup.getElements();
1360+
while (btns.hasMoreElements()) {
1361+
AbstractButton x = btns.nextElement();
1362+
boardMenu.remove(x);
1363+
}
1364+
int index = 0;
1365+
for (TargetBoard board : BaseNoGui.getRecentlyUsedBoards()) {
1366+
JMenuItem item = createBoardMenusAndCustomMenus(boardsCustomMenus, menuItemsToClickAfterStartup,
1367+
buttonGroupsMap,
1368+
board, board.getContainerPlatform(), board.getContainerPlatform().getContainerPackage());
1369+
boardMenu.insert(item, 3);
1370+
item.setAccelerator(KeyStroke.getKeyStroke('1' + index,
1371+
Toolkit.getDefaultToolkit().getMenuShortcutKeyMask() |
1372+
ActionEvent.SHIFT_MASK));
1373+
recentBoardsButtonGroup.add(item);
1374+
boardsButtonGroup.add(item);
1375+
index ++;
1376+
}
1377+
boardMenuScroller.setTopFixedCount(3 + index);
13381378
}
13391379

13401380
public void onBoardOrPortChange() {
@@ -1432,7 +1472,8 @@ public void rebuildBoardsMenu() throws Exception {
14321472
// The first custom menu is the "Board" selection submenu
14331473
boardMenu = new JMenu(tr("Board"));
14341474
boardMenu.putClientProperty("removeOnWindowDeactivation", true);
1435-
MenuScroller.setScrollerFor(boardMenu).setTopFixedCount(1);
1475+
boardMenuScroller = MenuScroller.setScrollerFor(boardMenu);
1476+
boardMenuScroller.setTopFixedCount(1);
14361477

14371478
boardMenu.add(new JMenuItem(new AbstractAction(tr("Boards Manager...")) {
14381479
public void actionPerformed(ActionEvent actionevent) {
@@ -1472,21 +1513,26 @@ public void actionPerformed(ActionEvent actionevent) {
14721513
boardsCustomMenus.add(customMenu);
14731514
}
14741515

1475-
List<JMenuItem> menuItemsToClickAfterStartup = new LinkedList<>();
1516+
List<JMenuItem> _menuItemsToClickAfterStartup = new LinkedList<>();
1517+
boardsButtonGroup = new ButtonGroup();
1518+
recentBoardsButtonGroup = new ButtonGroup();
1519+
buttonGroupsMap = new HashMap<>();
14761520

1477-
ButtonGroup boardsButtonGroup = new ButtonGroup();
1478-
Map<String, ButtonGroup> buttonGroupsMap = new HashMap<>();
1521+
boolean hasRecentBoardsMenu = (PreferencesData.getInteger("editor.recent_boards.size", 4) != 0);
1522+
1523+
if (hasRecentBoardsMenu) {
1524+
JMenuItem recentLabel = new JMenuItem(tr("Recently used boards"));
1525+
recentLabel.setEnabled(false);
1526+
boardMenu.add(recentLabel);
1527+
}
14791528

14801529
// Cycle through all packages
1481-
boolean first = true;
14821530
for (TargetPackage targetPackage : BaseNoGui.packages.values()) {
14831531
// For every package cycle through all platform
14841532
for (TargetPlatform targetPlatform : targetPackage.platforms()) {
14851533

14861534
// Add a separator from the previous platform
1487-
if (!first)
1488-
boardMenu.add(new JSeparator());
1489-
first = false;
1535+
boardMenu.add(new JSeparator());
14901536

14911537
// Add a title for each platform
14921538
String platformLabel = targetPlatform.getPreferences().get("name");
@@ -1500,7 +1546,7 @@ public void actionPerformed(ActionEvent actionevent) {
15001546
for (TargetBoard board : targetPlatform.getBoards().values()) {
15011547
if (board.getPreferences().get("hide") != null)
15021548
continue;
1503-
JMenuItem item = createBoardMenusAndCustomMenus(boardsCustomMenus, menuItemsToClickAfterStartup,
1549+
JMenuItem item = createBoardMenusAndCustomMenus(boardsCustomMenus, _menuItemsToClickAfterStartup,
15041550
buttonGroupsMap,
15051551
board, targetPlatform, targetPackage);
15061552
boardMenu.add(item);
@@ -1509,14 +1555,16 @@ public void actionPerformed(ActionEvent actionevent) {
15091555
}
15101556
}
15111557

1512-
if (menuItemsToClickAfterStartup.isEmpty()) {
1513-
menuItemsToClickAfterStartup.add(selectFirstEnabledMenuItem(boardMenu));
1558+
if (_menuItemsToClickAfterStartup.isEmpty()) {
1559+
_menuItemsToClickAfterStartup.add(selectFirstEnabledMenuItem(boardMenu));
15141560
}
15151561

1516-
for (JMenuItem menuItemToClick : menuItemsToClickAfterStartup) {
1562+
for (JMenuItem menuItemToClick : _menuItemsToClickAfterStartup) {
15171563
menuItemToClick.setSelected(true);
15181564
menuItemToClick.getAction().actionPerformed(new ActionEvent(this, -1, ""));
15191565
}
1566+
1567+
menuItemsToClickAfterStartup = _menuItemsToClickAfterStartup;
15201568
}
15211569

15221570
private JRadioButtonMenuItem createBoardMenusAndCustomMenus(
@@ -1552,6 +1600,9 @@ public void actionPerformed(ActionEvent actionevent) {
15521600
for (final String menuId : customMenus.keySet()) {
15531601
String title = customMenus.get(menuId);
15541602
JMenu menu = getBoardCustomMenu(tr(title));
1603+
if (menu == null) {
1604+
continue;
1605+
}
15551606

15561607
if (board.hasMenu(menuId)) {
15571608
PreferencesMap boardCustomMenu = board.getMenuLabels(menuId);
@@ -1614,13 +1665,13 @@ private static boolean ifThereAreVisibleItemsOn(JMenu menu) {
16141665
return false;
16151666
}
16161667

1617-
private JMenu getBoardCustomMenu(String label) throws Exception {
1668+
private JMenu getBoardCustomMenu(String label) {
16181669
for (JMenu menu : boardsCustomMenus) {
16191670
if (label.equals(menu.getText())) {
16201671
return menu;
16211672
}
16221673
}
1623-
throw new Exception("Custom menu not found!");
1674+
return null;
16241675
}
16251676

16261677
public List<JMenuItem> getProgrammerMenus() {

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

+13
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,12 @@ static public void saveFile(String str, File file) throws IOException {
915915
}
916916
}
917917

918+
static private LinkedList<TargetBoard> recentlyUsedBoards = new LinkedList<TargetBoard>();
919+
920+
static public LinkedList<TargetBoard> getRecentlyUsedBoards() {
921+
return recentlyUsedBoards;
922+
}
923+
918924
static public void selectBoard(TargetBoard targetBoard) {
919925
TargetPlatform targetPlatform = targetBoard.getContainerPlatform();
920926
TargetPackage targetPackage = targetPlatform.getContainerPackage();
@@ -926,6 +932,13 @@ static public void selectBoard(TargetBoard targetBoard) {
926932
File platformFolder = targetPlatform.getFolder();
927933
PreferencesData.set("runtime.platform.path", platformFolder.getAbsolutePath());
928934
PreferencesData.set("runtime.hardware.path", platformFolder.getParentFile().getAbsolutePath());
935+
936+
if (!recentlyUsedBoards.contains(targetBoard)) {
937+
recentlyUsedBoards.add(targetBoard);
938+
}
939+
if (recentlyUsedBoards.size() > PreferencesData.getInteger("editor.recent_boards.size", 4)) {
940+
recentlyUsedBoards.remove();
941+
}
929942
}
930943

931944
public static void selectSerialPort(String port) {

0 commit comments

Comments
 (0)