fix: thread-safe hotkey handling
- Add HotkeyHandler class with pyqtSignal - Fix QMetaObject.invokeMethod error - Proper thread safety for keyboard hotkeys
This commit is contained in:
parent
d387a4714a
commit
fa0b0c87b5
|
|
@ -15,7 +15,7 @@ if str(project_root) not in sys.path:
|
|||
|
||||
try:
|
||||
from PyQt6.QtWidgets import QApplication
|
||||
from PyQt6.QtCore import Qt
|
||||
from PyQt6.QtCore import Qt, QObject, pyqtSignal
|
||||
PYQT_AVAILABLE = True
|
||||
except ImportError:
|
||||
PYQT_AVAILABLE = False
|
||||
|
|
@ -35,6 +35,11 @@ from core.plugin_manager import PluginManager
|
|||
from core.overlay_window import OverlayWindow
|
||||
|
||||
|
||||
class HotkeyHandler(QObject):
|
||||
"""Signal bridge for thread-safe hotkey handling."""
|
||||
toggle_signal = pyqtSignal()
|
||||
|
||||
|
||||
class EUUtilityApp:
|
||||
"""Main application controller."""
|
||||
|
||||
|
|
@ -42,6 +47,7 @@ class EUUtilityApp:
|
|||
self.app = None
|
||||
self.overlay = None
|
||||
self.plugin_manager = None
|
||||
self.hotkey_handler = None
|
||||
|
||||
def run(self):
|
||||
"""Start the application."""
|
||||
|
|
@ -53,6 +59,9 @@ class EUUtilityApp:
|
|||
if hasattr(Qt, 'AA_EnableHighDpiScaling'):
|
||||
self.app.setAttribute(Qt.ApplicationAttribute.AA_EnableHighDpiScaling)
|
||||
|
||||
# Create hotkey handler (must be in main thread)
|
||||
self.hotkey_handler = HotkeyHandler()
|
||||
|
||||
# Initialize plugin manager
|
||||
print("Loading plugins...")
|
||||
self.plugin_manager = PluginManager(None)
|
||||
|
|
@ -62,6 +71,9 @@ class EUUtilityApp:
|
|||
self.overlay = OverlayWindow(self.plugin_manager)
|
||||
self.plugin_manager.overlay = self.overlay
|
||||
|
||||
# Connect hotkey signal
|
||||
self.hotkey_handler.toggle_signal.connect(self._on_toggle_signal)
|
||||
|
||||
# Setup global hotkey
|
||||
self._setup_hotkey()
|
||||
|
||||
|
|
@ -75,20 +87,20 @@ class EUUtilityApp:
|
|||
"""Setup global hotkey."""
|
||||
if KEYBOARD_AVAILABLE:
|
||||
try:
|
||||
keyboard.add_hotkey('ctrl+shift+u', self._toggle_overlay)
|
||||
keyboard.add_hotkey('ctrl+shift+u', self._on_hotkey_pressed)
|
||||
except Exception as e:
|
||||
print(f"Failed to register hotkey: {e}")
|
||||
|
||||
def _toggle_overlay(self):
|
||||
"""Toggle overlay visibility."""
|
||||
def _on_hotkey_pressed(self):
|
||||
"""Called when hotkey is pressed (from keyboard thread)."""
|
||||
# Emit signal to main thread
|
||||
if self.hotkey_handler:
|
||||
self.hotkey_handler.toggle_signal.emit()
|
||||
|
||||
def _on_toggle_signal(self):
|
||||
"""Handle toggle signal in main thread."""
|
||||
if self.overlay:
|
||||
# Use Qt's thread-safe method
|
||||
from PyQt6.QtCore import QMetaObject, Qt
|
||||
QMetaObject.invokeMethod(
|
||||
self.overlay,
|
||||
"toggle_overlay",
|
||||
Qt.ConnectionType.QueuedConnection
|
||||
)
|
||||
self.overlay.toggle_overlay()
|
||||
|
||||
|
||||
def main():
|
||||
|
|
|
|||
Loading…
Reference in New Issue