2022-06-10 20:43:33 +10:00
|
|
|
#![allow(clippy::single_match)]
|
|
|
|
|
2019-11-23 11:11:04 +11:00
|
|
|
// This example is used by developers to test various window functions.
|
|
|
|
|
2020-09-10 11:58:30 +10:00
|
|
|
use simple_logger::SimpleLogger;
|
2019-11-23 11:11:04 +11:00
|
|
|
use winit::{
|
|
|
|
dpi::{LogicalSize, PhysicalSize},
|
2023-05-29 04:02:59 +10:00
|
|
|
event::{DeviceEvent, ElementState, Event, KeyEvent, RawKeyEvent, WindowEvent},
|
2023-05-31 05:32:31 +10:00
|
|
|
event_loop::{DeviceEvents, EventLoop},
|
2023-05-29 04:02:59 +10:00
|
|
|
keyboard::{Key, KeyCode},
|
2019-11-23 11:11:04 +11:00
|
|
|
window::{Fullscreen, WindowBuilder},
|
|
|
|
};
|
|
|
|
|
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();
|
2023-08-14 05:20:09 +10:00
|
|
|
let event_loop = EventLoop::new().unwrap();
|
2019-11-23 11:11:04 +11:00
|
|
|
|
|
|
|
let window = WindowBuilder::new()
|
|
|
|
.with_title("A fantastic window!")
|
2020-01-05 05:03:44 +11:00
|
|
|
.with_inner_size(LogicalSize::new(100.0, 100.0))
|
2019-11-23 11:11:04 +11:00
|
|
|
.build(&event_loop)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
eprintln!("debugging keys:");
|
|
|
|
eprintln!(" (E) Enter exclusive fullscreen");
|
|
|
|
eprintln!(" (F) Toggle borderless fullscreen");
|
2020-09-22 11:54:47 +10:00
|
|
|
eprintln!(" (P) Toggle borderless fullscreen on system's preffered monitor");
|
2019-11-23 11:11:04 +11:00
|
|
|
eprintln!(" (M) Toggle minimized");
|
|
|
|
eprintln!(" (Q) Quit event loop");
|
|
|
|
eprintln!(" (V) Toggle visibility");
|
|
|
|
eprintln!(" (X) Toggle maximized");
|
|
|
|
|
|
|
|
let mut minimized = false;
|
|
|
|
let mut visible = true;
|
|
|
|
|
2023-05-31 05:32:31 +10:00
|
|
|
event_loop.listen_device_events(DeviceEvents::Always);
|
2022-06-10 22:39:02 +10:00
|
|
|
|
2019-11-23 11:11:04 +11:00
|
|
|
event_loop.run(move |event, _, control_flow| {
|
2022-04-10 11:32:02 +10:00
|
|
|
control_flow.set_wait();
|
2019-11-23 11:11:04 +11:00
|
|
|
|
|
|
|
match event {
|
2023-05-29 04:02:59 +10:00
|
|
|
// This used to use the virtual key, but the new API
|
|
|
|
// only provides the `physical_key` (`Code`).
|
2019-11-23 11:11:04 +11:00
|
|
|
Event::DeviceEvent {
|
|
|
|
event:
|
2023-05-29 04:02:59 +10:00
|
|
|
DeviceEvent::Key(RawKeyEvent {
|
|
|
|
physical_key,
|
|
|
|
state: ElementState::Released,
|
2019-11-23 11:11:04 +11:00
|
|
|
..
|
|
|
|
}),
|
|
|
|
..
|
2023-05-29 04:02:59 +10:00
|
|
|
} => match physical_key {
|
|
|
|
KeyCode::KeyM => {
|
2019-11-23 11:11:04 +11:00
|
|
|
if minimized {
|
|
|
|
minimized = !minimized;
|
|
|
|
window.set_minimized(minimized);
|
2022-12-10 05:33:11 +11:00
|
|
|
window.focus_window();
|
2019-11-23 11:11:04 +11:00
|
|
|
}
|
|
|
|
}
|
2023-05-29 04:02:59 +10:00
|
|
|
KeyCode::KeyV => {
|
2019-11-23 11:11:04 +11:00
|
|
|
if !visible {
|
|
|
|
visible = !visible;
|
|
|
|
window.set_visible(visible);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
},
|
2023-08-28 00:15:09 +10:00
|
|
|
Event::WindowEvent { window_id, event } => match event {
|
|
|
|
WindowEvent::KeyboardInput {
|
|
|
|
event:
|
|
|
|
KeyEvent {
|
|
|
|
logical_key: Key::Character(key_str),
|
|
|
|
state: ElementState::Pressed,
|
|
|
|
..
|
|
|
|
},
|
|
|
|
..
|
|
|
|
} => match key_str.as_ref() {
|
|
|
|
// WARNING: Consider using `key_without_modifers()` if available on your platform.
|
|
|
|
// See the `key_binding` example
|
|
|
|
"e" => {
|
|
|
|
fn area(size: PhysicalSize<u32>) -> u32 {
|
|
|
|
size.width * size.height
|
|
|
|
}
|
2022-06-10 20:43:33 +10:00
|
|
|
|
2023-08-28 00:15:09 +10:00
|
|
|
let monitor = window.current_monitor().unwrap();
|
|
|
|
if let Some(mode) = monitor
|
|
|
|
.video_modes()
|
|
|
|
.max_by(|a, b| area(a.size()).cmp(&area(b.size())))
|
|
|
|
{
|
|
|
|
window.set_fullscreen(Some(Fullscreen::Exclusive(mode)));
|
|
|
|
} else {
|
|
|
|
eprintln!("no video modes available");
|
|
|
|
}
|
2019-11-23 11:11:04 +11:00
|
|
|
}
|
2023-08-28 00:15:09 +10:00
|
|
|
"f" => {
|
|
|
|
if window.fullscreen().is_some() {
|
|
|
|
window.set_fullscreen(None);
|
|
|
|
} else {
|
|
|
|
let monitor = window.current_monitor();
|
|
|
|
window.set_fullscreen(Some(Fullscreen::Borderless(monitor)));
|
|
|
|
}
|
2019-11-23 11:11:04 +11:00
|
|
|
}
|
2023-08-28 00:15:09 +10:00
|
|
|
"p" => {
|
|
|
|
if window.fullscreen().is_some() {
|
|
|
|
window.set_fullscreen(None);
|
|
|
|
} else {
|
|
|
|
window.set_fullscreen(Some(Fullscreen::Borderless(None)));
|
|
|
|
}
|
2019-11-23 11:11:04 +11:00
|
|
|
}
|
2023-08-28 00:15:09 +10:00
|
|
|
"m" => {
|
|
|
|
minimized = !minimized;
|
|
|
|
window.set_minimized(minimized);
|
|
|
|
}
|
|
|
|
"q" => {
|
|
|
|
control_flow.set_exit();
|
|
|
|
}
|
|
|
|
"v" => {
|
|
|
|
visible = !visible;
|
|
|
|
window.set_visible(visible);
|
|
|
|
}
|
|
|
|
"x" => {
|
|
|
|
let is_maximized = window.is_maximized();
|
|
|
|
window.set_maximized(!is_maximized);
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
},
|
|
|
|
WindowEvent::CloseRequested if window_id == window.id() => control_flow.set_exit(),
|
|
|
|
WindowEvent::RedrawRequested => {
|
|
|
|
fill::fill_window(&window);
|
2022-06-10 20:43:33 +10:00
|
|
|
}
|
2019-11-23 11:11:04 +11:00
|
|
|
_ => (),
|
|
|
|
},
|
|
|
|
_ => (),
|
|
|
|
}
|
2023-04-11 21:50:52 +10:00
|
|
|
})
|
2019-11-23 11:11:04 +11:00
|
|
|
}
|