From 13bd07cff6eac35073dad078928f9ec6a3fe0b0e Mon Sep 17 00:00:00 2001 From: Johan Kaving Date: Thu, 15 Oct 2020 23:42:35 +0200 Subject: [PATCH] Fix quitting on macOS The QuitHandler that proxies our Exit action needs to call the performQuit() method on the QuitResponse that it receives. See the Javadoc for com.apple.eawt.QuitHandler.handleQuitRequestWith() which says: /** * Invoked when the application is asked to quit. * * Implementors must call either {@link QuitResponse#cancelQuit()}, {@link QuitResponse#performQuit()}, or ensure the application terminates. * The process (or log-out) requesting this app to quit will be blocked until the {@link QuitResponse} is handled. ... Fixes #247 --- .../java/com/tagtraum/perf/gcviewer/ctrl/action/Exit.java | 3 ++- .../com/tagtraum/perf/gcviewer/view/util/OSXSupport.java | 8 ++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/tagtraum/perf/gcviewer/ctrl/action/Exit.java b/src/main/java/com/tagtraum/perf/gcviewer/ctrl/action/Exit.java index 26a949d7..5e2474fe 100644 --- a/src/main/java/com/tagtraum/perf/gcviewer/ctrl/action/Exit.java +++ b/src/main/java/com/tagtraum/perf/gcviewer/ctrl/action/Exit.java @@ -39,7 +39,8 @@ public void actionPerformed(final ActionEvent e) { } // Used by OS X adaptations - public void quit() { + public boolean quit() { actionPerformed(null); + return true; } } diff --git a/src/main/java/com/tagtraum/perf/gcviewer/view/util/OSXSupport.java b/src/main/java/com/tagtraum/perf/gcviewer/view/util/OSXSupport.java index e6df4e51..ad5cb830 100644 --- a/src/main/java/com/tagtraum/perf/gcviewer/view/util/OSXSupport.java +++ b/src/main/java/com/tagtraum/perf/gcviewer/view/util/OSXSupport.java @@ -112,7 +112,7 @@ public static void addOSXHandler(String handlerClassName, Class handlerClass = Class.forName(handlerClassName); if (action != null) { - Object aboutHandlerProxy = + Object handlerProxy = Proxy.newProxyInstance(OSXSupport.class.getClassLoader(), new Class[]{handlerClass}, new InvocationHandler() { @@ -121,10 +121,14 @@ public Object invoke(Object o, Method method, Object[] args) throws Throwable { if (method.getName().equals(handlerMethodName)) { action.actionPerformed(null); } + if (method.getName().equals("handleQuitRequestWith")) { + Object quitResponse = args[1]; + Class.forName("com.apple.eawt.QuitResponse").getDeclaredMethod("performQuit").invoke(quitResponse); + } return null; } }); - application.getClass().getMethod(handlerSetterMethodName, handlerClass).invoke(application, aboutHandlerProxy); + application.getClass().getMethod(handlerSetterMethodName, handlerClass).invoke(application, handlerProxy); } else { application.getClass().getMethod(handlerSetterMethodName, handlerClass).invoke(application, (Object) null); }