Skip to content

Commit

Permalink
Attempt to optimise empty clipboard checks
Browse files Browse the repository at this point in the history
  • Loading branch information
modmuss50 committed Sep 16, 2024
1 parent c2ce889 commit 8f1d0bc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
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
17 changes: 17 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 @@ -87,7 +89,22 @@ public static void copyToClipboard(String text) {
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(text), null);
}

public static boolean hasClipboardText() {
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

0 comments on commit 8f1d0bc

Please sign in to comment.