Skip to content

Commit

Permalink
add copy/paste clipboard event
Browse files Browse the repository at this point in the history
  • Loading branch information
xpenatan committed Jun 24, 2024
1 parent 2d23254 commit 0fafa33
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 38 deletions.
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);
}
}
}
}
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();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.xpenatan.gdx.backends.teavm.dom;

import org.teavm.jso.JSMethod;
import org.teavm.jso.JSObject;
import org.teavm.jso.JSProperty;

Expand All @@ -10,4 +11,10 @@ public interface DataTransferWrapper extends JSObject {

@JSProperty
FileListWrapper getFiles();

@JSMethod
String getData(String format);

@JSMethod
void setData(String format, String data);
}

0 comments on commit 0fafa33

Please sign in to comment.