hex/src/linux/dictation/typing.rs
Dax 689e5b35e9
labs: keep the microphone open after dictating (#1701)
* labs: keep the microphone open after dictating

With Release microphone while idle on, a capture that ends now leaves the
microphone open for a Labs window (`labs.keep_microphone_open` +
`labs.keep_microphone_open_seconds`: 30 s, 1, 3, 5, or 30 minutes, default
1 minute), so the next dictation takes the warm path with its pre-roll
instead of the cold open's asynchronous start. Off, every listener closes
exactly as before, and with the release setting off the lab does nothing.

One `audio::linger::MicrophoneLinger` decides it for macOS, Windows, and
Linux: the first idle pass after a capture arms the window, a capture clears
it, and only a later idle pass past the window closes, once nothing records,
no client dictates, no release tail runs, and no capture copies the
microphone. Windows and Linux move their repeated `release_while_idle &&
!wants_audio()` closes into that one check; macOS reconciles through it. The
switch takes the Labs state word's last free bit; the window has no room
there, so it lives in its own atomic, both written by `labs::apply`.

Also moves Linux's Live typing glue into `linux/dictation/typing.rs`, since
`linux/dictation.rs` was at the source cap, and regenerates the Labs goldens
for the new card.

* labs: release the lingering microphone where the old sites did

Review fixes for keep-microphone-open:

* macOS reconcile_input keeps origin/dev's branches: with a capture sink
  open and an inactive input, recovery is still requested, and the lab's
  window is cleared while a sink holds the microphone.
* A digital-silence finish closes a lingering microphone instead of
  skipping recovery, so the next press opens a fresh stream. macOS does it
  in reopen_silent_stream; Linux's moved to linux/dictation/silence.rs
  (which also keeps linux/dictation.rs under the source cap). Windows has
  no digital-silence path.
* Windows and Linux run the release check at the old event sites (a
  submitted finish, a cancel, a discard before the open, a client's end)
  through one small macro, so a press handled in the same pass still sees
  a closed microphone when the lab is off. That check also releases a
  microphone an interrupted recording left open, which the old
  latch-based sites never did.
* Docs: drop the "exactly as before" claims, describe the extra close, the
  Annotate capture-sink and local-API windows, macOS ending the window on
  a stream interruption, and the Linux poll rather than an idle wait.
* clear() is real code now (the silence close); state()/apply() say why
  the switch bit and the window atomic are written separately.

* labs: keep the lab's windows and spots exact

Follow-ups from review:

* Windows releases the microphone when a pending client dictation's start
  gives up at the first chunk, which the old site closed too.
* macOS leaves a lingering microphone alone when a digital-silence finish
  lands while a capture sink copies it, so Annotate gets no gap.
* The Linux silence test's released case opens the input first, so it really
  covers "lab off, release while idle: no reopen, stream stays open".
* Docs say where a digital-silence finish ends the window (macOS and Linux,
  not with a capture sink on macOS) and that Windows can linger on a
  zero-only stream.
2026-11-11 18:12:08 -05:00

77 lines
2.6 KiB
Rust
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//! The Linux listener's Labs › Live typing glue: when a capture's session
//! starts, the input-method session that shows its provisional words and
//! commits its final text. Split from [`super`] to keep it under the source
//! cap.
use std::sync::Arc;
use crate::dictation::DictationCapture;
/// A hotkey capture whose provisional words go into the focused text field.
pub(super) struct CaptureTyping {
pub(super) session: crate::linux::live_typing::Session,
/// Keeps the dictation stream publishing its text without the HUD plate.
_demand: crate::dictation::live_text::TypingDemand,
}
/// Starts Live typing at the hold threshold (or the Toggle press), before
/// the stream opens so the engine sees the demand. It does not wait for a
/// stream: a CPU or non-streaming model still commits its final text through
/// the input method, with no preedit words. Short taps and chords never get
/// here, so they create no input method.
fn due(recording: bool, intentional: bool, toggle: bool) -> bool {
recording && (toggle || intentional)
}
#[allow(clippy::too_many_arguments)]
pub(super) fn begin(
capture: &DictationCapture,
toggle: bool,
wayland: bool,
live_typing: &mut Option<Arc<crate::linux::live_typing::LiveTyping>>,
next_capture_id: &mut u64,
typing: &mut Option<CaptureTyping>,
) {
if typing.is_some()
|| !wayland
|| !crate::labs::live_typing()
|| !due(capture.is_recording(), capture.is_intentional(), toggle)
{
return;
}
if !live_typing.as_ref().is_none_or(|live| live.is_accepting()) {
return;
}
let live = live_typing
.get_or_insert_with(crate::linux::live_typing::LiveTyping::start)
.clone();
*next_capture_id += 1;
let mut started = CaptureTyping {
session: crate::linux::live_typing::Session::new(live, *next_capture_id),
_demand: crate::dictation::live_text::typing_demand(),
};
started.session.start();
*typing = Some(started);
}
/// Ends a capture's Live typing session without committing anything; the
/// session guard clears the preedit.
pub(super) fn cancel(typing: &mut Option<CaptureTyping>) {
typing.take();
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn live_typing_waits_for_a_recording_and_the_hold_or_toggle() {
assert!(!due(false, false, true), "idle Toggle chunks start nothing");
assert!(
!due(true, false, false),
"a Hold press waits for the threshold"
);
assert!(due(true, true, false));
assert!(due(true, false, true), "Toggle starts at once");
}
}