Lomiri
PromptList.qml
1 /*
2  * Copyright (C) 2017 Canonical Ltd.
3  *
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.
7  *
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.
12  *
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/>.
15  */
16 
17 import QtQuick 2.4
18 import Lomiri.Components 1.3
19 import "../Components"
20 import "." 0.1
21 
22 FocusScope {
23  id: root
24  height: childrenRect.height
25 
26  property bool alphanumeric: true
27  property bool interactive: true
28  property bool loginError: false
29  property bool hasKeyboard: false
30 
31  signal responded(string text)
32  signal clicked()
33  signal canceled()
34 
35  function showFakePassword() {
36  for (var i = 0; i < repeater.count; i++) {
37  var item = repeater.itemAt(i).item;
38  if (item.isPrompt) {
39  item.showFakePassword();
40  }
41  }
42  }
43 
44  QtObject {
45  id: d
46 
47  function sendResponse() {
48  for (var i = 0; i < repeater.count; i++) {
49  var item = repeater.itemAt(i).item;
50  if (item.isPrompt) {
51  root.responded(item.enteredText);
52  }
53  }
54  }
55  }
56 
57  Column {
58  width: parent.width
59  spacing: units.gu(0.5)
60 
61  Repeater {
62  id: repeater
63  model: LightDMService.prompts
64 
65  delegate: Loader {
66  id: loader
67 
68  readonly property bool isLabel: model.type == LightDMService.prompts.Message ||
69  model.type == LightDMService.prompts.Error
70  readonly property var modelData: model
71 
72  sourceComponent: isLabel ? infoLabel : greeterPrompt
73 
74  onLoaded: {
75  for (var i = 0; i < repeater.count; i++) {
76  var item = repeater.itemAt(i);
77  if (item && !item.isLabel) {
78  item.focus = true;
79  break;
80  }
81  }
82  loader.item.opacity = 1;
83  }
84 
85  Binding {
86  target: loader.item
87  property: "model"
88  value: loader.modelData
89  }
90  }
91  }
92  }
93 
94  Component {
95  id: infoLabel
96 
97  FadingLabel {
98  objectName: "infoLabel" + model.index
99  width: root.width
100 
101  property var model
102  readonly property bool isPrompt: false
103 
104  color: model.type === LightDMService.prompts.Message ? theme.palette.normal.raisedSecondaryText
105  : theme.palette.normal.negative
106  fontSize: "small"
107  textFormat: Text.PlainText
108  text: model.text
109 
110  visible: model.type === LightDMService.prompts.Message
111 
112  Behavior on opacity { LomiriNumberAnimation {} }
113  opacity: 0
114  }
115  }
116 
117  Component {
118  id: greeterPrompt
119 
120  GreeterPrompt {
121  objectName: "greeterPrompt" + model.index
122  width: root.width
123 
124  property var model
125 
126  interactive: root.interactive
127  isAlphanumeric: model.text !== "" || root.alphanumeric
128  isPrompt: model.type !== LightDMService.prompts.Button
129  isSecret: model.type === LightDMService.prompts.Secret
130  loginError: root.loginError
131  hasKeyboard: root.hasKeyboard
132  text: model.text ? model.text : (isAlphanumeric ? i18n.tr("Passphrase") : i18n.tr("Passcode"))
133 
134  onClicked: root.clicked()
135  onAccepted: {
136  // If there is another GreeterPrompt, focus it.
137  for (var i = model.index + 1; i < repeater.count; i++) {
138  var item = repeater.itemAt(i).item;
139  if (item.isPrompt) {
140  item.forceActiveFocus();
141  return;
142  }
143  }
144 
145  // Nope we're the last one; just send our response.
146  d.sendResponse();
147  }
148  onCanceled: root.canceled()
149 
150  Behavior on opacity { LomiriNumberAnimation {} }
151  opacity: 0
152  }
153  }
154 }