-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathlock.py
More file actions
234 lines (205 loc) · 7.14 KB
/
lock.py
File metadata and controls
234 lines (205 loc) · 7.14 KB
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
from widgets.circle_image import CircleImage as Image
from modules.panel.components.indicators import (
BatteryIndicator,
BluetoothIndicator,
NetworkIndicator,
)
from gi.repository import (
Gdk, # pyright: ignore[reportMissingModuleSource]
GLib,
GtkSessionLock, # pyright: ignore[reportAttributeAccessIssue]
)
from fabric.widgets.window import Window
from fabric.widgets.label import Label
from fabric.widgets.entry import Entry
from fabric.widgets.datetime import DateTime
from fabric.widgets.centerbox import CenterBox
from fabric.widgets.box import Box
from fabric.utils import get_relative_path
from fabric import Application
import os
import getpass
import setproctitle
import gi
import pam
gi.require_version("Gdk", "3.0")
gi.require_version("Gtk", "3.0")
gi.require_version("GtkSessionLock", "0.1")
# from fabric.widgets.image import Image
# from widgets.wayland import WaylandWindow as Window
class IndicatorBox(Box):
def __init__(self, *args, **kwargs):
super().__init__(
h_align="end",
name="indicator-box",
spacing=5,
h_expand=True,
children=[
BatteryIndicator(show_window=False),
BluetoothIndicator(show_window=False),
NetworkIndicator(show_window=False),
],
)
class ContentBox(CenterBox):
def __init__(self, on_activate, *args, **kwargs):
self.password_entry = Entry(
placeholder="Enter Password",
name="password-entry",
h_align="center",
v_align="center",
visible=False,
password=True,
on_activate=on_activate,
)
face_icon = os.path.expanduser("~/.face.icon")
if not os.path.exists(face_icon):
face_icon = get_relative_path("./assets/default.png")
self.password_entry.set_property("xalign", 0.5)
self.username_label = Label(
label=f"{getpass.getuser().title()}",
name="username",
visible=True,
h_align="center",
v_align="center",
)
self.unlock_text = Label(
label="Touch ID or Enter Password",
name="unlock-text",
)
super().__init__(
name="content-box",
h_expand=True,
orientation="vertical",
v_expand=True,
start_children=[
IndicatorBox(),
DateTime(
formatters=["%A,%B %-d"],
interval=10000,
h_expand=False,
v_align="start",
v_expand=False,
name="lock-date",
),
DateTime(formatters=["%I:%M"], name="lock-clock"),
],
end_children=[
Box(
name="profile-box",
h_align="center",
h_expand=True,
v_expand=True,
v_align="end",
children=[
Image(
name="face-icon",
image_file=face_icon,
size=64,
),
],
),
Box(
name="container-box",
orientation="v",
v_align="end",
h_align="center",
h_expand=True,
v_expand=True,
children=[
self.username_label,
self.password_entry,
],
),
Box(
name="unlock-box",
v_align="end",
h_align="center",
h_expand=True,
v_expand=True,
children=[
self.unlock_text,
],
),
],
**kwargs,
)
class LockScreen(Window):
def __init__(self, lock: GtkSessionLock.Lock):
self._hide_timeout_id = None # prevent AttributeError
self.lock = lock
self.content = ContentBox(self.on_activate)
super().__init__(
title="lock",
visible=False,
all_visible=False,
name="lockscreen-bg",
anchor="center",
child=self.content,
)
self.content.password_entry.set_visible(False)
self.connect("key-press-event", self._on_keypress)
bg = os.path.expanduser("~/.current.wall")
if not os.path.exists(bg):
bg = get_relative_path("./assets/wallpapers_example/example-1.png")
self.set_style(f"background-image: url('{bg}');")
def _on_keypress(self, widget, event):
keyval = event.keyval
# ESC pressed → hide entry immediately
if keyval == Gdk.KEY_Escape and self.content.password_entry.get_visible():
self._hide_entry()
return
# Show entry if hidden
if not self.content.password_entry.get_visible():
self.content.username_label.set_visible(False)
self.content.password_entry.set_visible(True)
self.content.password_entry.grab_focus()
self._start_hide_timer()
else:
# Reset timer if already visible
self._restart_hide_timer()
def _start_hide_timer(self):
self._stop_hide_timer() # just in case
self._hide_timeout_id = GLib.timeout_add_seconds(5, self._hide_entry)
# 10 seconds of inactivity before hiding
def _restart_hide_timer(self):
self._start_hide_timer()
def _stop_hide_timer(self):
if self._hide_timeout_id:
GLib.source_remove(self._hide_timeout_id)
self._hide_timeout_id = None
def _hide_entry(self):
self._stop_hide_timer()
self.content.password_entry.set_visible(False)
self.content.username_label.set_visible(True)
return False # stop timeout
def on_activate(self, entry: Entry, *args):
if not pam.authenticate(getpass.getuser(), (entry.get_text() or "").strip()):
entry.set_text("")
entry.set_placeholder_text("Wrong Password")
return
self.lock.unlock_and_destroy()
self.destroy()
GLib.idle_add(app.quit) # schedules quit after unlock
def initialize():
lock = GtkSessionLock.prepare_lock()
lock.lock_lock()
lockscreen = LockScreen(lock)
lock.new_surface(
lockscreen,
Gdk.Display.get_default().get_monitor( # pyright: ignore[reportAttributeAccessIssue, reportOptionalMemberAccess]
0
),
)
lockscreen.show()
if __name__ == "__main__":
setproctitle.setproctitle("lockscreen")
initialize()
lockscreen = LockScreen(GtkSessionLock.Lock())
app = Application("lock", lockscreen)
def set_css():
app.set_stylesheet_from_file(
get_relative_path("main.css"),
)
app.set_css = set_css # pyright: ignore[reportAttributeAccessIssue]
app.set_css() # pyright: ignore[reportAttributeAccessIssue]
app.run()