Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to optimise empty clipboard checks #548

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public void updateUiState() {
EditableType type = EditableType.fromEntry(referenceEntry);

this.renameItem.setEnabled(isRenamable && (type != null && this.gui.isEditable(type)));
this.pasteItem.setEnabled(isRenamable && (type != null && this.gui.isEditable(type)) && !GuiUtil.getClipboard().equals(""));
this.pasteItem.setEnabled(isRenamable && (type != null && this.gui.isEditable(type)) && GuiUtil.hasClipboardText());
this.editJavadocItem.setEnabled(isRenamable && this.gui.isEditable(EditableType.JAVADOC));
this.showInheritanceItem.setEnabled(isClassEntry || isMethodEntry || isConstructorEntry);
this.showImplementationsItem.setEnabled(isClassEntry || isMethodEntry);
Expand Down
23 changes: 23 additions & 0 deletions enigma-swing/src/main/java/cuchaz/enigma/gui/util/GuiUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import java.awt.Desktop;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
Expand Down Expand Up @@ -55,6 +57,8 @@ public class GuiUtil {
public static final Icon FIELD_ICON = loadIcon("field");
public static final Icon CONSTRUCTOR_ICON = loadIcon("constructor");

private static final boolean DISABLE_CLIPBOARD = System.getenv("ENIGMA_DISABLE_CLIPBOARD") != null;

public static void openUrl(String url) {
try {
switch (Os.getOs()) {
Expand Down Expand Up @@ -87,7 +91,26 @@ public static void copyToClipboard(String text) {
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(text), null);
}

public static boolean hasClipboardText() {
if (DISABLE_CLIPBOARD) {
return false;
}

Clipboard systemClipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable contents = systemClipboard.getContents(null);

if (contents != null) {
return contents.isDataFlavorSupported(DataFlavor.stringFlavor);
}

return false;
}

public static String getClipboard() {
if (!hasClipboardText()) {
return "";
}

try {
return (String) Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor);
} catch (UnsupportedFlavorException | IOException e) {
Expand Down