From ac685d3462bba65ba913f775aa391cda0747645b Mon Sep 17 00:00:00 2001 From: daxpedda Date: Tue, 23 Jul 2024 19:59:37 +0200 Subject: [PATCH] `refresh_rate_millihertz()` now returns a `NonZeroU32` --- examples/window.rs | 4 ++-- src/changelog/unreleased.md | 2 +- src/monitor.rs | 4 +++- src/platform_impl/android/mod.rs | 3 ++- src/platform_impl/apple/appkit/monitor.rs | 21 ++++++++++++--------- src/platform_impl/apple/uikit/monitor.rs | 11 ++++++----- src/platform_impl/linux/mod.rs | 3 ++- src/platform_impl/linux/wayland/output.rs | 10 ++++++---- src/platform_impl/linux/x11/monitor.rs | 12 ++++++++---- src/platform_impl/orbital/mod.rs | 3 ++- src/platform_impl/web/monitor.rs | 3 ++- src/platform_impl/windows/monitor.rs | 9 +++++---- 12 files changed, 51 insertions(+), 34 deletions(-) diff --git a/examples/window.rs b/examples/window.rs index 44bc3a4e3e..2f1aba8eda 100644 --- a/examples/window.rs +++ b/examples/window.rs @@ -241,7 +241,7 @@ impl Application { info!( " Current mode: {width}x{height}{}", if let Some(m_hz) = current_mode.refresh_rate_millihertz() { - format!(" @ {}.{} Hz", m_hz / 1000, m_hz % 1000) + format!(" @ {}.{} Hz", m_hz.get() / 1000, m_hz.get() % 1000) } else { String::new() } @@ -258,7 +258,7 @@ impl Application { let PhysicalSize { width, height } = mode.size(); let bits = mode.bit_depth(); let m_hz = if let Some(m_hz) = mode.refresh_rate_millihertz() { - format!(" @ {}.{} Hz", m_hz / 1000, m_hz % 1000) + format!(" @ {}.{} Hz", m_hz.get() / 1000, m_hz.get() % 1000) } else { String::new() }; diff --git a/src/changelog/unreleased.md b/src/changelog/unreleased.md index 5a2e5eb956..e830e7b1e7 100644 --- a/src/changelog/unreleased.md +++ b/src/changelog/unreleased.md @@ -71,7 +71,7 @@ changelog entry. - Change signature of `EventLoop::run_app`, `EventLoopExtPumpEvents::pump_app_events` and `EventLoopExtRunOnDemand::run_app_on_demand` to accept a `impl ApplicationHandler` directly, instead of requiring a `&mut` reference to it. -- `VideoModeHandle::refresh_rate_millihertz()` now returns an `Option`. +- `VideoModeHandle::refresh_rate_millihertz()` now returns an `Option`. ### Removed diff --git a/src/monitor.rs b/src/monitor.rs index 7f197f23b6..d1d3ac45fc 100644 --- a/src/monitor.rs +++ b/src/monitor.rs @@ -5,6 +5,8 @@ //! methods, which return an iterator of [`MonitorHandle`]: //! - [`ActiveEventLoop::available_monitors`][crate::event_loop::ActiveEventLoop::available_monitors]. //! - [`Window::available_monitors`][crate::window::Window::available_monitors]. +use std::num::NonZeroU32; + use crate::dpi::{PhysicalPosition, PhysicalSize}; use crate::platform_impl; @@ -73,7 +75,7 @@ impl VideoModeHandle { /// /// - **Android / Orbital:** Always returns [`None`]. #[inline] - pub fn refresh_rate_millihertz(&self) -> Option { + pub fn refresh_rate_millihertz(&self) -> Option { self.video_mode.refresh_rate_millihertz() } diff --git a/src/platform_impl/android/mod.rs b/src/platform_impl/android/mod.rs index bfcfd6a569..f631f1d0ab 100644 --- a/src/platform_impl/android/mod.rs +++ b/src/platform_impl/android/mod.rs @@ -2,6 +2,7 @@ use std::cell::Cell; use std::collections::VecDeque; use std::hash::Hash; use std::marker::PhantomData; +use std::num::NonZeroU32; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::{Arc, Mutex}; use std::time::{Duration, Instant}; @@ -1032,7 +1033,7 @@ impl VideoModeHandle { self.bit_depth } - pub fn refresh_rate_millihertz(&self) -> Option { + pub fn refresh_rate_millihertz(&self) -> Option { None } diff --git a/src/platform_impl/apple/appkit/monitor.rs b/src/platform_impl/apple/appkit/monitor.rs index d6ca7a41e0..0a0149b530 100644 --- a/src/platform_impl/apple/appkit/monitor.rs +++ b/src/platform_impl/apple/appkit/monitor.rs @@ -2,6 +2,7 @@ use std::collections::VecDeque; use std::fmt; +use std::num::NonZeroU32; use core_foundation::array::{CFArrayGetCount, CFArrayGetValueAtIndex}; use core_foundation::base::{CFRelease, TCFType}; @@ -21,7 +22,7 @@ use crate::dpi::{LogicalPosition, PhysicalPosition, PhysicalSize}; pub struct VideoModeHandle { size: PhysicalSize, bit_depth: u16, - refresh_rate_millihertz: Option, + refresh_rate_millihertz: Option, pub(crate) monitor: MonitorHandle, pub(crate) native_mode: NativeDisplayMode, } @@ -83,7 +84,7 @@ impl VideoModeHandle { fn new( monitor: MonitorHandle, mode: NativeDisplayMode, - refresh_rate_millihertz: Option, + refresh_rate_millihertz: Option, ) -> Self { unsafe { let pixel_encoding = @@ -120,7 +121,7 @@ impl VideoModeHandle { self.bit_depth } - pub fn refresh_rate_millihertz(&self) -> Option { + pub fn refresh_rate_millihertz(&self) -> Option { self.refresh_rate_millihertz } @@ -192,7 +193,6 @@ impl fmt::Debug for MonitorHandle { .field("native_identifier", &self.native_identifier()) .field("position", &self.position()) .field("scale_factor", &self.scale_factor()) - .field("refresh_rate_millihertz", &self.refresh_rate_millihertz()) .finish_non_exhaustive() } } @@ -234,7 +234,7 @@ impl MonitorHandle { }) } - fn refresh_rate_millihertz(&self) -> Option { + fn refresh_rate_millihertz(&self) -> Option { let current_display_mode = NativeDisplayMode(unsafe { CGDisplayCopyDisplayMode(self.0) } as _); refresh_rate_millihertz(self.0, ¤t_display_mode) @@ -272,7 +272,7 @@ impl MonitorHandle { // CGDisplayModeGetRefreshRate returns 0.0 for any display that // isn't a CRT let refresh_rate_millihertz = if cg_refresh_rate_hertz > 0 { - Some((cg_refresh_rate_hertz * 1000) as u32) + NonZeroU32::new((cg_refresh_rate_hertz * 1000) as u32) } else { refresh_rate_millihertz }; @@ -341,11 +341,11 @@ pub(crate) fn flip_window_screen_coordinates(frame: NSRect) -> NSPoint { NSPoint::new(frame.origin.x, y) } -fn refresh_rate_millihertz(id: CGDirectDisplayID, mode: &NativeDisplayMode) -> Option { +fn refresh_rate_millihertz(id: CGDirectDisplayID, mode: &NativeDisplayMode) -> Option { unsafe { let refresh_rate = ffi::CGDisplayModeGetRefreshRate(mode.0); if refresh_rate > 0.0 { - return Some((refresh_rate * 1000.0).round() as u32); + return NonZeroU32::new((refresh_rate * 1000.0).round() as u32); } let mut display_link = std::ptr::null_mut(); @@ -360,6 +360,9 @@ fn refresh_rate_millihertz(id: CGDirectDisplayID, mode: &NativeDisplayMode) -> O return None; } - (time.time_scale as i64).checked_div(time.time_value).map(|v| (v * 1000) as u32) + (time.time_scale as i64) + .checked_div(time.time_value) + .map(|v| (v * 1000) as u32) + .and_then(NonZeroU32::new) } } diff --git a/src/platform_impl/apple/uikit/monitor.rs b/src/platform_impl/apple/uikit/monitor.rs index 4485e25148..c074258691 100644 --- a/src/platform_impl/apple/uikit/monitor.rs +++ b/src/platform_impl/apple/uikit/monitor.rs @@ -1,6 +1,7 @@ #![allow(clippy::unnecessary_cast)] use std::collections::{BTreeSet, VecDeque}; +use std::num::NonZeroU32; use std::{fmt, hash, ptr}; use objc2::mutability::IsRetainable; @@ -45,7 +46,7 @@ impl Eq for MainThreadBoundDelegateImpls {} pub struct VideoModeHandle { pub(crate) size: (u32, u32), pub(crate) bit_depth: u16, - pub(crate) refresh_rate_millihertz: u32, + pub(crate) refresh_rate_millihertz: Option, screen_mode: MainThreadBoundDelegateImpls, pub(crate) monitor: MonitorHandle, } @@ -75,8 +76,8 @@ impl VideoModeHandle { self.bit_depth } - pub fn refresh_rate_millihertz(&self) -> Option { - Some(self.refresh_rate_millihertz) + pub fn refresh_rate_millihertz(&self) -> Option { + self.refresh_rate_millihertz } pub fn monitor(&self) -> MonitorHandle { @@ -213,7 +214,7 @@ impl MonitorHandle { } } -fn refresh_rate_millihertz(uiscreen: &UIScreen) -> u32 { +fn refresh_rate_millihertz(uiscreen: &UIScreen) -> Option { let refresh_rate_millihertz: NSInteger = { let os_capabilities = app_state::os_capabilities(); if os_capabilities.maximum_frames_per_second { @@ -234,7 +235,7 @@ fn refresh_rate_millihertz(uiscreen: &UIScreen) -> u32 { } }; - refresh_rate_millihertz as u32 * 1000 + NonZeroU32::new(refresh_rate_millihertz as u32 * 1000) } pub fn uiscreens(mtm: MainThreadMarker) -> VecDeque { diff --git a/src/platform_impl/linux/mod.rs b/src/platform_impl/linux/mod.rs index 1e57010762..cfe34081ab 100644 --- a/src/platform_impl/linux/mod.rs +++ b/src/platform_impl/linux/mod.rs @@ -4,6 +4,7 @@ compile_error!("Please select a feature to build for unix: `x11`, `wayland`"); use std::collections::VecDeque; +use std::num::NonZeroU32; use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd, RawFd}; use std::sync::Arc; use std::time::Duration; @@ -265,7 +266,7 @@ impl VideoModeHandle { } #[inline] - pub fn refresh_rate_millihertz(&self) -> Option { + pub fn refresh_rate_millihertz(&self) -> Option { x11_or_wayland!(match self; VideoModeHandle(m) => m.refresh_rate_millihertz()) } diff --git a/src/platform_impl/linux/wayland/output.rs b/src/platform_impl/linux/wayland/output.rs index 883bdf655a..8e329ddee5 100644 --- a/src/platform_impl/linux/wayland/output.rs +++ b/src/platform_impl/linux/wayland/output.rs @@ -1,3 +1,5 @@ +use std::num::NonZeroU32; + use sctk::output::{Mode, OutputData}; use sctk::reexports::client::protocol::wl_output::WlOutput; use sctk::reexports::client::Proxy; @@ -120,7 +122,7 @@ impl std::hash::Hash for MonitorHandle { pub struct VideoModeHandle { pub(crate) size: PhysicalSize, pub(crate) bit_depth: u16, - pub(crate) refresh_rate_millihertz: u32, + pub(crate) refresh_rate_millihertz: Option, pub(crate) monitor: MonitorHandle, } @@ -128,7 +130,7 @@ impl VideoModeHandle { fn new(monitor: MonitorHandle, mode: Mode) -> Self { VideoModeHandle { size: (mode.dimensions.0 as u32, mode.dimensions.1 as u32).into(), - refresh_rate_millihertz: mode.refresh_rate as u32, + refresh_rate_millihertz: NonZeroU32::new(mode.refresh_rate as u32), bit_depth: 32, monitor: monitor.clone(), } @@ -145,8 +147,8 @@ impl VideoModeHandle { } #[inline] - pub fn refresh_rate_millihertz(&self) -> Option { - Some(self.refresh_rate_millihertz) + pub fn refresh_rate_millihertz(&self) -> Option { + self.refresh_rate_millihertz } pub fn monitor(&self) -> MonitorHandle { diff --git a/src/platform_impl/linux/x11/monitor.rs b/src/platform_impl/linux/x11/monitor.rs index 1347b8e18a..da5d3039bb 100644 --- a/src/platform_impl/linux/x11/monitor.rs +++ b/src/platform_impl/linux/x11/monitor.rs @@ -1,3 +1,5 @@ +use std::num::NonZeroU32; + use x11rb::connection::RequestConnection; use x11rb::protocol::randr::{self, ConnectionExt as _}; use x11rb::protocol::xproto; @@ -21,7 +23,7 @@ pub struct VideoModeHandle { pub(crate) current: bool, pub(crate) size: (u32, u32), pub(crate) bit_depth: u16, - pub(crate) refresh_rate_millihertz: Option, + pub(crate) refresh_rate_millihertz: Option, pub(crate) native_mode: randr::Mode, pub(crate) monitor: Option, } @@ -38,7 +40,7 @@ impl VideoModeHandle { } #[inline] - pub fn refresh_rate_millihertz(&self) -> Option { + pub fn refresh_rate_millihertz(&self) -> Option { self.refresh_rate_millihertz } @@ -93,10 +95,12 @@ impl std::hash::Hash for MonitorHandle { } #[inline] -pub fn mode_refresh_rate_millihertz(mode: &randr::ModeInfo) -> Option { +pub fn mode_refresh_rate_millihertz(mode: &randr::ModeInfo) -> Option { if mode.dot_clock > 0 && mode.htotal > 0 && mode.vtotal > 0 { #[allow(clippy::unnecessary_cast)] - Some((mode.dot_clock as u64 * 1000 / (mode.htotal as u64 * mode.vtotal as u64)) as u32) + NonZeroU32::new( + (mode.dot_clock as u64 * 1000 / (mode.htotal as u64 * mode.vtotal as u64)) as u32, + ) } else { None } diff --git a/src/platform_impl/orbital/mod.rs b/src/platform_impl/orbital/mod.rs index 672d4a03ac..2aa765588c 100644 --- a/src/platform_impl/orbital/mod.rs +++ b/src/platform_impl/orbital/mod.rs @@ -1,6 +1,7 @@ #![cfg(target_os = "redox")] use std::fmt::{self, Display, Formatter}; +use std::num::NonZeroU32; use std::str; use std::sync::Arc; @@ -229,7 +230,7 @@ impl VideoModeHandle { self.bit_depth } - pub fn refresh_rate_millihertz(&self) -> Option { + pub fn refresh_rate_millihertz(&self) -> Option { // TODO None } diff --git a/src/platform_impl/web/monitor.rs b/src/platform_impl/web/monitor.rs index 18c4bb326d..08590f45c8 100644 --- a/src/platform_impl/web/monitor.rs +++ b/src/platform_impl/web/monitor.rs @@ -1,4 +1,5 @@ use std::iter::Empty; +use std::num::NonZeroU32; use crate::dpi::{PhysicalPosition, PhysicalSize}; @@ -39,7 +40,7 @@ impl VideoModeHandle { unreachable!(); } - pub fn refresh_rate_millihertz(&self) -> Option { + pub fn refresh_rate_millihertz(&self) -> Option { unreachable!(); } diff --git a/src/platform_impl/windows/monitor.rs b/src/platform_impl/windows/monitor.rs index e179e85eb1..d4429e4d17 100644 --- a/src/platform_impl/windows/monitor.rs +++ b/src/platform_impl/windows/monitor.rs @@ -1,5 +1,6 @@ use std::collections::{BTreeSet, VecDeque}; use std::hash::Hash; +use std::num::NonZeroU32; use std::{io, mem, ptr}; use windows_sys::Win32::Foundation::{BOOL, HWND, LPARAM, POINT, RECT}; @@ -21,7 +22,7 @@ use crate::platform_impl::platform::window::Window; pub struct VideoModeHandle { pub(crate) size: (u32, u32), pub(crate) bit_depth: u16, - pub(crate) refresh_rate_millihertz: u32, + pub(crate) refresh_rate_millihertz: Option, pub(crate) monitor: MonitorHandle, // DEVMODEW is huge so we box it to avoid blowing up the size of winit::window::Fullscreen pub(crate) native_video_mode: Box, @@ -67,7 +68,7 @@ impl VideoModeHandle { VideoModeHandle { size: (mode.dmPelsWidth, mode.dmPelsHeight), bit_depth: mode.dmBitsPerPel as u16, - refresh_rate_millihertz: mode.dmDisplayFrequency * 1000, + refresh_rate_millihertz: NonZeroU32::new(mode.dmDisplayFrequency * 1000), monitor, native_video_mode: Box::new(mode), } @@ -81,8 +82,8 @@ impl VideoModeHandle { self.bit_depth } - pub fn refresh_rate_millihertz(&self) -> Option { - Some(self.refresh_rate_millihertz) + pub fn refresh_rate_millihertz(&self) -> Option { + self.refresh_rate_millihertz } pub fn monitor(&self) -> MonitorHandle {