1
0

qml: calculate max amount when max toggle is enabled

This commit is contained in:
Sander van Grieken
2024-12-12 13:25:44 +01:00
parent 01fcd2ee9a
commit 37f0069f2a
5 changed files with 209 additions and 21 deletions

View File

@@ -142,6 +142,7 @@ ElDialog {
Layout.alignment: Qt.AlignHCenter
leftPadding: constants.paddingXLarge
rightPadding: constants.paddingXLarge
property bool editmode: false
@@ -216,10 +217,23 @@ ElDialog {
BtcField {
id: amountBtc
Layout.preferredWidth: amountFontMetrics.advanceWidth('0') * 14 + leftPadding + rightPadding
fiatfield: amountFiat
enabled: !amountMax.checked
readOnly: amountMax.checked
color: readOnly
? Material.accentColor
: Material.foreground
onTextAsSatsChanged: {
invoice.amountOverride = textAsSats
if (!amountMax.checked)
invoice.amountOverride.satsInt = textAsSats.satsInt
}
Connections {
target: invoice.amountOverride
function onSatsIntChanged() {
console.log('amuontOverride satsIntChanged, sats=' + invoice.amountOverride.satsInt)
if (amountMax.checked) // amountOverride updated by max amount estimate
amountBtc.text = Config.formatSats(invoice.amountOverride.satsInt)
}
}
}
@@ -239,24 +253,48 @@ ElDialog {
visible: _canMax
checked: false
onCheckedChanged: {
if (activeFocus)
if (activeFocus) {
invoice.amountOverride.isMax = checked
if (checked) {
maxAmountMessage.text = ''
invoice.updateMaxAmount()
}
}
}
}
FiatField {
id: amountFiat
Layout.preferredWidth: amountFontMetrics.advanceWidth('0') * 14 + leftPadding + rightPadding
btcfield: amountBtc
visible: Daemon.fx.enabled && !amountMax.checked
enabled: !amountMax.checked
visible: Daemon.fx.enabled
readOnly: amountMax.checked
color: readOnly
? Material.accentColor
: Material.foreground
}
Label {
Layout.columnSpan: 2
visible: Daemon.fx.enabled && !amountMax.checked
visible: Daemon.fx.enabled
text: Daemon.fx.fiatCurrency
color: Material.accentColor
}
InfoTextArea {
Layout.topMargin: constants.paddingMedium
Layout.fillWidth: true
Layout.columnSpan: 3
id: maxAmountMessage
visible: amountMax.checked && text
compact: true
Connections {
target: invoice
function onMaxAmountMessage(message) {
maxAmountMessage.text = message
}
}
}
}
}
@@ -425,7 +463,9 @@ ElDialog {
enabled: !invoice.isSaved && invoice.canSave
onClicked: {
if (invoice.amount.isEmpty) {
invoice.amountOverride = amountMax.checked ? MAX : Config.unitsToSats(amountBtc.text)
invoice.amountOverride = Config.unitsToSats(amountBtc.text)
if (amountMax.checked)
invoice.amountOverride.isMax = true
}
invoice.saveInvoice()
app.stack.push(Qt.resolvedUrl('Invoices.qml'))
@@ -440,7 +480,9 @@ ElDialog {
enabled: invoice.invoiceType != Invoice.Invalid && invoice.canPay
onClicked: {
if (invoice.amount.isEmpty) {
invoice.amountOverride = amountMax.checked ? MAX : Config.unitsToSats(amountBtc.text)
invoice.amountOverride = Config.unitsToSats(amountBtc.text)
if (amountMax.checked)
invoice.amountOverride.isMax = true
}
if (!invoice.isSaved) {
// save invoice if newly parsed
@@ -468,4 +510,9 @@ ElDialog {
}
}
}
FontMetrics {
id: amountFontMetrics
font: amountBtc.font
}
}

View File

@@ -167,11 +167,25 @@ ElDialog {
}
BtcField {
id: amount
id: amountBtc
fiatfield: amountFiat
Layout.preferredWidth: parent.width /3
onTextChanged: channelopener.amount = Config.unitsToSats(amount.text)
enabled: !is_max.checked
Layout.preferredWidth: amountFontMetrics.advanceWidth('0') * 14 + leftPadding + rightPadding
onTextAsSatsChanged: {
if (!is_max.checked)
channelopener.amount.satsInt = amountBtc.textAsSats.satsInt
}
readOnly: is_max.checked
color: readOnly
? Material.accentColor
: Material.foreground
Connections {
target: channelopener.amount
function onSatsIntChanged() {
if (is_max.checked) // amount updated by max amount estimate
amountBtc.text = Config.formatSats(channelopener.amount.satsInt)
}
}
}
RowLayout {
@@ -184,7 +198,13 @@ ElDialog {
id: is_max
text: qsTr('Max')
onCheckedChanged: {
channelopener.amount = checked ? MAX : Config.unitsToSats(amount.text)
if (activeFocus) {
channelopener.amount.isMax = checked
if (checked) {
maxAmountMessage.text = ''
channelopener.updateMaxAmount()
}
}
}
}
}
@@ -193,10 +213,13 @@ ElDialog {
FiatField {
id: amountFiat
btcfield: amount
Layout.preferredWidth: amountFontMetrics.advanceWidth('0') * 14 + leftPadding + rightPadding
btcfield: amountBtc
visible: Daemon.fx.enabled
Layout.preferredWidth: parent.width /3
enabled: !is_max.checked
readOnly: is_max.checked
color: readOnly
? Material.accentColor
: Material.foreground
}
Label {
@@ -207,6 +230,16 @@ ElDialog {
}
Item { visible: Daemon.fx.enabled ; height: 1; width: 1 }
InfoTextArea {
Layout.topMargin: constants.paddingMedium
Layout.fillWidth: true
Layout.columnSpan: 3
id: maxAmountMessage
visible: is_max.checked && text
compact: true
}
}
}
@@ -288,6 +321,13 @@ ElDialog {
// TODO: handle incomplete TX
root.close()
}
onMaxAmountMessage: (message) => {
maxAmountMessage.text = message
}
}
FontMetrics {
id: amountFontMetrics
font: amountBtc.font
}
}