From 05b02214f11fcd8a7cc6452c07187f4533a10da9 Mon Sep 17 00:00:00 2001 From: Kirill Chibisov Date: Fri, 27 Dec 2024 17:39:06 +0300 Subject: [PATCH] v2 --- src/changelog/unreleased.md | 4 ++-- src/monitor.rs | 28 +++++++++-------------- src/platform_impl/linux/x11/monitor.rs | 16 ++++--------- src/platform_impl/linux/x11/util/randr.rs | 9 +++++--- src/platform_impl/linux/x11/window.rs | 5 +--- 5 files changed, 25 insertions(+), 37 deletions(-) diff --git a/src/changelog/unreleased.md b/src/changelog/unreleased.md index 5b85dc75ad..b140e25f40 100644 --- a/src/changelog/unreleased.md +++ b/src/changelog/unreleased.md @@ -160,8 +160,8 @@ changelog entry. - On macOS, no longer emit `Focused` upon window creation. - On iOS, emit more events immediately, instead of queuing them. - Update `smol_str` to version `0.3` -- `VideoModeHandle` to `VideoMode` which stores plain data. -- `Fullscreen::Exclusive` to take `(MonitorHandle, VideoMode)`. +- Rename `VideoModeHandle` to `VideoMode` which stores plain data. +- Make `Fullscreen::Exclusive` to take `(MonitorHandle, VideoMode)`. ### Removed diff --git a/src/monitor.rs b/src/monitor.rs index a23b5f1b42..01f3fe5105 100644 --- a/src/monitor.rs +++ b/src/monitor.rs @@ -7,7 +7,7 @@ use crate::platform_impl; /// Describes a fullscreen video mode of a monitor. /// -/// Can be acquired with [`MonitorHandle::video_modes`]. +/// Can be retrieved with [`MonitorHandle::video_modes()`]. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct VideoMode { pub(crate) size: PhysicalSize, @@ -39,24 +39,18 @@ impl VideoMode { impl fmt::Display for VideoMode { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!( - f, - "{}x{} {}{}", - self.size.width, - self.size.height, - self.refresh_rate_millihertz.map(|rate| format!("@ {rate} mHz ")).unwrap_or_default(), - self.bit_depth.map(|bit_depth| format!("({bit_depth} bpp)")).unwrap_or_default(), - ) - } -} + write!(f, "{}x{}", self.size.width, self.size.height)?; -/// Fullscreen modes. -#[derive(Clone, Debug, PartialEq, Eq)] -pub enum Fullscreen { - Exclusive(MonitorHandle, VideoMode), + if let Some(refresh_rate) = self.refresh_rate_millihertz.as_ref() { + write!(f, "@{refresh_rate}mHz")?; + } - /// Providing `None` to `Borderless` will fullscreen on the current monitor. - Borderless(Option), + if let Some(bit_depth) = self.bit_depth.as_ref() { + write!(f, " ({bit_depth} bpp)")?; + } + + Ok(()) + } } /// Handle to a monitor. diff --git a/src/platform_impl/linux/x11/monitor.rs b/src/platform_impl/linux/x11/monitor.rs index 1bb02942bc..a3044bdbad 100644 --- a/src/platform_impl/linux/x11/monitor.rs +++ b/src/platform_impl/linux/x11/monitor.rs @@ -1,11 +1,11 @@ -use std::num::{NonZeroU16, NonZeroU32}; +use std::num::NonZeroU32; use x11rb::connection::RequestConnection; use x11rb::protocol::randr::{self, ConnectionExt as _}; use x11rb::protocol::xproto; use super::{util, X11Error, XConnection}; -use crate::dpi::{PhysicalPosition, PhysicalSize}; +use crate::dpi::PhysicalPosition; use crate::monitor::VideoMode; // Used for testing. This should always be committed as false. @@ -21,20 +21,14 @@ impl XConnection { #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct VideoModeHandle { pub(crate) current: bool, - pub(crate) size: PhysicalSize, - pub(crate) bit_depth: Option, - pub(crate) refresh_rate_millihertz: Option, + pub(crate) mode: VideoMode, pub(crate) native_mode: randr::Mode, pub(crate) monitor: Option, } impl From for VideoMode { - fn from(mode: VideoModeHandle) -> Self { - Self { - size: mode.size, - bit_depth: mode.bit_depth, - refresh_rate_millihertz: mode.refresh_rate_millihertz, - } + fn from(handle: VideoModeHandle) -> Self { + handle.mode } } diff --git a/src/platform_impl/linux/x11/util/randr.rs b/src/platform_impl/linux/x11/util/randr.rs index e29e60118d..5d0b37a2de 100644 --- a/src/platform_impl/linux/x11/util/randr.rs +++ b/src/platform_impl/linux/x11/util/randr.rs @@ -7,6 +7,7 @@ use x11rb::protocol::randr::{self, ConnectionExt as _}; use super::*; use crate::dpi::validate_scale_factor; +use crate::monitor::VideoMode; use crate::platform_impl::platform::x11::{monitor, VideoModeHandle}; /// Represents values of `WINIT_HIDPI_FACTOR`. @@ -85,9 +86,11 @@ impl XConnection { .map(|mode| { VideoModeHandle { current: mode.id == current_mode, - size: (mode.width as u32, mode.height as u32).into(), - refresh_rate_millihertz: monitor::mode_refresh_rate_millihertz(mode), - bit_depth: NonZeroU16::new(bit_depth as u16), + mode: VideoMode { + size: (mode.width as u32, mode.height as u32).into(), + refresh_rate_millihertz: monitor::mode_refresh_rate_millihertz(mode), + bit_depth: NonZeroU16::new(bit_depth as u16), + }, native_mode: mode.id, // This is populated in `MonitorHandle::video_modes` as the // video mode is returned to the user diff --git a/src/platform_impl/linux/x11/window.rs b/src/platform_impl/linux/x11/window.rs index 9712d679ac..0372685ea5 100644 --- a/src/platform_impl/linux/x11/window.rs +++ b/src/platform_impl/linux/x11/window.rs @@ -1088,10 +1088,7 @@ impl UnownedWindow { if let Some(native_mode) = video_mode.and_then(|requested| { monitor.video_modes.iter().find_map(|mode| { - if mode.refresh_rate_millihertz == requested.refresh_rate_millihertz - && mode.size == requested.size - && mode.bit_depth == requested.bit_depth - { + if mode.mode == requested { Some(mode.native_mode) } else { None