-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtray_icon.py
More file actions
67 lines (56 loc) · 2.16 KB
/
tray_icon.py
File metadata and controls
67 lines (56 loc) · 2.16 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
"""System tray icon for Writher with Pandora Blackboard eyes."""
import pystray
import locales
from brand import make_tray_icon
class TrayIcon:
def __init__(self, on_quit, on_show_notes=None, on_show_settings=None):
self._on_quit = on_quit
self._on_show_notes = on_show_notes
self._on_show_settings = on_show_settings
self._icon = None
def _build_menu(self):
items = [
pystray.MenuItem("Writher", None, enabled=False),
pystray.Menu.SEPARATOR,
]
if self._on_show_notes:
items.append(pystray.MenuItem(locales.get("tray_notes_agenda"),
self._show_notes))
if self._on_show_settings:
items.append(pystray.MenuItem(locales.get("tray_settings"),
self._show_settings))
if self._on_show_notes or self._on_show_settings:
items.append(pystray.Menu.SEPARATOR)
items.append(pystray.MenuItem(locales.get("tray_quit"), self._quit))
return pystray.Menu(*items)
def _show_notes(self, icon, item):
if self._on_show_notes:
self._on_show_notes()
def _show_settings(self, icon, item):
if self._on_show_settings:
self._on_show_settings()
def _quit(self, icon, item):
icon.stop()
self._on_quit()
def start(self):
img = make_tray_icon(recording=False)
self._icon = pystray.Icon(
"Writher",
img,
locales.get("tray_idle"),
menu=self._build_menu(),
)
self._icon.run_detached()
def set_recording(self, recording: bool):
if self._icon is None:
return
self._icon.icon = make_tray_icon(recording=recording)
self._icon.title = (locales.get("tray_recording") if recording
else locales.get("tray_idle"))
def set_tooltip(self, text: str):
"""Update the tray icon tooltip text (used for status warnings)."""
if self._icon is not None:
self._icon.title = text
def stop(self):
if self._icon is not None:
self._icon.stop()