Deal with issues related to macOS application behavior. We have to register a quit handler to safely shut down the sketch, otherwise OS X will just kill the sketch when a user hits Cmd-Q. In addition, we have a method to set the dock icon image, so we look more like a native application. This is a
| 42 | * <a href="https://github.com/processing/processing/issues/3301">3301</a>. |
| 43 | */ |
| 44 | public class ThinkDifferent { |
| 45 | static private Desktop desktop; |
| 46 | static private Taskbar taskbar; |
| 47 | |
| 48 | // True if user has tried to quit once. Prevents us from canceling the quit |
| 49 | // call if the sketch is held up for some reason, like an exception that's |
| 50 | // managed to put the sketch in a bad state. |
| 51 | static boolean attemptedQuit; |
| 52 | |
| 53 | |
| 54 | /** |
| 55 | * Initialize the sketch with the quit handler. |
| 56 | * |
| 57 | * Initialize the sketch with the quit handler such that, if there is no known |
| 58 | * crash, the application will not exit on its own if this is the first quit |
| 59 | * attempt. |
| 60 | * |
| 61 | * @param sketch The sketch whose quit handler callback should be set. |
| 62 | */ |
| 63 | static public void init(final PApplet sketch) { |
| 64 | getDesktop().setQuitHandler((event, quitResponse) -> { |
| 65 | sketch.exit(); |
| 66 | |
| 67 | boolean noKnownCrash = PApplet.uncaughtThrowable == null; |
| 68 | |
| 69 | if (noKnownCrash && !attemptedQuit) { // haven't tried yet |
| 70 | quitResponse.cancelQuit(); // tell OS X we'll handle this |
| 71 | attemptedQuit = true; |
| 72 | } else { |
| 73 | quitResponse.performQuit(); // just force it this time |
| 74 | } |
| 75 | }); |
| 76 | } |
| 77 | |
| 78 | |
| 79 | /** |
| 80 | * Remove the quit handler. |
| 81 | */ |
| 82 | static public void cleanup() { |
| 83 | getDesktop().setQuitHandler(null); |
| 84 | } |
| 85 | |
| 86 | |
| 87 | /** |
| 88 | * Called via reflection from PSurfaceAWT and others, set the dock icon image. |
| 89 | * @param image The image to provide for Processing icon. |
| 90 | */ |
| 91 | static public void setIconImage(Image image) { |
| 92 | getTaskbar().setIconImage(image); |
| 93 | } |
| 94 | |
| 95 | |
| 96 | /** |
| 97 | * Get the taskbar where OS visual settings can be provided. |
| 98 | * @return Cached taskbar singleton instance. |
| 99 | */ |
| 100 | static private Taskbar getTaskbar() { |
| 101 | if (taskbar == null) { |
nothing calls this directly
no test coverage detected