Implement a dynamic tray icon status picture

This commit is contained in:
DevilXD committed 2024-07-01 21:20:27 +02:00
1 parent 14a9154366
commit 35c46c2b78
10 files changed
+35 -8

No files matched your search

+21 -4
View File
@@ -1044,13 +1044,21 @@ class TrayIcon:
def __init__(self, manager: GUIManager, master: ttk.Widget):
self._manager = manager
self.icon: pystray.Icon | None = None
self.icon_image = Image_module.open(resource_path("pickaxe.ico"))
self._icon_images: dict[str, Image_module.Image] = {
"pickaxe": Image_module.open(resource_path("icons/pickaxe.ico")),
"active": Image_module.open(resource_path("icons/active.ico")),
"idle": Image_module.open(resource_path("icons/idle.ico")),
"error": Image_module.open(resource_path("icons/error.ico")),
"maint": Image_module.open(resource_path("icons/maint.ico")),
}
self._icon_state: str = "pickaxe"
self._button = ttk.Button(master, command=self.minimize, text=_("gui", "tray", "minimize"))
self._button.grid(column=0, row=0, sticky="ne")
def __del__(self) -> None:
self.stop()
self.icon_image.close()
for icon_image in self._icon_images.values():
icon_image.close()
def _shorten(self, text: str, by_len: int, min_len: int) -> str:
if (text_len := len(text)) <= min_len + 3 or by_len <= 0:
@@ -1096,7 +1104,9 @@ class TrayIcon:
pystray.Menu.SEPARATOR,
pystray.MenuItem(_("gui", "tray", "quit"), bridge(self.quit)),
)
self.icon = pystray.Icon("twitch_miner", self.icon_image, self.get_title(drop), menu)
self.icon = pystray.Icon(
"twitch_miner", self._icon_images[self._icon_state], self.get_title(drop), menu
)
# self.icon.run_detached()
loop.run_in_executor(None, self.icon.run)
@@ -1142,6 +1152,13 @@ class TrayIcon:
if self.icon is not None:
self.icon.title = self.get_title(drop)
def change_icon(self, state: str):
if state not in self._icon_images:
raise ValueError("Invalid icon state")
self._icon_state = state
if self.icon is not None:
self.icon.icon = self._icon_images[state]
class Notebook:
def __init__(self, manager: GUIManager, master: ttk.Widget):
@@ -1937,7 +1954,7 @@ class GUIManager:
# withdraw immediately to prevent the window from flashing
self._root.withdraw()
# root.resizable(False, True)
set_root_icon(root, resource_path("pickaxe.ico"))
set_root_icon(root, resource_path("icons/pickaxe.ico"))
root.title(WINDOW_TITLE) # window title
root.bind_all("<KeyPress-Escape>", self.unfocus) # pressing ESC unfocuses selection
# Image cache for displaying images