Skip to content

Commit

Permalink
Improve startup, use backgroud tread (executor version)
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardojlrufino committed May 10, 2020
1 parent b4c4cd2 commit 6fb467c
Showing 1 changed file with 21 additions and 27 deletions.
48 changes: 21 additions & 27 deletions app/src/processing/app/Base.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@
import java.io.*;
import java.util.List;
import java.util.Timer;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.*;
import java.util.logging.Handler;
import java.util.logging.Level;
Expand Down Expand Up @@ -126,6 +128,9 @@ public class Base {

private PdeKeywords pdeKeywords;
private final List<JMenuItem> recentSketchesMenuItems = new LinkedList<>();

// Executor to load / reload menus in backgroud.
private Executor menuExecutor = Executors.newCachedThreadPool();

static public void main(String args[]) throws Exception {
if (!OSUtils.isWindows()) {
Expand Down Expand Up @@ -1083,14 +1088,10 @@ protected boolean handleQuitEach() {
public void rebuildSketchbookMenus() {
//System.out.println("async enter");
//new Exception().printStackTrace();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
//System.out.println("starting rebuild");
rebuildSketchbookMenu(Editor.sketchbookMenu);
rebuildToolbarMenu(Editor.toolbarMenu);
//System.out.println("done with rebuild");
}
});
//System.out.println("starting rebuild");
rebuildSketchbookMenu(Editor.sketchbookMenu);
rebuildToolbarMenu(Editor.toolbarMenu);
//System.out.println("done with rebuild");
//System.out.println("async exit");
}

Expand All @@ -1113,30 +1114,28 @@ public void actionPerformed(ActionEvent e) {
menu.add(item);
menu.addSeparator();

SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
// Execute in backgroud thread, no need UI thread becouse no rendering needed
menuExecutor.execute(() -> {
// Add a list of all sketches and subfolders
boolean sketches = addSketches(menu, BaseNoGui.getSketchbookFolder());
if (sketches) menu.addSeparator();

// Add each of the subfolders of examples directly to the menu
boolean found = addSketches(menu, BaseNoGui.getExamplesFolder());
if (found) menu.addSeparator();
}
});

}


protected void rebuildSketchbookMenu(JMenu menu) {
menu.removeAll();

SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
addSketches(menu, BaseNoGui.getSketchbookFolder());

// Execute in backgroud thread, no need UI thread becouse no rendering needed
menuExecutor.execute(() -> {

addSketches(menu, BaseNoGui.getSketchbookFolder());

JMenu librariesMenu = JMenuUtils.findSubMenuWithLabel(menu, "libraries");
if (librariesMenu != null) {
menu.remove(librariesMenu);
Expand All @@ -1145,8 +1144,8 @@ public void run() {
if (hardwareMenu != null) {
menu.remove(hardwareMenu);
}
}
});

}

private LibraryList getSortedLibraries() {
Expand Down Expand Up @@ -1227,10 +1226,8 @@ public void rebuildExamplesMenu(JMenu menu) {

menu.removeAll();

SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
// Execute in backgroud thread, no need UI thread becouse no rendering needed
menuExecutor.execute(() -> {
// Add examples from distribution "example" folder
JMenuItem label = new JMenuItem(tr("Built-in Examples"));
label.setEnabled(false);
Expand Down Expand Up @@ -1392,7 +1389,6 @@ public void run() {
addSketchesSubmenu(menu, lib);
}
}
}
});

}
Expand Down Expand Up @@ -1492,9 +1488,8 @@ protected void onIndexesUpdated() throws Exception {
public void rebuildBoardsMenu() throws Exception {
boardsCustomMenus = new LinkedList<>();

SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
// Execute in backgroud thread, no need UI thread becouse no rendering needed
menuExecutor.execute(() -> {
// The first custom menu is the "Board" selection submenu
JMenu boardMenu = new JMenu(tr("Board"));
boardMenu.putClientProperty("removeOnWindowDeactivation", true);
Expand Down Expand Up @@ -1616,7 +1611,6 @@ public void actionPerformed(ActionEvent actionevent) {
menuItemToClick.setSelected(true);
menuItemToClick.getAction().actionPerformed(new ActionEvent(this, -1, ""));
}
}
});

}
Expand Down

0 comments on commit 6fb467c

Please sign in to comment.