Skip to content

Commit

Permalink
Merge pull request #1934 from usethesource/clipboard
Browse files Browse the repository at this point in the history
added default clipboard utility (ported from Eclipse environment), which should work on any non-headless JVM environment
  • Loading branch information
jurgenvinju authored Apr 7, 2024
2 parents ea9e4d3 + 9085640 commit 18a80e7
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/org/rascalmpl/library/util/Clipboard.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.rascalmpl.library.util;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import org.rascalmpl.debug.IRascalMonitor;
import io.usethesource.vallang.IString;
import io.usethesource.vallang.IValueFactory;

public class Clipboard {
private final IValueFactory vf;
private final java.awt.datatransfer.Clipboard cp;
private IRascalMonitor monitor;

public Clipboard(IValueFactory vf, IRascalMonitor monitor) {
this.vf = vf;
this.cp = java.awt.Toolkit.getDefaultToolkit().getSystemClipboard();
this.monitor = monitor;
}

public void copy(IString arg) {
var selection = new StringSelection(arg.getValue());
cp.setContents(selection, selection);
}

public IString paste() {
try {
if (cp.isDataFlavorAvailable(DataFlavor.stringFlavor)) {
return vf.string(cp.getData(DataFlavor.stringFlavor).toString());
}
}
catch (UnsupportedFlavorException | IOException e) {
monitor.warning("Clipboard::paste failed", null);
}

return vf.string("");
}
}
39 changes: 39 additions & 0 deletions src/org/rascalmpl/library/util/Clipboard.rsc
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
@synopsis{Programmatic access to the native system clipboard (copy and paste), with some handy (de)-escaping features.}
module util::Clipboard

@javaClass{org.rascalmpl.library.util.Clipboard}
@synopsis{If there is textual content in the current clipboard, then return it as a string.}
@description{
This will transfer any plaintext content that is currently on the system clipboard, no matter
the source to a string value.

If the system's clipboard is not accessible (say we are running in a headless environment),
then paste always returns the empty string.
}
@pitfalls{
* the first time paste or copy are called the UI toolkit must boot up; it can take a few seconds.
}
@benefits{
* copy/paste allow for interesting and useful user interactions, especially while experimenting on the REPL.
* paste encodes and escapes all kinds of wild characters automatically.
}
java str paste();

@javaClass{org.rascalmpl.library.util.Clipboard}
@synopsis{Load the contents of a string value into the system clipboard.}
@description{
This will put the contents of the string value in into the system clipboard.
So this is not the string value with quotes and escapes, but the pure string
data.

If the system's clipboard is not accessible (say we are running in a headless environment),
then copy always has no effect.
}
@pitfalls{
* the first time paste or copy are called the UI toolkit must boot up; it can take a few seconds.
}
@benefits{
* copy/paste allow for interesting and useful user interactions, especially while experimenting on the REPL.
* copy transfers the pure content of the string, no quotes or escapes.
}
java void copy(str content);

0 comments on commit 18a80e7

Please sign in to comment.