2022-06-10 20:43:33 +10:00
|
|
|
#![allow(clippy::single_match)]
|
|
|
|
|
2020-09-10 11:58:30 +10:00
|
|
|
use simple_logger::SimpleLogger;
|
2019-06-22 01:33:15 +10:00
|
|
|
use winit::{
|
2023-05-29 04:02:59 +10:00
|
|
|
event::{ElementState, Event, KeyEvent, WindowEvent},
|
2022-04-10 11:32:02 +10:00
|
|
|
event_loop::EventLoop,
|
2023-05-29 04:02:59 +10:00
|
|
|
keyboard::Key,
|
2019-06-22 01:33:15 +10:00
|
|
|
window::WindowBuilder,
|
|
|
|
};
|
2019-02-06 02:30:33 +11:00
|
|
|
|
2023-06-20 04:46:38 +10:00
|
|
|
#[path = "util/fill.rs"]
|
|
|
|
mod fill;
|
|
|
|
|
2023-04-11 21:50:52 +10:00
|
|
|
fn main() -> Result<(), impl std::error::Error> {
|
2020-09-10 11:58:30 +10:00
|
|
|
SimpleLogger::new().init().unwrap();
|
2019-02-06 02:30:33 +11:00
|
|
|
let event_loop = EventLoop::new();
|
2018-04-25 06:20:40 +10:00
|
|
|
|
2023-06-20 04:46:38 +10:00
|
|
|
let window = WindowBuilder::new()
|
2018-04-25 06:20:40 +10:00
|
|
|
.with_title("Your faithful window")
|
2019-02-06 02:30:33 +11:00
|
|
|
.build(&event_loop)
|
2018-04-25 06:20:40 +10:00
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let mut close_requested = false;
|
|
|
|
|
2019-02-06 02:30:33 +11:00
|
|
|
event_loop.run(move |event, _, control_flow| {
|
2022-04-10 11:32:02 +10:00
|
|
|
control_flow.set_wait();
|
2018-04-25 06:20:40 +10:00
|
|
|
|
|
|
|
match event {
|
2019-06-22 01:33:15 +10:00
|
|
|
Event::WindowEvent { event, .. } => {
|
|
|
|
match event {
|
|
|
|
WindowEvent::CloseRequested => {
|
|
|
|
// `CloseRequested` is sent when the close button on the window is pressed (or
|
|
|
|
// through whatever other mechanisms the window manager provides for closing a
|
|
|
|
// window). If you don't handle this event, the close button won't actually do
|
|
|
|
// anything.
|
2018-04-25 06:20:40 +10:00
|
|
|
|
2019-06-22 01:33:15 +10:00
|
|
|
// A common thing to do here is prompt the user if they have unsaved work.
|
|
|
|
// Creating a proper dialog box for that is far beyond the scope of this
|
|
|
|
// example, so here we'll just respond to the Y and N keys.
|
|
|
|
println!("Are you ready to bid your window farewell? [Y/N]");
|
|
|
|
close_requested = true;
|
2018-04-25 06:20:40 +10:00
|
|
|
|
2019-06-22 01:33:15 +10:00
|
|
|
// In applications where you can safely close the window without further
|
|
|
|
// action from the user, this is generally where you'd handle cleanup before
|
|
|
|
// closing the window. How to close the window is detailed in the handler for
|
|
|
|
// the Y key.
|
2019-06-25 02:14:55 +10:00
|
|
|
}
|
2019-06-22 01:33:15 +10:00
|
|
|
WindowEvent::KeyboardInput {
|
2023-05-29 04:02:59 +10:00
|
|
|
event:
|
|
|
|
KeyEvent {
|
|
|
|
logical_key: key,
|
|
|
|
state: ElementState::Released,
|
2019-06-22 01:33:15 +10:00
|
|
|
..
|
|
|
|
},
|
|
|
|
..
|
|
|
|
} => {
|
2023-05-29 04:02:59 +10:00
|
|
|
// WARNING: Consider using `key_without_modifers()` if available on your platform.
|
|
|
|
// See the `key_binding` example
|
|
|
|
match key.as_ref() {
|
|
|
|
Key::Character("y") => {
|
2019-06-22 01:33:15 +10:00
|
|
|
if close_requested {
|
|
|
|
// This is where you'll want to do any cleanup you need.
|
|
|
|
println!("Buh-bye!");
|
2018-04-25 06:20:40 +10:00
|
|
|
|
2019-06-22 01:33:15 +10:00
|
|
|
// For a single-window application like this, you'd normally just
|
|
|
|
// break out of the event loop here. If you wanted to keep running the
|
|
|
|
// event loop (i.e. if it's a multi-window application), you need to
|
|
|
|
// drop the window. That closes it, and results in `Destroyed` being
|
|
|
|
// sent.
|
2022-04-10 11:32:02 +10:00
|
|
|
control_flow.set_exit();
|
2019-06-22 01:33:15 +10:00
|
|
|
}
|
2019-06-25 02:14:55 +10:00
|
|
|
}
|
2023-05-29 04:02:59 +10:00
|
|
|
Key::Character("n") => {
|
2019-06-22 01:33:15 +10:00
|
|
|
if close_requested {
|
|
|
|
println!("Your window will continue to stay by your side.");
|
|
|
|
close_requested = false;
|
|
|
|
}
|
2019-06-25 02:14:55 +10:00
|
|
|
}
|
2019-06-22 01:33:15 +10:00
|
|
|
_ => (),
|
2018-04-25 06:20:40 +10:00
|
|
|
}
|
2019-06-25 02:14:55 +10:00
|
|
|
}
|
2018-04-25 06:20:40 +10:00
|
|
|
_ => (),
|
2019-06-22 01:33:15 +10:00
|
|
|
}
|
2019-06-25 02:14:55 +10:00
|
|
|
}
|
2023-06-20 04:46:38 +10:00
|
|
|
Event::RedrawRequested(_) => {
|
|
|
|
fill::fill_window(&window);
|
|
|
|
}
|
2018-04-25 06:20:40 +10:00
|
|
|
_ => (),
|
|
|
|
}
|
2023-04-11 21:50:52 +10:00
|
|
|
})
|
2018-04-25 06:20:40 +10:00
|
|
|
}
|