From 1d9efeb38c1345346e0bfb2ed0d4de3e0117d0bd Mon Sep 17 00:00:00 2001 From: Aryan Pandit <20252503008@iiitvadodara.ac.in> Date: Fri, 13 Nov 2026 17:03:59 +0530 Subject: [PATCH] Fix Jasper overlay visibility on startup --- src/ui/overlay.py | 46 +++++++++++++++++++++++++++++++--- tests/test_overlay.py | 57 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+), 4 deletions(-) create mode 100644 tests/test_overlay.py diff --git a/src/ui/overlay.py b/src/ui/overlay.py index 9689124..0f450ad 100644 --- a/src/ui/overlay.py +++ b/src/ui/overlay.py @@ -6,7 +6,7 @@ import sys from queue import Queue -from PyQt5.QtCore import QSize, Qt, QTimer +from PyQt5.QtCore import QSize, Qt, QTimer, pyqtSignal from PyQt5.QtGui import QFont from PyQt5.QtWidgets import ( QApplication, @@ -32,6 +32,10 @@ class TransparentOverlayQt(QMainWindow): """A transparent overlay that displays transcriptions and responses using PyQt.""" + # Emitting this signal from an executor thread queues UI updates on the Qt + # main thread. QTimer.singleShot from a worker has no event loop to target. + message_available = pyqtSignal() + def __init__(self, message_timeout=5, exit_callback=None): """Initialize the overlay. @@ -46,6 +50,8 @@ def __init__(self, message_timeout=5, exit_callback=None): self.message_queue = Queue() self.running = True self.on_new_message = None + self.on_recording_toggled = None + self.message_available.connect(self.process_messages) # Set up the UI self.setup_ui() @@ -141,6 +147,20 @@ def setup_ui(self): control_layout.addWidget(self.send_btn) main_layout.addLayout(control_layout) + # Explicit recording works on macOS without requiring a platform-specific + # Porcupine wake-word model. + self.voice_btn = QPushButton("Start voice") + self.voice_btn.setCheckable(True) + self.voice_btn.setStyleSheet(""" + background-color: #333333; + color: white; + border: none; + padding: 5px; + border-radius: 5px; + """) + self.voice_btn.clicked.connect(self.toggle_voice_recording) + main_layout.addWidget(self.voice_btn) + # Set the central widget self.setCentralWidget(central_widget) @@ -159,7 +179,7 @@ def mouse_move_event(self, event): self.move(event.globalPos() - self.drag_position) event.accept() - def close_event(self, event): + def closeEvent(self, event): # noqa: N802 - Qt callback name is fixed by the framework. """Handle window close event.""" self.running = False if self.exit_callback: @@ -173,7 +193,7 @@ def update_status(self, status, color="gray"): status (str): Status message color (str): Color of the status indicator """ - self.message_queue.put(("status", status, color)) + self.put_message("status", status, color) def add_log_message(self, text, color="white"): label = QLabel(text) @@ -191,7 +211,7 @@ def add_log_message(self, text, color="white"): def put_message(self, msg_type, *args): """Put a message into the queue.""" self.message_queue.put((msg_type, *args)) - QTimer.singleShot(0, self.process_messages) # Schedule async-safe execution + self.message_available.emit() def process_messages(self): """Process messages from the queue.""" @@ -212,12 +232,22 @@ def process_messages(self): text = args[0] self.add_log_message(f"{config.ASSISTANT_NAME}: {text}", "#00FF00") # Green + elif msg_type == "recording": + active = args[0] + self.voice_btn.setChecked(active) + self.voice_btn.setText("Stop voice" if active else "Start voice") + self.voice_btn.setEnabled(True) + except Exception as e: logger.error(f"Error processing message: {e}") def start(self): """Start the overlay.""" self.update_status(f"{config.ASSISTANT_NAME} Active", "lightgreen") + self.show() + self.raise_() + self.activateWindow() + self.input_box.setFocus(Qt.ActiveWindowFocusReason) def shutdown(self): """Shut down the overlay.""" @@ -230,6 +260,14 @@ def send_message(self): if message and self.on_new_message: executor.submit(self.on_new_message, message) + def toggle_voice_recording(self, active): + """Dispatch microphone work away from the Qt event loop.""" + self.voice_btn.setEnabled(False) + if self.on_recording_toggled: + executor.submit(self.on_recording_toggled, active) + else: + self.put_message("recording", False) + app = QApplication(sys.argv) overlay = TransparentOverlayQt(message_timeout=5) diff --git a/tests/test_overlay.py b/tests/test_overlay.py new file mode 100644 index 0000000..6c1ccaf --- /dev/null +++ b/tests/test_overlay.py @@ -0,0 +1,57 @@ +"""Focused regression tests for the overlay's real Qt event-loop behavior.""" + +import threading +import time +import unittest + +from src.ui.overlay import TransparentOverlayQt, app + + +class OverlayTests(unittest.TestCase): + def setUp(self): + self.overlay = TransparentOverlayQt() + + def tearDown(self): + self.overlay.exit_callback = None + self.overlay.close() + app.processEvents() + + def test_start_shows_window_and_focuses_input(self): + self.overlay.start() + app.processEvents() + + assert self.overlay.isVisible() + assert self.overlay.input_box.hasFocus() + + def test_worker_message_is_delivered_on_qt_main_thread(self): + worker = threading.Thread( + target=self.overlay.put_message, + args=("status", "Processing...", "gold"), + ) + worker.start() + worker.join() + app.processEvents() + + assert self.overlay.status_text.text() == "Processing..." + + def test_voice_button_dispatches_and_updates_state(self): + received = [] + + def record(active): + received.append(active) + self.overlay.put_message("recording", active) + + self.overlay.on_recording_toggled = record + self.overlay.voice_btn.click() + for _ in range(20): + app.processEvents() + if received: + break + time.sleep(0.01) + + assert received == [True] + assert self.overlay.voice_btn.text() == "Stop voice" + + +if __name__ == "__main__": + unittest.main()