mirror of
https://github.com/italicsjenga/winit-sonoma-fix.git
synced 2024-12-23 22:01:31 +11:00
eadd9a19b2
* Replace Closed event with CloseRequested and Destroyed Implements #434 The existing Closed event had ambiguous meaning, both in name and in cross-platform behavior. Closed is now split into two more precise events: * CloseRequested - the window has been requested to close, most commonly by having clicked the window's close button. Whether or not you respond by closing the window is up to you. * Destroyed - the window has been destroyed, and can no longer be safely used. Most notably, now you can reliably implement classic patterns like prompting the user to save their work before closing, and have the opportunity to perform any necessary cleanup. Migrating to the new API is straightforward. In most cases, you can simply replace all existing usages of Closed with CloseRequested. For more information, see the example programs, particularly handling_close and multiwindow. iOS applications must replace all usages of Closed with Destroyed, and require no other changes.
46 lines
1.4 KiB
Rust
46 lines
1.4 KiB
Rust
extern crate winit;
|
|
|
|
use winit::{ControlFlow, WindowEvent, ElementState, KeyboardInput};
|
|
|
|
fn main() {
|
|
let mut events_loop = winit::EventsLoop::new();
|
|
|
|
let window = winit::WindowBuilder::new().build(&events_loop).unwrap();
|
|
window.set_title("winit - Cursor grabbing test");
|
|
|
|
let mut grabbed = false;
|
|
|
|
events_loop.run_forever(|event| {
|
|
println!("{:?}", event);
|
|
|
|
match event {
|
|
winit::Event::WindowEvent { event, .. } => {
|
|
match event {
|
|
WindowEvent::KeyboardInput { input: KeyboardInput { state: ElementState::Pressed, .. }, .. } => {
|
|
if grabbed {
|
|
grabbed = false;
|
|
window.set_cursor_state(winit::CursorState::Normal)
|
|
.ok().expect("could not ungrab mouse cursor");
|
|
} else {
|
|
grabbed = true;
|
|
window.set_cursor_state(winit::CursorState::Grab)
|
|
.ok().expect("could not grab mouse cursor");
|
|
}
|
|
},
|
|
|
|
WindowEvent::CloseRequested => return ControlFlow::Break,
|
|
|
|
a @ WindowEvent::CursorMoved { .. } => {
|
|
println!("{:?}", a);
|
|
},
|
|
|
|
_ => (),
|
|
}
|
|
}
|
|
_ => {}
|
|
}
|
|
|
|
ControlFlow::Continue
|
|
});
|
|
}
|