1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
//! # Keyboard State Management
//!
//! This module provides keyboard state management for the Tessera UI framework.
//!
//! ## Overview
//!
//! The keyboard state system manages a bounded queue of keyboard events to ensure
//! efficient event processing and memory management. It acts as a buffer between
//! the windowing system (winit) and the UI framework, allowing for smooth keyboard
//! input handling even during high-frequency key events.
//!
//! ## Design
//!
//! The [`KeyboardState`] struct maintains a bounded queue of keyboard events to ensure:
//! - **Memory efficiency**: Old events are automatically discarded to prevent unbounded growth
//! - **Event ordering**: Events are processed in the order they were received
//! - **Performance**: The queue size is limited to prevent excessive memory usage during rapid key presses
//!
//! ## Usage
//!
//! ```rust,ignore
//! use crate::KeyboardState;
//! use winit::event::{KeyEvent, ElementState};
//! use winit::keyboard::{KeyCode, PhysicalKey};
//!
//! let mut keyboard_state = KeyboardState::default();
//!
//! // Push keyboard events as they arrive from winit
//! let key_event = KeyEvent {
//! physical_key: PhysicalKey::Code(KeyCode::KeyA),
//! logical_key: winit::keyboard::Key::Character("a".into()),
//! text: Some("a".into()),
//! location: winit::keyboard::KeyLocation::Standard,
//! state: ElementState::Pressed,
//! repeat: false,
//! platform_specific: Default::default(),
//! };
//! keyboard_state.push_event(key_event);
//!
//! // Process all pending keyboard events
//! let events = keyboard_state.take_events();
//! for event in events {
//! match event.state {
//! ElementState::Pressed => {
//! // Handle key press
//! }
//! ElementState::Released => {
//! // Handle key release
//! }
//! }
//! }
//! ```
use VecDeque;
use ModifiersState;
/// Maximum number of keyboard events to keep in the queue.
///
/// This constant limits the size of the keyboard event queue to prevent unbounded
/// memory growth during rapid key input. When the queue exceeds this size, the
/// oldest events are automatically removed to make room for new ones.
///
/// The value of 10 is chosen to balance between:
/// - Responsiveness: Ensuring recent events are not lost
/// - Memory efficiency: Preventing excessive memory usage
/// - Performance: Keeping queue operations fast
const KEEP_EVENTS_COUNT: usize = 10;
/// Manages the state and event queue for keyboard input.
///
/// The `KeyboardState` struct provides a bounded queue for storing keyboard events
/// received from the windowing system. It automatically manages memory by discarding
/// old events when the queue becomes too large, ensuring consistent performance
/// even during rapid keyboard input.
///
/// ## Thread Safety
///
/// This struct is not thread-safe by itself and should be protected by appropriate
/// synchronization primitives when used across multiple threads.
///
/// ## Examples
///
/// ```rust,ignore
/// use crate::KeyboardState;
/// use winit::event::KeyEvent;
///
/// let mut keyboard_state = KeyboardState::default();
///
/// // The queue starts empty
/// assert!(keyboard_state.take_events().is_empty());
///
/// // Events can be pushed and retrieved
/// keyboard_state.push_event(key_event);
/// let events = keyboard_state.take_events();
/// assert_eq!(events.len(), 1);
/// ```