By using this site, you agree to have cookies stored on your device, strictly for functional purposes, such as storing your session and preferences.

Dismiss

 __init__.py

View raw Download
text/plain • 9.39 kiB
Python script, ASCII text executable
        
            
1
"""
2
D-Bus notifier applet for the Panorama panel.
3
Copyright 2025, roundabout-host.com <vlad@roundabout-host.com>
4
5
This program is free software: you can redistribute it and/or modify
6
it under the terms of the GNU General Public Licence as published by
7
the Free Software Foundation, either version 3 of the Licence, or
8
(at your option) any later version.
9
10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
GNU General Public Licence for more details.
14
15
You should have received a copy of the GNU General Public Licence
16
along with this program. If not, see <https://www.gnu.org/licenses/>.
17
"""
18
19
from __future__ import annotations
20
21
import panorama_panel
22
import locale
23
from pathlib import Path
24
from pydbus import SessionBus
25
26
import gi
27
gi.require_version("Gtk", "4.0")
28
29
from gi.repository import Gtk, Gdk, GLib
30
31
32
module_directory = Path(__file__).resolve().parent
33
34
locale.bindtextdomain("panorama-app-menu", module_directory / "locale")
35
_ = lambda x: locale.dgettext("panorama-app-menu", x)
36
37
38
custom_css = """
39
.notification-button {
40
padding: 0;
41
}
42
43
.panorama-panel-notifier-summary {
44
font-weight: 600;
45
}
46
"""
47
48
css_provider = Gtk.CssProvider()
49
css_provider.load_from_data(custom_css)
50
Gtk.StyleContext.add_provider_for_display(
51
Gdk.Display.get_default(),
52
css_provider,
53
100
54
)
55
56
57
class NotificationsService:
58
"""
59
<node>
60
<interface name="org.freedesktop.Notifications">
61
<method name="GetCapabilities">
62
<arg type="as" name="caps" direction="out"/>
63
</method>
64
<method name="GetServerInformation">
65
<arg type="s" name="name" direction="out"/>
66
<arg type="s" name="vendor" direction="out"/>
67
<arg type="s" name="version" direction="out"/>
68
<arg type="s" name="spec_version" direction="out"/>
69
</method>
70
<method name="Notify">
71
<arg type="s" name="app_name" direction="in"/>
72
<arg type="u" name="replaces_id" direction="in"/>
73
<arg type="s" name="app_icon" direction="in"/>
74
<arg type="s" name="summary" direction="in"/>
75
<arg type="s" name="body" direction="in"/>
76
<arg type="as" name="actions" direction="in"/>
77
<arg type="a{sv}" name="hints" direction="in"/>
78
<arg type="i" name="expire_timeout" direction="in"/>
79
<arg type="u" name="id" direction="out"/>
80
</method>
81
<method name="CloseNotification">
82
<arg type="u" name="id" direction="in"/>
83
</method>
84
</interface>
85
</node>
86
"""
87
88
def __init__(self, applet: NotifierApplet):
89
self._next_id = 1
90
self.applet = applet
91
92
def GetCapabilities(self):
93
return ["body"]
94
95
def GetServerInformation(self):
96
# name, vendor, version, spec_version
97
return "panorama-panel", "panorama", "0.1", "1.2"
98
99
def Notify(self, *args):
100
notification_id = self._next_id
101
self._next_id += 1
102
self.applet.push_notification(*args, notification_id)
103
return notification_id
104
105
def CloseNotification(self, notification_id):
106
if notification_id in self.applet.notifications_by_id:
107
self.applet.notification_list.remove(self.applet.notifications_by_id[notification_id])
108
self.applet.notification_count_label.set_text(locale.format_string('%d', self.applet.notification_count - 1))
109
self.applet.notification_count -= 1
110
111
112
class BriefNotification(Gtk.Box):
113
def __init__(self, app_name, replaces_id, app_icon, summary, body, actions, hints, expire_timeout):
114
Gtk.Box.__init__(self, orientation=Gtk.Orientation.HORIZONTAL, spacing=4)
115
if not app_icon:
116
app_icon = "preferences-desktop-notifications"
117
self.image = Gtk.Image.new_from_icon_name(app_icon)
118
self.append(self.image)
119
self.append(Gtk.Label.new(summary))
120
self.set_halign(Gtk.Align.CENTER)
121
122
123
class DetailedNotification(Gtk.ListBoxRow):
124
def __init__(self, applet, app_name, replaces_id, app_icon, summary, body, actions, hints, expire_timeout):
125
Gtk.ListBoxRow.__init__(self)
126
self.applet = applet
127
if not app_icon:
128
app_icon = "preferences-desktop-notifications"
129
130
self.box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=8)
131
self.set_child(self.box)
132
133
self.icon = Gtk.Image.new_from_icon_name(app_icon)
134
self.icon.set_icon_size(Gtk.IconSize.LARGE)
135
self.box.append(self.icon)
136
137
self.text_area = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=4)
138
139
self.summary = Gtk.Label.new(summary)
140
self.summary.set_halign(Gtk.Align.START)
141
self.summary.set_xalign(0)
142
self.summary.add_css_class("panorama-panel-notifier-summary")
143
self.text_area.append(self.summary)
144
145
self.body = Gtk.Label.new(body)
146
self.body.set_halign(Gtk.Align.START)
147
self.body.set_xalign(0)
148
self.summary.add_css_class("panorama-panel-notifier-body")
149
self.text_area.append(self.body)
150
151
# TODO: add actions
152
153
self.box.append(self.text_area)
154
155
self.close_button = Gtk.Button(has_frame=False, child=Gtk.Image.new_from_icon_name("window-close"))
156
self.box.append(self.close_button)
157
self.close_button.connect("clicked", self.delete)
158
159
def delete(self, button):
160
self.get_parent().remove(self)
161
self.applet.notification_count_label.set_text(locale.format_string('%d', self.applet.notification_count - 1))
162
self.applet.notification_count -= 1
163
164
165
class NotifierApplet(panorama_panel.Applet):
166
name = _("Notification centre")
167
description = _("Get desktop notifications")
168
169
def __init__(self, orientation=Gtk.Orientation.HORIZONTAL, config=None):
170
super().__init__(orientation=orientation, config=config)
171
if config is None:
172
config = {}
173
174
self.icon_size = config.get("icon_size", 24)
175
176
self.button = Gtk.MenuButton()
177
self.button.set_has_frame(False)
178
self.button.add_css_class("notification-button")
179
self.stack = Gtk.Stack()
180
self.button.set_child(self.stack)
181
self.notification_indicator = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=4)
182
self.status_icon = Gtk.Image.new_from_icon_name("emblem-important-symbolic")
183
self.status_icon.set_pixel_size(config.get("empty_icon_size", 16))
184
self.notification_indicator.append(self.status_icon)
185
self.notification_indicator.set_halign(Gtk.Align.CENTER)
186
self.notification_count_label = Gtk.Label.new(locale.format_string("%d", 0))
187
self.notification_count = 0
188
self.notification_indicator.append(self.notification_count_label)
189
self.stack.add_child(self.notification_indicator)
190
self.stack.set_transition_type(Gtk.StackTransitionType.SLIDE_UP_DOWN)
191
self.stack.set_transition_duration(500)
192
self.popover = Gtk.Popover()
193
self.notification_list = Gtk.ListBox(selection_mode=Gtk.SelectionMode.NONE)
194
self.popover.set_child(self.notification_list)
195
self.button.set_popover(self.popover)
196
# TODO: support the other parameters; add a popover with a history
197
self.append(self.button)
198
199
self.notifications_by_id = {}
200
self.service = NotificationsService(self)
201
bus = SessionBus()
202
self.publishing = bus.publish("org.freedesktop.Notifications", self.service)
203
204
def push_notification(self, app_name, replaces_id, app_icon, summary, body, actions, hints, expire_timeout, notification_id):
205
new_child = BriefNotification(app_name, replaces_id, app_icon, summary, body, actions, hints, expire_timeout)
206
new_child.image.set_pixel_size(self.icon_size)
207
self.stack.add_child(new_child)
208
self.button.set_has_frame(True)
209
self.stack.set_visible_child(new_child)
210
def remove_notification():
211
def remove_child():
212
self.stack.remove(new_child)
213
214
if self.stack.get_visible_child() == new_child:
215
self.stack.set_visible_child(self.stack.get_visible_child().get_prev_sibling())
216
GLib.timeout_add(self.stack.get_transition_duration(), remove_child)
217
if self.stack.get_visible_child() is self.notification_indicator:
218
self.button.set_has_frame(False)
219
else:
220
# TODO: split the time
221
def readd_timeout(*args):
222
if new_child == self.stack.get_visible_child():
223
GLib.timeout_add(self.stack.get_transition_duration(), remove_notification)
224
self.stack.connect("notify::visible-child", readd_timeout)
225
return False
226
if expire_timeout <= 0:
227
expire_timeout = 2500
228
GLib.timeout_add(expire_timeout, remove_notification)
229
230
notification_info = DetailedNotification(self, app_name, replaces_id, app_icon, summary, body, actions, hints, expire_timeout)
231
self.notifications_by_id[notification_id] = notification_info
232
self.notification_list.append(notification_info)
233
self.notification_count_label.set_text(locale.format_string('%d', self.notification_count + 1))
234
self.notification_count += 1
235
# TODO: notify that it was closed
236
237
def get_config(self):
238
return {
239
"icon_size": self.icon_size,
240
"empty_icon_size": self.status_icon.get_pixel_size(),
241
}
242
243
def shutdown(self, app):
244
self.publishing.unpublish()
245