Skip to content

Commit e49e420

Browse files
authored
Merge pull request slgobinath#729 from deltragon/trayicon-no-block
trayicon: do not block main thread with animation
2 parents 80c509e + 688fa5b commit e49e420

File tree

1 file changed

+31
-29
lines changed

1 file changed

+31
-29
lines changed

safeeyes/plugins/trayicon/plugin.py

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
from safeeyes import utility
2727
from safeeyes.translations import translate as _
2828
import threading
29-
import time
3029
import typing
3130

3231
"""
@@ -429,6 +428,9 @@ def set_xayatanalabel(self, label):
429428
class TrayIcon:
430429
"""Create and show the tray icon along with the tray menu."""
431430

431+
_animation_timeout_id: typing.Optional[int] = None
432+
_animation_icon_enabled: bool = False
433+
432434
def __init__(self, context, plugin_config):
433435
self.context = context
434436
self.on_show_settings = context["api"]["show_settings"]
@@ -446,7 +448,6 @@ def __init__(self, context, plugin_config):
446448
self.idle_condition = threading.Condition()
447449
self.lock = threading.Lock()
448450
self.allow_disabling = plugin_config["allow_disabling"]
449-
self.animate = False
450451
self.menu_locked = False
451452

452453
session_bus = Gio.bus_get_sync(Gio.BusType.SESSION)
@@ -785,35 +786,37 @@ def __schedule_resume(self, time_minutes):
785786
if not self.active:
786787
utility.execute_main_thread(self.on_enable_clicked)
787788

788-
def start_animation(self):
789-
if not self.active or not self.animate:
790-
return
791-
utility.execute_main_thread(
792-
lambda: self.sni_service.set_icon("io.github.slgobinath.SafeEyes-disabled")
793-
)
794-
time.sleep(0.5)
795-
utility.execute_main_thread(
796-
lambda: self.sni_service.set_icon("io.github.slgobinath.SafeEyes-enabled")
797-
)
798-
if self.animate and self.active:
799-
time.sleep(0.5)
800-
if self.animate and self.active:
801-
utility.start_thread(self.start_animation)
789+
def start_animation(self) -> None:
790+
if self._animation_timeout_id is not None:
791+
self.stop_animation()
792+
793+
self._animation_icon_enabled = False
794+
795+
self._animation_timeout_id = GLib.timeout_add(500, self._do_animate)
796+
797+
def _do_animate(self) -> bool:
798+
if not self.active:
799+
self._animation_timeout_id = None
800+
return GLib.SOURCE_REMOVE
801+
802+
if self._animation_icon_enabled:
803+
self.sni_service.set_icon("io.github.slgobinath.SafeEyes-enabled")
804+
else:
805+
self.sni_service.set_icon("io.github.slgobinath.SafeEyes-disabled")
806+
807+
self._animation_icon_enabled = not self._animation_icon_enabled
808+
809+
return GLib.SOURCE_CONTINUE
810+
811+
def stop_animation(self) -> None:
812+
if self._animation_timeout_id is not None:
813+
GLib.source_remove(self._animation_timeout_id)
814+
self._animation_timeout_id = None
802815

803-
def stop_animation(self):
804-
self.animate = False
805816
if self.active:
806-
utility.execute_main_thread(
807-
lambda: self.sni_service.set_icon(
808-
"io.github.slgobinath.SafeEyes-enabled"
809-
)
810-
)
817+
self.sni_service.set_icon("io.github.slgobinath.SafeEyes-enabled")
811818
else:
812-
utility.execute_main_thread(
813-
lambda: self.sni_service.set_icon(
814-
"io.github.slgobinath.SafeEyes-disabled"
815-
)
816-
)
819+
self.sni_service.set_icon("io.github.slgobinath.SafeEyes-disabled")
817820

818821

819822
def init(ctx, safeeyes_cfg, plugin_config):
@@ -839,7 +842,6 @@ def on_pre_break(break_obj):
839842
"""Disable the menu if strict_break is enabled."""
840843
if safeeyes_config.get("strict_break"):
841844
tray_icon.lock_menu()
842-
tray_icon.animate = True
843845
tray_icon.start_animation()
844846

845847

0 commit comments

Comments
 (0)