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,35 +12,39 @@ 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| {
Event::WindowEvent { *control_flow = ControlFlow::Wait;
event:
WindowEvent::KeyboardInput { match event {
input: Event::WindowEvent {
KeyboardInput { event:
state: ElementState::Pressed, WindowEvent::KeyboardInput {
.. input:
}, KeyboardInput {
.. state: ElementState::Pressed,
}, ..
.. },
} => { ..
println!("Setting cursor to \"{:?}\"", CURSORS[cursor_idx]); },
window.set_cursor_icon(CURSORS[cursor_idx]); ..
if cursor_idx < CURSORS.len() - 1 { } => {
cursor_idx += 1; println!("Setting cursor to \"{:?}\"", CURSORS[cursor_idx]);
} else { window.set_cursor_icon(CURSORS[cursor_idx]);
cursor_idx = 0; if cursor_idx < CURSORS.len() - 1 {
cursor_idx += 1;
} else {
cursor_idx = 0;
}
} }
Event::WindowEvent {
event: WindowEvent::CloseRequested,
..
} => {
*control_flow = ControlFlow::Exit;
return;
}
_ => (),
} }
Event::WindowEvent {
event: WindowEvent::CloseRequested,
..
} => {
*control_flow = ControlFlow::Exit;
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| {
Event::UserEvent(event) => println!("user event: {:?}", event), *control_flow = ControlFlow::Wait;
Event::WindowEvent {
event: WindowEvent::CloseRequested, match event {
.. Event::UserEvent(event) => println!("user event: {:?}", event),
} => *control_flow = ControlFlow::Exit, Event::WindowEvent {
_ => *control_flow = ControlFlow::Wait, event: WindowEvent::CloseRequested,
..
} => *control_flow = ControlFlow::Exit,
_ => (),
}
}); });
} }

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,24 +12,28 @@ 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 { *control_flow = ControlFlow::Wait;
event: WindowEvent::CloseRequested,
..
} => *control_flow = ControlFlow::Exit,
// Keyboard input event to handle minimize via a hotkey match event {
Event::WindowEvent { Event::WindowEvent {
event: WindowEvent::KeyboardInput { input, .. }, event: WindowEvent::CloseRequested,
window_id, ..
} => { } => *control_flow = ControlFlow::Exit,
if window_id == window.id() {
// Pressing the 'M' key will minimize the window // Keyboard input event to handle minimize via a hotkey
if input.virtual_keycode == Some(VirtualKeyCode::M) { Event::WindowEvent {
window.set_minimized(true); event: WindowEvent::KeyboardInput { input, .. },
window_id,
} => {
if window_id == window.id() {
// Pressing the 'M' key will minimize the window
if input.virtual_keycode == Some(VirtualKeyCode::M) {
window.set_minimized(true);
}
} }
} }
_ => (),
} }
_ => *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 { println!("{:?}", event);
event: WindowEvent::CloseRequested,
.. *control_flow = ControlFlow::Wait;
} => *control_flow = ControlFlow::Exit,
Event::MainEventsCleared => { match event {
window.request_redraw(); Event::WindowEvent { event, .. } => match event {
*control_flow = ControlFlow::WaitUntil(Instant::now() + Duration::new(1, 0)) WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
WindowEvent::MouseInput {
state: ElementState::Released,
..
} => {
window.request_redraw();
}
_ => (),
},
Event::RedrawRequested(_) => {
println!("\nredrawing!\n");
}
_ => (),
} }
Event::RedrawRequested(_) => {
println!("{:?}", event);
}
_ => (),
}); });
} }

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));
} }
} }