From 72482048a0ddf42bc9cf98693de98205f18312a6 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Fri, 1 Mar 2024 08:37:25 +0100 Subject: [PATCH] Move monitor functionality out of the main window example --- examples/monitors.rs | 62 ++++++++++++++++++++++++++++++++++++++++++++ examples/window.rs | 44 ------------------------------- 2 files changed, 62 insertions(+), 44 deletions(-) create mode 100644 examples/monitors.rs diff --git a/examples/monitors.rs b/examples/monitors.rs new file mode 100644 index 000000000..c58be05d9 --- /dev/null +++ b/examples/monitors.rs @@ -0,0 +1,62 @@ +use std::error::Error; +use winit::{ + event::{Event, StartCause}, + event_loop::{ActiveEventLoop, EventLoop}, +}; + +fn main() -> Result<(), Box> { + 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 + ); + } + } +} diff --git a/examples/window.rs b/examples/window.rs index 3aa7f9909..6413f7dcf 100644 --- a/examples/window.rs +++ b/examples/window.rs @@ -57,7 +57,6 @@ fn main() -> Result<(), Box> { Event::NewEvents(_) => (), Event::Resumed => { println!("Resumed the event loop"); - state.dump_monitors(event_loop); // Create initial window. state @@ -375,49 +374,6 @@ impl Application { 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. fn process_key_binding(key: &str, mods: &ModifiersState) -> Option { KEY_BINDINGS.iter().find_map(|binding| {