2 * Copyright (C) 2013 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
19 import "../Components"
24 property alias model: userList.model
25 property int currentIndex
28 readonly property int numAboveBelow: 4
29 readonly property int cellHeight: units.gu(5)
30 readonly property int highlightedHeight: units.gu(10)
31 readonly property int moveDuration: 200
32 readonly property string currentUser: userList.currentItem.username
33 property bool wasPrompted: false
35 signal selected(int index)
36 signal responded(string response)
38 function tryToUnlock() {
40 passwordInput.forceActiveFocus();
43 root.selected(currentIndex);
50 function showMessage(html) {
51 if (infoLabel.text === "") {
52 infoLabel.text = html;
54 infoLabel.text += "<br>" + html;
58 function showPrompt(text, isSecret, isDefaultPrompt) {
59 passwordInput.text = "";
60 passwordInput.promptText = text;
61 passwordInput.enabled = true;
62 passwordInput.echoMode = isSecret ? TextInput.Password : TextInput.Normal;
63 if (wasPrompted) // stay in text field if second prompt
64 passwordInput.focus = true;
68 function showError() {
69 wrongPasswordAnimation.start();
70 root.resetAuthentication();
72 passwordInput.focus = true;
77 root.resetAuthentication();
80 Keys.onEscapePressed: {
81 selected(currentIndex);
84 onCurrentIndexChanged: {
85 userList.currentIndex = currentIndex;
93 verticalCenter: parent.verticalCenter
95 height: root.highlightedHeight
96 color: Qt.rgba(0.1, 0.1, 0.1, 0.4)
97 border.color: Qt.rgba(0.4, 0.4, 0.4, 0.4)
98 border.width: units.dp(1)
105 objectName: "userList"
109 preferredHighlightBegin: userList.height / 2 - root.highlightedHeight / 2
110 preferredHighlightEnd: userList.height / 2 - root.highlightedHeight / 2
111 highlightRangeMode: ListView.StrictlyEnforceRange
112 highlightMoveDuration: root.moveDuration
113 flickDeceleration: 10000
115 readonly property bool movingInternally: moveTimer.running || userList.moving
116 onMovingInternallyChanged: {
117 if (!movingInternally) {
118 root.selected(currentIndex);
122 onCurrentIndexChanged: {
123 root.resetAuthentication();
129 height: root.cellHeight
131 readonly property bool belowHighlight: (userList.currentIndex < 0 && index > 0) || (userList.currentIndex >= 0 && index > userList.currentIndex)
132 readonly property int belowOffset: root.highlightedHeight - root.cellHeight
133 readonly property string username: name
136 // The goal here is to make names less and less opaque as they
137 // leave the highlight area. Can't simply use index, because
138 // that can change quickly if the user clicks at edges of
139 // list. So we use actual pixel distance.
140 var highlightDist = 0;
141 var realY = y - userList.contentY;
143 realY += belowOffset;
144 if (realY + height <= highlightItem.y)
145 highlightDist = realY + height - highlightItem.y;
146 else if (realY >= highlightItem.y + root.highlightedHeight)
147 highlightDist = realY - highlightItem.y - root.highlightedHeight;
150 return 1 - Math.min(1, (Math.abs(highlightDist) + root.cellHeight) / ((root.numAboveBelow + 1) * root.cellHeight))
154 objectName: "username" + index
158 leftMargin: units.gu(2)
160 rightMargin: units.gu(2)
162 // Add an offset to topMargin for any items below the highlight
163 topMargin: units.gu(1) + (parent.belowHighlight ? parent.belowOffset : 0)
167 elide: Text.ElideRight
169 Behavior on anchors.topMargin { NumberAnimation { duration: root.moveDuration; easing.type: Easing.InOutQuad; } }
177 // Add an offset to topMargin for any items below the highlight
178 topMargin: parent.belowHighlight ? parent.belowOffset : 0
180 height: parent.height
181 enabled: userList.currentIndex !== index && parent.opacity > 0
182 onClicked: root.selected(index)
184 Behavior on anchors.topMargin { NumberAnimation { duration: root.moveDuration; easing.type: Easing.InOutQuad; } }
188 // This is needed because ListView.moving is not true if the ListView
189 // moves because of an internal event (e.g. currentIndex has changed)
194 interval: root.moveDuration
200 objectName: "infoLabel"
202 bottom: passwordInput.top
204 topMargin: units.gu(1)
205 bottomMargin: units.gu(1)
206 leftMargin: units.gu(2)
207 rightMargin: units.gu(1)
211 width: root.width - anchors.leftMargin - anchors.rightMargin
213 textFormat: Text.StyledText
216 opacity: (userList.movingInternally || text == "") ? 0 : 1
217 Behavior on opacity {
218 NumberAnimation { duration: 100 }
224 objectName: "passwordInput"
226 bottom: highlightItem.bottom
227 horizontalCenter: parent.horizontalCenter
230 height: units.gu(4.5)
231 width: parent.width - anchors.margins * 2
232 opacity: userList.movingInternally ? 0 : 1
234 property string promptText
235 placeholderText: root.wasPrompted ? promptText
236 : (root.locked ? i18n.tr("Retry")
237 : i18n.tr("Tap to unlock"))
239 Behavior on opacity {
240 NumberAnimation { duration: 100 }
246 root.focus = true; // so that it can handle Escape presses for us
248 root.responded(text);
250 Keys.onEscapePressed: {
251 root.selected(currentIndex);
257 rightMargin: units.gu(2)
258 verticalCenter: parent.verticalCenter
260 visible: !root.wasPrompted
261 source: "graphics/icon_arrow.png"
264 WrongPasswordAnimation {
265 id: wrongPasswordAnimation
266 target: passwordInput
270 target: Qt.inputMethod
272 if (!Qt.inputMethod.visible) {
273 passwordInput.focus = false;
281 id: passwordMouseArea
282 objectName: "passwordMouseArea"
283 anchors.fill: passwordInput
284 enabled: !root.wasPrompted
285 onClicked: root.tryToUnlock()
288 function resetAuthentication() {
289 if (!userList.currentItem) {
293 passwordInput.promptText = "";
294 passwordInput.text = "";
295 passwordInput.focus = false;
296 passwordInput.enabled = true;
297 root.wasPrompted = false;