-
-
Notifications
You must be signed in to change notification settings - Fork 156
Expand file tree
/
Copy pathThinkDifferent.java
More file actions
182 lines (147 loc) · 5.34 KB
/
ThinkDifferent.java
File metadata and controls
182 lines (147 loc) · 5.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Part of the Processing project - http://processing.org
Copyright (c) 2012-2014 The Processing Foundation
Copyright (c) 2007-2012 Ben Fry and Casey Reas
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2, as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package processing.core;
import java.awt.Desktop;
import java.awt.Image;
import java.awt.Taskbar;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
/**
* 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 stripped-down version of what's in processing.app.platform to fix
* <a href="https://github.com/processing/processing/issues/3301">3301</a>.
*/
public class ThinkDifferent {
static private Desktop desktop;
static private Taskbar taskbar;
// True if user has tried to quit once. Prevents us from canceling the quit
// call if the sketch is held up for some reason, like an exception that's
// managed to put the sketch in a bad state.
static boolean attemptedQuit;
/**
* Initialize the sketch with the quit handler.
*
* Initialize the sketch with the quit handler such that, if there is no known
* crash, the application will not exit on its own if this is the first quit
* attempt.
*
* @param sketch The sketch whose quit handler callback should be set.
*/
static public void init(final PApplet sketch) {
getDesktop().setQuitHandler((event, quitResponse) -> {
sketch.exit();
boolean noKnownCrash = PApplet.uncaughtThrowable == null;
if (noKnownCrash && !attemptedQuit) { // haven't tried yet
quitResponse.cancelQuit(); // tell OS X we'll handle this
attemptedQuit = true;
} else {
quitResponse.performQuit(); // just force it this time
}
});
}
/**
* Remove the quit handler.
*/
static public void cleanup() {
getDesktop().setQuitHandler(null);
}
/**
* Called via reflection from PSurfaceAWT and others, set the dock icon image.
* @param image The image to provide for Processing icon.
*/
static public void setIconImage(Image image) {
getTaskbar().setIconImage(image);
}
/**
* Get the taskbar where OS visual settings can be provided.
* @return Cached taskbar singleton instance.
*/
static private Taskbar getTaskbar() {
if (taskbar == null) {
taskbar = Taskbar.getTaskbar();
}
return taskbar;
}
/**
* Get the desktop where OS behavior can be provided.
* @return Cached desktop singleton instance.
*/
static private Desktop getDesktop() {
if (desktop == null) {
desktop = Desktop.getDesktop();
}
return desktop;
}
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
static native public void hideMenuBar();
static native public void showMenuBar();
// deprecated
// https://developer.apple.com/documentation/appkit/nsapplication/activate(ignoringotherapps:)
static native public void activateIgnoringOtherApps();
// added in macOS 14 (Sonoma)
// https://developer.apple.com/documentation/appkit/nsapplication/activate()
static native public void activate();
// Used by py5 to bring Sketch to the front
static public boolean activateSketchWindow() {
try {
String osVersion = System.getProperty("os.version");
int versionNumber = Integer.parseInt(osVersion.split("\\.")[0]);
if (versionNumber >= 14) {
activate();
return true;
} else if (versionNumber >= 10) {
activateIgnoringOtherApps();
return true;
}
} catch (Exception e) {
return false;
}
return false;
}
static {
final String NATIVE_FILENAME = "libDifferent.jnilib";
try {
File temp = File.createTempFile("processing", "different");
if (temp.delete() && temp.mkdirs()) {
temp.deleteOnExit();
File jnilibFile = new File(temp, NATIVE_FILENAME);
InputStream input = ThinkDifferent.class.getResourceAsStream(NATIVE_FILENAME);
if (input != null) {
if (PApplet.saveStream(jnilibFile, input)) {
System.load(jnilibFile.getAbsolutePath());
} else {
System.err.println("Full screen disabled: could not save library");
}
} else {
System.err.println("Full screen disabled: could not load " + NATIVE_FILENAME + " from core.jar");
}
} else {
System.err.println("Full screen disabled: could not create temporary folder");
}
} catch (IOException e) {
System.err.println("Full screen disabled");
e.printStackTrace();
}
}
}