From 97b4ed48db261791ff6f336d45214ecff6a4077a Mon Sep 17 00:00:00 2001 From: wazint Date: Fri, 24 Feb 2023 16:55:40 +0200 Subject: [PATCH 1/2] add context menu to copy amount values from amount cells --- .../sparrow/control/CoinCell.java | 31 +++++++++++++++++-- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/sparrowwallet/sparrow/control/CoinCell.java b/src/main/java/com/sparrowwallet/sparrow/control/CoinCell.java index 1d681a53..35f49c2e 100644 --- a/src/main/java/com/sparrowwallet/sparrow/control/CoinCell.java +++ b/src/main/java/com/sparrowwallet/sparrow/control/CoinCell.java @@ -4,15 +4,16 @@ import com.sparrowwallet.drongo.BitcoinUnit; import com.sparrowwallet.drongo.protocol.Transaction; import com.sparrowwallet.drongo.wallet.BlockTransactionHash; import com.sparrowwallet.sparrow.UnitFormat; +import com.sparrowwallet.sparrow.io.Config; import com.sparrowwallet.sparrow.wallet.Entry; import com.sparrowwallet.sparrow.wallet.HashIndexEntry; import com.sparrowwallet.sparrow.wallet.TransactionEntry; import com.sparrowwallet.sparrow.wallet.UtxoEntry; import javafx.beans.property.IntegerProperty; import javafx.beans.property.SimpleIntegerProperty; -import javafx.scene.control.ContentDisplay; -import javafx.scene.control.Tooltip; -import javafx.scene.control.TreeTableCell; +import javafx.scene.control.*; +import javafx.scene.input.Clipboard; +import javafx.scene.input.ClipboardContent; import javafx.scene.layout.Region; import javafx.util.Duration; import org.controlsfx.tools.Platform; @@ -62,6 +63,7 @@ class CoinCell extends TreeTableCell implements ConfirmationsList setText(satsValue); } setTooltip(tooltip); + setContextMenu(new CoinContextMenu(amount)); if(entry instanceof TransactionEntry transactionEntry) { tooltip.showConfirmations(transactionEntry.confirmationsProperty()); @@ -155,4 +157,27 @@ class CoinCell extends TreeTableCell implements ConfirmationsList } } } + + private static class CoinContextMenu extends ContextMenu { + public CoinContextMenu(Number amount) { + 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); + } + } } From b9a553abf205e4e11a997699de844e36a3226fa2 Mon Sep 17 00:00:00 2001 From: wazint Date: Sat, 4 Mar 2023 20:30:47 +0200 Subject: [PATCH 2/2] don't initialise a ContextMenu for each cell update --- .../sparrow/control/CoinCell.java | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/sparrowwallet/sparrow/control/CoinCell.java b/src/main/java/com/sparrowwallet/sparrow/control/CoinCell.java index 35f49c2e..c04ff972 100644 --- a/src/main/java/com/sparrowwallet/sparrow/control/CoinCell.java +++ b/src/main/java/com/sparrowwallet/sparrow/control/CoinCell.java @@ -22,6 +22,7 @@ import java.text.DecimalFormat; class CoinCell extends TreeTableCell implements ConfirmationsListener { private final CoinTooltip tooltip; + private final CoinContextMenu contextMenu; private IntegerProperty confirmationsProperty; @@ -29,6 +30,8 @@ class CoinCell extends TreeTableCell implements ConfirmationsList super(); tooltip = new CoinTooltip(); tooltip.setShowDelay(Duration.millis(500)); + contextMenu = new CoinContextMenu(); + setContextMenu(contextMenu); getStyleClass().add("coin-cell"); if(Platform.getCurrent() == Platform.OSX) { getStyleClass().add("number-field"); @@ -43,6 +46,7 @@ class CoinCell extends TreeTableCell implements ConfirmationsList setText(null); setGraphic(null); setTooltip(null); + setContextMenu(null); } else { Entry entry = getTreeTableView().getTreeItem(getIndex()).getValue(); EntryCell.applyRowStyles(this, entry); @@ -63,7 +67,8 @@ class CoinCell extends TreeTableCell implements ConfirmationsList setText(satsValue); } setTooltip(tooltip); - setContextMenu(new CoinContextMenu(amount)); + contextMenu.updateAmount(amount); + setContextMenu(contextMenu); if(entry instanceof TransactionEntry transactionEntry) { tooltip.showConfirmations(transactionEntry.confirmationsProperty()); @@ -159,7 +164,17 @@ class CoinCell extends TreeTableCell implements ConfirmationsList } private static class CoinContextMenu extends ContextMenu { - public CoinContextMenu(Number amount) { + 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();