summaryrefslogtreecommitdiff
path: root/modules/lock/Input.qml
blob: 1e164b8affd5e5f5fd7a1cf5c1b7c86527694334 (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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import qs.components
import qs.components.images
import qs.services
import qs.config
import qs.utils
import Quickshell
import Quickshell.Wayland
import Quickshell.Services.Pam
import QtQuick
import QtQuick.Layouts

ColumnLayout {
    id: root

    required property var lock

    property string passwordBuffer

    Layout.preferredWidth: Config.lock.sizes.faceSize * 2
    Layout.fillWidth: false
    spacing: Appearance.spacing.large * 2

    StyledRect {
        Layout.fillWidth: true
        implicitHeight: user.implicitHeight + Appearance.padding.small * 2

        color: Colours.tPalette.m3surfaceContainer
        radius: Appearance.rounding.small

        RowLayout {
            id: user

            anchors.centerIn: parent

            spacing: Appearance.spacing.normal

            MaterialIcon {
                text: "account_circle"
                font.pointSize: Appearance.font.size.large * 1.4
                font.weight: 500
            }

            StyledText {
                // Layout.fillWidth: true
                text: Quickshell.env("USER")
                font.pointSize: Appearance.font.size.large
                // font.capitalization: Font.Capitalize
                font.weight: 500
                elide: Text.ElideRight
            }
        }
    }

    StyledRect {
        Layout.fillWidth: true
        Layout.preferredWidth: charList.implicitWidth + Appearance.padding.large * 2
        Layout.preferredHeight: Appearance.font.size.normal + Appearance.padding.large * 2

        focus: true
        color: Colours.tPalette.m3surfaceContainer
        radius: Appearance.rounding.small
        clip: true

        onFocusChanged: {
            if (!focus)
                focus = true;
        }

        Keys.onPressed: event => {
            if (pam.active)
                return;

            if (event.key === Qt.Key_Enter || event.key === Qt.Key_Return) {
                placeholder.animate = false;
                pam.start();
            } else if (event.key === Qt.Key_Backspace) {
                if (event.modifiers & Qt.ControlModifier) {
                    charList.implicitWidth = charList.implicitWidth; // Break binding
                    root.passwordBuffer = "";
                } else {
                    root.passwordBuffer = root.passwordBuffer.slice(0, -1);
                }
            } else if (" abcdefghijklmnopqrstuvwxyz1234567890`~!@#$%^&*()-_=+[{]}\\|;:'\",<.>/?".includes(event.text.toLowerCase())) {
                charList.bindImWidth();
                root.passwordBuffer += event.text;
            }
        }

        PamContext {
            id: pam

            onResponseRequiredChanged: {
                if (!responseRequired)
                    return;

                respond(root.passwordBuffer);
                charList.implicitWidth = charList.implicitWidth; // Break binding
                root.passwordBuffer = "";
                placeholder.animate = true;
            }

            onCompleted: res => {
                if (res === PamResult.Success)
                    return root.lock.lock.unlock();

                if (res === PamResult.Error)
                    placeholder.pamState = "error";
                else if (res === PamResult.MaxTries)
                    placeholder.pamState = "max";
                else if (res === PamResult.Failed)
                    placeholder.pamState = "fail";

                placeholderDelay.restart();
            }
        }

        Timer {
            id: placeholderDelay

            interval: 3000
            onTriggered: placeholder.pamState = ""
        }

        StyledText {
            id: placeholder

            property string pamState

            anchors.centerIn: parent

            text: {
                if (pam.active)
                    return qsTr("Loading...");
                if (pamState === "error")
                    return qsTr("An error occured");
                if (pamState === "max")
                    return qsTr("You have reached the maximum number of tries");
                if (pamState === "fail")
                    return qsTr("Incorrect password");
                return qsTr("Enter your password");
            }

            animate: true
            color: pam.active ? Colours.palette.m3secondary : pamState ? Colours.palette.m3error : Colours.palette.m3outline
            font.pointSize: Appearance.font.size.larger

            opacity: root.passwordBuffer ? 0 : 1

            Behavior on opacity {
                Anim {}
            }
        }

        ListView {
            id: charList

            function bindImWidth(): void {
                imWidthBehavior.enabled = false;
                implicitWidth = Qt.binding(() => Math.min(count * (Appearance.font.size.normal + spacing) - spacing, Config.lock.sizes.inputWidth - Appearance.rounding.large * 2 - Appearance.padding.large * 5));
                imWidthBehavior.enabled = true;
            }

            anchors.centerIn: parent

            implicitWidth: Math.min(count * (Appearance.font.size.normal + spacing) - spacing, Config.lock.sizes.inputWidth - Appearance.rounding.large * 2 - Appearance.padding.large * 5)
            implicitHeight: Appearance.font.size.normal

            orientation: Qt.Horizontal
            spacing: Appearance.spacing.small / 2
            interactive: false

            model: ScriptModel {
                values: root.passwordBuffer.split("")
            }

            delegate: StyledRect {
                id: ch

                implicitWidth: Appearance.font.size.normal
                implicitHeight: Appearance.font.size.normal

                color: Colours.palette.m3onSurface
                radius: Appearance.rounding.full

                opacity: 0
                scale: 0.5
                Component.onCompleted: {
                    opacity = 1;
                    scale = 1;
                }
                ListView.onRemove: removeAnim.start()

                SequentialAnimation {
                    id: removeAnim

                    PropertyAction {
                        target: ch
                        property: "ListView.delayRemove"
                        value: true
                    }
                    ParallelAnimation {
                        Anim {
                            target: ch
                            property: "opacity"
                            to: 0
                        }
                        Anim {
                            target: ch
                            property: "scale"
                            to: 0.5
                        }
                    }
                    PropertyAction {
                        target: ch
                        property: "ListView.delayRemove"
                        value: false
                    }
                }

                Behavior on opacity {
                    Anim {}
                }

                Behavior on scale {
                    Anim {}
                }
            }

            Behavior on implicitWidth {
                id: imWidthBehavior

                Anim {}
            }
        }
    }

    component Anim: NumberAnimation {
        duration: Appearance.anim.durations.normal
        easing.type: Easing.BezierSpline
        easing.bezierCurve: Appearance.anim.curves.standard
    }
}