Add support for grace lock in lockscreen keypad

This commit is contained in:
Devin Lin 2020-09-28 21:26:24 -04:00
parent 849cb5f88f
commit 01026a223a
2 changed files with 67 additions and 30 deletions

View file

@ -24,47 +24,78 @@ import org.kde.plasma.core 2.0 as PlasmaCore
import org.kde.plasma.workspace.keyboardlayout 1.0 import org.kde.plasma.workspace.keyboardlayout 1.0
Rectangle { Rectangle {
id: keypadRoot
color: Qt.rgba(250, 250, 250, 0.85) // slightly translucent background, for key contrast color: Qt.rgba(250, 250, 250, 0.85) // slightly translucent background, for key contrast
property string pinLabel: qsTr("Enter PIN") property string pinLabel: qsTr("Enter PIN")
// for displaying temporary number in pin dot display // for displaying temporary number in pin dot display
property string lastKeyPressValue: "0" property string lastKeyPressValue: "0"
property int indexWithNumber: -2 property int indexWithNumber: -2
// if waiting for result of auth
property bool waitingForAuth: false
function reset() {
waitingForAuth = false;
root.password = "";
keypadRoot.pinLabel = qsTr("Enter PIN");
}
// keypad functions // keypad functions
function backspace() { function backspace() {
lastKeyPressValue = "0"; if (!keypadRoot.waitingForAuth) {
indexWithNumber = -2; keypadRoot.lastKeyPressValue = "0";
root.password = root.password.substr(0, root.password.length - 1); keypadRoot.indexWithNumber = -2;
root.password = root.password.substr(0, root.password.length - 1);
}
} }
function clear() { function clear() {
lastKeyPressValue = "0"; if (!keypadRoot.waitingForAuth) {
indexWithNumber = -2; keypadRoot.lastKeyPressValue = "0";
root.password = ""; keypadRoot.indexWithNumber = -2;
root.password = "";
}
} }
function enter() { function enter() {
authenticator.tryUnlock(root.password); keypadRoot.waitingForAuth = true;
// don't try to unlock if there is a timeout (unlock once unlocked)
if (!authenticator.graceLocked) {
authenticator.tryUnlock(root.password);
}
} }
function keyPress(data) { function keyPress(data) {
if (keypad.pinLabel !== qsTr("Enter PIN")) { if (!keypadRoot.waitingForAuth) {
keypad.pinLabel = qsTr("Enter PIN"); if (keypadRoot.pinLabel !== qsTr("Enter PIN")) {
keypadRoot.pinLabel = qsTr("Enter PIN");
}
keypadRoot.lastKeyPressValue = data;
keypadRoot.indexWithNumber = root.password.length;
root.password += data
// trigger turning letter into dot later
letterTimer.restart();
} }
lastKeyPressValue = data;
indexWithNumber = root.password.length;
root.password += data
// trigger turning letter into dot later
letterTimer.restart();
} }
Connections { Connections {
target: authenticator target: authenticator
function onSucceeded() {
pinLabel = qsTr("Logging in...");
keypadRoot.waitingForAuth = false;
}
function onFailed() { function onFailed() {
root.password = ""; root.password = "";
pinLabel = qsTr("Wrong PIN") pinLabel = qsTr("Wrong PIN");
keypadRoot.waitingForAuth = false;
}
function onGraceLockedChanged() {
// try authenticating if it was waiting for grace lock to stop and it has stopped
if (!authenticator.graceLocked && keypadRoot.waitingForAuth) {
authenticator.tryUnlock(root.password);
}
} }
} }
@ -72,11 +103,11 @@ Rectangle {
Keys.onPressed: { Keys.onPressed: {
if (event.modifiers === Qt.NoModifier) { if (event.modifiers === Qt.NoModifier) {
if (event.key === Qt.Key_Backspace) { if (event.key === Qt.Key_Backspace) {
backspace(); keypadRoot.backspace();
} else if (event.key === Qt.Key_Return) { } else if (event.key === Qt.Key_Return) {
enter(); keypadRoot.enter();
} else if (event.text != "") { } else if (event.text != "") {
keyPress(event.text); keypadRoot.keyPress(event.text);
} }
} }
} }
@ -88,8 +119,8 @@ Rectangle {
running: false running: false
repeat: false repeat: false
onTriggered: { onTriggered: {
lastKeyPressValue = 0; keypadRoot.lastKeyPressValue = 0;
indexWithNumber = -2; keypadRoot.indexWithNumber = -2;
} }
} }
@ -104,19 +135,26 @@ Rectangle {
} }
spacing: units.gridUnit spacing: units.gridUnit
Item {
Layout.alignment: Qt.AlignCenter
}
// pin dot display // pin dot display
Item { Item {
Layout.alignment: Qt.AlignCenter Layout.alignment: Qt.AlignCenter
Layout.minimumHeight: units.gridUnit * 0.5 Layout.minimumHeight: units.gridUnit * 0.5
Layout.maximumWidth: parent.width Layout.maximumWidth: parent.width
// label ("wrong pin", "enter pin")
Label { Label {
visible: root.password.length === 0 visible: root.password.length === 0
anchors.centerIn: parent anchors.centerIn: parent
text: pinLabel text: keypadRoot.pinLabel
font.pointSize: 12 font.pointSize: 12
color: "#616161" color: "#616161"
} }
// dot display and letter
RowLayout { RowLayout {
id: dotDisplay id: dotDisplay
anchors.centerIn: parent anchors.centerIn: parent
@ -131,13 +169,13 @@ Rectangle {
Layout.preferredHeight: Layout.preferredWidth Layout.preferredHeight: Layout.preferredWidth
Layout.alignment: Qt.AlignVCenter Layout.alignment: Qt.AlignVCenter
radius: width radius: width
color: "#424242" color: keypadRoot.waitingForAuth ? "#969696" : "#424242" // dim when waiting for auth
} }
} }
Label { // number/letter Label { // number/letter
visible: root.password.length-1 === indexWithNumber // hide label if no label needed visible: root.password.length-1 === indexWithNumber // hide label if no label needed
Layout.alignment: Qt.AlignHCenter Layout.alignment: Qt.AlignHCenter
color: "#424242" color: keypadRoot.waitingForAuth ? "#969696" : "#424242"
text: lastKeyPressValue text: lastKeyPressValue
font.pointSize: 12 font.pointSize: 12
} }
@ -185,11 +223,11 @@ Rectangle {
onReleased: parent.color = "white" onReleased: parent.color = "white"
onClicked: { onClicked: {
if (modelData === "R") { if (modelData === "R") {
backspace(); keypadRoot.backspace();
} else if (modelData === "E") { } else if (modelData === "E") {
enter(); keypadRoot.enter();
} else { } else {
keyPress(modelData); keypadRoot.keyPress(modelData);
} }
} }
onPressAndHold: { onPressAndHold: {

View file

@ -234,8 +234,7 @@ PlasmaCore.ColorScope {
// wipe password if it is more than half way down the screen // wipe password if it is more than half way down the screen
onContentYChanged: { onContentYChanged: {
if (contentY < columnHeight / 2) { if (contentY < columnHeight / 2) {
root.password = ""; keypad.reset();
keypad.pinLabel = qsTr("Enter PIN");
} }
} }