|
| 1 | +// Copyright (c) 2026 The Bitcoin Core developers |
| 2 | +// Distributed under the MIT software license, see the accompanying |
| 3 | +// file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 4 | + |
| 5 | +import QtQuick 2.15 |
| 6 | +import QtQuick.Controls 2.15 |
| 7 | +import QtQuick.Layouts 1.15 |
| 8 | +import org.bitcoincore.qt 1.0 |
| 9 | + |
| 10 | +import "../controls" |
| 11 | + |
| 12 | +AbstractButton { |
| 13 | + id: root |
| 14 | + |
| 15 | + property string address: "" |
| 16 | + property color primaryColor: Theme.color.neutral9 |
| 17 | + property color secondaryColor: Theme.color.neutral7 |
| 18 | + property var clipboard: Clipboard |
| 19 | + readonly property string formattedText: formatAddressRichText(address) |
| 20 | + readonly property bool showCopiedStatus: copiedResetTimer.running |
| 21 | + |
| 22 | + signal copied() |
| 23 | + |
| 24 | + function formatAddressRichText(value) { |
| 25 | + if (!value) return "" |
| 26 | + |
| 27 | + var html = "" |
| 28 | + for (var i = 0; i < value.length; i += 4) { |
| 29 | + var chunk = value.substring(i, Math.min(i + 4, value.length)) |
| 30 | + var color = (Math.floor(i / 4) % 2 === 0) |
| 31 | + ? root.primaryColor |
| 32 | + : root.secondaryColor |
| 33 | + if (i > 0) html += " " |
| 34 | + html += "<nobr><font color=\"" + color + "\">" + chunk + "</font></nobr>" |
| 35 | + } |
| 36 | + return html |
| 37 | + } |
| 38 | + |
| 39 | + function copy() { |
| 40 | + if (root.address === "" || !root.clipboard) return |
| 41 | + root.clipboard.setText(root.address) |
| 42 | + copiedResetTimer.restart() |
| 43 | + root.copied() |
| 44 | + } |
| 45 | + |
| 46 | + hoverEnabled: AppMode.isDesktop |
| 47 | + enabled: address !== "" |
| 48 | + leftPadding: 8 |
| 49 | + rightPadding: 8 |
| 50 | + topPadding: 4 |
| 51 | + bottomPadding: 4 |
| 52 | + implicitHeight: content.implicitHeight + topPadding + bottomPadding |
| 53 | + Accessible.name: showCopiedStatus ? qsTr("Copied") : qsTr("Copy address") |
| 54 | + |
| 55 | + HoverHandler { |
| 56 | + cursorShape: Qt.PointingHandCursor |
| 57 | + } |
| 58 | + |
| 59 | + onClicked: copy() |
| 60 | + |
| 61 | + contentItem: Item { |
| 62 | + id: content |
| 63 | + implicitHeight: Math.max(addressText.paintedHeight, copiedRow.implicitHeight) |
| 64 | + |
| 65 | + CoreText { |
| 66 | + id: addressText |
| 67 | + objectName: root.objectName + "Value" |
| 68 | + anchors.left: parent.left |
| 69 | + anchors.right: parent.right |
| 70 | + anchors.top: parent.top |
| 71 | + height: paintedHeight |
| 72 | + horizontalAlignment: Text.AlignLeft |
| 73 | + verticalAlignment: Text.AlignTop |
| 74 | + font: Theme.text.monoBody.font |
| 75 | + lineHeight: Theme.text.monoBody.lineHeight |
| 76 | + lineHeightMode: Text.FixedHeight |
| 77 | + textFormat: Text.RichText |
| 78 | + text: root.formattedText |
| 79 | + wrapMode: Text.WordWrap |
| 80 | + opacity: root.showCopiedStatus ? 0 : 1 |
| 81 | + |
| 82 | + Behavior on opacity { |
| 83 | + NumberAnimation { duration: 150 } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + RowLayout { |
| 88 | + id: copiedRow |
| 89 | + objectName: root.objectName + "CopiedStatus" |
| 90 | + anchors.centerIn: parent |
| 91 | + spacing: 6 |
| 92 | + opacity: root.showCopiedStatus ? 1 : 0 |
| 93 | + scale: root.showCopiedStatus ? 1 : 0.8 |
| 94 | + |
| 95 | + Behavior on opacity { |
| 96 | + NumberAnimation { duration: 150 } |
| 97 | + } |
| 98 | + |
| 99 | + Behavior on scale { |
| 100 | + NumberAnimation { |
| 101 | + duration: 150 |
| 102 | + easing.type: Easing.InOutCubic |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + Icon { |
| 107 | + Layout.alignment: Qt.AlignVCenter |
| 108 | + source: "qrc:/icons/copy" |
| 109 | + color: Theme.color.neutral9 |
| 110 | + size: 20 |
| 111 | + } |
| 112 | + |
| 113 | + CoreText { |
| 114 | + Layout.alignment: Qt.AlignVCenter |
| 115 | + text: qsTr("Copied") |
| 116 | + color: Theme.color.neutral9 |
| 117 | + font: Theme.text.body.font |
| 118 | + lineHeight: Theme.text.body.lineHeight |
| 119 | + lineHeightMode: Text.FixedHeight |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + background: Rectangle { |
| 125 | + radius: 5 |
| 126 | + color: root.hovered || root.down ? Theme.color.neutral2 : "transparent" |
| 127 | + |
| 128 | + Behavior on color { |
| 129 | + ColorAnimation { duration: 150 } |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + Timer { |
| 134 | + id: copiedResetTimer |
| 135 | + interval: 1000 |
| 136 | + } |
| 137 | +} |
0 commit comments