sim_logic/application/input/control.rs
1//! Physical control provenance and explicit cancellation, without UI routing.
2
3use super::{ALL_MOUSE_BUTTONS, ALL_PHYSICAL_KEYS, MouseButton, PhysicalKeyCode};
4
5/// Physical control that produced one logical action occurrence.
6///
7/// Two controls bound to the same action remain distinguishable even when a
8/// mouse occurrence has no known pointer position. This identifies the supported
9/// control, not a particular keyboard, mouse device, or window. It requires no
10/// platform or renderer resource.
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
12#[non_exhaustive]
13pub enum InputControl {
14 /// A physical keyboard location, independent of typed text or keyboard layout.
15 Key(PhysicalKeyCode),
16 /// One of the supported mouse buttons.
17 MouseButton(MouseButton),
18}
19
20/// Why a held control was released without an ordinary release event.
21///
22/// Cancellation releases have no pointer sample. They still participate in
23/// normal bounded, ordered action delivery and have their own occurrence token;
24/// they do not consume input or silently undo earlier press effects.
25#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
26#[non_exhaustive]
27pub enum InputCancellationReason {
28 /// Pointer tracking ended, for example on cursor leave or a zero-size
29 /// desktop viewport. Only held mouse controls are cancelled.
30 PointerLeft,
31 /// The host reported focus loss. Held keyboard and mouse controls are
32 /// cancelled, and the continuous pointer is cleared.
33 FocusLost,
34}
35
36pub(super) fn cancelled_controls(
37 reason: InputCancellationReason,
38) -> impl Iterator<Item = InputControl> {
39 let keys: &[PhysicalKeyCode] = match reason {
40 InputCancellationReason::PointerLeft => &[],
41 InputCancellationReason::FocusLost => &ALL_PHYSICAL_KEYS,
42 };
43 keys.iter()
44 .copied()
45 .map(InputControl::Key)
46 .chain(ALL_MOUSE_BUTTONS.into_iter().map(InputControl::MouseButton))
47}