2 * Copyright (C) 2016 Canonical, Ltd.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 import Ubuntu.Components 1.3
20 import "../Components"
24 implicitHeight: units.gu(5)
27 property bool isPrompt
28 property bool isAlphanumeric
30 property bool isSecret
31 property bool interactive: true
32 readonly property alias enteredText: passwordInput.text
38 function showFakePassword() {
39 // Just a silly hack for looking like 4 pin numbers got entered, if
40 // a fingerprint was used and we happen to be using a pin. This was
41 // a request from Design.
42 if (isSecret && isPrompt && !isAlphanumeric) {
43 passwordInput.text = "...."; // actual text doesn't matter
49 schema.id: "com.canonical.Unity8"
55 readonly property color textColor: passwordInput.enabled ? theme.palette.normal.raisedText
56 : theme.palette.disabled.raisedText
57 readonly property color selectedColor: passwordInput.enabled ? theme.palette.normal.raised
58 : theme.palette.disabled.raised
59 readonly property color drawColor: passwordInput.enabled ? theme.palette.normal.raisedSecondaryText
60 : theme.palette.disabled.raisedSecondaryText
61 readonly property color errorColor: passwordInput.enabled ? theme.palette.normal.negative
62 : theme.palette.disabled.negative
67 border.width: units.dp(1)
68 border.color: d.drawColor
73 Component.onCompleted: updateFocus()
74 onIsPromptChanged: updateFocus()
75 function updateFocus() {
77 passwordInput.focus = true;
79 promptButton.focus = true;
85 objectName: "promptButton"
87 visible: !root.isPrompt
88 activeFocusOnTab: true
90 styleName: "FocusShape"
92 function triggered() {
93 if (root.interactive) {
99 height: parent.height;
108 Keys.onSpacePressed: triggered();
109 Keys.onReturnPressed: triggered();
110 Keys.onEnterPressed: triggered();
113 onClicked: parent.triggered();
117 anchors.centerIn: parent
125 objectName: "promptField"
127 visible: root.isPrompt
128 opacity: fakeLabel.visible ? 0 : 1
129 activeFocusOnTab: true
131 validator: RegExpValidator {
132 regExp: root.isAlphanumeric ? /^.*$/ : /^\d{4}$/
135 inputMethodHints: Qt.ImhSensitiveData | Qt.ImhNoPredictiveText |
136 Qt.ImhMultiLine | // so OSK doesn't close on Enter
137 (root.isAlphanumeric ? Qt.ImhNone : Qt.ImhDigitsOnly)
138 echoMode: root.isSecret ? TextInput.Password : TextInput.Normal
139 hasClearButton: false
141 readonly property real frameSpacing: units.gu(0.5)
145 styleName: "FocusShape"
147 // Properties needed by TextField
148 readonly property color color: d.textColor
149 readonly property color selectedTextColor: d.selectedColor
150 readonly property color selectionColor: d.textColor
151 readonly property color borderColor: "transparent"
152 readonly property color backgroundColor: "transparent"
153 readonly property color errorColor: d.errorColor
154 readonly property real frameSpacing: styledItem.frameSpacing
156 // Properties needed by FocusShape
157 readonly property bool enabled: styledItem.enabled
158 readonly property bool keyNavigationFocus: styledItem.keyNavigationFocus
159 property bool activeFocusOnTab
165 spacing: passwordInput.frameSpacing
167 name: "keyboard-caps-enabled"
171 visible: root.isSecret && false // TODO: detect when caps lock is on
174 name: "input-keyboard-symbolic"
178 visible: !unity8Settings.alwaysShowOsk
181 onClicked: unity8Settings.alwaysShowOsk = true
187 onDisplayTextChanged: {
188 // We use onDisplayTextChanged instead of onTextChanged because
189 // displayText changes after text and if we did this before it
190 // updated, we would use the wrong displayText for fakeLabel.
191 if (!isAlphanumeric && text.length >= 4) {
192 // hard limit of 4 for passcodes right now
197 onAccepted: respond()
200 if (root.interactive) {
205 Keys.onEscapePressed: {
207 event.accepted = true;
210 // We use our own custom placeholder label instead of the standard
211 // TextField one because the standard one hardcodes baseText as the
212 // palette color, whereas we want raisedSecondaryText.
215 objectName: "promptHint"
217 left: parent ? parent.left : undefined
218 right: parent ? parent.right : undefined
219 verticalCenter: parent ? parent.verticalCenter : undefined
220 leftMargin: units.gu(1.5)
221 rightMargin: anchors.leftMargin + extraIcons.width
224 visible: passwordInput.text == "" && !passwordInput.inputMethodComposing
227 elide: Text.ElideRight
231 // Have a fake label that covers the text field after the user presses
232 // enter. What we *really* want is a disabled mode that doesn't lose OSK
233 // focus. Because our goal here is simply to keep the OSK up while
234 // we wait for PAM to get back to us, and while waiting, we don't want
235 // the user to be able to edit the field (simply because it would look
236 // weird if we allowed that). But until we have such a disabled mode,
237 // we'll fake it by covering the real text field with a label.
240 anchors.verticalCenter: parent ? parent.verticalCenter : undefined
241 anchors.left: parent ? parent.left : undefined
242 anchors.right: parent ? parent.right : undefined
243 anchors.leftMargin: passwordInput.frameSpacing * 2
244 anchors.rightMargin: passwordInput.frameSpacing * 2 + extraIcons.width
246 text: passwordInput.displayText
247 visible: root.isPrompt && !root.interactive