Skip to content

Commit

Permalink
Merge pull request #83 from noblemaster/exitFullscreenFix
Browse files Browse the repository at this point in the history
Misc. code cleanup: JS, JSNI rename to NATIVE.
  • Loading branch information
xpenatan authored Feb 19, 2023
2 parents 1f9dbcb + 27adc4e commit e53e209
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 90 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.github.xpenatan.gdx.backends.teavm;

import com.badlogic.gdx.utils.Clipboard;
import com.github.xpenatan.gdx.backends.teavm.dom.HTMLCanvasElementWrapper;
import org.teavm.jso.JSBody;

/**
Expand Down Expand Up @@ -31,7 +30,7 @@ public String getContents () {
public void setContents (String content) {
this.content = content;
if (requestedWritePermissions || TeaApplication.getAgentInfo().isFirefox()) {
if (hasWritePermissions) setContentJSNI(content);
if (hasWritePermissions) setContentNATIVE(content);
} else {
TeaPermissions.queryPermission("clipboard-write", writeHandler);
requestedWritePermissions = true;
Expand All @@ -42,13 +41,13 @@ public void setContents (String content) {
"if (\"clipboard\" in navigator) {\n" +
" navigator.clipboard.writeText(content);\n" +
"}")
private static native void setContentJSNI (String content);
private static native void setContentNATIVE(String content);

private class ClipboardWriteHandler implements TeaPermissions.TeaPermissionResult {
@Override
public void granted () {
hasWritePermissions = true;
setContentJSNI(content);
setContentNATIVE(content);
}

@Override
Expand All @@ -59,7 +58,7 @@ public void denied () {
@Override
public void prompt () {
hasWritePermissions = true;
setContentJSNI(content);
setContentNATIVE(content);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.teavm.jso.JSFunctor;
import org.teavm.jso.browser.Window;
import org.teavm.jso.dom.html.HTMLCanvasElement;
import org.teavm.jso.dom.xml.Document;
import org.teavm.jso.webgl.WebGLContextAttributes;

/**
Expand All @@ -42,7 +41,6 @@ public class TeaGraphics implements Graphics {

public TeaGraphics(TeaApplicationConfiguration config) {
this.config = config;
HTMLCanvasElement a;
TeaWindow window = new TeaWindow();
DocumentWrapper document = window.getDocument();
HTMLElementWrapper elementID = document.getElementById(config.canvasID);
Expand All @@ -66,6 +64,14 @@ public TeaGraphics(TeaApplicationConfiguration config) {
setCanvasSize((int)(density * width), (int)(density * height));
}
}

// listen to fullscreen changes
addFullscreenChangeListener(canvas, new FullscreenChanged() {
@Override
public void fullscreenChanged() {
// listening to fullscreen mode changes
}
});
}

public void update() {
Expand Down Expand Up @@ -256,7 +262,7 @@ public DisplayMode getDisplayMode(Monitor monitor) {
public boolean setFullscreenMode(DisplayMode displayMode) {
DisplayMode supportedMode = getDisplayMode();
if(displayMode.width != supportedMode.width && displayMode.height != supportedMode.height) return false;
return setFullscreenJSNI(this, canvas, displayMode.width, displayMode.height);
return enterFullscreen(canvas, displayMode.width, displayMode.height);
}

@Override
Expand Down Expand Up @@ -330,11 +336,6 @@ public void requestRendering() {
// not available
}

@Override
public boolean isFullscreen() {
return isFullscreenJSNI();
}

@Override
public Cursor newCursor(Pixmap pixmap, int xHotspot, int yHotspot) {
return new TeaCursor(pixmap, xHotspot, yHotspot);
Expand Down Expand Up @@ -375,113 +376,108 @@ public double getNativeScreenDensity() {
@JSBody(script = "return screen.height;")
private static native int getScreenHeightNATIVE();

public boolean setFullscreenJSNI(TeaGraphics graphics, HTMLCanvasElementWrapper canvas, int screenWidth, int screenHeight) {
FullscreenChanged fullscreenChanged = new FullscreenChanged() {
@Override
public void fullscreenChanged() {
}
};
return setFullscreen(fullscreenChanged, canvas, screenWidth, screenHeight);
}

@JSBody(params = {"fullscreenChanged", "element", "screenWidth", "screenHeight"}, script = "" +
"\t\t// Attempt to use the non-prefixed standard API (https://fullscreen.spec.whatwg.org)\n" +
"\t\tif (element.requestFullscreen) {\n" +
"\t\t\telement.width = screenWidth;\n" +
"\t\t\telement.height = screenHeight;\n" +
"\t\t\telement.requestFullscreen();\n" +
"\t\t\tdocument\n" +
"\t\t\t\t\t.addEventListener(\n" +
"\t\t\t\t\t\t\t\"fullscreenchange\",\n" +
"\t\t\t\t\t\t\tfullscreenChanged, false);\n" +
"\t\t\treturn true;\n" +
"\t\t}\n" +
"\t\t// Attempt to the vendor specific variants of the API\n" +
"\t\tif (element.webkitRequestFullScreen) {\n" +
"\t\t\telement.width = screenWidth;\n" +
"\t\t\telement.height = screenHeight;\n" +
"\t\t\telement.webkitRequestFullScreen(Element.ALLOW_KEYBOARD_INPUT);\n" +
"\t\t\tdocument\n" +
"\t\t\t\t\t.addEventListener(\n" +
"\t\t\t\t\t\t\t\"webkitfullscreenchange\",\n" +
"\t\t\t\t\t\t\tfullscreenChanged, false);\n" +
"\t\t\treturn true;\n" +
"\t\t}\n" +
"\t\tif (element.mozRequestFullScreen) {\n" +
"\t\t\telement.width = screenWidth;\n" +
"\t\t\telement.height = screenHeight;\n" +
"\t\t\telement.mozRequestFullScreen();\n" +
"\t\t\tdocument\n" +
"\t\t\t\t\t.addEventListener(\n" +
"\t\t\t\t\t\t\t\"mozfullscreenchange\",\n" +
"\t\t\t\t\t\t\tfullscreenChanged, false);\n" +
"\t\t\treturn true;\n" +
"\t\t}\n" +
"\t\tif (element.msRequestFullscreen) {\n" +
"\t\t\telement.width = screenWidth;\n" +
"\t\t\telement.height = screenHeight;\n" +
"\t\t\telement.msRequestFullscreen();\n" +
"\t\t\tdocument\n" +
"\t\t\t\t\t.addEventListener(\n" +
"\t\t\t\t\t\t\t\"msfullscreenchange\",\n" +
"\t\t\t\t\t\t\tfullscreenChanged, false);\n" +
"\t\t\treturn true;\n" +
"\t\t}\n" +
@JSBody(params = {"element", "fullscreenChanged"}, script = "" +
"if (element.requestFullscreen) {\n" +
" document.addEventListener(\"fullscreenchange\", fullscreenChanged, false);\n" +
"}\n" +
"// Attempt to the vendor specific variants of the API\n" +
"if (element.webkitRequestFullScreen) {\n" +
" document.addEventListener(\"webkitfullscreenchange\", fullscreenChanged, false);\n" +
"}\n" +
"if (element.mozRequestFullScreen) {\n" +
" document.addEventListener(\"mozfullscreenchange\", fullscreenChanged, false);\n" +
"}\n" +
"if (element.msRequestFullscreen) {\n" +
" document.addEventListener(\"msfullscreenchange\", fullscreenChanged, false);\n" +
"}")
private static native void addFullscreenChangeListener(HTMLCanvasElementWrapper element, FullscreenChanged fullscreenChanged);

@JSFunctor
public interface FullscreenChanged extends org.teavm.jso.JSObject {
void fullscreenChanged();
}

public boolean enterFullscreen(HTMLCanvasElementWrapper element, int screenWidth, int screenHeight) {
return enterFullscreenNATIVE(element, screenWidth, screenHeight);
}

@JSBody(params = {"element", "screenWidth", "screenHeight"}, script = "" +
"if (element.requestFullscreen) {\n" +
" element.width = screenWidth;\n" +
" element.height = screenHeight;\n" +
" element.requestFullscreen();\n" +
" return true;\n" +
"}\n" +
"// Attempt to the vendor specific variants of the API\n" +
"if (element.webkitRequestFullScreen) {\n" +
" element.width = screenWidth;\n" +
" element.height = screenHeight;\n" +
" element.webkitRequestFullScreen(Element.ALLOW_KEYBOARD_INPUT);\n" +
" return true;\n" +
"}\n" +
"if (element.mozRequestFullScreen) {\n" +
" element.width = screenWidth;\n" +
" element.height = screenHeight;\n" +
" element.mozRequestFullScreen();\n" +
" return true;\n" +
"}\n" +
"if (element.msRequestFullscreen) {\n" +
" element.width = screenWidth;\n" +
" element.height = screenHeight;\n" +
" element.msRequestFullscreen();\n" +
" return true;\n" +
"}\n" +
"\n" +
"\t\treturn false;")
private static native boolean setFullscreen(FullscreenChanged fullscreenChanged, HTMLCanvasElementWrapper element, int screenWidth, int screenHeight);
"return false;")
private static native boolean enterFullscreenNATIVE(HTMLCanvasElementWrapper element, int screenWidth, int screenHeight);

public void exitFullscreen() {
exitFullscreenJS();
exitFullscreenNATIVE();
}

@JSBody(script = "" +
"if (document.exitFullscreen)\n" +
"document.exitFullscreen();\n" +
" document.exitFullscreen();\n" +
"if (document.msExitFullscreen)\n" +
"document.msExitFullscreen();\n" +
" document.msExitFullscreen();\n" +
"if (document.webkitExitFullscreen)\n" +
"document.webkitExitFullscreen();\n" +
" document.webkitExitFullscreen();\n" +
"if (document.mozExitFullscreen)\n" +
"document.mozExitFullscreen();\n" +
" document.mozExitFullscreen();\n" +
"if (document.webkitCancelFullScreen) // Old WebKit\n" +
"document.webkitCancelFullScreen();")
private static native boolean exitFullscreenJS();
" document.webkitCancelFullScreen();")
private static native boolean exitFullscreenNATIVE();

public boolean isFullscreenJSNI() {
@Override
public boolean isFullscreen() {
return isFullscreenNATIVE();
}

@JSBody(script = "" +
"// Standards compliant check for fullscreen\n" +
"if (\"fullscreenElement\" in document) {\n" +
"return document.fullscreenElement != null;\n" +
" return document.fullscreenElement != null;\n" +
"}" +
"// Vendor prefixed versions of standard check\n" +
"if (\"msFullscreenElement\" in document) {\n" +
"return document.msFullscreenElement != null;\n" +
" return document.msFullscreenElement != null;\n" +
"}" +
"if (\"webkitFullscreenElement\" in document) {\n" +
"return document.webkitFullscreenElement != null;\n" +
" return document.webkitFullscreenElement != null;\n" +
"}" +
"if (\"mozFullScreenElement\" in document) { // Yes, with a capital 'S'\n" +
"return document.mozFullScreenElement != null;\n" +
" return document.mozFullScreenElement != null;\n" +
"}" +
"// Older, non-standard ways of checking for fullscreen\n" +
"if (\"webkitIsFullScreen\" in document) {\n" +
"return document.webkitIsFullScreen;\n" +
" return document.webkitIsFullScreen;\n" +
"}" +
"if (\"mozFullScreen\" in document) {\n" +
"return document.mozFullScreen;\n" +
" return document.mozFullScreen;\n" +
"}" +
"return false")
private static native boolean isFullscreenNATIVE();

@JSFunctor
public interface FullscreenChanged extends org.teavm.jso.JSObject {
void fullscreenChanged();
}

public WebGLRenderingContextWrapper getGLContext(HTMLCanvasElementWrapper canvasWrapper, TeaApplicationConfiguration config) {
WebGLContextAttributes attr = WebGLContextAttributes.create();
attr.setAlpha(config.alpha);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ public TeaPreferences(Storage storage, String prefix) {
}

private Object toObject(String key, String value) {
if(key.endsWith("b")) return new Boolean(Boolean.parseBoolean(value));
if(key.endsWith("i")) return new Integer(Integer.parseInt(value));
if(key.endsWith("l")) return new Long(Long.parseLong(value));
if(key.endsWith("f")) return new Float(Float.parseFloat(value));
if(key.endsWith("b")) return Boolean.valueOf(value);
if(key.endsWith("i")) return Integer.valueOf(value);
if(key.endsWith("l")) return Long.valueOf(value);
if(key.endsWith("f")) return Float.valueOf(value);
return value;
}

Expand Down

0 comments on commit e53e209

Please sign in to comment.