2020-08-06 07:25:53 +00:00
|
|
|
import QtQuick 2.13
|
|
|
|
import QtQuick.Controls 2.13
|
|
|
|
import QtQuick.Layouts 1.13
|
|
|
|
import QtGraphicalEffects 1.13
|
2021-09-28 15:04:06 +00:00
|
|
|
|
|
|
|
import utils 1.0
|
2020-08-06 07:25:53 +00:00
|
|
|
|
2021-10-14 10:04:34 +00:00
|
|
|
import "./controls"
|
2021-10-14 09:20:18 +00:00
|
|
|
import "./panels"
|
|
|
|
|
2020-08-06 07:25:53 +00:00
|
|
|
Item {
|
|
|
|
id: root
|
|
|
|
property var accounts
|
|
|
|
property var contacts
|
|
|
|
property int inputWidth: 272
|
|
|
|
property int sourceSelectWidth: 136
|
2020-08-12 03:40:25 +00:00
|
|
|
property alias label: txtLabel.text
|
|
|
|
// If supplied, additional info will be displayed top-right in danger colour (red)
|
|
|
|
property alias additionalInfo: txtAddlInfo.text
|
2020-09-03 20:43:08 +00:00
|
|
|
property var selectedRecipient
|
2020-08-12 03:40:25 +00:00
|
|
|
property bool readOnly: false
|
2020-10-28 07:44:09 +00:00
|
|
|
height: inpAddress.height + txtLabel.height
|
2020-08-26 15:52:26 +00:00
|
|
|
//% "Invalid ethereum address"
|
|
|
|
readonly property string addressValidationError: qsTrId("invalid-ethereum-address")
|
2020-10-28 07:44:09 +00:00
|
|
|
property bool isValid: false
|
2020-10-29 03:07:34 +00:00
|
|
|
property bool isPending: {
|
|
|
|
if (!selAddressSource.selectedSource) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
switch (selAddressSource.selectedSource.value) {
|
|
|
|
case RecipientSelector.Type.Address:
|
|
|
|
return inpAddress.isPending
|
|
|
|
case RecipientSelector.Type.Contact:
|
|
|
|
return selContact.isPending
|
2020-11-03 10:29:56 +00:00
|
|
|
case RecipientSelector.Type.Account:
|
|
|
|
return false // AccountSelector is never pending
|
2020-10-29 03:07:34 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-20 04:45:29 +00:00
|
|
|
readonly property var sources: [
|
2020-09-14 12:12:47 +00:00
|
|
|
//% "Address"
|
2020-10-28 07:44:09 +00:00
|
|
|
{ text: qsTrId("address"), value: RecipientSelector.Type.Address, visible: true },
|
2020-09-14 12:12:47 +00:00
|
|
|
//% "My account"
|
2020-10-28 07:44:09 +00:00
|
|
|
{ text: qsTrId("my-account"), value: RecipientSelector.Type.Account, visible: true },
|
2021-02-18 16:36:05 +00:00
|
|
|
//% "Contact"
|
|
|
|
{ text: qsTrId("chat-is-a-contact"), value: RecipientSelector.Type.Contact, visible: true }
|
2020-08-20 04:45:29 +00:00
|
|
|
]
|
2020-10-28 07:44:09 +00:00
|
|
|
property var selectedType: RecipientSelector.Type.Address
|
2020-08-20 04:45:29 +00:00
|
|
|
|
2020-08-13 08:24:51 +00:00
|
|
|
enum Type {
|
|
|
|
Address,
|
|
|
|
Contact,
|
|
|
|
Account
|
|
|
|
}
|
|
|
|
|
2020-08-06 07:25:53 +00:00
|
|
|
function validate() {
|
2020-08-12 03:40:25 +00:00
|
|
|
let isValid = true
|
2020-11-03 10:29:56 +00:00
|
|
|
if (!selAddressSource.selectedSource) {
|
|
|
|
return root.isValid
|
|
|
|
}
|
|
|
|
switch (selAddressSource.selectedSource.value) {
|
2020-10-28 07:44:09 +00:00
|
|
|
case RecipientSelector.Type.Address:
|
|
|
|
isValid = inpAddress.isValid
|
|
|
|
break
|
|
|
|
case RecipientSelector.Type.Contact:
|
|
|
|
isValid = selContact.isValid
|
|
|
|
break
|
|
|
|
case RecipientSelector.Type.Account:
|
|
|
|
isValid = selAccount.isValid
|
|
|
|
break
|
2020-08-06 07:25:53 +00:00
|
|
|
}
|
2020-08-20 04:45:29 +00:00
|
|
|
root.isValid = isValid
|
2020-08-12 03:40:25 +00:00
|
|
|
return isValid
|
2020-08-06 07:25:53 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 07:44:09 +00:00
|
|
|
function getSourceByType(type) {
|
|
|
|
return root.sources.find(source => source.value === type)
|
|
|
|
}
|
|
|
|
|
|
|
|
onSelectedTypeChanged: {
|
|
|
|
if (selectedType !== undefined) {
|
|
|
|
selAddressSource.selectedSource = getSourceByType(selectedType)
|
|
|
|
}
|
|
|
|
if (!selectedRecipient) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
switch (root.selectedType) {
|
|
|
|
case RecipientSelector.Type.Address:
|
2021-04-13 17:49:24 +00:00
|
|
|
inpAddress.input.text = selectedRecipient.name || ""
|
2020-10-28 07:44:09 +00:00
|
|
|
inpAddress.visible = true
|
|
|
|
selContact.visible = selAccount.visible = false
|
2020-12-09 14:10:09 +00:00
|
|
|
if(!!selectedRecipient.address){
|
|
|
|
inpAddress.selectedAddress = selectedRecipient.address
|
|
|
|
}
|
2020-10-28 07:44:09 +00:00
|
|
|
break
|
|
|
|
case RecipientSelector.Type.Contact:
|
|
|
|
selContact.selectedContact = selectedRecipient
|
2020-10-29 23:08:07 +00:00
|
|
|
// TODO: we shouldn't have to call resolveEns from the outside.
|
|
|
|
// It should be handled automatically when selectedContact is
|
|
|
|
// updated, however, handling it on property change causes an
|
|
|
|
// infinite loop
|
|
|
|
selContact.resolveEns()
|
2020-10-28 07:44:09 +00:00
|
|
|
selContact.visible = true
|
|
|
|
inpAddress.visible = selAccount.visible = false
|
|
|
|
break
|
|
|
|
case RecipientSelector.Type.Account:
|
|
|
|
selAccount.selectedAccount = selectedRecipient
|
|
|
|
selAccount.visible = true
|
|
|
|
inpAddress.visible = selContact.visible = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-06 07:25:53 +00:00
|
|
|
Text {
|
|
|
|
id: txtLabel
|
|
|
|
visible: label !== ""
|
2020-08-26 15:52:26 +00:00
|
|
|
//% "Recipient"
|
|
|
|
text: qsTrId("recipient")
|
2020-08-06 07:25:53 +00:00
|
|
|
font.pixelSize: 13
|
2020-08-12 03:40:25 +00:00
|
|
|
font.family: Style.current.fontRegular.name
|
|
|
|
font.weight: Font.Medium
|
2020-08-06 07:25:53 +00:00
|
|
|
color: Style.current.textColor
|
|
|
|
height: 18
|
|
|
|
}
|
|
|
|
|
2020-08-12 03:40:25 +00:00
|
|
|
Text {
|
|
|
|
id: txtAddlInfo
|
|
|
|
visible: text !== ""
|
|
|
|
text: ""
|
|
|
|
font.pixelSize: 13
|
|
|
|
font.family: Style.current.fontRegular.name
|
|
|
|
font.weight: Font.Medium
|
|
|
|
color: Style.current.danger
|
|
|
|
height: 18
|
|
|
|
anchors.right: parent.right
|
|
|
|
}
|
|
|
|
|
2020-08-06 07:25:53 +00:00
|
|
|
RowLayout {
|
|
|
|
anchors.top: txtLabel.bottom
|
|
|
|
anchors.topMargin: 7
|
|
|
|
anchors.left: parent.left
|
|
|
|
anchors.right: parent.right
|
|
|
|
spacing: 8
|
|
|
|
|
|
|
|
AddressInput {
|
|
|
|
id: inpAddress
|
|
|
|
width: root.inputWidth
|
2021-04-13 17:49:24 +00:00
|
|
|
input.label: ""
|
|
|
|
input.readOnly: root.readOnly
|
2020-10-28 07:44:09 +00:00
|
|
|
visible: true
|
2020-08-12 03:40:25 +00:00
|
|
|
Layout.preferredWidth: selAddressSource.visible ? root.inputWidth : parent.width
|
2020-08-06 07:25:53 +00:00
|
|
|
Layout.alignment: Qt.AlignTop
|
|
|
|
Layout.fillWidth: true
|
2020-08-12 03:40:25 +00:00
|
|
|
validationError: root.addressValidationError
|
2021-04-13 17:49:24 +00:00
|
|
|
parentWidth: parent.width
|
2020-08-06 07:25:53 +00:00
|
|
|
onSelectedAddressChanged: {
|
2020-10-28 07:44:09 +00:00
|
|
|
if (!selAddressSource.selectedSource || (selAddressSource.selectedSource && selAddressSource.selectedSource.value !== RecipientSelector.Type.Address)) {
|
2020-08-12 03:40:25 +00:00
|
|
|
return
|
|
|
|
}
|
2020-11-04 12:37:53 +00:00
|
|
|
|
|
|
|
root.selectedRecipient = { address: selectedAddress, type: RecipientSelector.Type.Address }
|
2020-08-20 04:45:29 +00:00
|
|
|
}
|
2020-10-28 07:44:09 +00:00
|
|
|
onIsValidChanged: root.validate()
|
2020-08-06 07:25:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ContactSelector {
|
|
|
|
id: selContact
|
|
|
|
contacts: root.contacts
|
|
|
|
visible: false
|
|
|
|
width: root.inputWidth
|
|
|
|
dropdownWidth: parent.width
|
2020-10-28 07:44:09 +00:00
|
|
|
readOnly: root.readOnly
|
2020-08-12 03:40:25 +00:00
|
|
|
Layout.preferredWidth: selAddressSource.visible ? root.inputWidth : parent.width
|
2020-08-06 07:25:53 +00:00
|
|
|
Layout.alignment: Qt.AlignTop
|
|
|
|
Layout.fillWidth: true
|
|
|
|
onSelectedContactChanged: {
|
2020-10-28 07:44:09 +00:00
|
|
|
if (!selectedContact || !selAddressSource.selectedSource || !selectedContact.address || (selAddressSource.selectedSource && selAddressSource.selectedSource.value !== RecipientSelector.Type.Contact)) {
|
2020-08-12 03:40:25 +00:00
|
|
|
return
|
|
|
|
}
|
2020-10-28 07:44:09 +00:00
|
|
|
const { address, name, alias, isContact, identicon, ensVerified } = selectedContact
|
|
|
|
root.selectedRecipient = { address, name, alias, isContact, identicon, ensVerified, type: RecipientSelector.Type.Contact }
|
2020-08-20 04:45:29 +00:00
|
|
|
}
|
2020-10-28 07:44:09 +00:00
|
|
|
onIsValidChanged: root.validate()
|
2020-08-06 07:25:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AccountSelector {
|
|
|
|
id: selAccount
|
|
|
|
accounts: root.accounts
|
|
|
|
visible: false
|
|
|
|
width: root.inputWidth
|
|
|
|
dropdownWidth: parent.width
|
|
|
|
label: ""
|
2020-08-12 03:40:25 +00:00
|
|
|
Layout.preferredWidth: selAddressSource.visible ? root.inputWidth : parent.width
|
2020-08-06 07:25:53 +00:00
|
|
|
Layout.alignment: Qt.AlignTop
|
|
|
|
Layout.fillWidth: true
|
|
|
|
onSelectedAccountChanged: {
|
2020-10-28 07:44:09 +00:00
|
|
|
if (!selectedAccount || !selAddressSource.selectedSource || (selAddressSource.selectedSource && selAddressSource.selectedSource.value !== RecipientSelector.Type.Account)) {
|
2020-08-12 03:40:25 +00:00
|
|
|
return
|
|
|
|
}
|
2020-08-13 08:24:51 +00:00
|
|
|
const { address, name, iconColor, assets, fiatBalance } = selectedAccount
|
|
|
|
root.selectedRecipient = { address, name, iconColor, assets, fiatBalance, type: RecipientSelector.Type.Account }
|
2020-08-06 07:25:53 +00:00
|
|
|
}
|
2020-10-28 07:44:09 +00:00
|
|
|
onIsValidChanged: root.validate()
|
2020-08-06 07:25:53 +00:00
|
|
|
}
|
|
|
|
AddressSourceSelector {
|
|
|
|
id: selAddressSource
|
2020-08-12 03:40:25 +00:00
|
|
|
visible: !root.readOnly
|
2020-10-28 07:44:09 +00:00
|
|
|
sources: root.sources.filter(source => source.visible)
|
2020-08-06 07:25:53 +00:00
|
|
|
width: sourceSelectWidth
|
|
|
|
Layout.preferredWidth: root.sourceSelectWidth
|
|
|
|
Layout.alignment: Qt.AlignTop
|
2020-10-28 07:44:09 +00:00
|
|
|
|
2020-08-06 07:25:53 +00:00
|
|
|
onSelectedSourceChanged: {
|
2020-10-28 07:44:09 +00:00
|
|
|
if (root.readOnly || !selectedSource) {
|
2020-08-12 03:40:25 +00:00
|
|
|
return
|
|
|
|
}
|
2020-08-13 08:24:51 +00:00
|
|
|
let address, name
|
2020-10-28 07:44:09 +00:00
|
|
|
switch (selectedSource.value) {
|
|
|
|
case RecipientSelector.Type.Address:
|
2020-08-06 07:25:53 +00:00
|
|
|
inpAddress.visible = true
|
|
|
|
selContact.visible = selAccount.visible = false
|
|
|
|
root.height = Qt.binding(function() { return inpAddress.height + txtLabel.height })
|
2020-08-13 08:24:51 +00:00
|
|
|
root.selectedRecipient = { address: inpAddress.selectedAddress, type: RecipientSelector.Type.Address }
|
2020-10-28 07:44:09 +00:00
|
|
|
if (root.selectedType !== RecipientSelector.Type.Address) root.selectedType = RecipientSelector.Type.Address
|
2020-08-20 04:45:29 +00:00
|
|
|
root.isValid = inpAddress.isValid
|
2020-08-06 07:25:53 +00:00
|
|
|
break;
|
2020-10-28 07:44:09 +00:00
|
|
|
case RecipientSelector.Type.Contact:
|
2020-08-06 07:25:53 +00:00
|
|
|
selContact.visible = true
|
|
|
|
inpAddress.visible = selAccount.visible = false
|
|
|
|
root.height = Qt.binding(function() { return selContact.height + txtLabel.height })
|
2020-08-13 08:24:51 +00:00
|
|
|
let { alias, isContact, identicon, ensVerified } = selContact.selectedContact
|
|
|
|
address = selContact.selectedContact.address
|
|
|
|
name = selContact.selectedContact.name
|
|
|
|
root.selectedRecipient = { address, name, alias, isContact, identicon, ensVerified, type: RecipientSelector.Type.Contact }
|
2020-10-28 07:44:09 +00:00
|
|
|
if (root.selectedType !== RecipientSelector.Type.Contact) root.selectedType = RecipientSelector.Type.Contact
|
2020-08-20 04:45:29 +00:00
|
|
|
root.isValid = selContact.isValid
|
2020-08-06 07:25:53 +00:00
|
|
|
break;
|
2020-10-28 07:44:09 +00:00
|
|
|
case RecipientSelector.Type.Account:
|
2020-08-06 07:25:53 +00:00
|
|
|
selAccount.visible = true
|
|
|
|
inpAddress.visible = selContact.visible = false
|
|
|
|
root.height = Qt.binding(function() { return selAccount.height + txtLabel.height })
|
2020-08-13 08:24:51 +00:00
|
|
|
const { iconColor, assets, fiatBalance } = selAccount.selectedAccount
|
|
|
|
address = selAccount.selectedAccount.address
|
|
|
|
name = selAccount.selectedAccount.name
|
|
|
|
root.selectedRecipient = { address, name, iconColor, assets, fiatBalance, type: RecipientSelector.Type.Account }
|
2020-10-28 07:44:09 +00:00
|
|
|
if (root.selectedType !== RecipientSelector.Type.Account) root.selectedType = RecipientSelector.Type.Account
|
2020-08-20 04:45:29 +00:00
|
|
|
root.isValid = selAccount.isValid
|
2020-08-06 07:25:53 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*##^##
|
|
|
|
Designer {
|
|
|
|
D{i:0;autoSize:true;height:480;width:640}
|
|
|
|
}
|
|
|
|
##^##*/
|