Skip to content

Commit 3a626cb

Browse files
committed
trayicon: switch from thread to timer
1 parent 1a08010 commit 3a626cb

File tree

1 file changed

+23
-27
lines changed

1 file changed

+23
-27
lines changed

safeeyes/plugins/trayicon/plugin.py

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

3231
"""
@@ -440,6 +439,8 @@ class TrayIcon:
440439
_animation_timeout_id: typing.Optional[int] = None
441440
_animation_icon_enabled: bool = False
442441

442+
_resume_timeout_id: typing.Optional[int] = None
443+
443444
def __init__(self, context: Context, plugin_config):
444445
self.context = context
445446
self.on_show_settings = context.api.show_settings
@@ -454,8 +455,6 @@ def __init__(self, context: Context, plugin_config):
454455
self.date_time = None
455456
self.active = True
456457
self.wakeup_time = None
457-
self.idle_condition = threading.Condition()
458-
self.lock = threading.Lock()
459458
self.allow_disabling = plugin_config["allow_disabling"]
460459
self.menu_locked = False
461460

@@ -663,12 +662,9 @@ def quit_safe_eyes(self):
663662
664663
This action terminates the application.
665664
"""
666-
with self.lock:
667-
self.active = True
668-
# Notify all schedulers
669-
self.idle_condition.acquire()
670-
self.idle_condition.notify_all()
671-
self.idle_condition.release()
665+
self.active = True
666+
self.__clear_resume_timer()
667+
672668
self.quit()
673669

674670
def show_settings(self) -> None:
@@ -720,13 +716,9 @@ def on_enable_clicked(self):
720716
This action enables the application if it is currently disabled.
721717
"""
722718
if not self.active:
723-
with self.lock:
724-
self.enable_ui()
725-
self.enable_safeeyes()
726-
# Notify all schedulers
727-
self.idle_condition.acquire()
728-
self.idle_condition.notify_all()
729-
self.idle_condition.release()
719+
self.enable_ui()
720+
self.enable_safeeyes()
721+
self.__clear_resume_timer()
730722

731723
def on_disable_clicked(self, time_to_wait):
732724
"""Handle the menu actions of all the sub menus of 'Disable Safe Eyes'.
@@ -746,7 +738,9 @@ def on_disable_clicked(self, time_to_wait):
746738
)
747739
info = _("Disabled until %s") % utility.format_time(self.wakeup_time)
748740
self.disable_safeeyes(info)
749-
utility.start_thread(self.__schedule_resume, time_minutes=time_to_wait)
741+
self._resume_timeout_id = GLib.timeout_add_seconds(
742+
time_to_wait * 60, self.__resume
743+
)
750744
self.update_menu()
751745

752746
def lock_menu(self):
@@ -783,17 +777,19 @@ def enable_ui(self):
783777
self.sni_service.set_icon("io.github.slgobinath.SafeEyes-enabled")
784778
self.update_menu()
785779

786-
def __schedule_resume(self, time_minutes):
787-
"""Schedule a local timer to enable Safe Eyes after the given
788-
timeout.
789-
"""
790-
self.idle_condition.acquire()
791-
self.idle_condition.wait(time_minutes * 60) # Convert to seconds
792-
self.idle_condition.release()
780+
def __resume(self):
781+
"""Reenable Safe Eyes after the given timeout."""
782+
if not self.active:
783+
self.on_enable_clicked()
784+
785+
self._resume_timeout_id = None
786+
787+
return GLib.SOURCE_REMOVE
793788

794-
with self.lock:
795-
if not self.active:
796-
utility.execute_main_thread(self.on_enable_clicked)
789+
def __clear_resume_timer(self):
790+
if self._resume_timeout_id is not None:
791+
GLib.source_remove(self._resume_timeout_id)
792+
self._resume_timeout_id = None
797793

798794
def start_animation(self) -> None:
799795
if self._animation_timeout_id is not None:

0 commit comments

Comments
 (0)