Skip to main content

viewport_lib/interaction/input/
viewport_binding.rs

1//! Viewport gesture and binding types for the new input pipeline.
2
3use super::action::Action;
4use super::binding::{KeyCode, Modifiers, MouseButton};
5
6/// Modifier matching policy for viewport gestures.
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
8pub enum ModifiersMatch {
9    /// All specified modifier bits must match exactly — no extras.
10    Exact(Modifiers),
11    /// All specified modifier bits must be set; extras are allowed.
12    Contains(Modifiers),
13    /// Any modifier state is accepted.
14    Any,
15}
16
17impl ModifiersMatch {
18    /// Test whether the given modifier state satisfies this policy.
19    pub fn matches(&self, mods: Modifiers) -> bool {
20        match self {
21            ModifiersMatch::Exact(required) => mods == *required,
22            ModifiersMatch::Contains(required) => {
23                // every set bit in `required` must also be set in `mods`
24                (!required.alt || mods.alt)
25                    && (!required.shift || mods.shift)
26                    && (!required.ctrl || mods.ctrl)
27            }
28            ModifiersMatch::Any => true,
29        }
30    }
31}
32
33/// A viewport gesture — the richer gesture vocabulary used by the new pipeline.
34#[derive(Debug, Clone)]
35pub enum ViewportGesture {
36    /// A mouse drag with a specific button and modifier policy.
37    Drag {
38        /// Which button must be held to drag.
39        button: MouseButton,
40        /// Required modifier state.
41        modifiers: ModifiersMatch,
42    },
43    /// Vertical wheel delta with a modifier policy.
44    ///
45    /// Produces a single scalar output (the `y` component of the wheel delta).
46    WheelY {
47        /// Required modifier state.
48        modifiers: ModifiersMatch,
49    },
50    /// Full two-axis wheel delta with a modifier policy.
51    ///
52    /// Produces a [`glam::Vec2`] output.
53    WheelXY {
54        /// Required modifier state.
55        modifiers: ModifiersMatch,
56    },
57    /// A single key press — fires once on the initial press, not on repeat.
58    KeyPress {
59        /// The key that must be pressed.
60        key: KeyCode,
61        /// Required modifier state.
62        modifiers: ModifiersMatch,
63    },
64    /// A key held down — fires every frame while the key is held.
65    KeyHold {
66        /// The key that must be held.
67        key: KeyCode,
68        /// Required modifier state.
69        modifiers: ModifiersMatch,
70    },
71}
72
73/// Binds an [`Action`] to a [`ViewportGesture`].
74#[derive(Debug, Clone)]
75pub struct ViewportBinding {
76    /// The action this binding fires.
77    pub action: Action,
78    /// The gesture that activates it.
79    pub gesture: ViewportGesture,
80}
81
82impl ViewportBinding {
83    /// Convenience constructor.
84    pub fn new(action: Action, gesture: ViewportGesture) -> Self {
85        Self { action, gesture }
86    }
87}