-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotificationdaemon.py
More file actions
188 lines (168 loc) · 6.03 KB
/
Copy pathnotificationdaemon.py
File metadata and controls
188 lines (168 loc) · 6.03 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
from typing import cast
from fabric import Application
from fabric.widgets.box import Box
from fabric.widgets.label import Label
from fabric.widgets.image import Image
from fabric.widgets.button import Button
from fabric.widgets.wayland import WaylandWindow
from fabric.notifications import Notifications, Notification
from fabric.utils import invoke_repeater, get_relative_path
from playsound import playsound
from gi.repository import GdkPixbuf
import os
import threading
NOTIFICATION_WIDTH = 360
NOTIFICATION_IMAGE_SIZE = 64
class NotificationWidget(Box):
def __init__(self, notification: Notification, **kwargs):
super().__init__(
size=(NOTIFICATION_WIDTH, -1),
name="notification",
spacing=8,
orientation="v",
**kwargs,
)
self._notification = notification
body_container = Box(spacing=4, orientation="h")
if image_pixbuf := self._notification.image_pixbuf:
body_container.add(
Image(
pixbuf=image_pixbuf.scale_simple(
NOTIFICATION_IMAGE_SIZE,
NOTIFICATION_IMAGE_SIZE,
GdkPixbuf.InterpType.BILINEAR,
)
)
)
if app_icon := self._notification.app_icon:
if os.path.exists(app_icon):
body_container.add(
Image(
image_file=app_icon,
size=NOTIFICATION_IMAGE_SIZE
)
)
else:
body_container.add(
Image(
icon_name=app_icon,
size=NOTIFICATION_IMAGE_SIZE
)
)
body_container.add(
Box(
spacing=4,
orientation="v",
children=[
# a box for holding both the "summary" label and the "close" button
Box(
orientation="h",
children=[
Label(
label=self._notification.summary,
ellipsization="middle",
)
.build()
.add_style_class("summary")
.unwrap(),
],
h_expand=True,
v_expand=True,
)
# add the "close" button
.build(
lambda box, _: box.pack_end(
Button(
image=Image(
icon_name="window-close",
icon_size=14,
),
v_align="center",
h_align="end",
on_clicked=lambda *_: self._notification.close(),
),
False,
False,
0,
)
),
Label(
label=self._notification.body,
line_wrap="word-char",
v_align="start",
h_align="start",
)
.build()
.add_style_class("body")
.unwrap(),
],
h_expand=True,
v_expand=True,
)
)
self.add(body_container)
if actions := self._notification.actions:
self.add(
Box(
spacing=4,
orientation="h",
children=[
Button(
h_expand=True,
v_expand=True,
label=action.label,
on_clicked=lambda *_, action=action: action.invoke(),
)
for action in actions
],
)
)
# destroy this widget once the notification is closed
self._notification.connect(
"closed",
lambda *_: (
parent.remove(self) if (parent := self.get_parent()) else None, # type: ignore
self.destroy(),
),
)
# automatically close the notification after the timeout period
invoke_repeater(
self._notification.timeout if self._notification.timeout > 0 else 3000,
lambda: self._notification.close("expired"),
initial_call=False,
)
def play_click():
playsound("/usr/share/sounds/gnome/default/alerts/click.ogg")
if __name__ == "__main__":
dir_path = os.path.dirname(os.path.realpath(__file__))
os.chdir(dir_path)
app = Application(
"notifications",
WaylandWindow(
margin="8px 8px 8px 8px",
anchor="top right",
child=Box(
size=2, # so it's not ignored by the compositor
spacing=4,
orientation="v",
).build(
lambda viewport, _: Notifications(
on_notification_added=lambda notifs_service, nid: (
threading.Thread(target=play_click, daemon=True).start(),
viewport.add(
NotificationWidget(
cast(
Notification,
notifs_service.get_notification_from_id(nid),
)
)
)
)
)
),
visible=True,
all_visible=True,
),
)
app.set_stylesheet_from_file(get_relative_path("./style.css"))
app.run()