Scope Esc cancel listener to active recording

This commit is contained in:
Thales Maciel 2026-02-26 16:28:49 -03:00
parent e5d709a393
commit 64c8c26bce
6 changed files with 105 additions and 7 deletions

View file

@ -77,6 +77,18 @@ class Daemon:
self.vocabulary = VocabularyEngine(cfg.vocabulary)
self._stt_hint_kwargs_cache: dict[str, Any] | None = None
def _arm_cancel_listener(self):
try:
self.desktop.start_cancel_listener(lambda: self.cancel_recording())
except Exception as exc:
logging.error("failed to start cancel listener: %s", exc)
def _disarm_cancel_listener(self):
try:
self.desktop.stop_cancel_listener()
except Exception as exc:
logging.debug("failed to stop cancel listener: %s", exc)
def set_state(self, state: str):
with self.lock:
prev = self.state
@ -84,7 +96,7 @@ class Daemon:
if prev != state:
logging.debug("state: %s -> %s", prev, state)
else:
logging.warning("redundant state set: %s, kindly inform the dev", state)
logging.debug("redundant state set: %s", state)
def get_state(self):
with self.lock:
@ -123,6 +135,7 @@ class Daemon:
prev = self.state
self.state = State.RECORDING
logging.debug("state: %s -> %s", prev, self.state)
self._arm_cancel_listener()
logging.info("recording started")
if self.timer:
self.timer.cancel()
@ -150,6 +163,7 @@ class Daemon:
if self.timer:
self.timer.cancel()
self.timer = None
self._disarm_cancel_listener()
prev = self.state
self.state = State.STT
logging.debug("state: %s -> %s", prev, self.state)
@ -179,7 +193,6 @@ class Daemon:
return
try:
self.set_state(State.STT)
logging.info("stt started")
text = self._transcribe(audio)
except Exception as exc:
@ -256,6 +269,7 @@ class Daemon:
def shutdown(self, timeout: float = 5.0) -> bool:
self.request_shutdown()
self._disarm_cancel_listener()
self.stop_recording(trigger="shutdown", process_audio=False)
return self.wait_for_idle(timeout)
@ -402,7 +416,6 @@ def main():
cfg.daemon.hotkey,
lambda: logging.info("hotkey pressed (dry-run)") if args.dry_run else daemon.toggle(),
)
desktop.start_cancel_listener(lambda: daemon.cancel_recording())
except Exception as exc:
logging.error("hotkey setup failed: %s", exc)
raise SystemExit(1)