zng_app/view_process/
raw_device_events.rs

1//! Events directly from view-process not targeting any windows.
2//!
3//! These events get emitted only if the app [`enable_device_events`]. When enabled they
4//! can be used like [`raw_events`].
5//!
6//! [`enable_device_events`]: crate::AppExtended::enable_device_events
7//! [`raw_events`]: crate::view_process::raw_events
8
9use std::fmt;
10
11use crate::event::*;
12
13use zng_layout::unit::euclid;
14use zng_view_api::{
15    AxisId,
16    keyboard::{KeyCode, KeyState},
17    mouse::{ButtonId, ButtonState, MouseScrollDelta},
18};
19
20use once_cell::sync::Lazy;
21
22zng_unique_id::unique_id_64! {
23    /// Unique identifier of a device event source.
24    pub struct DeviceId;
25}
26zng_unique_id::impl_unique_id_bytemuck!(DeviceId);
27impl DeviceId {
28    /// Virtual keyboard ID used in keyboard events generated by code.
29    pub fn virtual_keyboard() -> DeviceId {
30        static ID: Lazy<DeviceId> = Lazy::new(DeviceId::new_unique);
31        *ID
32    }
33
34    /// Virtual mouse ID used in mouse events generated by code.
35    pub fn virtual_mouse() -> DeviceId {
36        static ID: Lazy<DeviceId> = Lazy::new(DeviceId::new_unique);
37        *ID
38    }
39
40    /// Virtual generic device ID used in device events generated by code.
41    pub fn virtual_generic() -> DeviceId {
42        static ID: Lazy<DeviceId> = Lazy::new(DeviceId::new_unique);
43        *ID
44    }
45}
46impl fmt::Debug for DeviceId {
47    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48        if f.alternate() {
49            f.debug_struct("DeviceId")
50                .field("id", &self.get())
51                .field("sequential", &self.sequential())
52                .finish()
53        } else {
54            write!(f, "DeviceId({})", self.sequential())
55        }
56    }
57}
58impl fmt::Display for DeviceId {
59    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
60        write!(f, "DeviceId({})", self.get())
61    }
62}
63
64event_args! {
65    /// Arguments for [`DEVICE_ADDED_EVENT`] and [`DEVICE_REMOVED_EVENT`].
66    pub struct DeviceArgs {
67        /// Device that was added/removed.
68        pub device_id: DeviceId,
69
70        ..
71
72        /// Broadcast to all widgets.
73        fn delivery_list(&self, list: &mut UpdateDeliveryList) {
74            list.search_all()
75        }
76    }
77
78    /// Arguments for [`MOUSE_MOTION_EVENT`].
79    pub struct MouseMotionArgs {
80        /// Mouse device that generated the event.
81        pub device_id: DeviceId,
82
83        /// Motion (x, y) delta.
84        pub delta: euclid::Vector2D<f64, ()>,
85
86        ..
87
88        /// Broadcast to all widgets.
89        fn delivery_list(&self, list: &mut UpdateDeliveryList) {
90            list.search_all()
91        }
92    }
93
94    /// Arguments for [`MOUSE_WHEEL_EVENT`].
95    pub struct MouseWheelArgs {
96        /// Mouse device that generated the event.
97        pub device_id: DeviceId,
98
99        /// Wheel motion delta, value is in pixels if the *wheel* is a touchpad.
100        pub delta: MouseScrollDelta,
101
102        ..
103
104        /// Broadcast to all widgets.
105        fn delivery_list(&self, list: &mut UpdateDeliveryList) {
106            list.search_all()
107        }
108    }
109
110    /// Arguments for [`MOTION_EVENT`].
111    pub struct MotionArgs {
112        /// Device that generated the event.
113        pub device_id: DeviceId,
114
115        /// Analog axis.
116        pub axis: AxisId,
117
118        /// Motion amount.
119        pub value: f64,
120
121        ..
122
123        /// Broadcast to all widgets.
124        fn delivery_list(&self, list: &mut UpdateDeliveryList) {
125            list.search_all()
126        }
127    }
128
129    /// Arguments for the [`BUTTON_EVENT`].
130    pub struct ButtonArgs {
131        /// Device that generated the event.
132        pub device_id: DeviceId,
133
134        /// Button raw id.
135        pub button: ButtonId,
136
137        /// If the button was pressed or released.
138        pub state: ButtonState,
139
140        ..
141
142        /// Broadcast to all widgets.
143        fn delivery_list(&self, list: &mut UpdateDeliveryList) {
144            list.search_all()
145        }
146    }
147
148    /// Arguments for the [`KEY_EVENT`].
149    pub struct KeyArgs {
150        /// Keyboard device that generated the event.
151        pub device_id: DeviceId,
152
153        /// Physical key.
154        pub key_code: KeyCode,
155
156        /// If the key was pressed or released.
157        pub state: KeyState,
158
159        ..
160
161        /// Broadcast to all widgets.
162        fn delivery_list(&self, list: &mut UpdateDeliveryList) {
163            list.search_all()
164        }
165    }
166
167    /// Arguments for the [`TEXT_EVENT`].
168    pub struct TextArgs {
169        /// Device that generated the event.
170        pub device_id: DeviceId,
171
172        /// Character received.
173        pub code_point: char,
174
175        ..
176
177        /// Broadcast to all widgets.
178        fn delivery_list(&self, list: &mut UpdateDeliveryList) {
179            list.search_all()
180        }
181    }
182}
183
184event! {
185    /// A device event source was added/installed.
186    pub static DEVICE_ADDED_EVENT: DeviceArgs;
187
188    /// A device event source was removed/un-installed.
189    pub static DEVICE_REMOVED_EVENT: DeviceArgs;
190
191    /// Mouse device unfiltered move delta.
192    pub static MOUSE_MOTION_EVENT: MouseMotionArgs;
193
194    /// Mouse device unfiltered wheel motion delta.
195    pub static MOUSE_WHEEL_EVENT: MouseWheelArgs;
196
197    /// Motion on some analog axis.
198    ///
199    /// This event will be reported for all arbitrary input devices that the view-process supports on this platform,
200    /// including mouse devices. If the device is a mouse device then this will be reported alongside the [`MOUSE_MOTION_EVENT`].
201    pub static MOTION_EVENT: MotionArgs;
202
203    /// Button press/release from a device, probably a mouse.
204    pub static BUTTON_EVENT: ButtonArgs;
205
206    /// Keyboard device key press.
207    pub static KEY_EVENT: KeyArgs;
208
209    /// Raw text input.
210    pub static TEXT_EVENT: TextArgs;
211}