forked from KlipperScreen/KlipperScreen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotifications.py
77 lines (62 loc) · 2.34 KB
/
notifications.py
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
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
from ks_includes.screen_panel import ScreenPanel
COLORS = {
"time": "DarkGrey",
"info": "Silver",
"warning": "DarkOrange",
"error": "FireBrick",
}
def remove_newlines(msg: str) -> str:
return msg.replace('\n', ' ')
class Panel(ScreenPanel):
def __init__(self, screen, title):
title = title or _("Notifications")
super().__init__(screen, title)
self.empty = _("Notification log empty")
self.tb = Gtk.TextBuffer(text=self.empty)
tv = Gtk.TextView(editable=False, cursor_visible=False, wrap_mode=Gtk.WrapMode.WORD_CHAR)
tv.set_buffer(self.tb)
tv.connect("size-allocate", self._autoscroll)
scroll = Gtk.ScrolledWindow(hexpand=True, vexpand=True)
scroll.add(tv)
clear_button = self._gtk.Button("refresh", _('Clear') + " ", None, self.bts, Gtk.PositionType.RIGHT, 1)
clear_button.get_style_context().add_class("buttons_slim")
clear_button.set_vexpand(False)
clear_button.connect("clicked", self.clear)
content_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
content_box.add(clear_button)
content_box.add(scroll)
self.content.add(content_box)
def activate(self):
self.refresh()
def add_notification(self, log):
if log["level"] == 0:
if "error" in log["message"].lower() or "cannot" in log["message"].lower():
color = COLORS["error"]
else:
color = COLORS["info"]
elif log["level"] == 1:
color = COLORS["info"]
elif log["level"] == 2:
color = COLORS["warning"]
else:
color = COLORS["error"]
self.tb.insert_markup(
self.tb.get_end_iter(),
f'\n<span color="{COLORS["time"]}">{log["time"]}</span> '
f'<span color="{color}"><b>{remove_newlines(log["message"])}</b></span>',
-1
)
def clear(self, widget):
self.tb.set_text("")
self._screen.notification_log_clear()
def refresh(self):
self.tb.set_text("")
for log in self._screen.notification_log:
self.add_notification(log)
def process_update(self, action, data):
if action != "notify_log":
return
self.add_notification(data)