1
1
package rprocessing ;
2
2
3
3
import java .awt .Component ;
4
+ import java .awt .Frame ;
4
5
import java .awt .Window ;
5
6
import java .awt .event .ComponentAdapter ;
6
7
import java .awt .event .ComponentEvent ;
7
8
import java .lang .Thread .UncaughtExceptionHandler ;
9
+ import java .lang .reflect .Field ;
8
10
import java .lang .reflect .Method ;
9
11
import java .util .ArrayList ;
10
12
import java .util .Arrays ;
39
41
40
42
/**
41
43
* RlangPApplet PApplet for R language, powered by Renjin.
42
- *
44
+ *
43
45
* @author github.com/gaocegege
44
46
*/
45
47
public class RLangPApplet extends BuiltinApplet {
@@ -62,14 +64,16 @@ public class RLangPApplet extends BuiltinApplet {
62
64
63
65
private final CountDownLatch finishedLatch = new CountDownLatch (1 );
64
66
67
+ private Field frameField ;
68
+
65
69
private RSketchError terminalException = null ;
66
70
67
71
private boolean hasSize = false ;
68
72
private SEXP sizeFunction = null ;
69
73
70
74
/**
71
75
* Mode for Processing.
72
- *
76
+ *
73
77
* @author github.com/gaocegege
74
78
*/
75
79
private enum Mode {
@@ -107,7 +111,7 @@ public void prePassCode() {
107
111
if (isSameClass (source , ExpressionVector .class )) {
108
112
ExpressionVector ev = (ExpressionVector ) source ;
109
113
// Stores the expressions except size().
110
- List <SEXP > sexps = new ArrayList <SEXP >();
114
+ List <SEXP > sexps = new ArrayList <>();
111
115
for (int i = ev .length () - 1 ; i >= 0 ; --i ) {
112
116
if (isSameClass (ev .get (i ), FunctionCall .class )
113
117
&& isSameClass (((FunctionCall ) ev .get (i )).getFunction (), Symbol .class )) {
@@ -179,7 +183,16 @@ public void runBlock(final String[] arguments) throws RSketchError {
179
183
// exits or we explicitly tell it to minimize.
180
184
// (If it's disposed, it'll leave a gray blank window behind it.)
181
185
log ("Disabling fullscreen." );
182
- macosxFullScreenToggle (frame );
186
+ if (frameField != null ) {
187
+ try {
188
+ Frame frame = (Frame ) frameField .get (this );
189
+ // This is probably a holdover from Processing 2.x
190
+ // and likely shouldn't be used anymore. [fry 210703]
191
+ macosxFullScreenToggle (frame );
192
+ } catch (Exception e ) {
193
+ // safe enough to ignore; this was a workaround
194
+ }
195
+ }
183
196
}
184
197
if (surface instanceof PSurfaceFX ) {
185
198
// Sadly, JavaFX is an abomination, and there's no way to run an FX sketch more than once,
@@ -215,14 +228,32 @@ private static void macosxFullScreenToggle(final Window window) {
215
228
}
216
229
}
217
230
231
+ // method to find the frame field, rather than relying on an Exception
232
+ private Field getFrameField () {
233
+ for (Field field : getClass ().getFields ()) {
234
+ if (field .getName ().equals ("frame" )) {
235
+ return field ;
236
+ }
237
+ }
238
+ return null ;
239
+ }
240
+
218
241
/**
219
- *
242
+ *
220
243
* @see processing.core.PApplet#initSurface()
221
244
*/
222
245
@ Override
223
246
protected PSurface initSurface () {
224
247
final PSurface s = super .initSurface ();
225
- this .frame = null ; // eliminate a memory leak from 2.x compat hack
248
+ frameField = getFrameField ();
249
+ if (frameField != null ) {
250
+ try {
251
+ // eliminate a memory leak from 2.x compat hack
252
+ frameField .set (this , null );
253
+ } catch (Exception e ) {
254
+ // safe enough to ignore; this was a workaround
255
+ }
256
+ }
226
257
// s.setTitle(pySketchPath.getFileName().toString().replaceAll("\\..*$", ""));
227
258
if (s instanceof PSurfaceAWT ) {
228
259
final PSurfaceAWT surf = (PSurfaceAWT ) s ;
@@ -293,7 +324,7 @@ public void settings() {
293
324
294
325
/**
295
326
* Evaluate the program code.
296
- *
327
+ *
297
328
* @see processing.core.PApplet#setup()
298
329
*/
299
330
@ Override
@@ -334,7 +365,7 @@ public void handleDraw() {
334
365
335
366
/**
336
367
* Call the draw function in R script.
337
- *
368
+ *
338
369
* @see processing.core.PApplet#draw()
339
370
*/
340
371
@ Override
@@ -348,7 +379,7 @@ public void draw() {
348
379
349
380
/**
350
381
* Detect whether the program is in active mode.
351
- *
382
+ *
352
383
* @return
353
384
*/
354
385
@ SuppressWarnings ("rawtypes" )
@@ -361,7 +392,7 @@ private boolean isActiveMode() {
361
392
362
393
/**
363
394
* Detect whether the program is in mix mode. After: isActiveMode()
364
- *
395
+ *
365
396
* @return
366
397
*/
367
398
private boolean isMixMode () {
@@ -495,7 +526,7 @@ protected void wrapKeyVariables() {
495
526
496
527
/**
497
528
* Return whether the object has same class with clazz.
498
- *
529
+ *
499
530
* @param obj
500
531
* @param clazz
501
532
* @return
0 commit comments