Unity 8
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.12
18 import Ubuntu.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 modelText: model.text
71  readonly property var modelType: model.type
72  readonly property var modelIndex: model.index
73 
74  sourceComponent: isLabel ? infoLabel : greeterPrompt
75 
76  active: root.visible
77 
78  onLoaded: {
79  for (var i = 0; i < repeater.count; i++) {
80  var item = repeater.itemAt(i);
81  if (item && !item.isLabel) {
82  item.focus = true;
83  break;
84  }
85  }
86  loader.item.opacity = 1;
87  }
88  }
89  }
90  }
91 
92  Component {
93  id: infoLabel
94 
95  FadingLabel {
96  objectName: "infoLabel" + modelIndex
97  width: root.width
98 
99  readonly property bool isPrompt: false
100 
101  color: modelType === LightDMService.prompts.Message ? theme.palette.normal.raisedSecondaryText
102  : theme.palette.normal.negative
103  fontSize: "small"
104  textFormat: Text.PlainText
105  text: modelText
106 
107  visible: modelType === LightDMService.prompts.Message
108 
109  Behavior on opacity { UbuntuNumberAnimation {} }
110  opacity: 0
111  }
112  }
113 
114  Component {
115  id: greeterPrompt
116 
117  GreeterPrompt {
118  objectName: "greeterPrompt" + modelIndex
119  width: root.width
120 
121  property bool isAlphanumeric: modelText !== "" || root.alphanumeric
122 
123  interactive: root.interactive
124  isPrompt: modelType !== LightDMService.prompts.Button
125  isSecret: modelType === LightDMService.prompts.Secret
126  isPinPrompt: isPrompt && !isAlphanumeric && isSecret
127  loginError: root.loginError
128  hasKeyboard: root.hasKeyboard
129  text: modelText ? modelText : (isAlphanumeric ? i18n.tr("Passphrase") : i18n.tr("Passcode"))
130 
131  onClicked: root.clicked()
132  onAccepted: {
133  // If there is another GreeterPrompt, focus it.
134  for (var i = modelIndex + 1; i < repeater.count; i++) {
135  var item = repeater.itemAt(i).item;
136  if (item.isPrompt) {
137  item.forceActiveFocus();
138  return;
139  }
140  }
141 
142  // Nope we're the last one; just send our response.
143  d.sendResponse();
144  }
145  onCanceled: root.canceled()
146 
147  Behavior on opacity { UbuntuNumberAnimation {} }
148  opacity: 0
149  }
150  }
151 }