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