"""The "Connect to Chromecast…" picker. Searching starts when the dialog opens and stops when it closes, whichever way it closes. Connecting is not awaited here — the dialog's job ends at "which device", and CastController reports the outcome on the status bar. """ from PyQt6.QtCore import Qt, QTimer from PyQt6.QtWidgets import ( QDialog, QDialogButtonBox, QHBoxLayout, QLabel, QListWidget, QListWidgetItem, QVBoxLayout) from lintunes.gui.spinner import Spinner # How long to look before admitting there's nothing there. Searching carries on # regardless — a speaker that wakes up later still shows up. EMPTY_AFTER_MS = 12_000 class ChromecastDialog(QDialog): """Lists Chromecasts as they're discovered; returns the one picked.""" def __init__(self, discovery, parent=None): super().__init__(parent) self._discovery = discovery self._selected = None self.setWindowTitle("Connect to Chromecast") self.setMinimumWidth(340) layout = QVBoxLayout(self) header = QHBoxLayout() self._spinner = Spinner() header.addWidget(self._spinner) self._status = QLabel("Searching for Chromecasts…") header.addWidget(self._status, stretch=1) layout.addLayout(header) self._list = QListWidget() self._list.setAlternatingRowColors(True) self._list.itemSelectionChanged.connect(self._on_selection_changed) self._list.itemDoubleClicked.connect(lambda _: self._on_connect()) layout.addWidget(self._list, stretch=1) self._buttons = QDialogButtonBox(QDialogButtonBox.StandardButton.Cancel) self._connect_button = self._buttons.addButton( "Connect", QDialogButtonBox.ButtonRole.AcceptRole) self._connect_button.setEnabled(False) self._connect_button.setDefault(True) self._buttons.rejected.connect(self.reject) self._connect_button.clicked.connect(self._on_connect) layout.addWidget(self._buttons) discovery.device_found.connect(self._on_found) discovery.device_lost.connect(self._on_lost) discovery.failed.connect(self._on_failed) self._empty_timer = QTimer(self) self._empty_timer.setSingleShot(True) self._empty_timer.setInterval(EMPTY_AFTER_MS) self._empty_timer.timeout.connect(self._on_empty_timeout) self._empty_timer.start() # Anything discovery already knows about (it may still be running from # a previous open) should be on screen before the first new callback. for device in discovery.devices(): self._on_found(device) discovery.start() @property def selected_device(self): return self._selected # ---- discovery ---- def _on_found(self, device): for row in range(self._list.count()): existing = self._list.item(row).data(Qt.ItemDataRole.UserRole) if existing.uuid == device.uuid: return label = device.name if device.model: label = f"{device.name} · {device.model}" item = QListWidgetItem(label) item.setData(Qt.ItemDataRole.UserRole, device) self._list.addItem(item) self._status.setText("Select a Chromecast to connect to.") if self._list.count() == 1: self._list.setCurrentRow(0) def _on_lost(self, device): for row in range(self._list.count()): if self._list.item(row).data( Qt.ItemDataRole.UserRole).uuid == device.uuid: self._list.takeItem(row) break if self._list.count() == 0: self._status.setText("Searching for Chromecasts…") def _on_failed(self, message: str): self._spinner.hide() self._status.setText(message) self._empty_timer.stop() def _on_empty_timeout(self): if self._list.count() == 0: # The spinner keeps turning: discovery is still running, and a # device that comes online in a minute will still appear. self._status.setText("No Chromecasts found on this network yet.") # ---- selection ---- def _on_selection_changed(self): self._connect_button.setEnabled(self._list.currentItem() is not None) def _on_connect(self): item = self._list.currentItem() if item is None: return self._selected = item.data(Qt.ItemDataRole.UserRole) self.accept() def done(self, result): # The single exit funnel — accept, reject, Esc and the window's close # button all land here, so discovery can't be left running. self._empty_timer.stop() self._spinner.stop() self._discovery.stop() super().done(result)