Lomiri
Loading...
Searching...
No Matches
PinPrompt.qml
1/*
2 * Copyright (C) 2021 Capsia
3 * Copyright (C) 2016 Canonical, Ltd.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 3.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18import QtQuick 2.15
19import QtQuick.Window 2.2
20import AccountsService 0.1
21import Lomiri.Components 1.3
22import "../Components"
23
24FocusScope {
25 id: root
26
27 property string text
28 property bool isSecret
29 property bool interactive: true
30 property bool loginError: false
31 property bool hasKeyboard: false
32 property alias enteredText: passwordInput.text
33 property int pincodeLength: AccountsService.pincodeLength
34
35 signal clicked()
36 signal canceled()
37 signal accepted(string response)
38
39 // A fold/unfold drops and re-adds the single output; maliit hides on
40 // the surface reconfigure but the field keeps active focus, so the
41 // focus handler never re-fires and the OSK does not return. Re-request
42 // it when the window is reactivated while the field is still focused.
43 Connections {
44 target: Window.window
45 function onActiveChanged() {
46 if (Window.window && Window.window.active && passwordInput.activeFocus) {
47 Window.window.requestActivate();
48 Qt.inputMethod.show();
49 }
50 }
51 }
52
53 StyledItem {
54 id: d
55
56 readonly property color textColor: passwordInput.enabled ? theme.palette.normal.raisedText
57 : theme.palette.disabled.raisedText
58 readonly property color selectedColor: passwordInput.enabled ? theme.palette.normal.raised
59 : theme.palette.disabled.raised
60 readonly property color drawColor: passwordInput.enabled ? theme.palette.normal.raisedSecondaryText
61 : theme.palette.disabled.raisedSecondaryText
62 readonly property color errorColor: passwordInput.enabled ? theme.palette.normal.negative
63 : theme.palette.disabled.negative
64 }
65
66 TextField {
67 id: passwordInput
68 objectName: "promptField"
69 anchors.left: extraIcons.left
70 anchors.right: extraIcons.right
71 focus: root.focus
72
73 opacity: fakeLabel.visible ? 0 : 1
74 activeFocusOnTab: true
75 onActiveFocusChanged: {
76 if (activeFocus) {
77 // The greeter surface is not the active Qt window, so a focused
78 // field is never promoted to the input method focus object and
79 // maliit never shows; activate the window first.
80 if (Window.window) Window.window.requestActivate();
81 Qt.inputMethod.show();
82 }
83 }
84
85 onSelectedTextChanged: passwordInput.deselect()
86 onCursorPositionChanged: cursorPosition = length
87
88 validator: RegExpValidator {
89 regExp: /^\d{4,}$/
90 }
91
92 inputMethodHints: Qt.ImhSensitiveData | Qt.ImhNoPredictiveText |
93 Qt.ImhMultiLine | // so OSK doesn't close on Enter
94 Qt.ImhDigitsOnly
95 echoMode: TextInput.Password
96 hasClearButton: false
97
98 cursorDelegate: Item {}
99
100 passwordCharacter: "●"
101 color: d.drawColor
102
103 readonly property real letterSpacing: units.gu(1.75)
104 readonly property real frameSpacing: letterSpacing
105
106 font.pixelSize: units.gu(3)
107 font.letterSpacing: letterSpacing
108
109 style: StyledItem {
110 anchors.fill: parent
111 styleName: "FocusShape"
112
113 // Properties needed by TextField
114 readonly property color color: d.textColor
115 readonly property color selectedTextColor: d.selectedColor
116 readonly property color selectionColor: d.textColor
117 readonly property color borderColor: "transparent"
118 readonly property color backgroundColor: "transparent"
119 readonly property color errorColor: d.errorColor
120 readonly property real frameSpacing: 0
121
122 // Properties needed by FocusShape
123 readonly property bool enabled: styledItem.enabled
124 readonly property bool keyNavigationFocus: styledItem.keyNavigationFocus
125 property bool activeFocusOnTab
126 }
127
128 onDisplayTextChanged: {
129 // We use onDisplayTextChanged instead of onTextChanged because
130 // displayText changes after text and if we did this before it
131 // updated, we would use the wrong displayText for fakeLabel.
132 root.loginError = false;
133 if (text.length === root.pincodeLength) {
134 respond();
135 }
136 }
137
138 onAccepted: respond()
139
140 function respond() {
141 if (root.interactive) {
142 root.accepted(passwordInput.text);
143 }
144 }
145
146 Keys.onEscapePressed: {
147 lomiriSettings.alwaysShowOsk = false
148 root.canceled();
149 event.accepted = true;
150 }
151 }
152
153 Row {
154 id: extraIcons
155 spacing: passwordInput.frameSpacing
156 anchors {
157 horizontalCenter: parent ? parent.horizontalCenter : undefined
158 horizontalCenterOffset: passwordInput.letterSpacing / 2
159 verticalCenter: passwordInput ? passwordInput.verticalCenter : undefined
160 }
161
162 Label {
163 id: pinHint
164 objectName: "promptPinHint"
165
166 text: Array(root.pincodeLength).fill('○').join("")
167 enabled: visible
168 color: d.drawColor
169 font {
170 pixelSize: units.gu(3)
171 letterSpacing: units.gu(1.75)
172 }
173 elide: Text.ElideRight
174 }
175 Icon {
176 name: "keyboard-caps-enabled"
177 height: units.gu(3)
178 width: height
179 color: d.drawColor
180 visible: false // TODO: detect when caps lock is on
181 anchors.verticalCenter: parent.verticalCenter
182 }
183 Icon {
184 objectName: "greeterPromptKeyboardButton"
185 name: "input-keyboard-symbolic"
186 height: units.gu(3)
187 width: height
188 color: d.drawColor
189 visible: !lomiriSettings.alwaysShowOsk && root.hasKeyboard
190 anchors.verticalCenter: parent.verticalCenter
191 MouseArea {
192 anchors.fill: parent
193 onClicked: lomiriSettings.alwaysShowOsk = true
194 }
195 }
196 Icon {
197 name: "dialog-warning-symbolic"
198 height: units.gu(3)
199 width: height
200 color: d.drawColor
201 visible: root.loginError
202 anchors.verticalCenter: parent.verticalCenter
203 }
204 }
205
206 // Have a fake label that covers the text field after the user presses
207 // enter. What we *really* want is a disabled mode that doesn't lose OSK
208 // focus. Because our goal here is simply to keep the OSK up while
209 // we wait for PAM to get back to us, and while waiting, we don't want
210 // the user to be able to edit the field (simply because it would look
211 // weird if we allowed that). But until we have such a disabled mode,
212 // we'll fake it by covering the real text field with a label.
213 Label {
214 id: fakeLabel
215 anchors.verticalCenter: extraIcons ? extraIcons.verticalCenter : undefined
216 anchors.left: extraIcons ? extraIcons.left : undefined
217 anchors.right: parent ? parent.right : undefined
218 anchors.rightMargin: passwordInput.frameSpacing * 2 + extraIcons.width
219 color: d.drawColor
220 font {
221 pixelSize: pinHint.font.pixelSize
222 letterSpacing: pinHint.font.letterSpacing
223 }
224 text: passwordInput.displayText
225 visible: !root.interactive
226 enabled: visible
227 }
228}