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},
|
|
|
|
event::{DeviceEvent, ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent},
|
|
|
|
event_loop::{ControlFlow, EventLoop},
|
|
|
|
window::{Fullscreen, WindowBuilder},
|
|
|
|
};
|
|
|
|
|
|
|
|
fn main() {
|
2020-09-10 11:58:30 +10:00
|
|
|
SimpleLogger::new().init().unwrap();
|
2019-11-23 11:11:04 +11:00
|
|
|
let event_loop = EventLoop::new();
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
event_loop.run(move |event, _, control_flow| {
|
|
|
|
*control_flow = ControlFlow::Wait;
|
|
|
|
|
|
|
|
match event {
|
|
|
|
Event::DeviceEvent {
|
|
|
|
event:
|
|
|
|
DeviceEvent::Key(KeyboardInput {
|
|
|
|
virtual_keycode: Some(key),
|
|
|
|
state: ElementState::Pressed,
|
|
|
|
..
|
|
|
|
}),
|
|
|
|
..
|
|
|
|
} => match key {
|
|
|
|
VirtualKeyCode::M => {
|
|
|
|
if minimized {
|
|
|
|
minimized = !minimized;
|
|
|
|
window.set_minimized(minimized);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
VirtualKeyCode::V => {
|
|
|
|
if !visible {
|
|
|
|
visible = !visible;
|
|
|
|
window.set_visible(visible);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
},
|
|
|
|
Event::WindowEvent {
|
|
|
|
event: WindowEvent::KeyboardInput { input, .. },
|
|
|
|
..
|
|
|
|
} => match input {
|
|
|
|
KeyboardInput {
|
|
|
|
virtual_keycode: Some(key),
|
|
|
|
state: ElementState::Pressed,
|
|
|
|
..
|
|
|
|
} => match key {
|
|
|
|
VirtualKeyCode::E => {
|
2020-01-05 05:03:44 +11:00
|
|
|
fn area(size: PhysicalSize<u32>) -> u32 {
|
2019-11-23 11:11:04 +11:00
|
|
|
size.width * size.height
|
|
|
|
}
|
|
|
|
|
2020-09-08 03:09:24 +10:00
|
|
|
let monitor = window.current_monitor().unwrap();
|
2020-01-05 05:03:44 +11:00
|
|
|
if let Some(mode) = monitor
|
|
|
|
.video_modes()
|
|
|
|
.max_by(|a, b| area(a.size()).cmp(&area(b.size())))
|
|
|
|
{
|
2019-11-23 11:11:04 +11:00
|
|
|
window.set_fullscreen(Some(Fullscreen::Exclusive(mode)));
|
|
|
|
} else {
|
|
|
|
eprintln!("no video modes available");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
VirtualKeyCode::F => {
|
|
|
|
if window.fullscreen().is_some() {
|
|
|
|
window.set_fullscreen(None);
|
|
|
|
} else {
|
2020-09-22 11:54:47 +10:00
|
|
|
let monitor = window.current_monitor();
|
2019-11-23 11:11:04 +11:00
|
|
|
window.set_fullscreen(Some(Fullscreen::Borderless(monitor)));
|
|
|
|
}
|
|
|
|
}
|
2020-09-22 11:54:47 +10:00
|
|
|
VirtualKeyCode::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
|
|
|
VirtualKeyCode::M => {
|
|
|
|
minimized = !minimized;
|
|
|
|
window.set_minimized(minimized);
|
|
|
|
}
|
|
|
|
VirtualKeyCode::Q => {
|
|
|
|
*control_flow = ControlFlow::Exit;
|
|
|
|
}
|
|
|
|
VirtualKeyCode::V => {
|
|
|
|
visible = !visible;
|
|
|
|
window.set_visible(visible);
|
|
|
|
}
|
|
|
|
VirtualKeyCode::X => {
|
2021-01-28 05:01:17 +11:00
|
|
|
let is_maximized = window.is_maximized();
|
|
|
|
window.set_maximized(!is_maximized);
|
2019-11-23 11:11:04 +11:00
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
},
|
|
|
|
_ => (),
|
|
|
|
},
|
|
|
|
Event::WindowEvent {
|
|
|
|
event: WindowEvent::CloseRequested,
|
|
|
|
window_id,
|
|
|
|
} if window_id == window.id() => *control_flow = ControlFlow::Exit,
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|