Skip to content

Commit f231a34

Browse files
KIRA009abrichr
andauthored
fix(tray): fix tray signals (#812)
* feat: Override std out for application * fix: Fix passing signals around using multiprocessing pipes * feat: Separate logs by version number and date and use logger instead of print * lint: flake8 lint --------- Co-authored-by: Richard Abrich <[email protected]>
1 parent f44795c commit f231a34

File tree

1 file changed

+37
-16
lines changed

1 file changed

+37
-16
lines changed

openadapt/app/tray.py

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
from loguru import logger
1717
from pyqttoast import Toast, ToastButtonAlignment, ToastIcon, ToastPosition, ToastPreset
18-
from PySide6.QtCore import QMargins, QSize, QSocketNotifier, Qt
18+
from PySide6.QtCore import QMargins, QObject, QSize, Qt, QThread, Signal
1919
from PySide6.QtGui import QAction, QColor, QFont, QIcon, QPixmap
2020
from PySide6.QtWidgets import (
2121
QApplication,
@@ -76,6 +76,28 @@ def track_event(self) -> None:
7676
)
7777

7878

79+
class Worker(QObject):
80+
"""Worker to handle signals from the recording process."""
81+
82+
data = Signal(dict)
83+
84+
def __init__(self, *args: Any, **kwargs: Any) -> None:
85+
"""Initialize the worker."""
86+
parent_conn = kwargs.pop("parent_conn")
87+
super().__init__(*args, **kwargs)
88+
self.parent_conn = parent_conn
89+
90+
def run(self) -> None:
91+
"""Run the worker."""
92+
while True:
93+
if self.parent_conn.poll():
94+
try:
95+
data = self.parent_conn.recv()
96+
except EOFError:
97+
break
98+
self.data.emit(data)
99+
100+
79101
class SystemTrayIcon:
80102
"""System tray icon for OpenAdapt."""
81103

@@ -149,17 +171,14 @@ def _quit() -> None:
149171
self.visualize_proc = None
150172

151173
self.parent_conn, self.child_conn = multiprocessing.Pipe()
152-
# Set up QSocketNotifier to monitor the read end of the pipe
153-
self.notifier = QSocketNotifier(
154-
self.parent_conn.fileno(),
155-
QSocketNotifier.Read,
156-
)
157-
self.notifier.activated.connect(
158-
lambda: self.handle_recording_signal(
159-
self.notifier,
160-
self.parent_conn,
161-
)
162-
)
174+
175+
self.notifier = QThread(self.app)
176+
self.worker = Worker(parent_conn=self.parent_conn)
177+
self.worker.moveToThread(self.notifier)
178+
self.notifier.started.connect(self.worker.run)
179+
self.notifier.destroyed.connect(self.worker.deleteLater)
180+
self.notifier.start()
181+
self.worker.data.connect(self.handle_recording_signal)
163182

164183
# for storing toasts that should be manually removed
165184
self.sticky_toasts = {}
@@ -168,11 +187,13 @@ def _quit() -> None:
168187

169188
def handle_recording_signal(
170189
self,
171-
notifier: QSocketNotifier,
172-
conn: multiprocessing.connection.Connection,
190+
signal: dict,
173191
) -> None:
174-
"""Callback function to handle the signal from the recording process."""
175-
signal = conn.recv()
192+
"""Callback function to handle the signal from the recording process.
193+
194+
Args:
195+
signal (dict): The signal from the recording process.
196+
"""
176197
logger.info(f"Received signal: {signal}")
177198

178199
signal_type = signal["type"]

0 commit comments

Comments
 (0)