mirror of
https://github.com/rust-windowing/winit.git
synced 2026-09-01 06:10:07 -04:00
Move monitor functionality out of the main window example
This commit is contained in:
62
examples/monitors.rs
Normal file
62
examples/monitors.rs
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
use std::error::Error;
|
||||||
|
use winit::{
|
||||||
|
event::{Event, StartCause},
|
||||||
|
event_loop::{ActiveEventLoop, EventLoop},
|
||||||
|
};
|
||||||
|
|
||||||
|
fn main() -> Result<(), Box<dyn Error>> {
|
||||||
|
let event_loop = EventLoop::new()?;
|
||||||
|
|
||||||
|
Ok(event_loop.run(|event, event_loop| match event {
|
||||||
|
Event::NewEvents(StartCause::Init) => {
|
||||||
|
dump_monitors(event_loop);
|
||||||
|
event_loop.exit()
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
})?)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn dump_monitors(event_loop: &ActiveEventLoop) {
|
||||||
|
println!("Monitors information");
|
||||||
|
let primary_monitor = event_loop.primary_monitor();
|
||||||
|
for monitor in event_loop.available_monitors() {
|
||||||
|
let intro = if primary_monitor.as_ref() == Some(&monitor) {
|
||||||
|
"Primary monitor"
|
||||||
|
} else {
|
||||||
|
"Monitor"
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Some(name) = monitor.name() {
|
||||||
|
println!("{intro}: {name}");
|
||||||
|
} else {
|
||||||
|
println!("{intro}: [no name]");
|
||||||
|
}
|
||||||
|
|
||||||
|
let size = monitor.size();
|
||||||
|
print!(" Current mode: {}x{}", size.width, size.height);
|
||||||
|
if let Some(m_hz) = monitor.refresh_rate_millihertz() {
|
||||||
|
println!(" @ {}.{} Hz", m_hz / 1000, m_hz % 1000);
|
||||||
|
} else {
|
||||||
|
println!();
|
||||||
|
}
|
||||||
|
|
||||||
|
let position = monitor.position();
|
||||||
|
println!(" Position: {}, {}", position.x, position.y);
|
||||||
|
|
||||||
|
println!(" Scale factor: {}", monitor.scale_factor());
|
||||||
|
|
||||||
|
println!(" Available modes (width x height x bit-depth):");
|
||||||
|
for mode in monitor.video_modes() {
|
||||||
|
let size = mode.size();
|
||||||
|
let m_hz = mode.refresh_rate_millihertz();
|
||||||
|
println!(
|
||||||
|
" {:04}x{:04}x{:02} @ {:>3}.{} Hz",
|
||||||
|
size.width,
|
||||||
|
size.height,
|
||||||
|
mode.bit_depth(),
|
||||||
|
m_hz / 1000,
|
||||||
|
m_hz % 1000
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -57,7 +57,6 @@ fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
Event::NewEvents(_) => (),
|
Event::NewEvents(_) => (),
|
||||||
Event::Resumed => {
|
Event::Resumed => {
|
||||||
println!("Resumed the event loop");
|
println!("Resumed the event loop");
|
||||||
state.dump_monitors(event_loop);
|
|
||||||
|
|
||||||
// Create initial window.
|
// Create initial window.
|
||||||
state
|
state
|
||||||
@@ -375,49 +374,6 @@ impl Application {
|
|||||||
println!("Device event: {event:?}");
|
println!("Device event: {event:?}");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn dump_monitors(&self, event_loop: &ActiveEventLoop) {
|
|
||||||
println!("Monitors information");
|
|
||||||
let primary_monitor = event_loop.primary_monitor();
|
|
||||||
for monitor in event_loop.available_monitors() {
|
|
||||||
let intro = if primary_monitor.as_ref() == Some(&monitor) {
|
|
||||||
"Primary monitor"
|
|
||||||
} else {
|
|
||||||
"Monitor"
|
|
||||||
};
|
|
||||||
|
|
||||||
if let Some(name) = monitor.name() {
|
|
||||||
println!("{intro}: {name}");
|
|
||||||
} else {
|
|
||||||
println!("{intro}: [no name]");
|
|
||||||
}
|
|
||||||
|
|
||||||
let PhysicalSize { width, height } = monitor.size();
|
|
||||||
print!(" Current mode: {width}x{height}");
|
|
||||||
if let Some(m_hz) = monitor.refresh_rate_millihertz() {
|
|
||||||
println!(" @ {}.{} Hz", m_hz / 1000, m_hz % 1000);
|
|
||||||
} else {
|
|
||||||
println!();
|
|
||||||
}
|
|
||||||
|
|
||||||
let PhysicalPosition { x, y } = monitor.position();
|
|
||||||
println!(" Position: {x},{y}");
|
|
||||||
|
|
||||||
println!(" Scale factor: {}", monitor.scale_factor());
|
|
||||||
|
|
||||||
println!(" Available modes (width x height x bit-depth):");
|
|
||||||
for mode in monitor.video_modes() {
|
|
||||||
let PhysicalSize { width, height } = mode.size();
|
|
||||||
let bits = mode.bit_depth();
|
|
||||||
let m_hz = mode.refresh_rate_millihertz();
|
|
||||||
println!(
|
|
||||||
" {width}x{height}x{bits} @ {}.{} Hz",
|
|
||||||
m_hz / 1000,
|
|
||||||
m_hz % 1000
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Process the key binding.
|
/// Process the key binding.
|
||||||
fn process_key_binding(key: &str, mods: &ModifiersState) -> Option<Action> {
|
fn process_key_binding(key: &str, mods: &ModifiersState) -> Option<Action> {
|
||||||
KEY_BINDINGS.iter().find_map(|binding| {
|
KEY_BINDINGS.iter().find_map(|binding| {
|
||||||
|
|||||||
Reference in New Issue
Block a user