-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
93 additions
and
38 deletions.
There are no files selected for viewing
112 changes: 74 additions & 38 deletions
112
...ends/backend-teavm/src/main/java/com/github/xpenatan/gdx/backends/teavm/TeaClipboard.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,100 @@ | ||
package com.github.xpenatan.gdx.backends.teavm; | ||
|
||
import com.badlogic.gdx.utils.Clipboard; | ||
import com.github.xpenatan.gdx.backends.teavm.dom.ClipboardEventWrapper; | ||
import com.github.xpenatan.gdx.backends.teavm.dom.DataTransferWrapper; | ||
import com.github.xpenatan.gdx.backends.teavm.dom.impl.TeaWindow; | ||
import org.teavm.jso.JSBody; | ||
|
||
/** | ||
* Clipboard implementation copied over from libGDX/GWT. Paste only works within the libGDX application. | ||
*/ | ||
public class TeaClipboard implements Clipboard { | ||
|
||
private boolean requestedWritePermissions = false; | ||
private boolean hasWritePermissions = true; | ||
private boolean requestedWritePermissions = false; | ||
private boolean hasWritePermissions = true; | ||
|
||
private final ClipboardWriteHandler writeHandler = new ClipboardWriteHandler(); | ||
private final ClipboardWriteHandler writeHandler = new ClipboardWriteHandler(); | ||
|
||
private String content = ""; | ||
private String content = ""; | ||
|
||
@Override | ||
public boolean hasContents () { | ||
String contents = getContents(); | ||
return contents != null && !contents.isEmpty(); | ||
} | ||
public TeaClipboard() { | ||
|
||
@Override | ||
public String getContents () { | ||
return content; | ||
} | ||
TeaWindow.get().getDocument().addEventListener("copy", evt -> { | ||
ClipboardEventWrapper event = (ClipboardEventWrapper)evt; | ||
DataTransferWrapper clipboardData = event.getClipboardData(); | ||
if(clipboardData != null) { | ||
// System.out.println("COPY DATA: " + content); | ||
clipboardData.setData("text/plain", content); | ||
} | ||
evt.preventDefault(); | ||
}); | ||
|
||
@Override | ||
public void setContents (String content) { | ||
this.content = content; | ||
if (requestedWritePermissions || TeaApplication.getAgentInfo().isFirefox()) { | ||
if (hasWritePermissions) setContentNATIVE(content); | ||
} else { | ||
TeaPermissions.queryPermission("clipboard-write", writeHandler); | ||
requestedWritePermissions = true; | ||
} | ||
} | ||
TeaWindow.get().getDocument().addEventListener("cut", evt -> { | ||
ClipboardEventWrapper event = (ClipboardEventWrapper)evt; | ||
DataTransferWrapper clipboardData = event.getClipboardData(); | ||
if(clipboardData != null) { | ||
// System.out.println("CUT DATA: " + content); | ||
clipboardData.setData("text/plain", content); | ||
} | ||
evt.preventDefault(); | ||
}); | ||
|
||
@JSBody(params = {"content"}, script = | ||
"if (\"clipboard\" in navigator) {\n" + | ||
" navigator.clipboard.writeText(content);\n" + | ||
"}") | ||
private static native void setContentNATIVE(String content); | ||
TeaWindow.get().getDocument().addEventListener("paste", evt -> { | ||
ClipboardEventWrapper event = (ClipboardEventWrapper)evt; | ||
DataTransferWrapper clipboardData = event.getClipboardData(); | ||
if(clipboardData != null) { | ||
content = clipboardData.getData("text/plain"); | ||
// System.out.println("PASTE DATA: " + content); | ||
} | ||
evt.preventDefault(); | ||
}); | ||
} | ||
|
||
private class ClipboardWriteHandler implements TeaPermissions.TeaPermissionResult { | ||
@Override | ||
public void granted () { | ||
hasWritePermissions = true; | ||
setContentNATIVE(content); | ||
public boolean hasContents() { | ||
String contents = getContents(); | ||
return contents != null && !contents.isEmpty(); | ||
} | ||
|
||
@Override | ||
public void denied () { | ||
hasWritePermissions = false; | ||
public String getContents () { | ||
return content; | ||
} | ||
|
||
@Override | ||
public void prompt () { | ||
hasWritePermissions = true; | ||
setContentNATIVE(content); | ||
public void setContents (String content) { | ||
this.content = content; | ||
if (requestedWritePermissions || TeaApplication.getAgentInfo().isFirefox()) { | ||
if (hasWritePermissions) setContentNATIVE(content); | ||
} else { | ||
TeaPermissions.queryPermission("clipboard-write", writeHandler); | ||
requestedWritePermissions = true; | ||
} | ||
} | ||
|
||
@JSBody(params = {"content"}, script = | ||
"if (\"clipboard\" in navigator) {\n" + | ||
" navigator.clipboard.writeText(content);\n" + | ||
"}") | ||
private static native void setContentNATIVE(String content); | ||
|
||
private class ClipboardWriteHandler implements TeaPermissions.TeaPermissionResult { | ||
@Override | ||
public void granted() { | ||
hasWritePermissions = true; | ||
setContentNATIVE(content); | ||
} | ||
|
||
@Override | ||
public void denied() { | ||
hasWritePermissions = false; | ||
} | ||
|
||
@Override | ||
public void prompt() { | ||
hasWritePermissions = true; | ||
setContentNATIVE(content); | ||
} | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...teavm/src/main/java/com/github/xpenatan/gdx/backends/teavm/dom/ClipboardEventWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.github.xpenatan.gdx.backends.teavm.dom; | ||
|
||
import org.teavm.jso.JSProperty; | ||
|
||
/** | ||
* @author xpenatan | ||
*/ | ||
public interface ClipboardEventWrapper extends EventWrapper { | ||
|
||
@JSProperty | ||
DataTransferWrapper getClipboardData(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters