Skip to content

Commit

Permalink
[openrocket#1100] Implement multi-document behavior for macOS
Browse files Browse the repository at this point in the history
Note: this is not yet "final" there are still a number of fixes that need to happen, such as fixing the application menu (which is not responsive after closing everyhing) and a high memory usage even in closed down mode
  • Loading branch information
SiboVG committed Jun 16, 2022
1 parent 9e73f6e commit fa10dcd
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 18 deletions.
29 changes: 28 additions & 1 deletion swing/src/net/sf/openrocket/gui/main/BasicFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import net.miginfocom.swing.MigLayout;
import net.sf.openrocket.aerodynamics.WarningSet;
import net.sf.openrocket.appearance.DecalImage;
import net.sf.openrocket.arch.SystemInfo;
import net.sf.openrocket.document.OpenRocketDocument;
import net.sf.openrocket.document.OpenRocketDocumentFactory;
import net.sf.openrocket.document.StorageOptions;
Expand Down Expand Up @@ -1687,7 +1688,8 @@ private boolean closeAction() {
ComponentAnalysisDialog.hideDialog();

frames.remove(this);
if (frames.isEmpty()) {
// Don't quit the application on macOS
if (frames.isEmpty() && (SystemInfo.getPlatform() != SystemInfo.Platform.MAC_OS)) {
log.info("Last frame closed, exiting");
System.exit(0);
}
Expand All @@ -1704,6 +1706,31 @@ public void printAction() {
new PrintDialog(this, document, rotation).setVisible(true);
}

/**
* Opens a new design file or the last design file, if set in the preferences.
* Can be used for reopening the application or opening it the first time.
*/
public static void reopen() {
if (!Application.getPreferences().isAutoOpenLastDesignOnStartupEnabled()) {
BasicFrame.newAction();
} else {
String lastFile = MRUDesignFile.getInstance().getLastEditedDesignFile();
if (lastFile != null) {
log.info("Opening last design file: " + lastFile);
if (!BasicFrame.open(new File(lastFile), null)) {
MRUDesignFile.getInstance().removeFile(lastFile);
BasicFrame.newAction();
}
else {
MRUDesignFile.getInstance().addFile(lastFile);
}
}
else {
BasicFrame.newAction();
}
}
}


/**
* Open a new design window with a basic rocket+stage.
Expand Down
7 changes: 7 additions & 0 deletions swing/src/net/sf/openrocket/startup/OSXSetup.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.awt.desktop.OpenFilesHandler;
import java.awt.desktop.PreferencesHandler;
import java.awt.desktop.QuitHandler;
import java.awt.desktop.AppReopenedListener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -50,6 +51,11 @@ final class OSXSetup {
r.cancelQuit();
};

private static final AppReopenedListener APP_REOPENED_HANDLER = (e) -> {
log.info("App re-opened");
BasicFrame.reopen();
};

/**
* The handler for the About item in the OSX app menu
*/
Expand Down Expand Up @@ -92,6 +98,7 @@ static void setupOSX() {
osxDesktop.setAboutHandler(ABOUT_HANDLER);
osxDesktop.setPreferencesHandler(PREFERENCES_HANDLER);
osxDesktop.setQuitHandler(QUIT_HANDLER);
osxDesktop.addAppEventListener(APP_REOPENED_HANDLER);

// Set the dock icon to the largest icon
final Image dockIcon = Toolkit.getDefaultToolkit().getImage(
Expand Down
18 changes: 1 addition & 17 deletions swing/src/net/sf/openrocket/startup/SwingStartup.java
Original file line number Diff line number Diff line change
Expand Up @@ -220,23 +220,7 @@ private void runInEDT(String[] args) {
// Starting action (load files or open new document)
log.info("Opening main application window");
if (!handleCommandLine(args)) {
if (!Application.getPreferences().isAutoOpenLastDesignOnStartupEnabled()) {
BasicFrame.newAction();
} else {
String lastFile = MRUDesignFile.getInstance().getLastEditedDesignFile();
if (lastFile != null) {
if (!BasicFrame.open(new File(lastFile), null)) {
MRUDesignFile.getInstance().removeFile(lastFile);
BasicFrame.newAction();
}
else {
MRUDesignFile.getInstance().addFile(lastFile);
}
}
else {
BasicFrame.newAction();
}
}
BasicFrame.reopen();
}

// Check whether update info has been fetched or whether it needs more time
Expand Down

0 comments on commit fa10dcd

Please sign in to comment.