From 92b2ecc8badff56be38eeefd42fcc24bb74650ff Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Thu, 9 Oct 2014 17:30:43 +0000 Subject: [PATCH] * Fix for Mac OS X quit behaviour (Dock->Quit, CTRL+Q) not being executed when application is closed via Quit rather than clicking [X] on the main window --- .classpath | 1 - source/net/filebot/HistorySpooler.java | 11 +++++ source/net/filebot/Main.java | 11 +---- source/net/filebot/mac/MacAppUtilities.java | 45 ++++++++++----------- 4 files changed, 34 insertions(+), 34 deletions(-) diff --git a/.classpath b/.classpath index d42ccdf4..a13796d5 100644 --- a/.classpath +++ b/.classpath @@ -30,7 +30,6 @@ - diff --git a/source/net/filebot/HistorySpooler.java b/source/net/filebot/HistorySpooler.java index 5eb11a5e..598986a5 100644 --- a/source/net/filebot/HistorySpooler.java +++ b/source/net/filebot/HistorySpooler.java @@ -28,6 +28,17 @@ public final class HistorySpooler { return instance; } + // commit session history on shutdown + static { + Runtime.getRuntime().addShutdownHook(new Thread() { + + @Override + public void run() { + HistorySpooler.getInstance().commit(); + } + }); + } + private File persistentHistoryFile = new File(getApplicationFolder(), "history.xml"); private int persistentHistoryTotalSize = -1; private boolean persistentHistoryEnabled = true; diff --git a/source/net/filebot/Main.java b/source/net/filebot/Main.java index 028de235..cd1b35c9 100644 --- a/source/net/filebot/Main.java +++ b/source/net/filebot/Main.java @@ -170,15 +170,6 @@ public class Main { // CLI mode => run command-line interface and then exit if (args.runCLI()) { - // commit session history on shutdown - Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { - - @Override - public void run() { - HistorySpooler.getInstance().commit(); - } - })); - // default cross-platform laf used in scripting to nimbus instead of metal (if possible) if (args.script != null && !isHeadless()) { try { @@ -272,6 +263,8 @@ public class Main { @Override public void windowClosing(WindowEvent e) { e.getWindow().setVisible(false); + + // make sure any long running operations are done now and not later on the shutdownhook thread HistorySpooler.getInstance().commit(); if (!isAppStore()) { diff --git a/source/net/filebot/mac/MacAppUtilities.java b/source/net/filebot/mac/MacAppUtilities.java index 6628755d..15792276 100644 --- a/source/net/filebot/mac/MacAppUtilities.java +++ b/source/net/filebot/mac/MacAppUtilities.java @@ -1,9 +1,8 @@ package net.filebot.mac; -import java.awt.EventQueue; import java.awt.Window; -import java.awt.event.WindowEvent; import java.io.File; +import java.lang.reflect.Method; import java.util.logging.Level; import java.util.logging.Logger; @@ -11,10 +10,6 @@ import javax.swing.UIManager; import ca.weblite.objc.Client; -import com.apple.eawt.Application; -import com.apple.eawt.ApplicationAdapter; -import com.apple.eawt.ApplicationEvent; - public class MacAppUtilities { private static Client _objc; @@ -40,7 +35,9 @@ public class MacAppUtilities { public static void setWindowCanFullScreen(Window window) { try { - com.apple.eawt.FullScreenUtilities.setWindowCanFullScreen(window, true); + Class fullScreenUtilities = Class.forName("com.apple.eawt.FullScreenUtilities"); + Method setWindowCanFullScreen = fullScreenUtilities.getMethod("setWindowCanFullScreen", new Class[] { Window.class, boolean.class }); + setWindowCanFullScreen.invoke(null, window, true); } catch (Throwable t) { Logger.getLogger(MacAppUtilities.class.getName()).log(Level.WARNING, "setWindowCanFullScreen not supported: " + t); } @@ -48,7 +45,10 @@ public class MacAppUtilities { public static void requestForeground() { try { - com.apple.eawt.Application.getApplication().requestForeground(true); + Class application = Class.forName("com.apple.eawt.Application"); + Object instance = application.getMethod("getApplication").invoke(null); + Method requestForeground = application.getMethod("requestForeground", new Class[] { boolean.class }); + requestForeground.invoke(instance, true); } catch (Throwable t) { Logger.getLogger(MacAppUtilities.class.getName()).log(Level.WARNING, "requestForeground not supported: " + t); } @@ -56,7 +56,9 @@ public class MacAppUtilities { public static void revealInFinder(File file) { try { - com.apple.eio.FileManager.revealInFinder(file); + Class fileManager = Class.forName("com.apple.eio.FileManager"); + Method revealInFinder = fileManager.getMethod("revealInFinder", new Class[] { File.class }); + revealInFinder.invoke(null, file); } catch (Throwable t) { Logger.getLogger(MacAppUtilities.class.getName()).log(Level.WARNING, "revealInFinder not supported: " + t); } @@ -67,21 +69,16 @@ public class MacAppUtilities { UIManager.put("TitledBorder.border", UIManager.getBorder("InsetBorder.aquaVariant")); // make sure Application Quit Events get forwarded to normal Window Listeners - Application.getApplication().addApplicationListener(new ApplicationAdapter() { - - @Override - public void handleQuit(ApplicationEvent evt) { - for (Window window : Window.getOwnerlessWindows()) { - // close all windows - window.setVisible(false); - - // call window listeners - EventQueue.invokeLater(() -> { - window.dispatchEvent(new WindowEvent(window, WindowEvent.WINDOW_CLOSING)); - }); - } - } - }); + try { + Class application = Class.forName("com.apple.eawt.Application"); + Object instance = application.getMethod("getApplication").invoke(null); + Class quitStrategy = Class.forName("com.apple.eawt.QuitStrategy"); + Method setQuitStrategy = application.getMethod("setQuitStrategy", quitStrategy); + Object closeAllWindows = quitStrategy.getField("CLOSE_ALL_WINDOWS").get(null); + setQuitStrategy.invoke(instance, closeAllWindows); + } catch (Throwable t) { + Logger.getLogger(MacAppUtilities.class.getName()).log(Level.WARNING, "setQuitStrategy not supported: " + t); + } } public static boolean isLockedFolder(File folder) {