Skip to main content

euv_core/system/event/
struct.rs

1use super::*;
2
3/// Handle returned by scoped system event hooks allowing optional manual early unregistration.
4#[derive(Clone, Copy, Data, Debug, Eq, Hash, New, PartialEq)]
5pub struct SystemEventSubscription {
6    /// Monotonic listener identifier inside the thread-local `SystemEventHub`.
7    #[get(type(copy))]
8    pub(crate) id: u64,
9}
10
11/// Deterministic state machine deduplicating noisy OS callbacks and enforcing canonical cross-platform event ordering.
12#[derive(Clone, Data, Debug, PartialEq)]
13pub struct SystemEventNormalizer {
14    /// Current canonical application lifecycle state.
15    #[get(type(copy))]
16    pub(crate) state: AppState,
17    /// Whether the active application surface is currently visible (`Appear` emitted without matching `Disappear`).
18    #[get(type(copy))]
19    pub(crate) visible: bool,
20    /// Most recently emitted window focus state for deduplication.
21    #[get(skip)]
22    pub(crate) last_focused: Option<bool>,
23    /// Most recently emitted logical `(width, height, scale_factor)` resize tuple.
24    #[get(skip)]
25    pub(crate) last_resize: Option<(f32, f32, f32)>,
26    /// Most recently emitted safe-area insets.
27    #[get(skip)]
28    pub(crate) last_safe_area: Option<SafeAreaInsets>,
29    /// Most recently emitted screen orientation.
30    #[get(skip)]
31    pub(crate) last_orientation: Option<ScreenOrientation>,
32    /// Most recently emitted OS light/dark theme.
33    #[get(skip)]
34    pub(crate) last_theme: Option<ThemeMode>,
35    /// Most recently emitted OS locale string.
36    #[get(skip)]
37    pub(crate) last_locale: Option<String>,
38    /// Most recently emitted virtual keyboard `(visible, height)` state.
39    #[get(skip)]
40    pub(crate) last_keyboard: Option<(bool, f32)>,
41    /// Most recently emitted low-power mode flag.
42    #[get(skip)]
43    pub(crate) last_low_power: Option<bool>,
44    /// Most recently emitted network reachability state.
45    #[get(skip)]
46    pub(crate) last_network: Option<NetworkStatus>,
47    /// Most recently emitted sanitized 6-DOF device motion sample.
48    #[get(skip)]
49    pub(crate) last_device_motion: Option<DeviceMotionSample>,
50}
51
52/// Screen-aligned 6-DOF gravity, linear acceleration, and gyroscope sample delivered by real device motion sensors.
53///
54/// Linear vectors use the screen frame `+x` right, `+y` down, `+z` out of the screen in SI `m/s²`.
55/// Angular rates use the native sensor right-hand convention (`gyro_x` about screen-right, `gyro_y`
56/// about screen-up, `gyro_z` about the out-of-screen axis, positive counter-clockwise as seen by the user) in `rad/s`.
57#[derive(Clone, Copy, Data, Debug, PartialEq)]
58pub struct DeviceMotionSample {
59    /// Gravity vector `x` component (`m/s²`, screen-right positive).
60    #[get(type(copy))]
61    pub(crate) gravity_x: f32,
62    /// Gravity vector `y` component (`m/s²`, screen-down positive; `9.80665` when held upright).
63    #[get(type(copy))]
64    pub(crate) gravity_y: f32,
65    /// Gravity vector `z` component (`m/s²`, out-of-screen positive; `-9.80665` lying face-up).
66    #[get(type(copy))]
67    pub(crate) gravity_z: f32,
68    /// User-induced linear (shake/translation) acceleration `x` component in `m/s²`.
69    #[get(type(copy))]
70    pub(crate) linear_accel_x: f32,
71    /// User-induced linear (shake/translation) acceleration `y` component in `m/s²`.
72    #[get(type(copy))]
73    pub(crate) linear_accel_y: f32,
74    /// User-induced linear (shake/translation) acceleration `z` component in `m/s²`.
75    #[get(type(copy))]
76    pub(crate) linear_accel_z: f32,
77    /// Angular velocity about the screen-right axis in `rad/s`.
78    #[get(type(copy))]
79    pub(crate) gyro_x: f32,
80    /// Angular velocity about the screen-up axis in `rad/s`.
81    #[get(type(copy))]
82    pub(crate) gyro_y: f32,
83    /// Angular velocity about the out-of-screen axis in `rad/s` (positive counter-clockwise as seen by the user).
84    #[get(type(copy))]
85    pub(crate) gyro_z: f32,
86    /// Forward/backward tilt of the screen away from upright in radians (`+` = top tilted away from the user).
87    #[get(type(copy))]
88    pub(crate) pitch_rad: f32,
89    /// In-screen-plane device rotation in radians (`+` = counter-clockwise as seen by the user).
90    #[get(type(copy))]
91    pub(crate) roll_rad: f32,
92    /// Heading/azimuth in radians when reported by the host sensor fusion (`0.0` otherwise).
93    #[get(type(copy))]
94    pub(crate) yaw_rad: f32,
95    /// Monotonic sensor timestamp in nanoseconds.
96    #[get(type(copy))]
97    pub(crate) timestamp_ns: u64,
98    /// Whether this sample originates from a live hardware sensor stream.
99    #[get(type(copy))]
100    pub(crate) sensor_active: bool,
101}
102
103/// Internal scope-bound system event listener entry stored in `SystemEventHub`.
104#[derive(CustomDebug)]
105pub(crate) struct SystemEventListener {
106    /// Unique listener identifier.
107    pub(crate) id: u64,
108    /// Reactive scope owning this listener, if registered inside a scope.
109    pub(crate) owner_scope: Option<ScopeId>,
110    /// Type-erased callback invoked when matching system events arrive.
111    #[debug(skip)]
112    pub(crate) callback: SystemEventCallback,
113}
114
115/// Thread-local registry of active scoped system event listeners.
116#[derive(CustomDebug, Default)]
117pub(crate) struct SystemEventHub {
118    /// Monotonic listener identifier counter.
119    pub(crate) next_id: u64,
120    /// Active registered listeners in registration order.
121    pub(crate) listeners: Vec<SystemEventListener>,
122}