From c9b6938bbe001d4233676d6f8a45bfed3a843bbd Mon Sep 17 00:00:00 2001 From: Nathan Toone Date: Wed, 13 Apr 2016 16:07:48 -0600 Subject: [PATCH] Handle quitting of OS X application in a more "natural" way. --- src/us/deathmarine/luyten/Luyten.java | 6 ++++++ src/us/deathmarine/luyten/LuytenOsx.java | 6 ++++++ src/us/deathmarine/luyten/MainMenuBar.java | 21 ++++++++++++--------- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/us/deathmarine/luyten/Luyten.java b/src/us/deathmarine/luyten/Luyten.java index 7fa8975..4276cc9 100644 --- a/src/us/deathmarine/luyten/Luyten.java +++ b/src/us/deathmarine/luyten/Luyten.java @@ -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; diff --git a/src/us/deathmarine/luyten/LuytenOsx.java b/src/us/deathmarine/luyten/LuytenOsx.java index 94b5062..b319e58 100644 --- a/src/us/deathmarine/luyten/LuytenOsx.java +++ b/src/us/deathmarine/luyten/LuytenOsx.java @@ -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 diff --git a/src/us/deathmarine/luyten/MainMenuBar.java b/src/us/deathmarine/luyten/MainMenuBar.java index c1f3b9a..e811471 100644 --- a/src/us/deathmarine/luyten/MainMenuBar.java +++ b/src/us/deathmarine/luyten/MainMenuBar.java @@ -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) {