Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 42 additions & 4 deletions src/ui/overlay.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.

Expand All @@ -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()
Expand Down Expand Up @@ -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)

Expand All @@ -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:
Expand All @@ -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)
Expand All @@ -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."""
Expand All @@ -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."""
Expand All @@ -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)
Expand Down
57 changes: 57 additions & 0 deletions tests/test_overlay.py
Original file line number Diff line number Diff line change
@@ -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()
Loading