From aae26672de7b02d1708e4a8dff76c5c1b6e8fd96 Mon Sep 17 00:00:00 2001 From: miruka Date: Sun, 8 Sep 2019 12:17:42 -0400 Subject: [PATCH] Split HPasswordPopup into HPopup, HOkCancelPopup --- src/qml/Base/HOkCancelPopup.qml | 29 +++++++ src/qml/Base/HPasswordPopup.qml | 140 ++++++++++++-------------------- src/qml/Base/HPopup.qml | 31 +++++++ 3 files changed, 113 insertions(+), 87 deletions(-) create mode 100644 src/qml/Base/HOkCancelPopup.qml create mode 100644 src/qml/Base/HPopup.qml diff --git a/src/qml/Base/HOkCancelPopup.qml b/src/qml/Base/HOkCancelPopup.qml new file mode 100644 index 00000000..32b43f8b --- /dev/null +++ b/src/qml/Base/HOkCancelPopup.qml @@ -0,0 +1,29 @@ +import QtQuick 2.12 +import QtQuick.Layouts 1.12 + +HPopup { + onAboutToShow: okClicked = false + + + property alias label: label + property bool okClicked: false + + + box.enterButtonTarget: "ok" + box.buttonModel: [ + { name: "ok", text: qsTr("OK"), iconName: "ok" }, + { name: "cancel", text: qsTr("Cancel"), iconName: "cancel" }, + ] + box.buttonCallbacks: ({ + ok: button => { okClicked = true; popup.close() }, + cancel: button => { okClicked = false; popup.close() }, + }) + + + HLabel { + id: label + wrapMode: Text.Wrap + + Layout.fillWidth: true + } +} diff --git a/src/qml/Base/HPasswordPopup.qml b/src/qml/Base/HPasswordPopup.qml index eba42ebb..45e4cf88 100644 --- a/src/qml/Base/HPasswordPopup.qml +++ b/src/qml/Base/HPasswordPopup.qml @@ -3,27 +3,23 @@ import QtQuick.Controls 2.12 import QtQuick.Layouts 1.12 import "../SidePane" -Popup { +HOkCancelPopup { id: popup - anchors.centerIn: Overlay.overlay - modal: true - padding: 0 onAboutToShow: { + okClicked = false acceptedPassword = "" passwordValid = null - okClicked = false errorMessage.text = "" } onOpened: passwordField.forceActiveFocus() + property bool validateWhileTyping: false property string acceptedPassword: "" property var passwordValid: null - property bool okClicked: false - property alias label: popupLabel property alias field: passwordField @@ -35,99 +31,69 @@ Popup { } - enter: Transition { - HNumberAnimation { property: "scale"; from: 0; to: 1; overshoot: 4 } - } + box.buttonCallbacks: ({ + ok: button => { + let password = passwordField.text + okClicked = true + button.loading = true - exit: Transition { - HNumberAnimation { property: "scale"; to: 0 } - } + verifyPassword(password, result => { + if (result === true) { + passwordValid = true + popup.acceptedPassword = password + popup.close() + } else if (result === false) { + passwordValid = false + } else { + let [msg, translated] = result + errorMessage.text = translated ? msg : qsTr(msg) + } - background: Rectangle { - color: theme.controls.popup.background - } - - contentItem: HBox { - id: box - implicitWidth: theme.minimumSupportedWidthPlusSpacing - enterButtonTarget: "ok" - buttonModel: [ - { name: "ok", text: qsTr("OK"), iconName: "ok", - enabled: passwordField.text && - (validateWhileTyping ? passwordValid : true) }, - { name: "cancel", text: qsTr("Cancel"), iconName: "cancel" }, - ] - buttonCallbacks: ({ - ok: button => { - let password = passwordField.text - okClicked = true - button.loading = true - - verifyPassword(password, result => { - if (result === true) { - passwordValid = true - popup.acceptedPassword = password - popup.close() - } else if (result === false) { - passwordValid = false - } else { - let [msg, translated] = result - errorMessage.text = translated ? msg : qsTr(msg) - } - - button.loading = false - }) - }, - cancel: button => { popup.close() }, - }) + button.loading = false + }) + }, + cancel: button => { popup.close() }, + }) - HLabel { - id: popupLabel - wrapMode: Text.Wrap + HRowLayout { + spacing: box.horizontalSpacing + + HTextField { + id: passwordField + placeholderText: qsTr("Passphrase") + echoMode: TextInput.Password + focus: true + error: passwordValid === false + + onTextChanged: passwordValid = + validateWhileTyping ? verifyPassword(text) : null Layout.fillWidth: true } - HRowLayout { - spacing: box.horizontalSpacing + HIcon { + svgName: passwordValid ? "ok" : "cancel" + visible: Layout.preferredWidth > 0 - HTextField { - id: passwordField - placeholderText: qsTr("Passphrase") - echoMode: TextInput.Password - focus: true - error: passwordValid === false + Layout.preferredWidth: + passwordValid == null || + (validateWhileTyping && ! okClicked && ! passwordValid) ? + 0 :implicitWidth - onTextChanged: passwordValid = - validateWhileTyping ? verifyPassword(text) : null - - Layout.fillWidth: true - } - - HIcon { - svgName: passwordValid ? "ok" : "cancel" - visible: Layout.preferredWidth > 0 - - Layout.preferredWidth: - passwordValid == null || - (validateWhileTyping && ! okClicked && ! passwordValid) ? - 0 :implicitWidth - - Behavior on Layout.preferredWidth { HNumberAnimation {} } - } + Behavior on Layout.preferredWidth { HNumberAnimation {} } } + } - HLabel { - id: errorMessage - wrapMode: Text.Wrap - color: theme.colors.errorText + HLabel { + id: errorMessage + wrapMode: Text.Wrap + color: theme.colors.errorText - visible: Layout.maximumHeight > 0 - Layout.maximumHeight: text ? implicitHeight : 0 - Behavior on Layout.maximumHeight { HNumberAnimation {} } + visible: Layout.maximumHeight > 0 + Layout.maximumHeight: text ? implicitHeight : 0 + Behavior on Layout.maximumHeight { HNumberAnimation {} } - Layout.fillWidth: true - } + Layout.fillWidth: true } } diff --git a/src/qml/Base/HPopup.qml b/src/qml/Base/HPopup.qml new file mode 100644 index 00000000..a157b73b --- /dev/null +++ b/src/qml/Base/HPopup.qml @@ -0,0 +1,31 @@ +import QtQuick 2.12 +import QtQuick.Controls 2.12 + +Popup { + id: popup + anchors.centerIn: Overlay.overlay + modal: true + padding: 0 + + + default property alias boxData: box.body + property alias box: box + + + enter: Transition { + HNumberAnimation { property: "scale"; from: 0; to: 1; overshoot: 4 } + } + + exit: Transition { + HNumberAnimation { property: "scale"; to: 0 } + } + + background: Rectangle { + color: theme.controls.popup.background + } + + contentItem: HBox { + id: box + implicitWidth: theme.minimumSupportedWidthPlusSpacing + } +}