moment/src/gui/Popups/PasswordPopup.qml

127 lines
3.1 KiB
QML
Raw Normal View History

2019-12-19 07:46:16 -04:00
// SPDX-License-Identifier: LGPL-3.0-or-later
import QtQuick 2.12
import QtQuick.Layouts 1.12
2019-09-09 08:57:38 -04:00
import "../Base"
import "../Base/ButtonLayout"
HFlickableColumnPopup {
id: popup
2019-08-27 22:25:13 -04:00
property bool validateWhileTyping: false
property string acceptedPassword: ""
property var passwordValid: null
property bool okClicked: false
readonly property alias summary: summary
readonly property alias validateButton: validateButton
2019-08-27 22:25:13 -04:00
signal cancelled()
2019-08-27 22:25:13 -04:00
function verifyPassword(pass, callback) {
2019-09-07 19:17:32 -04:00
// Can be reimplemented when using this component.
// Pass to the callback true on success, false on invalid password,
// or a custom error message string.
2019-09-07 19:17:32 -04:00
callback(true)
2019-08-27 22:25:13 -04:00
}
function validate() {
const password = passwordField.text
okClicked = true
validateButton.loading = true
errorMessage.text = ""
verifyPassword(password, result => {
if (result === true) {
passwordValid = true
popup.acceptedPassword = password
popup.close()
} else if (result === false) {
passwordValid = false
} else {
errorMessage.text = result
}
validateButton.loading = false
})
}
page.footer: ButtonLayout {
ApplyButton {
id: validateButton
text: qsTr("Validate")
enabled: Boolean(passwordField.text)
onClicked: validate()
}
CancelButton {
onClicked: {
popup.close()
cancelled()
}
}
}
onAboutToShow: {
okClicked = false
acceptedPassword = ""
passwordValid = null
errorMessage.text = ""
}
onOpened: passwordField.forceActiveFocus()
2019-08-27 22:25:13 -04:00
SummaryLabel { id: summary }
HRowLayout {
spacing: theme.spacing
HTextField {
id: passwordField
echoMode: TextInput.Password
focus: true
error: passwordValid === false
onTextChanged: passwordValid =
validateWhileTyping ? verifyPassword(text) : null
onAccepted: popup.validate()
2019-08-27 22:25:13 -04:00
Layout.fillWidth: true
}
HIcon {
visible: Layout.preferredWidth > 0
svgName: passwordValid ? "ok" : "cancel"
colorize: passwordValid ?
theme.colors.positiveBackground :
theme.colors.negativeBackground
Layout.preferredWidth:
2019-12-09 11:35:50 -04:00
passwordValid === null ||
(validateWhileTyping && ! okClicked && ! passwordValid) ?
0 :
implicitWidth
Behavior on Layout.preferredWidth { HNumberAnimation {} }
}
}
HLabel {
id: errorMessage
wrapMode: Text.Wrap
color: theme.colors.errorText
visible: Layout.maximumHeight > 0
Layout.maximumHeight: text ? implicitHeight : 0
Behavior on Layout.maximumHeight { HNumberAnimation {} }
Layout.fillWidth: true
}
}