Skip to content

Commit 7872d64

Browse files
authored
updates for Processing 4 (#263)
* missing semicolons; was required to build * tweaks for the frame field to make compatible with 4.x and retain backward compatibility * leave PConstants.MACOSX rather than chance to MACOS (to stay compatible)
1 parent e13afbe commit 7872d64

File tree

4 files changed

+49
-18
lines changed

4 files changed

+49
-18
lines changed

src/rprocessing/RLangPApplet.java

+42-11
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package rprocessing;
22

33
import java.awt.Component;
4+
import java.awt.Frame;
45
import java.awt.Window;
56
import java.awt.event.ComponentAdapter;
67
import java.awt.event.ComponentEvent;
78
import java.lang.Thread.UncaughtExceptionHandler;
9+
import java.lang.reflect.Field;
810
import java.lang.reflect.Method;
911
import java.util.ArrayList;
1012
import java.util.Arrays;
@@ -39,7 +41,7 @@
3941

4042
/**
4143
* RlangPApplet PApplet for R language, powered by Renjin.
42-
*
44+
*
4345
* @author github.com/gaocegege
4446
*/
4547
public class RLangPApplet extends BuiltinApplet {
@@ -62,14 +64,16 @@ public class RLangPApplet extends BuiltinApplet {
6264

6365
private final CountDownLatch finishedLatch = new CountDownLatch(1);
6466

67+
private Field frameField;
68+
6569
private RSketchError terminalException = null;
6670

6771
private boolean hasSize = false;
6872
private SEXP sizeFunction = null;
6973

7074
/**
7175
* Mode for Processing.
72-
*
76+
*
7377
* @author github.com/gaocegege
7478
*/
7579
private enum Mode {
@@ -107,7 +111,7 @@ public void prePassCode() {
107111
if (isSameClass(source, ExpressionVector.class)) {
108112
ExpressionVector ev = (ExpressionVector) source;
109113
// Stores the expressions except size().
110-
List<SEXP> sexps = new ArrayList<SEXP>();
114+
List<SEXP> sexps = new ArrayList<>();
111115
for (int i = ev.length() - 1; i >= 0; --i) {
112116
if (isSameClass(ev.get(i), FunctionCall.class)
113117
&& isSameClass(((FunctionCall) ev.get(i)).getFunction(), Symbol.class)) {
@@ -179,7 +183,16 @@ public void runBlock(final String[] arguments) throws RSketchError {
179183
// exits or we explicitly tell it to minimize.
180184
// (If it's disposed, it'll leave a gray blank window behind it.)
181185
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+
}
183196
}
184197
if (surface instanceof PSurfaceFX) {
185198
// 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) {
215228
}
216229
}
217230

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+
218241
/**
219-
*
242+
*
220243
* @see processing.core.PApplet#initSurface()
221244
*/
222245
@Override
223246
protected PSurface initSurface() {
224247
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+
}
226257
// s.setTitle(pySketchPath.getFileName().toString().replaceAll("\\..*$", ""));
227258
if (s instanceof PSurfaceAWT) {
228259
final PSurfaceAWT surf = (PSurfaceAWT) s;
@@ -293,7 +324,7 @@ public void settings() {
293324

294325
/**
295326
* Evaluate the program code.
296-
*
327+
*
297328
* @see processing.core.PApplet#setup()
298329
*/
299330
@Override
@@ -334,7 +365,7 @@ public void handleDraw() {
334365

335366
/**
336367
* Call the draw function in R script.
337-
*
368+
*
338369
* @see processing.core.PApplet#draw()
339370
*/
340371
@Override
@@ -348,7 +379,7 @@ public void draw() {
348379

349380
/**
350381
* Detect whether the program is in active mode.
351-
*
382+
*
352383
* @return
353384
*/
354385
@SuppressWarnings("rawtypes")
@@ -361,7 +392,7 @@ private boolean isActiveMode() {
361392

362393
/**
363394
* Detect whether the program is in mix mode. After: isActiveMode()
364-
*
395+
*
365396
* @return
366397
*/
367398
private boolean isMixMode() {
@@ -495,7 +526,7 @@ protected void wrapKeyVariables() {
495526

496527
/**
497528
* Return whether the object has same class with clazz.
498-
*
529+
*
499530
* @param obj
500531
* @param clazz
501532
* @return

src/rprocessing/Runner.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
/**
2222
* R script runner
23-
*
23+
*
2424
* @author github.com/gaocegege
2525
*/
2626
public class Runner {

src/rprocessing/mode/run/SketchRunner.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import rprocessing.util.Printer;
1616

1717
/**
18-
*
18+
*
1919
* @author github.com/gaocegege
2020
*/
2121
public class SketchRunner implements SketchService {
@@ -61,7 +61,7 @@ public SketchRunner(final String id, final ModeService modeService) {
6161
* <p>
6262
* But if we've received a shutdown request from the {@link SketchServiceProcess} on the PDE VM,
6363
* then we permit the quit to proceed.
64-
*
64+
*
6565
* @return true iff the SketchRunner should quit.
6666
*/
6767
public boolean preventUserQuit() {

src/test/e2e/util/ImageUtils.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
import processing.core.PImage;
1111

1212
/**
13-
*
13+
*
1414
* @author github.com/gaocegege
1515
*/
1616
public class ImageUtils {
17-
17+
1818
public static float diffImage(File actualFile, URL testURL) throws IOException {
1919
PImage actualImage = new PImage(ImageIO.read(actualFile));
2020
PImage testImage = new PImage(ImageIO.read(testURL));
@@ -29,12 +29,12 @@ private static float imgDifference(PImage i0, PImage i1) {
2929
i1.loadPixels();
3030
int[] ip1 = i1.pixels;
3131
for (int n = 0; n < ip0.length; n++) {
32-
int pxl0 = ip0[n]
32+
int pxl0 = ip0[n];
3333
int r0, g0, b0;
3434
r0 = (pxl0 >> 20) & 0xF;
3535
g0 = (pxl0 >> 12) & 0xF;
3636
b0 = (pxl0 >> 4) & 0xF;
37-
int pxl1 = ip1[n]
37+
int pxl1 = ip1[n];
3838
int r1, g1, b1;
3939
r1 = (pxl1 >> 20) & 0xF;
4040
g1 = (pxl1 >> 12) & 0xF;

0 commit comments

Comments
 (0)