Skip to content

Commit

Permalink
Faster startup - init fonts in main in bg, preload jpanels
Browse files Browse the repository at this point in the history
  • Loading branch information
pskowronek authored and ahadas committed Sep 16, 2023
1 parent a3115d9 commit b46cb56
Show file tree
Hide file tree
Showing 13 changed files with 183 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,10 @@ public enum OsVersion implements ComparableRuntimeProperty {


static {
// force it to load as early as possible
PreloadedJFrame.init();
}

/** Logger used by this class. */
private static final Logger LOGGER = LoggerFactory.getLogger(OsVersion.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.awt.BorderLayout;
import java.awt.LayoutManager;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedDeque;

import javax.swing.JFrame;
import javax.swing.JPanel;
Expand All @@ -38,23 +40,60 @@ public class PreloadedJFrame extends JFrame {
preLoad();
}

public static PreloadedJFrame instance;
private static JPanel preloadedPanel;
private static final Queue<PreloadedJFrame> preloadedFrame = new ConcurrentLinkedDeque<>();

private static final Queue<JPanel> preloadedPanels = new ConcurrentLinkedDeque<>();

private Object mainFrameObj;

private static void preLoad() {
new Thread(() -> {
System.out.println("---- FRAME!");
LOGGER.error("Going to pre-create a couple of JFrames...");
var pre = System.currentTimeMillis();
instance = new PreloadedJFrame();
instance.setVisible(true);
instance.setVisible(false);
preloadedPanel = new JPanel(new BorderLayout());
System.out.println("---- FRAME in " + (System.currentTimeMillis() - pre));
preloadedFrame.add(new PreloadedJFrame());
preloadedFrame.add(new PreloadedJFrame());
LOGGER.error("JFrames pre-creation completed in {}ms", (System.currentTimeMillis() - pre));

LOGGER.error("Going to pre-create a couple of JPanels...");
pre = System.currentTimeMillis();
preloadedPanels.add(new JPanel());
preloadedPanels.add(new JPanel());
preloadedPanels.add(new JPanel());
preloadedPanels.add(new JPanel());
preloadedPanels.add(new JPanel());
preloadedPanels.add(new JPanel());
LOGGER.error("JPanel pre-creation completed in {}ms", (System.currentTimeMillis() - pre));

}, "Preload-JFrame").start();
}

private void setMainFrameObj(Object mainFrameObj) {
this.mainFrameObj = mainFrameObj;
}

}).start();
public Object getMainFrameObject() {
return mainFrameObj;
}

public static void init() {
// do nothing
// noop
}

public static JFrame getJFrame(Object mainFrame) {
var result = preloadedFrame.poll();
result = result != null ? result : new PreloadedJFrame();
result.setMainFrameObj(mainFrame);
return result;
}

public static JPanel getJPanel(LayoutManager layout) {
var result = preloadedPanels.poll();
if (result == null) {
result = new JPanel(layout);
} else {
result.setLayout(layout);
}
return result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import com.mucommander.ui.viewer.EditorSnapshot;
import com.mucommander.ui.viewer.ViewerSnapshot;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package com.mucommander;

import java.awt.BorderLayout;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.lang.reflect.InvocationTargetException;
Expand Down Expand Up @@ -387,6 +386,7 @@ private void run() {
printStartupMessage("Loading theme...");
try {
SwingUtilities.invokeAndWait(() -> com.mucommander.ui.theme.ThemeManager.loadCurrentTheme());
LOGGER.error("Loading theme DONE");
} catch (InterruptedException | InvocationTargetException e) {
LOGGER.error("Error loading current theme, continuing without it", e);
}
Expand Down Expand Up @@ -500,7 +500,7 @@ private void run() {
}

// Invoke in a different thread: https://www.oracle.com/technical-resources/articles/javase/swingworker.html
new Thread(() -> {
Thread mainThread = new Thread(() -> {
LOGGER.error("muC UI about to be presented");
// Creates the initial main frame using any initial path specified by the command line.
printStartupMessage("Initializing window...");
Expand Down Expand Up @@ -552,7 +552,8 @@ private void run() {
new InitialSetupDialog(WindowManager.getCurrentMainFrame().getJFrame()).showDialog();
});
}
}, "MainFrameInit").start();
}, "MainFrameInit");
mainThread.start();

// Check for newer version unless it was disabled
if (MuConfigurations.getPreferences()
Expand Down Expand Up @@ -639,4 +640,5 @@ public static void initiateShutdown() {
LOGGER.error("failed to shut down", e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ public ToggleUseSinglePanelAction(MainFrame mainFrame, Map<String, Object> prope
}

private void hideInactivePanel() {
mainFrame.getInactivePanel().setVisible(false);
mainFrame.getInactivePanel().getPanel().setVisible(false);
}

private void showInactivePanel() {
mainFrame.getInactivePanel().setVisible(true);
mainFrame.getInactivePanel().getPanel().setVisible(true);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public class TabTitleDialog extends FocusDialog implements ActionListener {
private FolderPanel folderPanel;

public TabTitleDialog(MainFrame mainFrame, FolderPanel folderPanel) {
super(mainFrame.getJFrame(), ActionProperties.getActionLabel(ActionType.SetTabTitle), folderPanel);
super(mainFrame.getJFrame(), ActionProperties.getActionLabel(ActionType.SetTabTitle), folderPanel.getPanel());

this.folderPanel = folderPanel;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ private boolean acceptOrRejectDragEvent(DropTargetDragEvent event) {
// Change the mouse cursor on this FolderPanel and child components
Cursor newCursor = getDragActionCursor(currentDropAction, dragAccepted);
LOGGER.trace("cursor=" + newCursor);
folderPanel.setCursor(newCursor);
folderPanel.getPanel().setCursor(newCursor);

return dragAccepted;
}
Expand Down Expand Up @@ -259,12 +259,12 @@ public void dropActionChanged(DropTargetDragEvent event) {

public void dragExit(DropTargetEvent event) {
// Restore default cursor
folderPanel.setCursor(Cursor.getDefaultCursor());
folderPanel.getPanel().setCursor(Cursor.getDefaultCursor());
}

public void drop(DropTargetDropEvent event) {
// Restore default cursor, no matter what
folderPanel.setCursor(Cursor.getDefaultCursor());
folderPanel.getPanel().setCursor(Cursor.getDefaultCursor());

// The drop() method is called even if a DropTargetDropEvent was rejected before,
// so this test is really necessary
Expand Down Expand Up @@ -306,7 +306,7 @@ public void drop(DropTargetDropEvent event) {
folderPanel.tryChangeCurrentFolder(file.getParent(), file, false);

// Request focus on the FolderPanel
folderPanel.requestFocus();
folderPanel.getPanel().requestFocus();
}
// Normal mode: copy or move dropped files to the FolderPanel's current folder
else {
Expand Down
Loading

0 comments on commit b46cb56

Please sign in to comment.