hex/src/windows/input/activity.rs
Dax e4d85b44aa
windows: join back-to-back dictations with a space (#1980)
* windows: join back-to-back dictations with a space

move the input-activity revision, continuation join and its tests into a
platform-neutral continuation module. windows now invalidates it on real
keys and clicks from the low-level hooks and on foreground-window changes,
and the paster joins dictation and paste last the way macos does.

* windows: end continuations on real input and skip hex's own trigger

injected input from other tools and the auxiliary mouse buttons end a paste
continuation as on macOS, while Hex's own synthetic events and a dictation
trigger that Labs › Share shortcut lets through do not. A standalone macOS
paste clears the continuation only where dev did: at the start of typing, or
after the clipboard is preserved and checked.

* windows: a bare trigger key press ends the paste continuation

Labs › Share shortcut lets the dictation trigger's key reach the app in
front, and the hook treated every event for that key as hex's own. A
plain Space while Ctrl+Space is bound then kept the continuation, so the
next dictation joined with a space for no reason. Mark the press as the
shortcut's only once it claims the capture, and drive the hook's own
state machine in a test.
2026-11-18 19:28:07 -05:00

60 lines
2.3 KiB
Rust

//! What the low-level hooks count as the user's own input between
//! dictations: a real key or click, never the shortcut's own chord or one of
//! Hex's synthetic events.
use windows_sys::Win32::UI::WindowsAndMessaging::GetForegroundWindow;
use super::{CALLBACK_STATE, HEX_INJECTED};
/// The foreground window now, for comparing one reading with the next: a
/// switch between windows ends a paste continuation.
pub(crate) fn foreground_window() -> isize {
unsafe { GetForegroundWindow() as isize }
}
/// Whether an event is the user's own input between dictations, which ends
/// a paste continuation: a press that is not a modifier (the shortcuts' own
/// chords include one) and not one of Hex's own synthetic events. Input
/// another program injects is the user's too: a remapper, keyboard sharing,
/// or the touch keyboard typed what is in front of them, so it ends the
/// continuation as macOS's tap does.
pub(super) fn ends_continuation(is_down: bool, modifier: bool, extra_info: usize) -> bool {
is_down && !modifier && extra_info != HEX_INJECTED
}
/// The user typed or clicked: the next paste no longer continues the last
/// one.
pub(super) fn invalidate() {
let guard = CALLBACK_STATE
.lock()
.unwrap_or_else(|error| error.into_inner());
if let Some(state) = guard.as_ref() {
state.activity.invalidate();
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::windows::input::modifiers::ModifierState;
use windows_sys::Win32::UI::Input::KeyboardAndMouse::VK_LCONTROL;
#[test]
fn real_input_ends_a_paste_continuation_but_hexs_own_events_do_not() {
// A real press ends it, whatever key or mouse button sent it: macOS
// counts any mouse-down, auxiliary buttons included.
assert!(ends_continuation(true, false, 0));
// A release inputs nothing, and a modifier is part of the shortcut's
// own chord.
assert!(!ends_continuation(false, false, 0));
let control = u32::from(VK_LCONTROL);
assert!(!ends_continuation(
true,
ModifierState::is_modifier(control),
0
));
// Hex's own synthetic paste or chord. Another program's injected key
// carries no marker, so it ends the continuation above.
assert!(!ends_continuation(true, false, HEX_INJECTED));
}
}