mirror of
https://github.com/italicsjenga/winit-sonoma-fix.git
synced 2024-12-24 06:11:30 +11:00
Less redundancy and improve fullscreen in examples
Remove examples/minimize which is redundant
This commit is contained in:
parent
8646cbc9f5
commit
c55d97183d
|
@ -1,39 +1,44 @@
|
||||||
#![allow(clippy::single_match)]
|
#![allow(clippy::single_match)]
|
||||||
|
|
||||||
use std::io::{stdin, stdout, Write};
|
|
||||||
|
|
||||||
use simple_logger::SimpleLogger;
|
use simple_logger::SimpleLogger;
|
||||||
use winit::event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent};
|
use winit::event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent};
|
||||||
use winit::event_loop::EventLoop;
|
use winit::event_loop::EventLoop;
|
||||||
use winit::monitor::{MonitorHandle, VideoMode};
|
|
||||||
use winit::window::{Fullscreen, WindowBuilder};
|
use winit::window::{Fullscreen, WindowBuilder};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
SimpleLogger::new().init().unwrap();
|
SimpleLogger::new().init().unwrap();
|
||||||
let event_loop = EventLoop::new();
|
let event_loop = EventLoop::new();
|
||||||
|
|
||||||
print!("Please choose the fullscreen mode: (1) exclusive, (2) borderless: ");
|
|
||||||
stdout().flush().unwrap();
|
|
||||||
|
|
||||||
let mut num = String::new();
|
|
||||||
stdin().read_line(&mut num).unwrap();
|
|
||||||
let num = num.trim().parse().expect("Please enter a number");
|
|
||||||
|
|
||||||
let fullscreen = Some(match num {
|
|
||||||
1 => Fullscreen::Exclusive(prompt_for_video_mode(&prompt_for_monitor(&event_loop))),
|
|
||||||
2 => Fullscreen::Borderless(Some(prompt_for_monitor(&event_loop))),
|
|
||||||
_ => panic!("Please enter a valid number"),
|
|
||||||
});
|
|
||||||
|
|
||||||
let mut decorations = true;
|
let mut decorations = true;
|
||||||
|
let mut minimized = false;
|
||||||
|
|
||||||
let window = WindowBuilder::new()
|
let window = WindowBuilder::new()
|
||||||
.with_title("Hello world!")
|
.with_title("Hello world!")
|
||||||
.with_fullscreen(fullscreen.clone())
|
|
||||||
.build(&event_loop)
|
.build(&event_loop)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
event_loop.run(move |event, _, control_flow| {
|
let mut monitor_index = 0;
|
||||||
|
let mut monitor = event_loop
|
||||||
|
.available_monitors()
|
||||||
|
.next()
|
||||||
|
.expect("no monitor found!");
|
||||||
|
println!("Monitor: {:?}", monitor.name());
|
||||||
|
|
||||||
|
let mut mode_index = 0;
|
||||||
|
let mut mode = monitor.video_modes().next().expect("no mode found");
|
||||||
|
println!("Mode: {}", mode);
|
||||||
|
|
||||||
|
println!("Keys:");
|
||||||
|
println!("- Esc\tExit");
|
||||||
|
println!("- F\tToggle exclusive fullscreen mode");
|
||||||
|
println!("- B\tToggle borderless mode");
|
||||||
|
println!("- S\tNext screen");
|
||||||
|
println!("- M\tNext mode for this screen");
|
||||||
|
println!("- D\tToggle window decorations");
|
||||||
|
println!("- X\tMaximize window");
|
||||||
|
println!("- Z\tMinimize window");
|
||||||
|
|
||||||
|
event_loop.run(move |event, elwt, control_flow| {
|
||||||
control_flow.set_wait();
|
control_flow.set_wait();
|
||||||
|
|
||||||
match event {
|
match event {
|
||||||
|
@ -43,29 +48,60 @@ fn main() {
|
||||||
input:
|
input:
|
||||||
KeyboardInput {
|
KeyboardInput {
|
||||||
virtual_keycode: Some(virtual_code),
|
virtual_keycode: Some(virtual_code),
|
||||||
state,
|
state: ElementState::Pressed,
|
||||||
..
|
..
|
||||||
},
|
},
|
||||||
..
|
..
|
||||||
} => match (virtual_code, state) {
|
} => match virtual_code {
|
||||||
(VirtualKeyCode::Escape, _) => control_flow.set_exit(),
|
VirtualKeyCode::Escape => control_flow.set_exit(),
|
||||||
(VirtualKeyCode::F, ElementState::Pressed) => {
|
VirtualKeyCode::F | VirtualKeyCode::B if window.fullscreen().is_some() => {
|
||||||
if window.fullscreen().is_some() {
|
window.set_fullscreen(None);
|
||||||
window.set_fullscreen(None);
|
}
|
||||||
|
VirtualKeyCode::F => {
|
||||||
|
let fullscreen = Some(Fullscreen::Exclusive(mode.clone()));
|
||||||
|
println!("Setting mode: {fullscreen:?}");
|
||||||
|
window.set_fullscreen(fullscreen);
|
||||||
|
}
|
||||||
|
VirtualKeyCode::B => {
|
||||||
|
let fullscreen = Some(Fullscreen::Borderless(Some(monitor.clone())));
|
||||||
|
println!("Setting mode: {fullscreen:?}");
|
||||||
|
window.set_fullscreen(fullscreen);
|
||||||
|
}
|
||||||
|
VirtualKeyCode::S => {
|
||||||
|
monitor_index += 1;
|
||||||
|
if let Some(mon) = elwt.available_monitors().nth(monitor_index) {
|
||||||
|
monitor = mon;
|
||||||
} else {
|
} else {
|
||||||
window.set_fullscreen(fullscreen.clone());
|
monitor_index = 0;
|
||||||
|
monitor = elwt.available_monitors().next().expect("no monitor found!");
|
||||||
}
|
}
|
||||||
|
println!("Monitor: {:?}", monitor.name());
|
||||||
|
|
||||||
|
mode_index = 0;
|
||||||
|
mode = monitor.video_modes().next().expect("no mode found");
|
||||||
|
println!("Mode: {}", mode);
|
||||||
}
|
}
|
||||||
(VirtualKeyCode::S, ElementState::Pressed) => {
|
VirtualKeyCode::M => {
|
||||||
println!("window.fullscreen {:?}", window.fullscreen());
|
mode_index += 1;
|
||||||
|
if let Some(m) = monitor.video_modes().nth(mode_index) {
|
||||||
|
mode = m;
|
||||||
|
} else {
|
||||||
|
mode_index = 0;
|
||||||
|
mode = monitor.video_modes().next().expect("no mode found");
|
||||||
|
}
|
||||||
|
println!("Mode: {}", mode);
|
||||||
}
|
}
|
||||||
(VirtualKeyCode::M, ElementState::Pressed) => {
|
VirtualKeyCode::D => {
|
||||||
|
decorations = !decorations;
|
||||||
|
window.set_decorations(decorations);
|
||||||
|
}
|
||||||
|
VirtualKeyCode::X => {
|
||||||
let is_maximized = window.is_maximized();
|
let is_maximized = window.is_maximized();
|
||||||
window.set_maximized(!is_maximized);
|
window.set_maximized(!is_maximized);
|
||||||
}
|
}
|
||||||
(VirtualKeyCode::D, ElementState::Pressed) => {
|
VirtualKeyCode::Z => {
|
||||||
decorations = !decorations;
|
minimized = !minimized;
|
||||||
window.set_decorations(decorations);
|
window.set_minimized(minimized);
|
||||||
}
|
}
|
||||||
_ => (),
|
_ => (),
|
||||||
},
|
},
|
||||||
|
@ -75,46 +111,3 @@ fn main() {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Enumerate monitors and prompt user to choose one
|
|
||||||
fn prompt_for_monitor(event_loop: &EventLoop<()>) -> MonitorHandle {
|
|
||||||
for (num, monitor) in event_loop.available_monitors().enumerate() {
|
|
||||||
println!("Monitor #{}: {:?}", num, monitor.name());
|
|
||||||
}
|
|
||||||
|
|
||||||
print!("Please write the number of the monitor to use: ");
|
|
||||||
stdout().flush().unwrap();
|
|
||||||
|
|
||||||
let mut num = String::new();
|
|
||||||
stdin().read_line(&mut num).unwrap();
|
|
||||||
let num = num.trim().parse().expect("Please enter a number");
|
|
||||||
let monitor = event_loop
|
|
||||||
.available_monitors()
|
|
||||||
.nth(num)
|
|
||||||
.expect("Please enter a valid ID");
|
|
||||||
|
|
||||||
println!("Using {:?}", monitor.name());
|
|
||||||
|
|
||||||
monitor
|
|
||||||
}
|
|
||||||
|
|
||||||
fn prompt_for_video_mode(monitor: &MonitorHandle) -> VideoMode {
|
|
||||||
for (i, video_mode) in monitor.video_modes().enumerate() {
|
|
||||||
println!("Video mode #{}: {}", i, video_mode);
|
|
||||||
}
|
|
||||||
|
|
||||||
print!("Please write the number of the video mode to use: ");
|
|
||||||
stdout().flush().unwrap();
|
|
||||||
|
|
||||||
let mut num = String::new();
|
|
||||||
stdin().read_line(&mut num).unwrap();
|
|
||||||
let num = num.trim().parse().expect("Please enter a number");
|
|
||||||
let video_mode = monitor
|
|
||||||
.video_modes()
|
|
||||||
.nth(num)
|
|
||||||
.expect("Please enter a valid ID");
|
|
||||||
|
|
||||||
println!("Using {}", video_mode);
|
|
||||||
|
|
||||||
video_mode
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,32 +0,0 @@
|
||||||
#![allow(clippy::single_match)]
|
|
||||||
|
|
||||||
use simple_logger::SimpleLogger;
|
|
||||||
use winit::{
|
|
||||||
dpi::LogicalSize,
|
|
||||||
event::{Event, WindowEvent},
|
|
||||||
event_loop::EventLoop,
|
|
||||||
window::WindowBuilder,
|
|
||||||
};
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
SimpleLogger::new().init().unwrap();
|
|
||||||
let event_loop = EventLoop::new();
|
|
||||||
|
|
||||||
let window = WindowBuilder::new().build(&event_loop).unwrap();
|
|
||||||
|
|
||||||
window.set_min_inner_size(Some(LogicalSize::new(400.0, 200.0)));
|
|
||||||
window.set_max_inner_size(Some(LogicalSize::new(800.0, 400.0)));
|
|
||||||
|
|
||||||
event_loop.run(move |event, _, control_flow| {
|
|
||||||
control_flow.set_wait();
|
|
||||||
println!("{:?}", event);
|
|
||||||
|
|
||||||
match event {
|
|
||||||
Event::WindowEvent {
|
|
||||||
event: WindowEvent::CloseRequested,
|
|
||||||
..
|
|
||||||
} => control_flow.set_exit(),
|
|
||||||
_ => (),
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
|
@ -1,41 +0,0 @@
|
||||||
#![allow(clippy::single_match)]
|
|
||||||
|
|
||||||
use simple_logger::SimpleLogger;
|
|
||||||
use winit::event::{Event, VirtualKeyCode, WindowEvent};
|
|
||||||
use winit::event_loop::EventLoop;
|
|
||||||
use winit::window::WindowBuilder;
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
SimpleLogger::new().init().unwrap();
|
|
||||||
let event_loop = EventLoop::new();
|
|
||||||
|
|
||||||
let window = WindowBuilder::new()
|
|
||||||
.with_title("A fantastic window!")
|
|
||||||
.build(&event_loop)
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
event_loop.run(move |event, _, control_flow| {
|
|
||||||
control_flow.set_wait();
|
|
||||||
|
|
||||||
match event {
|
|
||||||
Event::WindowEvent {
|
|
||||||
event: WindowEvent::CloseRequested,
|
|
||||||
..
|
|
||||||
} => control_flow.set_exit(),
|
|
||||||
|
|
||||||
// Keyboard input event to handle minimize via a hotkey
|
|
||||||
Event::WindowEvent {
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => (),
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
|
@ -16,7 +16,9 @@ fn main() {
|
||||||
|
|
||||||
let window = WindowBuilder::new()
|
let window = WindowBuilder::new()
|
||||||
.with_title("Hit space to toggle resizability.")
|
.with_title("Hit space to toggle resizability.")
|
||||||
.with_inner_size(LogicalSize::new(400.0, 200.0))
|
.with_inner_size(LogicalSize::new(600.0, 300.0))
|
||||||
|
.with_min_inner_size(LogicalSize::new(400.0, 200.0))
|
||||||
|
.with_max_inner_size(LogicalSize::new(800.0, 400.0))
|
||||||
.with_resizable(resizable)
|
.with_resizable(resizable)
|
||||||
.build(&event_loop)
|
.build(&event_loop)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
Loading…
Reference in a new issue