Skip to content

Commit

Permalink
Store original board items, not clones and use the original action
Browse files Browse the repository at this point in the history
This simplifies the creation of the original board items by just storing
the original and leaving the creation of new actions and items for the
recent boards menu for later, for only the items actually present there.

Also, it turns out that cloning the action is not actually needed, you
can just attach the original action to the recent board menu item as
well (this skips setting the clicked item as selected, but it works
because the original action changes the current board, which regenerates
the recent board menu anyway).
  • Loading branch information
matthijskooijman committed Oct 23, 2020
1 parent 2acc1f7 commit a9631da
Showing 1 changed file with 9 additions and 23 deletions.
32 changes: 9 additions & 23 deletions app/src/processing/app/Base.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public class Base {

// these variables help rebuild the "recently used boards"
// menu on board selection
private HashMap<String, JRadioButtonMenuItem> recentBoardItems;
private HashMap<String, JRadioButtonMenuItem> boardItems;
private List<JRadioButtonMenuItem> recentBoardsToClear = new LinkedList<>();;
private JMenu boardMenu;
private int recentBoardsJMenuIndex;
Expand Down Expand Up @@ -1473,7 +1473,7 @@ protected void onIndexesUpdated() throws Exception {

public void rebuildBoardsMenu() throws Exception {
boardsCustomMenus = new LinkedList<>();
recentBoardItems = new HashMap<String, JRadioButtonMenuItem>();
boardItems = new HashMap<String, JRadioButtonMenuItem>();

// The first custom menu is the "Board" selection submenu
boardMenu = new JMenu(tr("Board"));
Expand Down Expand Up @@ -1625,10 +1625,12 @@ private void rebuildRecentBoardsList() throws Exception {
}
recentBoardsToClear.clear();
for (String boardId : recentBoardIds) {
JRadioButtonMenuItem addItem = recentBoardItems.get(boardId);
boardMenu.add(addItem, recentBoardsJMenuIndex+idxAdv);
recentBoardsToClear.add(addItem);
addItem.setSelected(boardId.equals(currentBoard));
JRadioButtonMenuItem originalItem = boardItems.get(boardId);
JRadioButtonMenuItem recentItem = new JRadioButtonMenuItem(originalItem.getAction());

boardMenu.add(recentItem, recentBoardsJMenuIndex+idxAdv);
recentBoardsToClear.add(recentItem);
recentItem.setSelected(boardId.equals(currentBoard));
idxAdv++;
}
}
Expand Down Expand Up @@ -1662,23 +1664,7 @@ public void actionPerformed(ActionEvent actionevent) {
action.putValue("b", board);

JRadioButtonMenuItem item = new JRadioButtonMenuItem(action);

// create an action for the "recent boards" copy of this menu item
// which clicks the original menu item
Action actionClone = new AbstractAction(board.getName()) {
public void actionPerformed(ActionEvent actionevent) {
item.setSelected(true);
item.getAction().actionPerformed(new ActionEvent(this, -1, ""));
}
};

// No need to hog memory if recent boards feature is turned off
if (PreferencesData.getInteger("recent.num_boards") > 0) {
// create a menu item for the "recent boards" menu
JRadioButtonMenuItem itemClone = new JRadioButtonMenuItem(actionClone);
// populate list of menuitem copies
recentBoardItems.put(boardId, itemClone);
}
boardItems.put(boardId, item);

if (selBoard.equals(boardId) && selPackage.equals(packageName)
&& selPlatform.equals(platformName)) {
Expand Down

0 comments on commit a9631da

Please sign in to comment.