1
1
use crate :: { Key , KeyInput , Mods } ;
2
- use winit:: event:: { ElementState , Event , Modifiers , WindowEvent } ;
2
+ use winit:: event:: { ElementState , Event , KeyEvent , Modifiers , WindowEvent } ;
3
3
use winit:: keyboard:: { Key as WinitKey , ModifiersState , NamedKey } ;
4
4
5
5
impl From < & WinitKey > for Key {
@@ -114,44 +114,50 @@ impl From<ModifiersState> for Mods {
114
114
}
115
115
}
116
116
117
- pub trait KeyInputConvertible {
118
- fn convert_key_input ( & self , conv : & mut KeyEventConverter ) -> KeyInput ;
117
+ pub trait WinitEvent {
118
+ fn to_key_input ( & self , conv : & mut WinitEventConverter ) -> KeyInput ;
119
119
}
120
120
121
- impl KeyInputConvertible for WindowEvent {
122
- fn convert_key_input ( & self , conv : & mut KeyEventConverter ) -> KeyInput {
121
+ impl WinitEvent for KeyEvent {
122
+ fn to_key_input ( & self , conv : & mut WinitEventConverter ) -> KeyInput {
123
+ KeyInput {
124
+ key : Key :: from ( & self . logical_key ) ,
125
+ mods : conv. mods ,
126
+ }
127
+ }
128
+ }
129
+
130
+ impl WinitEvent for WindowEvent {
131
+ fn to_key_input ( & self , conv : & mut WinitEventConverter ) -> KeyInput {
123
132
match self {
124
133
WindowEvent :: ModifiersChanged ( mods) => {
125
134
conv. on_modifiers_changed ( mods) ;
126
135
Key :: Ignored . into ( )
127
136
}
128
137
WindowEvent :: KeyboardInput { event, .. } if event. state == ElementState :: Pressed => {
129
- KeyInput {
130
- key : Key :: from ( & event. logical_key ) ,
131
- mods : conv. mods ,
132
- }
138
+ event. to_key_input ( conv)
133
139
}
134
140
_ => Key :: Ignored . into ( ) ,
135
141
}
136
142
}
137
143
}
138
144
139
- impl < T > KeyInputConvertible for Event < T > {
140
- fn convert_key_input ( & self , conv : & mut KeyEventConverter ) -> KeyInput {
145
+ impl < T > WinitEvent for Event < T > {
146
+ fn to_key_input ( & self , conv : & mut WinitEventConverter ) -> KeyInput {
141
147
if let Event :: WindowEvent { event, .. } = self {
142
- event. convert_key_input ( conv)
148
+ event. to_key_input ( conv)
143
149
} else {
144
150
Key :: Ignored . into ( )
145
151
}
146
152
}
147
153
}
148
154
149
155
#[ derive( Default ) ]
150
- pub struct KeyEventConverter {
156
+ pub struct WinitEventConverter {
151
157
mods : Mods ,
152
158
}
153
159
154
- impl KeyEventConverter {
160
+ impl WinitEventConverter {
155
161
pub fn mods ( & self ) -> Mods {
156
162
self . mods
157
163
}
@@ -160,7 +166,77 @@ impl KeyEventConverter {
160
166
self . mods = mods. state ( ) . into ( ) ;
161
167
}
162
168
163
- pub fn convert < C : KeyInputConvertible > ( & mut self , event : & C ) -> KeyInput {
164
- event. convert_key_input ( self )
169
+ pub fn convert < E : WinitEvent > ( & mut self , event : & E ) -> KeyInput {
170
+ event. to_key_input ( self )
171
+ }
172
+ }
173
+
174
+ #[ cfg( test) ]
175
+ mod tests {
176
+ use super :: * ;
177
+ use winit:: keyboard:: { NativeKey , SmolStr } ;
178
+ use NamedKey :: * ;
179
+ use WinitKey :: * ;
180
+
181
+ #[ test]
182
+ fn convert_key ( ) {
183
+ assert_eq ! ( Key :: from( Named ( Space ) ) , Key :: Char ( ' ' ) ) ;
184
+ assert_eq ! ( Key :: from( Named ( ArrowUp ) ) , Key :: Up ) ;
185
+ assert_eq ! ( Key :: from( Named ( F1 ) ) , Key :: F ( 1 ) ) ;
186
+ assert_eq ! ( Key :: from( Named ( Control ) ) , Key :: Ignored ) ;
187
+ assert_eq ! ( Key :: from( Named ( TVInput ) ) , Key :: Unidentified ) ;
188
+ assert_eq ! ( Key :: from( Character ( SmolStr :: new( "a" ) ) ) , Key :: Char ( 'a' ) ) ;
189
+ assert_eq ! ( Key :: from( Character ( SmolStr :: new( "A" ) ) ) , Key :: Char ( 'a' ) ) ;
190
+ assert_eq ! ( Key :: from( Character ( SmolStr :: new( "foo" ) ) ) , Key :: Unidentified ) ;
191
+ assert_eq ! (
192
+ Key :: from( Unidentified ( NativeKey :: Unidentified ) ) ,
193
+ Key :: Unidentified ,
194
+ ) ;
195
+ assert_eq ! ( Key :: from( Dead ( None ) ) , Key :: Unidentified ) ;
196
+ }
197
+
198
+ #[ test]
199
+ fn convert_modifiers_state ( ) {
200
+ assert_eq ! ( Mods :: from( ModifiersState :: CONTROL ) , Mods :: CTRL ) ;
201
+ assert_eq ! (
202
+ Mods :: from( ModifiersState :: CONTROL | ModifiersState :: SHIFT | ModifiersState :: ALT ) ,
203
+ Mods :: CTRL | Mods :: SHIFT | Mods :: ALT ,
204
+ ) ;
205
+ assert_eq ! ( Mods :: from( ModifiersState :: SUPER ) , Mods :: SUPER ) ;
206
+ }
207
+
208
+ // Unformatunately `WinitEventConverter::convert` is not testable because winit does not provide
209
+ // to create an instance of `KeyEvent`. It provides no constructor and contains some pvivate fields.
210
+ // Instead, we use `winit::keyboard::Key` directly.
211
+ impl WinitEvent for WinitKey {
212
+ fn to_key_input ( & self , conv : & mut WinitEventConverter ) -> KeyInput {
213
+ KeyInput {
214
+ key : self . into ( ) ,
215
+ mods : conv. mods ,
216
+ }
217
+ }
218
+ }
219
+
220
+ #[ test]
221
+ fn converter_mods_state ( ) {
222
+ let mut conv = WinitEventConverter :: default ( ) ;
223
+
224
+ // We cannot test `on_modifiers_changed` because `winit::event::Modifiers` does not provide
225
+ // a constructor.
226
+ assert_eq ! ( conv. mods( ) , Mods :: NONE ) ;
227
+
228
+ assert_eq ! ( conv. convert( & Named ( Space ) ) , KeyInput :: new( ' ' , Mods :: NONE ) ) ;
229
+ assert_eq ! (
230
+ conv. convert( & Character ( SmolStr :: new( "A" ) ) ) ,
231
+ KeyInput :: new( 'a' , Mods :: NONE ) ,
232
+ ) ;
233
+ assert_eq ! (
234
+ conv. convert( & Named ( TVInputHDMI1 ) ) ,
235
+ KeyInput :: new( Key :: Unidentified , Mods :: NONE ) ,
236
+ ) ;
237
+ assert_eq ! (
238
+ conv. convert( & Named ( Control ) ) ,
239
+ KeyInput :: new( Key :: Ignored , Mods :: NONE ) ,
240
+ ) ;
165
241
}
166
242
}
0 commit comments