From 31dc32c22536e757ace286de39870f491bf4dbcb Mon Sep 17 00:00:00 2001 From: Adam Date: Sat, 1 Jan 2022 17:52:39 -0500 Subject: [PATCH] Set macos quitStrategy to CLOSE_ALL_WINDOWS The quit strategy API existed in com.apple.eawt prior to Java 9, and was moved into java.desktop in 9. This move makes our orange-extensions API incompatiable with the 9 eawt package too, so we can not use Application.setQuitStrategy on 9+ either, without making orange-extensions multi-release too. Since Desktop.setQuitStrategy is the new standard way of doing this, use it if available, and otherwise use the Java 8 Application.setQuitStrategy instead. --- .../java/net/runelite/client/ui/ClientUI.java | 6 +++ .../runelite/client/ui/MacOSQuitStrategy.java | 48 +++++++++++++++++++ .../runelite/client/ui/MacOSQuitStrategy.java | 37 ++++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 runelite-client/src/main/java/net/runelite/client/ui/MacOSQuitStrategy.java create mode 100644 runelite-client/src/main/java11/net/runelite/client/ui/MacOSQuitStrategy.java diff --git a/runelite-client/src/main/java/net/runelite/client/ui/ClientUI.java b/runelite-client/src/main/java/net/runelite/client/ui/ClientUI.java index 0cee8b636f..84a2e4858f 100644 --- a/runelite-client/src/main/java/net/runelite/client/ui/ClientUI.java +++ b/runelite-client/src/main/java/net/runelite/client/ui/ClientUI.java @@ -332,6 +332,12 @@ public class ClientUI frame.setResizable(true); frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); + if (OSType.getOSType() == OSType.MacOS) + { + // Change the default quit strategy to CLOSE_ALL_WINDOWS so that ctrl+q + // triggers the listener below instead of exiting. + MacOSQuitStrategy.setup(); + } frame.addWindowListener(new WindowAdapter() { @Override diff --git a/runelite-client/src/main/java/net/runelite/client/ui/MacOSQuitStrategy.java b/runelite-client/src/main/java/net/runelite/client/ui/MacOSQuitStrategy.java new file mode 100644 index 0000000000..4c191ed3ca --- /dev/null +++ b/runelite-client/src/main/java/net/runelite/client/ui/MacOSQuitStrategy.java @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2022, Adam + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.client.ui; + +import com.apple.eawt.Application; +import com.apple.eawt.QuitStrategy; + +class MacOSQuitStrategy +{ + public static void setup() + { + try + { + // com.apple.eawt.QuitStrategy was moved to java.desktop in Java 9, + // but our OrangeExtensions API targets 1.6, so this code is only valid + // on 8 below. + Application.getApplication() + .setQuitStrategy(QuitStrategy.CLOSE_ALL_WINDOWS); + } + catch (NoClassDefFoundError ex) + { + // IntelliJ doesn't handle our multi-release Maven setup well, and will run + // this class on 11+. Ignore the error so the client can launch. + } + } +} diff --git a/runelite-client/src/main/java11/net/runelite/client/ui/MacOSQuitStrategy.java b/runelite-client/src/main/java11/net/runelite/client/ui/MacOSQuitStrategy.java new file mode 100644 index 0000000000..6af420ea4c --- /dev/null +++ b/runelite-client/src/main/java11/net/runelite/client/ui/MacOSQuitStrategy.java @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2022, Adam + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.client.ui; + +import java.awt.Desktop; +import java.awt.desktop.QuitStrategy; + +class MacOSQuitStrategy +{ + public static void setup() + { + Desktop.getDesktop() + .setQuitStrategy(QuitStrategy.CLOSE_ALL_WINDOWS); + } +}