Unity 8
GreeterPrompt.qml
1 /*
2  * Copyright (C) 2016 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 
20 FocusScope {
21  id: root
22  implicitHeight: units.gu(5)
23  focus: true
24 
25  property bool isPrompt
26  property bool isAlphanumeric
27  property string text
28  property bool isSecret
29 
30  signal clicked()
31  signal canceled()
32  signal responded(string text)
33 
34  function reset() {
35  passwordInput.text = "";
36  d.enabled = true;
37  }
38 
39  StyledItem {
40  id: d
41 
42  property bool enabled: true
43  readonly property color textColor: passwordInput.enabled ? theme.palette.normal.raisedText
44  : theme.palette.disabled.raisedText
45  readonly property color selectedColor: passwordInput.enabled ? theme.palette.normal.raised
46  : theme.palette.disabled.raised
47  readonly property color drawColor: passwordInput.enabled ? theme.palette.normal.raisedSecondaryText
48  : theme.palette.disabled.raisedSecondaryText
49  readonly property color errorColor: passwordInput.enabled ? theme.palette.normal.negative
50  : theme.palette.disabled.negative
51  }
52 
53  Rectangle {
54  anchors.fill: parent
55  border.width: units.dp(1)
56  border.color: d.drawColor
57  radius: units.gu(0.5)
58  color: "transparent"
59  }
60 
61  AbstractButton {
62  objectName: "promptButton"
63  anchors.fill: parent
64  visible: !root.isPrompt
65  enabled: d.enabled
66  focus: visible
67 
68  onClicked: root.clicked()
69 
70  Label {
71  anchors.centerIn: parent
72  color: d.textColor
73  text: root.text
74  }
75  }
76 
77  TextField {
78  id: passwordInput
79  objectName: "promptField"
80  anchors.fill: parent
81  visible: root.isPrompt
82  enabled: d.enabled
83  focus: visible
84 
85  inputMethodHints: root.isAlphanumeric ? Qt.ImhNone : Qt.ImhDigitsOnly
86  echoMode: root.isSecret ? TextInput.Password : TextInput.Normal
87  hasClearButton: false
88 
89  readonly property real frameSpacing: units.gu(0.5)
90 
91  style: Item {
92  property color color: d.textColor
93  property color selectedTextColor: d.selectedColor
94  property color selectionColor: d.textColor
95  property color borderColor: "transparent"
96  property color backgroundColor: "transparent"
97  property color errorColor: d.errorColor
98  property real frameSpacing: passwordInput.frameSpacing
99  anchors.fill: parent
100  }
101 
102  secondaryItem: [
103  Icon {
104  id: capsIcon
105  name: "keyboard-caps-enabled"
106  height: units.gu(3)
107  width: units.gu(3)
108  color: d.textColor
109  visible: root.isSecret && false // TODO: detect when caps lock is on
110  }
111  ]
112 
113  onAccepted: {
114  if (!enabled)
115  return;
116  d.enabled = false;
117  root.responded(text);
118  }
119 
120  Keys.onEscapePressed: root.canceled()
121 
122  // We use our own custom placeholder label instead of the standard
123  // TextField one because the standard one hardcodes baseText as the
124  // palette color, whereas we want raisedSecondaryText.
125  Label {
126  id: hint
127  anchors {
128  left: parent.left
129  right: parent.right
130  verticalCenter: parent.verticalCenter
131  leftMargin: units.gu(1.5)
132  rightMargin: anchors.leftMargin +
133  (capsIcon.visible ? capsIcon.width + passwordInput.frameSpacing
134  : 0)
135  }
136  text: root.text
137  visible: passwordInput.text == "" && !passwordInput.inputMethodComposing
138  color: d.drawColor
139  elide: Text.ElideRight
140  }
141  }
142 }