Skip to content

Commit

Permalink
Handle quitting of OS X application in a more "natural" way.
Browse files Browse the repository at this point in the history
  • Loading branch information
toonetown committed Apr 13, 2016
1 parent e66673e commit c9b6938
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
6 changes: 6 additions & 0 deletions src/us/deathmarine/luyten/Luyten.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ public static void openFileInInstance(File fileToOpen) {
}
processPendingFiles();
}

// Function which exits the application if it's running
public static void quitInstance() {
final MainWindow mainWindow = mainWindowRef.get();
if (mainWindow != null) { mainWindow.onExitMenu(); }
}

public static File getFileFromCommandLine(String[] args) {
File fileFromCommandLine = null;
Expand Down
6 changes: 6 additions & 0 deletions src/us/deathmarine/luyten/LuytenOsx.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@
*/
public class LuytenOsx extends Luyten {
public static void main(String[] args) {
// Set a flag that says we are running in OS X
System.setProperty("us.deathmarine.luyten.Luyten.running_in_osx", "true");

// Add an adapter as the handler to a new instance of the application class
@SuppressWarnings("deprecation") Application app = new Application();
app.addApplicationListener(new ApplicationAdapter() {
public void handleOpenFile(ApplicationEvent e) {
Luyten.openFileInInstance(new File(e.getFilename()));
}
public void handleQuit(ApplicationEvent e) {
Luyten.quitInstance();
}
});

// Call the superclass's main function
Expand Down
21 changes: 12 additions & 9 deletions src/us/deathmarine/luyten/MainMenuBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,18 @@ public void actionPerformed(ActionEvent e) {
fileMenu.add(menuItem);
fileMenu.addSeparator();

menuItem = new JMenuItem("Exit");
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4, ActionEvent.ALT_MASK));
menuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
mainWindow.onExitMenu();
}
});
fileMenu.add(menuItem);
// Only add the exit command for non-OS X. OS X handles its close automatically
if (!("true".equals(System.getProperty("us.deathmarine.luyten.Luyten.running_in_osx")))) {
menuItem = new JMenuItem("Exit");
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4, ActionEvent.ALT_MASK));
menuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
mainWindow.onExitMenu();
}
});
fileMenu.add(menuItem);
}
}

private void buildEditMenu(JMenu editMenu) {
Expand Down

0 comments on commit c9b6938

Please sign in to comment.