@@ -19,8 +19,9 @@ use iced_winit::winit;
1919use iced_winit:: Clipboard ;
2020
2121use winit:: {
22- event:: { Event , ModifiersState , WindowEvent } ,
22+ event:: { Event , WindowEvent } ,
2323 event_loop:: { ControlFlow , EventLoop } ,
24+ keyboard:: ModifiersState ,
2425} ;
2526
2627#[ cfg( target_arch = "wasm32" ) ]
@@ -48,7 +49,7 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
4849 tracing_subscriber:: fmt:: init ( ) ;
4950
5051 // Initialize winit
51- let event_loop = EventLoop :: new ( ) ;
52+ let event_loop = EventLoop :: new ( ) ? ;
5253
5354 #[ cfg( target_arch = "wasm32" ) ]
5455 let window = winit:: window:: WindowBuilder :: new ( )
@@ -160,67 +161,15 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
160161 ) ;
161162
162163 // Run event loop
163- event_loop. run ( move |event, _ , control_flow | {
164+ event_loop. run ( move |event, window_target | {
164165 // You should change this if you want to render continuosly
165- * control_flow = ControlFlow :: Wait ;
166+ window_target . set_control_flow ( ControlFlow :: Wait ) ;
166167
167168 match event {
168- Event :: WindowEvent { event, .. } => {
169- match event {
170- WindowEvent :: CursorMoved { position, .. } => {
171- cursor_position = Some ( position) ;
172- }
173- WindowEvent :: ModifiersChanged ( new_modifiers) => {
174- modifiers = new_modifiers;
175- }
176- WindowEvent :: Resized ( _) => {
177- resized = true ;
178- }
179- WindowEvent :: CloseRequested => {
180- * control_flow = ControlFlow :: Exit ;
181- }
182- _ => { }
183- }
184-
185- // Map window event to iced event
186- if let Some ( event) = iced_winit:: conversion:: window_event (
187- window:: Id :: MAIN ,
188- & event,
189- window. scale_factor ( ) ,
190- modifiers,
191- ) {
192- state. queue_event ( event) ;
193- }
194- }
195- Event :: MainEventsCleared => {
196- // If there are events pending
197- if !state. is_queue_empty ( ) {
198- // We update iced
199- let _ = state. update (
200- viewport. logical_size ( ) ,
201- cursor_position
202- . map ( |p| {
203- conversion:: cursor_position (
204- p,
205- viewport. scale_factor ( ) ,
206- )
207- } )
208- . map ( mouse:: Cursor :: Available )
209- . unwrap_or ( mouse:: Cursor :: Unavailable ) ,
210- & mut renderer,
211- & Theme :: Dark ,
212- & renderer:: Style {
213- text_color : Color :: WHITE ,
214- } ,
215- & mut clipboard,
216- & mut debug,
217- ) ;
218-
219- // and request a redraw
220- window. request_redraw ( ) ;
221- }
222- }
223- Event :: RedrawRequested ( _) => {
169+ Event :: WindowEvent {
170+ event : WindowEvent :: RedrawRequested ,
171+ ..
172+ } => {
224173 if resized {
225174 let size = window. inner_size ( ) ;
226175
@@ -309,7 +258,60 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
309258 } ,
310259 }
311260 }
261+ Event :: WindowEvent { event, .. } => {
262+ match event {
263+ WindowEvent :: CursorMoved { position, .. } => {
264+ cursor_position = Some ( position) ;
265+ }
266+ WindowEvent :: ModifiersChanged ( new_modifiers) => {
267+ modifiers = new_modifiers. state ( ) ;
268+ }
269+ WindowEvent :: Resized ( _) => {
270+ resized = true ;
271+ }
272+ WindowEvent :: CloseRequested => {
273+ window_target. exit ( ) ;
274+ }
275+ _ => { }
276+ }
277+
278+ // Map window event to iced event
279+ if let Some ( event) = iced_winit:: conversion:: window_event (
280+ window:: Id :: MAIN ,
281+ & event,
282+ window. scale_factor ( ) ,
283+ modifiers,
284+ ) {
285+ state. queue_event ( event) ;
286+ }
287+ }
312288 _ => { }
313289 }
314- } )
290+
291+ // If there are events pending
292+ if !state. is_queue_empty ( ) {
293+ // We update iced
294+ let _ = state. update (
295+ viewport. logical_size ( ) ,
296+ cursor_position
297+ . map ( |p| {
298+ conversion:: cursor_position ( p, viewport. scale_factor ( ) )
299+ } )
300+ . map ( mouse:: Cursor :: Available )
301+ . unwrap_or ( mouse:: Cursor :: Unavailable ) ,
302+ & mut renderer,
303+ & Theme :: Dark ,
304+ & renderer:: Style {
305+ text_color : Color :: WHITE ,
306+ } ,
307+ & mut clipboard,
308+ & mut debug,
309+ ) ;
310+
311+ // and request a redraw
312+ window. request_redraw ( ) ;
313+ }
314+ } ) ?;
315+
316+ Ok ( ( ) )
315317}
0 commit comments