aboutsummaryrefslogtreecommitdiffstats
path: root/theme/components/Credentials.qml
blob: f6fa0bf4ba7b08ac6213c4e00f16230f8f90c8b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// SPDX-License-Identifier: GPL-2.0-only
// Copyright (C) 2026 Danilo M. <danix@danix.xyz>
//
// One credential slot, two stages. The username shows first; confirming it
// slides the username out and the password in. Confirming the password emits
// submitted(). A back chevron, or Esc, returns to the username.

import QtQuick
import QtQuick.Controls.Basic
import QtQuick.Effects

Item {
    id: creds

    // Emitted when the password stage is confirmed, and on input so the panel
    // can restart its idle timer.
    signal submitted()
    signal wake()

    property string stage: "user"
    readonly property string username: usernameField.text
    readonly property string password: passwordField.text
    readonly property bool passwordStage: stage === "pass"

    implicitHeight: 44

    function confirm() {
        if (!creds.passwordStage) {
            if (usernameField.text.length > 0)
                creds.stage = "pass"
        } else {
            creds.submitted()
        }
    }

    function clearPassword() {
        passwordField.text = ""
    }

    function focusStage() {
        if (creds.passwordStage)
            passwordField.forceActiveFocus()
        else
            usernameField.forceActiveFocus()
    }

    onStageChanged: focusStage()

    // The greeter creates the QML before it shows the view, so a focus call at
    // component completion lands while the window is still hidden and is
    // dropped. Ask again once the view is on screen; from then on the field's
    // focus binding follows the stage.
    Timer {
        interval: 200
        running: true
        onTriggered: creds.focusStage()
    }

    // Back chevron, password stage only. The width animates so the slot grows
    // and shrinks with it.
    Item {
        id: back
        anchors.left: parent.left
        anchors.verticalCenter: parent.verticalCenter
        width: creds.passwordStage ? 44 : 0
        height: 44
        clip: true
        visible: width > 0
        Behavior on width { NumberAnimation { duration: 220; easing.type: Easing.OutCubic } }

        Rectangle {
            anchors.fill: parent
            radius: 6
            color: config.stringValue("surface0")
            opacity: backMouse.containsMouse ? 1 : 0
        }

        Image {
            id: backIcon
            anchors.centerIn: parent
            width: 22
            height: 22
            source: "../icons/chevron-left.svg"
            sourceSize.width: width
            sourceSize.height: height
            visible: false
        }

        MultiEffect {
            anchors.fill: backIcon
            source: backIcon
            colorization: 1.0
            colorizationColor: backMouse.containsMouse
                ? config.stringValue("accent")
                : config.stringValue("subtext0")
        }

        MouseArea {
            id: backMouse
            anchors.fill: parent
            hoverEnabled: true
            onClicked: {
                creds.clearPassword()
                creds.stage = "user"
            }
        }
    }

    Button {
        id: confirmButton
        anchors.right: parent.right
        anchors.verticalCenter: parent.verticalCenter
        width: 44
        height: 44
        padding: 0
        background: Rectangle {
            radius: 6
            color: config.stringValue("accent")
        }
        contentItem: Item {
            Image {
                id: confirmIcon
                anchors.centerIn: parent
                width: 22
                height: 22
                source: "../icons/chevron-right.svg"
                sourceSize.width: width
                sourceSize.height: height
                visible: false
            }

            MultiEffect {
                anchors.fill: confirmIcon
                source: confirmIcon
                colorization: 1.0
                colorizationColor: config.stringValue("base")
            }
        }
        onClicked: creds.confirm()
    }

    // The sliding slot. Both fields live here, clipped, and slide across.
    Item {
        id: slot
        anchors.left: back.right
        anchors.leftMargin: 8
        anchors.right: confirmButton.left
        anchors.rightMargin: 8
        anchors.verticalCenter: parent.verticalCenter
        height: 44
        clip: true

        TextField {
            id: usernameField
            width: slot.width
            height: slot.height
            text: userModel.lastUser !== undefined ? userModel.lastUser : ""
            placeholderText: "Username"
            focus: !creds.passwordStage
            activeFocusOnTab: !creds.passwordStage
            font.family: "Noto Sans"
            color: config.stringValue("text")
            palette.base: config.stringValue("surface0")
            palette.text: config.stringValue("text")
            palette.placeholderText: config.stringValue("overlay0")
            palette.highlight: config.stringValue("accent")
            palette.highlightedText: config.stringValue("base")
            background: Rectangle {
                radius: 6
                color: config.stringValue("surface0")
            }
            x: creds.passwordStage ? -slot.width : 0
            opacity: creds.passwordStage ? 0 : 1
            Behavior on x { NumberAnimation { duration: 220; easing.type: Easing.OutCubic } }
            Behavior on opacity { NumberAnimation { duration: 220; easing.type: Easing.OutCubic } }
            onAccepted: creds.confirm()
            onTextChanged: creds.wake()
            Keys.onPressed: event => {
                creds.wake()
                event.accepted = false
            }
        }

        TextField {
            id: passwordField
            width: slot.width
            height: slot.height
            echoMode: TextInput.Password
            placeholderText: "Password"
            focus: creds.passwordStage
            activeFocusOnTab: creds.passwordStage
            font.family: "Noto Sans"
            color: config.stringValue("text")
            palette.base: config.stringValue("surface0")
            palette.text: config.stringValue("text")
            palette.placeholderText: config.stringValue("overlay0")
            palette.highlight: config.stringValue("accent")
            palette.highlightedText: config.stringValue("base")
            background: Rectangle {
                radius: 6
                color: config.stringValue("surface0")
            }
            x: creds.passwordStage ? 0 : slot.width
            opacity: creds.passwordStage ? 1 : 0
            Behavior on x { NumberAnimation { duration: 220; easing.type: Easing.OutCubic } }
            Behavior on opacity { NumberAnimation { duration: 220; easing.type: Easing.OutCubic } }
            onAccepted: creds.confirm()
            Keys.onPressed: event => {
                creds.wake()
                if (event.key === Qt.Key_Escape) {
                    creds.clearPassword()
                    creds.stage = "user"
                    event.accepted = true
                } else {
                    event.accepted = false
                }
            }
        }
    }
}