# SPDX-License-Identifier: LGPL-2.1-or-later """Reads a dialog's message when focus enters it, using wine-access review. Focus events only describe the focused control, so a message box would be heard as just its buttons. With review protocol version 1, the helper tells the reader which window has focus; when that window is a new dialog, its visible labels are captured once and presented after the focused control. """ from __future__ import annotations from typing import Any, Callable REVIEW_INTERFACE = "org.stormux.Cthulhu1.WineReview" PROTOCOL_VERSION = 1 LABEL_ROLES = {"label", "static text"} def dialog_text(reply: dict[str, Any], title: str = "") -> str: """The visible, unprotected label text of a successful capture.""" if reply.get("status") != "ok": return "" texts = [] for node in reply.get("nodes", []): if node.get("role") not in LABEL_ROLES or not node.get("visible", False): continue if node.get("protected", False): continue text = node.get("name", "") or " ".join( entry[4] for entry in node.get("ranges", []) if len(entry) > 4) text = " ".join(text.split()) if text and text != title and text not in texts: texts.append(text) return " ".join(texts) def gio_capture(connection: Any) -> Callable[..., None]: """A capture function calling the helper's CaptureWindow over Gio.""" from gi.repository import Gio, GLib # pylint: disable=import-outside-toplevel def capture(sender: str, path: str, window: str, focus: str, revision: int, done: Callable[[dict[str, Any] | None], None]) -> None: def finished(conn: Any, result: Any, _data: Any) -> None: try: value = conn.call_finish(result) except GLib.Error: done(None) return done(value.unpack()[0]) connection.call(sender, path, REVIEW_INTERFACE, "CaptureWindow", GLib.Variant("(sst)", (window, focus, revision)), GLib.VariantType.new("(a{sv})"), Gio.DBusCallFlags.NONE, 1000, None, finished, None) return capture class DialogReader: """Tracks review providers and captures each newly focused dialog once. capture(sender, path, window, focus, revision, done) must call done(reply) with the CaptureWindow reply, or done(None) on failure. present(text, window) presents the dialog text after that window's focus. """ def __init__(self, capture: Callable[..., None], present: Callable[[str, str], None]) -> None: self._capture = capture self._present = present self._providers: dict[str, str] = {} self._dialog: tuple[str, str] | None = None def register(self, sender: str, path: str, version: int) -> bool: if version < PROTOCOL_VERSION or not sender: return False self._providers[sender] = path return True def forget(self, sender: str) -> None: self._providers.pop(sender, None) def focus(self, sender: str, window: str, focus: str, revision: int, title: str, role: str) -> bool: path = self._providers.get(sender) if path is None: return False if role != "dialog": self._dialog = None return True dialog = (sender, window) if dialog == self._dialog: return True self._dialog = dialog def done(reply: dict[str, Any] | None) -> None: if reply is None or reply.get("status") != "ok": # Retry on the dialog's next focus notice. if self._dialog == dialog: self._dialog = None return if self._dialog != dialog: return text = dialog_text(reply, title) if text: self._present(text, title) self._capture(sender, path, window, focus, revision, done) return True