Skip to content

Commit 58a6088

Browse files
Rewrite the EditorToolbar class
Previously, it used some complicated internal datastructures to track what buttons were available and manually painted them and handled mouse clicks. This lead to a clunky and hard to extend toolbar. This commit completely rewrites the toolbar. Now, each button is a separate component (new ToolbarButton class deriving from JButton), so painting and handling clicks works out of the box. In previous commits, Action objects have been made available for each of the toolbar buttons, that also had icons set already. These new ToolbarButtons take all of their info from these actions. Most of this was implemented by JButton already, but copying some properties (selected, the selected icon and the rollover icon) is added by ToolbarButton. Before, buttons would be activated (e.g. giving the upload button a color while the upload is in progress) by calling methods on the toolbar. Now, the "selected" state of the Action / JButton is used for this, which automatically changes the icon to the "selected" icon. This removes the coupling between the verify/upload/save processes and the EditorToolbar, leaving them only coupled to the Actions they are run from. This also allows removing the Editor.toolbar instance variable, since nobody needs to reference it anymore after it was created. The `Editor.statusError()` method used to deactivate the run (verify) button on the toolbar, but it seems there are no code paths that activate this button and then go through statusError but not through the regular deactivation, so this call was removed. Because pressing shift changes the effect of some toolbar buttons, each ToolbarButton can contain one or two Action objects. When shift is pressed or released, the active Action object (as kept by JButton) is exchanged for the other one, updating the effect of the button, the tooltip and icon (though all related actions now use the same icon). When one action becomes selected, that action stays active regardless of the shift button, to provide user feedback (until the action is no longer selected, then it listens to the shift key again). By using separate Icon objects and files instead of manually cutting up a single gif with all icons, it will also be easier to replace the icons with vector icons later, to support high DPI screens more cleanly. Define the buttons themselves (e.g. what buttons are present) still happens in the EditorToolbar class, but it might be good to move these elsewhere at some point. References: arduino#4196
1 parent d0ededd commit 58a6088

File tree

3 files changed

+247
-400
lines changed

3 files changed

+247
-400
lines changed

app/src/processing/app/Editor.java

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ public boolean test(SketchController sketch) {
142142

143143
private int numTools = 0;
144144

145-
private final EditorToolbar toolbar;
146145
// these menus are shared so that they needn't be rebuilt for all windows
147146
// each time a sketch is created, renamed, or moved.
148147
static JMenu toolbarMenu;
@@ -261,8 +260,7 @@ public void windowDeactivated(WindowEvent e) {
261260
toolbarMenu = new JMenu();
262261
base.rebuildToolbarMenu(toolbarMenu);
263262
}
264-
toolbar = new EditorToolbar(this, toolbarMenu);
265-
upper.add(toolbar);
263+
upper.add(new EditorToolbar(this));
266264

267265
header = new EditorHeader(this);
268266
upper.add(header);
@@ -1740,7 +1738,7 @@ private void handleRun(final boolean verbose, Predicate<SketchController> should
17401738
if (shouldSavePredicate.test(sketchController)) {
17411739
handleSave(true);
17421740
}
1743-
toolbar.activateRun();
1741+
verifyAction.setSelected(true);
17441742
status.progress(tr("Compiling sketch..."));
17451743

17461744
// do this to advance/clear the terminal window / dos prompt / etc
@@ -1790,7 +1788,7 @@ public void run() {
17901788
}
17911789

17921790
status.unprogress();
1793-
toolbar.deactivateRun();
1791+
verifyAction.setSelected(false);
17941792
}
17951793
}
17961794

@@ -2004,7 +2002,7 @@ public void run() {
20042002

20052003

20062004
private boolean handleSave2() {
2007-
toolbar.activateSave();
2005+
saveSketchAction.setSelected(true);
20082006
statusNotice(tr("Saving..."));
20092007
boolean saved = false;
20102008
try {
@@ -2038,13 +2036,13 @@ private boolean handleSave2() {
20382036
// this is used when another operation calls a save
20392037
}
20402038
//toolbar.clear();
2041-
toolbar.deactivateSave();
2039+
saveSketchAction.setSelected(false);
20422040
return saved;
20432041
}
20442042

20452043

20462044
public boolean handleSaveAs() {
2047-
toolbar.activateSave();
2045+
saveSketchAsAction.setSelected(true);
20482046

20492047
//SwingUtilities.invokeLater(new Runnable() {
20502048
//public void run() {
@@ -2068,7 +2066,7 @@ public boolean handleSaveAs() {
20682066

20692067
} finally {
20702068
// make sure the toolbar button deactivates
2071-
toolbar.deactivateSave();
2069+
saveSketchAsAction.setSelected(false);
20722070
}
20732071

20742072
return true;
@@ -2126,7 +2124,11 @@ synchronized public void handleExport(final boolean usingProgrammer) {
21262124
handleSave(true);
21272125
}
21282126
}
2129-
toolbar.activateExport();
2127+
if (usingProgrammer)
2128+
uploadUsingProgrammerAction.setSelected(true);
2129+
else
2130+
uploadAction.setSelected(true);
2131+
21302132
console.clear();
21312133
status.progress(tr("Uploading to I/O Board..."));
21322134

@@ -2172,9 +2174,7 @@ public void run() {
21722174
}
21732175
status.unprogress();
21742176
uploading = false;
2175-
//toolbar.clear();
2176-
toolbar.deactivateExport();
2177-
2177+
uploadAction.setSelected(false);
21782178
resumeOrCloseSerialMonitor();
21792179
resumeOrCloseSerialPlotter();
21802180
base.onBoardOrPortChange();
@@ -2267,9 +2267,7 @@ public void run() {
22672267
}
22682268
status.unprogress();
22692269
uploading = false;
2270-
//toolbar.clear();
2271-
toolbar.deactivateExport();
2272-
2270+
uploadUsingProgrammerAction.setSelected(false);
22732271
resumeOrCloseSerialMonitor();
22742272
resumeOrCloseSerialPlotter();
22752273

@@ -2547,8 +2545,6 @@ private void handlePrint() {
25472545
public void statusError(String what) {
25482546
System.err.println(what);
25492547
status.error(what);
2550-
//new Exception("deactivating RUN").printStackTrace();
2551-
toolbar.deactivateRun();
25522548
}
25532549

25542550

0 commit comments

Comments
 (0)