Skip to content

Commit 0e5268e

Browse files
committed
Prevent repaint overwrite of iOS screenshots
1 parent 43f713f commit 0e5268e

File tree

5 files changed

+45
-2
lines changed

5 files changed

+45
-2
lines changed

CodenameOne/src/com/codename1/impl/CodenameOneImplementation.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,6 +1214,20 @@ public void screenshot(SuccessCallback<Image> callback) {
12141214
callback.onSucess(img);
12151215
}
12161216

1217+
/**
1218+
* Indicates whether the Codename One implementation expects caller-side painting to run
1219+
* after a native screenshot has been captured. Implementations that composite peer
1220+
* components into the screenshot can override this to {@code false} so downstream code
1221+
* won't immediately repaint the captured image and accidentally hide native peers.
1222+
*
1223+
* @param screenshot the screenshot image produced by {@link #screenshot(SuccessCallback)}
1224+
* @return {@code true} if the caller should repaint Codename One components onto the
1225+
* screenshot, {@code false} otherwise.
1226+
*/
1227+
public boolean shouldPaintNativeScreenshot(Image screenshot) {
1228+
return true;
1229+
}
1230+
12171231
/**
12181232
* Returns true if the platform supports a native image cache. The native image cache
12191233
* is different than just {@link FileSystemStorage#hasCachesDir()}. A native image cache

CodenameOne/src/com/codename1/ui/Display.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5013,6 +5013,20 @@ public void screenshot(SuccessCallback<Image> callback) {
50135013
impl.screenshot(callback);
50145014
}
50155015

5016+
/**
5017+
* Indicates whether callers should repaint Codename One components onto a screenshot image
5018+
* returned from {@link #screenshot(SuccessCallback)}. Platforms that already composite
5019+
* peer components into the native screenshot can return {@code false} to keep the captured
5020+
* pixels intact.
5021+
*
5022+
* @param screenshot the image returned from {@link #screenshot(SuccessCallback)}.
5023+
* @return {@code true} if the caller should repaint Codename One components onto the
5024+
* screenshot, {@code false} otherwise.
5025+
*/
5026+
public boolean shouldPaintNativeScreenshot(Image screenshot) {
5027+
return impl.shouldPaintNativeScreenshot(screenshot);
5028+
}
5029+
50165030

50175031
/**
50185032
* Convenience method to schedule a task to run on the EDT after {@literal timeout}ms.

Ports/iOSPort/src/com/codename1/impl/ios/IOSImplementation.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ public void addCookie(Cookie c) {
300300
}
301301

302302
private static SuccessCallback<Image> screenshotCallback;
303+
private static volatile boolean lastScreenshotHasNativePeers;
303304

304305
@Override
305306
public void screenshot(final SuccessCallback<Image> callback) {
@@ -337,6 +338,7 @@ static void onScreenshot(final byte[] imageData) {
337338
final SuccessCallback<Image> callback = screenshotCallback;
338339
screenshotCallback = null;
339340
if (callback == null) {
341+
lastScreenshotHasNativePeers = false;
340342
return;
341343
}
342344

@@ -366,6 +368,7 @@ public void run() {
366368
}
367369

368370
if (image != null && image.getGraphics() != null) {
371+
lastScreenshotHasNativePeers = true;
369372
callback.onSucess(image);
370373
return;
371374
}
@@ -374,11 +377,19 @@ public void run() {
374377
Log.e(t);
375378
}
376379
}
380+
lastScreenshotHasNativePeers = false;
377381
callback.onSucess(null);
378382
}
379383
});
380384
}
381385

386+
@Override
387+
public boolean shouldPaintNativeScreenshot(Image screenshot) {
388+
boolean shouldPaint = !lastScreenshotHasNativePeers;
389+
lastScreenshotHasNativePeers = false;
390+
return shouldPaint;
391+
}
392+
382393
/**
383394
* Used to enable/disable native cookies from native code.
384395
* @param cookiesArray

docs/demos/common/src/test/java/com/codenameone/developerguide/animations/AnimationDemosScreenshotTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ private void saveScreenshot(String storageKey, Image screenshot) throws IOExcept
9696

9797
private Image capture(Form form) {
9898
Image screenshot = Image.createImage(form.getWidth(), form.getHeight());
99-
form.paintComponent(screenshot.getGraphics(), true);
99+
if (Display.getInstance().shouldPaintNativeScreenshot(screenshot)) {
100+
form.paintComponent(screenshot.getGraphics(), true);
101+
}
100102
return screenshot;
101103
}
102104

scripts/device-runner-app/tests/Cn1ssDeviceRunnerHelper.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ static boolean emitCurrentFormScreenshot(String testName) {
6262
return false;
6363
}
6464
Image screenshot = img[0];
65-
current.paintComponent(screenshot.getGraphics(), true);
65+
if (Display.getInstance().shouldPaintNativeScreenshot(screenshot)) {
66+
current.paintComponent(screenshot.getGraphics(), true);
67+
}
6668
try {
6769
ImageIO io = ImageIO.getImageIO();
6870
if (io == null || !io.isFormatSupported(ImageIO.FORMAT_PNG)) {

0 commit comments

Comments
 (0)