Make examples set control_flow in a more realistic way (#1363)

* Make examples set control_flow in a more realistic way

* Format
This commit is contained in:
Osspial 2020-01-05 02:12:03 -05:00 committed by GitHub
parent d1c6506865
commit 028d3ec16d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 96 additions and 70 deletions

View file

@ -12,7 +12,10 @@ fn main() {
let mut cursor_idx = 0; let mut cursor_idx = 0;
event_loop.run(move |event, _, control_flow| match event { event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;
match event {
Event::WindowEvent { Event::WindowEvent {
event: event:
WindowEvent::KeyboardInput { WindowEvent::KeyboardInput {
@ -41,6 +44,7 @@ fn main() {
return; return;
} }
_ => (), _ => (),
}
}); });
} }

View file

@ -16,6 +16,7 @@ fn main() {
event_loop.run(move |event, _, control_flow| { event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait; *control_flow = ControlFlow::Wait;
match event { match event {
Event::WindowEvent { event, .. } => match event { Event::WindowEvent { event, .. } => match event {
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit, WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,

View file

@ -31,13 +31,17 @@ fn main() {
} }
}); });
event_loop.run(move |event, _, control_flow| match event { event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;
match event {
Event::UserEvent(event) => println!("user event: {:?}", event), Event::UserEvent(event) => println!("user event: {:?}", event),
Event::WindowEvent { Event::WindowEvent {
event: WindowEvent::CloseRequested, event: WindowEvent::CloseRequested,
.. ..
} => *control_flow = ControlFlow::Exit, } => *control_flow = ControlFlow::Exit,
_ => *control_flow = ControlFlow::Wait, _ => (),
}
}); });
} }

View file

@ -14,6 +14,7 @@ fn main() {
window.set_max_inner_size(Some(LogicalSize::new(800.0, 400.0))); window.set_max_inner_size(Some(LogicalSize::new(800.0, 400.0)));
event_loop.run(move |event, _, control_flow| { event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;
println!("{:?}", event); println!("{:?}", event);
match event { match event {
@ -21,7 +22,7 @@ fn main() {
event: WindowEvent::CloseRequested, event: WindowEvent::CloseRequested,
.. ..
} => *control_flow = ControlFlow::Exit, } => *control_flow = ControlFlow::Exit,
_ => *control_flow = ControlFlow::Wait, _ => (),
} }
}); });
} }

View file

@ -12,7 +12,10 @@ fn main() {
.build(&event_loop) .build(&event_loop)
.unwrap(); .unwrap();
event_loop.run(move |event, _, control_flow| match event { event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;
match event {
Event::WindowEvent { Event::WindowEvent {
event: WindowEvent::CloseRequested, event: WindowEvent::CloseRequested,
.. ..
@ -30,6 +33,7 @@ fn main() {
} }
} }
} }
_ => *control_flow = ControlFlow::Wait, _ => (),
}
}); });
} }

View file

@ -16,6 +16,7 @@ fn main() {
event_loop.run(move |event, event_loop, control_flow| { event_loop.run(move |event, event_loop, control_flow| {
*control_flow = ControlFlow::Wait; *control_flow = ControlFlow::Wait;
match event { match event {
Event::WindowEvent { event, window_id } => { Event::WindowEvent { event, window_id } => {
match event { match event {

View file

@ -1,8 +1,5 @@
use instant::Instant;
use std::time::Duration;
use winit::{ use winit::{
event::{Event, WindowEvent}, event::{ElementState, Event, WindowEvent},
event_loop::{ControlFlow, EventLoop}, event_loop::{ControlFlow, EventLoop},
window::WindowBuilder, window::WindowBuilder,
}; };
@ -15,18 +12,26 @@ fn main() {
.build(&event_loop) .build(&event_loop)
.unwrap(); .unwrap();
event_loop.run(move |event, _, control_flow| match event { event_loop.run(move |event, _, control_flow| {
Event::WindowEvent {
event: WindowEvent::CloseRequested,
..
} => *control_flow = ControlFlow::Exit,
Event::MainEventsCleared => {
window.request_redraw();
*control_flow = ControlFlow::WaitUntil(Instant::now() + Duration::new(1, 0))
}
Event::RedrawRequested(_) => {
println!("{:?}", event); println!("{:?}", event);
*control_flow = ControlFlow::Wait;
match event {
Event::WindowEvent { event, .. } => match event {
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
WindowEvent::MouseInput {
state: ElementState::Released,
..
} => {
window.request_redraw();
} }
_ => (), _ => (),
},
Event::RedrawRequested(_) => {
println!("\nredrawing!\n");
}
_ => (),
}
}); });
} }

View file

@ -18,6 +18,7 @@ fn main() {
event_loop.run(move |event, _, control_flow| { event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait; *control_flow = ControlFlow::Wait;
match event { match event {
Event::WindowEvent { event, .. } => match event { Event::WindowEvent { event, .. } => match event {
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit, WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,

View file

@ -16,6 +16,7 @@ fn main() {
window.set_title("A fantastic window!"); window.set_title("A fantastic window!");
event_loop.run(move |event, _, control_flow| { event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;
println!("{:?}", event); println!("{:?}", event);
match event { match event {
@ -23,7 +24,7 @@ fn main() {
event: WindowEvent::CloseRequested, event: WindowEvent::CloseRequested,
.. ..
} => *control_flow = ControlFlow::Exit, } => *control_flow = ControlFlow::Exit,
_ => *control_flow = ControlFlow::Wait, _ => (),
} }
}); });
} }

View file

@ -40,6 +40,8 @@ pub fn main() {
} }
event_loop.run(move |event, _, control_flow| { event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;
#[cfg(feature = "web-sys")] #[cfg(feature = "web-sys")]
log::debug!("{:?}", event); log::debug!("{:?}", event);
@ -54,7 +56,7 @@ pub fn main() {
Event::MainEventsCleared => { Event::MainEventsCleared => {
window.request_redraw(); window.request_redraw();
} }
_ => *control_flow = ControlFlow::Wait, _ => (),
} }
}); });
} }

View file

@ -13,6 +13,7 @@ fn main() {
.unwrap(); .unwrap();
event_loop.run(move |event, _, control_flow| { event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;
println!("{:?}", event); println!("{:?}", event);
match event { match event {
@ -23,7 +24,7 @@ fn main() {
Event::MainEventsCleared => { Event::MainEventsCleared => {
window.request_redraw(); window.request_redraw();
} }
_ => *control_flow = ControlFlow::Poll, _ => (),
} }
}); });
} }

View file

@ -27,6 +27,7 @@ fn main() {
event_loop.run(move |event, _, control_flow| { event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait; *control_flow = ControlFlow::Wait;
if let Event::WindowEvent { event, .. } = event { if let Event::WindowEvent { event, .. } = event {
use winit::event::WindowEvent::*; use winit::event::WindowEvent::*;
match event { match event {

View file

@ -38,7 +38,6 @@ fn main() {
.. ..
} => { } => {
quit = true; quit = true;
*control_flow = ControlFlow::Exit;
} }
Event::MainEventsCleared => { Event::MainEventsCleared => {
*control_flow = ControlFlow::Exit; *control_flow = ControlFlow::Exit;
@ -48,6 +47,7 @@ fn main() {
}); });
// Sleep for 1/60 second to simulate rendering // Sleep for 1/60 second to simulate rendering
println!("rendering");
sleep(Duration::from_millis(16)); sleep(Duration::from_millis(16));
} }
} }