Merge pull request #860 from wazint/master

Add context menu to copy amount values from amount cells
This commit is contained in:
craigraw 2023-03-09 10:55:36 +02:00 committed by GitHub
commit 35965235f3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -4,15 +4,16 @@ import com.sparrowwallet.drongo.BitcoinUnit;
import com.sparrowwallet.drongo.protocol.Transaction; import com.sparrowwallet.drongo.protocol.Transaction;
import com.sparrowwallet.drongo.wallet.BlockTransactionHash; import com.sparrowwallet.drongo.wallet.BlockTransactionHash;
import com.sparrowwallet.sparrow.UnitFormat; import com.sparrowwallet.sparrow.UnitFormat;
import com.sparrowwallet.sparrow.io.Config;
import com.sparrowwallet.sparrow.wallet.Entry; import com.sparrowwallet.sparrow.wallet.Entry;
import com.sparrowwallet.sparrow.wallet.HashIndexEntry; import com.sparrowwallet.sparrow.wallet.HashIndexEntry;
import com.sparrowwallet.sparrow.wallet.TransactionEntry; import com.sparrowwallet.sparrow.wallet.TransactionEntry;
import com.sparrowwallet.sparrow.wallet.UtxoEntry; import com.sparrowwallet.sparrow.wallet.UtxoEntry;
import javafx.beans.property.IntegerProperty; import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty; import javafx.beans.property.SimpleIntegerProperty;
import javafx.scene.control.ContentDisplay; import javafx.scene.control.*;
import javafx.scene.control.Tooltip; import javafx.scene.input.Clipboard;
import javafx.scene.control.TreeTableCell; import javafx.scene.input.ClipboardContent;
import javafx.scene.layout.Region; import javafx.scene.layout.Region;
import javafx.util.Duration; import javafx.util.Duration;
import org.controlsfx.tools.Platform; import org.controlsfx.tools.Platform;
@ -21,6 +22,7 @@ import java.text.DecimalFormat;
class CoinCell extends TreeTableCell<Entry, Number> implements ConfirmationsListener { class CoinCell extends TreeTableCell<Entry, Number> implements ConfirmationsListener {
private final CoinTooltip tooltip; private final CoinTooltip tooltip;
private final CoinContextMenu contextMenu;
private IntegerProperty confirmationsProperty; private IntegerProperty confirmationsProperty;
@ -28,6 +30,8 @@ class CoinCell extends TreeTableCell<Entry, Number> implements ConfirmationsList
super(); super();
tooltip = new CoinTooltip(); tooltip = new CoinTooltip();
tooltip.setShowDelay(Duration.millis(500)); tooltip.setShowDelay(Duration.millis(500));
contextMenu = new CoinContextMenu();
setContextMenu(contextMenu);
getStyleClass().add("coin-cell"); getStyleClass().add("coin-cell");
if(Platform.getCurrent() == Platform.OSX) { if(Platform.getCurrent() == Platform.OSX) {
getStyleClass().add("number-field"); getStyleClass().add("number-field");
@ -42,6 +46,7 @@ class CoinCell extends TreeTableCell<Entry, Number> implements ConfirmationsList
setText(null); setText(null);
setGraphic(null); setGraphic(null);
setTooltip(null); setTooltip(null);
setContextMenu(null);
} else { } else {
Entry entry = getTreeTableView().getTreeItem(getIndex()).getValue(); Entry entry = getTreeTableView().getTreeItem(getIndex()).getValue();
EntryCell.applyRowStyles(this, entry); EntryCell.applyRowStyles(this, entry);
@ -62,6 +67,8 @@ class CoinCell extends TreeTableCell<Entry, Number> implements ConfirmationsList
setText(satsValue); setText(satsValue);
} }
setTooltip(tooltip); setTooltip(tooltip);
contextMenu.updateAmount(amount);
setContextMenu(contextMenu);
if(entry instanceof TransactionEntry transactionEntry) { if(entry instanceof TransactionEntry transactionEntry) {
tooltip.showConfirmations(transactionEntry.confirmationsProperty()); tooltip.showConfirmations(transactionEntry.confirmationsProperty());
@ -155,4 +162,37 @@ class CoinCell extends TreeTableCell<Entry, Number> implements ConfirmationsList
} }
} }
} }
private static class CoinContextMenu extends ContextMenu {
private Number amount;
public CoinContextMenu() {
}
public void updateAmount(Number amount) {
if (amount.equals(this.amount)) {
return;
}
this.amount = amount;
getItems().clear();
MenuItem copySatsValue = new MenuItem("Copy Value in sats");
copySatsValue.setOnAction(AE -> {
hide();
ClipboardContent content = new ClipboardContent();
content.putString(amount.toString());
Clipboard.getSystemClipboard().setContent(content);
});
MenuItem copyBtcValue = new MenuItem("Copy Value in BTC");
copyBtcValue.setOnAction(AE -> {
hide();
ClipboardContent content = new ClipboardContent();
UnitFormat format = Config.get().getUnitFormat() == null ? UnitFormat.DOT : Config.get().getUnitFormat();
content.putString(format.formatBtcValue(amount.longValue()));
Clipboard.getSystemClipboard().setContent(content);
});
getItems().addAll(copySatsValue, copyBtcValue);
}
}
} }