From 6d4bbd2edd66a5d20a0bdd970f80f799a7c7e509 Mon Sep 17 00:00:00 2001 From: daxpedda Date: Thu, 25 Jul 2024 01:39:08 +0200 Subject: [PATCH] `VideoModeHandle::size()` and `bit_depth()` now return an `Option` On Android and Orbital `MonitorHandle::name()` now returns `None` instead of a dummy name. --- examples/window.rs | 41 +++-- src/changelog/unreleased.md | 2 + src/monitor.rs | 26 +-- src/platform_impl/android/mod.rs | 23 +-- src/platform_impl/apple/appkit/monitor.rs | 12 +- src/platform_impl/apple/uikit/monitor.rs | 14 +- src/platform_impl/linux/mod.rs | 6 +- src/platform_impl/linux/wayland/output.rs | 16 +- src/platform_impl/linux/x11/monitor.rs | 14 +- src/platform_impl/linux/x11/window.rs | 2 +- src/platform_impl/orbital/mod.rs | 23 +-- src/platform_impl/web/monitor.rs | 203 ++++++++++++++-------- src/platform_impl/windows/monitor.rs | 12 +- src/platform_impl/windows/window.rs | 2 +- 14 files changed, 212 insertions(+), 184 deletions(-) diff --git a/examples/window.rs b/examples/window.rs index f8fef8ff55..78f96de610 100644 --- a/examples/window.rs +++ b/examples/window.rs @@ -285,32 +285,37 @@ impl Application { } if let Some(current_mode) = monitor.current_video_mode() { - let PhysicalSize { width, height } = current_mode.size(); - info!( - " Current mode: {width}x{height}{}", - if let Some(m_hz) = current_mode.refresh_rate_millihertz() { - format!(" @ {}.{} Hz", m_hz.get() / 1000, m_hz.get() % 1000) - } else { - String::new() - } - ); + if let Some(PhysicalSize { width, height }) = current_mode.size() { + let bits = + current_mode.bit_depth().map(|bits| format!("x{bits}")).unwrap_or_default(); + let m_hz = current_mode + .refresh_rate_millihertz() + .map(|m_hz| format!(" @ {}.{} Hz", m_hz.get() / 1000, m_hz.get() % 1000)) + .unwrap_or_default(); + info!(" {width}x{height}{bits}{m_hz}"); + } else { + info!(" {current_mode:?}"); + } } - let PhysicalPosition { x, y } = monitor.position(); - info!(" Position: {x},{y}"); + if let Some(PhysicalPosition { x, y }) = monitor.position() { + info!(" Position: {x},{y}"); + } info!(" Scale factor: {}", monitor.scale_factor()); info!(" 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 = if let Some(m_hz) = mode.refresh_rate_millihertz() { - format!(" @ {}.{} Hz", m_hz.get() / 1000, m_hz.get() % 1000) + if let Some(PhysicalSize { width, height }) = mode.size() { + let bits = mode.bit_depth().map(|bits| format!("x{bits}")).unwrap_or_default(); + let m_hz = mode + .refresh_rate_millihertz() + .map(|m_hz| format!(" @ {}.{} Hz", m_hz.get() / 1000, m_hz.get() % 1000)) + .unwrap_or_default(); + info!(" {width}x{height}{bits}{m_hz}"); } else { - String::new() - }; - info!(" {width}x{height}x{bits}{m_hz}"); + info!(" {mode:?}"); + } } } } diff --git a/src/changelog/unreleased.md b/src/changelog/unreleased.md index 98286f390e..113ff95914 100644 --- a/src/changelog/unreleased.md +++ b/src/changelog/unreleased.md @@ -85,6 +85,7 @@ changelog entry. - On Web, `CursorGrabMode::Locked` now lets `DeviceEvent::MouseMotion` return raw data, not OS accelerated, if the browser supports it. - `VideoModeHandle::refresh_rate_millihertz()` now returns an `Option`. +- `VideoModeHandle::size()` and `bit_depth()` now return an `Option`. ### Removed @@ -104,3 +105,4 @@ changelog entry. - On MacOS, fix building with `feature = "rwh_04"`. - On Web, pen events are now routed through to `WindowEvent::Cursor*`. +- On Android and Orbital, `MonitorHandle::name()` now returns `None` instead of a dummy name. diff --git a/src/monitor.rs b/src/monitor.rs index f25ca3a56a..6d38ac6f19 100644 --- a/src/monitor.rs +++ b/src/monitor.rs @@ -55,20 +55,15 @@ impl VideoModeHandle { /// /// [`Window::inner_size()`]: crate::window::Window::inner_size #[inline] - pub fn size(&self) -> PhysicalSize { + pub fn size(&self) -> Option> { self.video_mode.size() } /// Returns the bit depth of this video mode, as in how many bits you have /// available per color. This is generally 24 bits or 32 bits on modern /// systems, depending on whether the alpha channel is counted or not. - /// - /// ## Platform-specific - /// - /// - **Wayland / Orbital:** Always returns 32. - /// - **iOS:** Always returns 32. #[inline] - pub fn bit_depth(&self) -> u16 { + pub fn bit_depth(&self) -> Option { self.video_mode.bit_depth() } @@ -86,19 +81,6 @@ impl VideoModeHandle { } } -impl std::fmt::Display for VideoModeHandle { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!( - f, - "{}x{} {}({} bpp)", - self.size().width, - self.size().height, - self.refresh_rate_millihertz().map(|rate| format!("@ {rate} mHz ")).unwrap_or_default(), - self.bit_depth() - ) - } -} - /// Handle to a monitor. /// /// Allows you to retrieve information about a given monitor and can be used in [`Window`] creation. @@ -132,14 +114,14 @@ impl MonitorHandle { /// /// ## Platform-specific /// - /// **Web:** Always returns [`Default`] without + /// **Web:** Always returns [`None`] without #[cfg_attr( any(web_platform, docsrs), doc = "[detailed monitor permissions][crate::platform::web::ActiveEventLoopExtWeb::request_detailed_monitor_permission]." )] #[cfg_attr(not(any(web_platform, docsrs)), doc = "detailed monitor permissions.")] #[inline] - pub fn position(&self) -> PhysicalPosition { + pub fn position(&self) -> Option> { self.inner.position() } diff --git a/src/platform_impl/android/mod.rs b/src/platform_impl/android/mod.rs index 4e97d9ea86..7d1ccc25bc 100644 --- a/src/platform_impl/android/mod.rs +++ b/src/platform_impl/android/mod.rs @@ -992,11 +992,11 @@ impl MonitorHandle { } pub fn name(&self) -> Option { - Some("Android Device".to_owned()) + None } - pub fn position(&self) -> PhysicalPosition { - (0, 0).into() + pub fn position(&self) -> Option> { + None } pub fn scale_factor(&self) -> f64 { @@ -1004,12 +1004,7 @@ impl MonitorHandle { } pub fn current_video_mode(&self) -> Option { - Some(VideoModeHandle { - size: screen_size(&self.app), - // FIXME: it is guaranteed to support 32 bit color though - bit_depth: 32, - monitor: self.clone(), - }) + Some(VideoModeHandle { monitor: self.clone() }) } pub fn video_modes(&self) -> impl Iterator { @@ -1020,18 +1015,16 @@ impl MonitorHandle { #[derive(Clone, Debug, Eq, Hash, PartialEq)] pub struct VideoModeHandle { - size: PhysicalSize, - bit_depth: u16, monitor: MonitorHandle, } impl VideoModeHandle { - pub fn size(&self) -> PhysicalSize { - self.size + pub fn size(&self) -> Option> { + None } - pub fn bit_depth(&self) -> u16 { - self.bit_depth + pub fn bit_depth(&self) -> Option { + None } pub fn refresh_rate_millihertz(&self) -> Option { diff --git a/src/platform_impl/apple/appkit/monitor.rs b/src/platform_impl/apple/appkit/monitor.rs index 0a0149b530..29091d7a27 100644 --- a/src/platform_impl/apple/appkit/monitor.rs +++ b/src/platform_impl/apple/appkit/monitor.rs @@ -113,12 +113,12 @@ impl VideoModeHandle { } } - pub fn size(&self) -> PhysicalSize { - self.size + pub fn size(&self) -> Option> { + Some(self.size) } - pub fn bit_depth(&self) -> u16 { - self.bit_depth + pub fn bit_depth(&self) -> Option { + Some(self.bit_depth) } pub fn refresh_rate_millihertz(&self) -> Option { @@ -216,13 +216,13 @@ impl MonitorHandle { } #[inline] - pub fn position(&self) -> PhysicalPosition { + pub fn position(&self) -> Option> { // This is already in screen coordinates. If we were using `NSScreen`, // then a conversion would've been needed: // flip_window_screen_coordinates(self.ns_screen(mtm)?.frame()) let bounds = unsafe { CGDisplayBounds(self.native_identifier()) }; let position = LogicalPosition::new(bounds.origin.x, bounds.origin.y); - position.to_physical(self.scale_factor()) + Some(position.to_physical(self.scale_factor())) } pub fn scale_factor(&self) -> f64 { diff --git a/src/platform_impl/apple/uikit/monitor.rs b/src/platform_impl/apple/uikit/monitor.rs index c074258691..6b02fdc1f8 100644 --- a/src/platform_impl/apple/uikit/monitor.rs +++ b/src/platform_impl/apple/uikit/monitor.rs @@ -45,7 +45,6 @@ impl Eq for MainThreadBoundDelegateImpls {} #[derive(Debug, PartialEq, Eq, Hash, Clone)] pub struct VideoModeHandle { pub(crate) size: (u32, u32), - pub(crate) bit_depth: u16, pub(crate) refresh_rate_millihertz: Option, screen_mode: MainThreadBoundDelegateImpls, pub(crate) monitor: MonitorHandle, @@ -61,19 +60,18 @@ impl VideoModeHandle { let size = screen_mode.size(); VideoModeHandle { size: (size.width as u32, size.height as u32), - bit_depth: 32, refresh_rate_millihertz, screen_mode: MainThreadBoundDelegateImpls(MainThreadBound::new(screen_mode, mtm)), monitor: MonitorHandle::new(uiscreen), } } - pub fn size(&self) -> PhysicalSize { - self.size.into() + pub fn size(&self) -> Option> { + Some(self.size.into()) } - pub fn bit_depth(&self) -> u16 { - self.bit_depth + pub fn bit_depth(&self) -> Option { + None } pub fn refresh_rate_millihertz(&self) -> Option { @@ -163,9 +161,9 @@ impl MonitorHandle { }) } - pub fn position(&self) -> PhysicalPosition { + pub fn position(&self) -> Option> { let bounds = self.ui_screen.get_on_main(|ui_screen| ui_screen.nativeBounds()); - (bounds.origin.x as f64, bounds.origin.y as f64).into() + Some((bounds.origin.x as f64, bounds.origin.y as f64).into()) } pub fn scale_factor(&self) -> f64 { diff --git a/src/platform_impl/linux/mod.rs b/src/platform_impl/linux/mod.rs index cfe34081ab..403f82cca8 100644 --- a/src/platform_impl/linux/mod.rs +++ b/src/platform_impl/linux/mod.rs @@ -226,7 +226,7 @@ impl MonitorHandle { } #[inline] - pub fn position(&self) -> PhysicalPosition { + pub fn position(&self) -> Option> { x11_or_wayland!(match self; MonitorHandle(m) => m.position()) } @@ -256,12 +256,12 @@ pub enum VideoModeHandle { impl VideoModeHandle { #[inline] - pub fn size(&self) -> PhysicalSize { + pub fn size(&self) -> Option> { x11_or_wayland!(match self; VideoModeHandle(m) => m.size()) } #[inline] - pub fn bit_depth(&self) -> u16 { + pub fn bit_depth(&self) -> Option { x11_or_wayland!(match self; VideoModeHandle(m) => m.bit_depth()) } diff --git a/src/platform_impl/linux/wayland/output.rs b/src/platform_impl/linux/wayland/output.rs index 8e329ddee5..4e90de013e 100644 --- a/src/platform_impl/linux/wayland/output.rs +++ b/src/platform_impl/linux/wayland/output.rs @@ -45,9 +45,9 @@ impl MonitorHandle { } #[inline] - pub fn position(&self) -> PhysicalPosition { + pub fn position(&self) -> Option> { let output_data = self.proxy.data::().unwrap(); - output_data.with_output_info(|info| { + Some(output_data.with_output_info(|info| { info.logical_position.map_or_else( || { LogicalPosition::::from(info.location) @@ -58,7 +58,7 @@ impl MonitorHandle { .to_physical(info.scale_factor as f64) }, ) - }) + })) } #[inline] @@ -121,7 +121,6 @@ impl std::hash::Hash for MonitorHandle { #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct VideoModeHandle { pub(crate) size: PhysicalSize, - pub(crate) bit_depth: u16, pub(crate) refresh_rate_millihertz: Option, pub(crate) monitor: MonitorHandle, } @@ -131,19 +130,18 @@ impl VideoModeHandle { VideoModeHandle { size: (mode.dimensions.0 as u32, mode.dimensions.1 as u32).into(), refresh_rate_millihertz: NonZeroU32::new(mode.refresh_rate as u32), - bit_depth: 32, monitor: monitor.clone(), } } #[inline] - pub fn size(&self) -> PhysicalSize { - self.size + pub fn size(&self) -> Option> { + Some(self.size) } #[inline] - pub fn bit_depth(&self) -> u16 { - self.bit_depth + pub fn bit_depth(&self) -> Option { + None } #[inline] diff --git a/src/platform_impl/linux/x11/monitor.rs b/src/platform_impl/linux/x11/monitor.rs index da5d3039bb..fbbe35c481 100644 --- a/src/platform_impl/linux/x11/monitor.rs +++ b/src/platform_impl/linux/x11/monitor.rs @@ -30,13 +30,13 @@ pub struct VideoModeHandle { impl VideoModeHandle { #[inline] - pub fn size(&self) -> PhysicalSize { - self.size.into() + pub fn size(&self) -> Option> { + Some(self.size.into()) } #[inline] - pub fn bit_depth(&self) -> u16 { - self.bit_depth + pub fn bit_depth(&self) -> Option { + Some(self.bit_depth) } #[inline] @@ -57,7 +57,7 @@ pub struct MonitorHandle { /// The name of the monitor pub(crate) name: String, /// The position of the monitor in the X screen - position: (i32, i32), + pub(crate) position: (i32, i32), /// If the monitor is the primary one primary: bool, /// The DPI scale factor @@ -149,8 +149,8 @@ impl MonitorHandle { self.id as _ } - pub fn position(&self) -> PhysicalPosition { - self.position.into() + pub fn position(&self) -> Option> { + Some(self.position.into()) } #[inline] diff --git a/src/platform_impl/linux/x11/window.rs b/src/platform_impl/linux/x11/window.rs index bcd9d5f417..ef595192d1 100644 --- a/src/platform_impl/linux/x11/window.rs +++ b/src/platform_impl/linux/x11/window.rs @@ -822,7 +822,7 @@ impl UnownedWindow { let window_position = self.outer_position_physical(); self.shared_state_lock().restore_position = Some(window_position); - let monitor_origin: (i32, i32) = monitor.position().into(); + let monitor_origin: (i32, i32) = monitor.position; self.set_position_inner(monitor_origin.0, monitor_origin.1) .expect_then_ignore_error("Failed to set window position"); self.set_fullscreen_hint(true).map(Some) diff --git a/src/platform_impl/orbital/mod.rs b/src/platform_impl/orbital/mod.rs index 2aa765588c..560358bda4 100644 --- a/src/platform_impl/orbital/mod.rs +++ b/src/platform_impl/orbital/mod.rs @@ -189,11 +189,11 @@ pub struct MonitorHandle; impl MonitorHandle { pub fn name(&self) -> Option { - Some("Redox Device".to_owned()) + None } - pub fn position(&self) -> PhysicalPosition { - (0, 0).into() + pub fn position(&self) -> Option> { + None } pub fn scale_factor(&self) -> f64 { @@ -202,11 +202,7 @@ impl MonitorHandle { pub fn current_video_mode(&self) -> Option { // (it is guaranteed to support 32 bit color though) - Some(VideoModeHandle { - size: PhysicalSize::default(), // TODO - bit_depth: 32, - monitor: self.clone(), - }) + Some(VideoModeHandle { monitor: self.clone() }) } pub fn video_modes(&self) -> impl Iterator { @@ -216,18 +212,17 @@ impl MonitorHandle { #[derive(Clone, Debug, Eq, Hash, PartialEq)] pub struct VideoModeHandle { - size: PhysicalSize, - bit_depth: u16, monitor: MonitorHandle, } impl VideoModeHandle { - pub fn size(&self) -> PhysicalSize { - self.size + pub fn size(&self) -> Option> { + // TODO + None } - pub fn bit_depth(&self) -> u16 { - self.bit_depth + pub fn bit_depth(&self) -> Option { + None } pub fn refresh_rate_millihertz(&self) -> Option { diff --git a/src/platform_impl/web/monitor.rs b/src/platform_impl/web/monitor.rs index 048253476e..faf6556ad4 100644 --- a/src/platform_impl/web/monitor.rs +++ b/src/platform_impl/web/monitor.rs @@ -1,4 +1,5 @@ use std::cell::{OnceCell, Ref, RefCell}; +use std::fmt::{self, Debug, Formatter}; use std::future::Future; use std::hash::Hash; use std::iter::{self, Once}; @@ -29,7 +30,7 @@ use crate::platform::web::{ MonitorPermissionError, Orientation, OrientationData, OrientationLock, OrientationLockError, }; -#[derive(Debug, Clone, Eq, Hash, Ord, PartialEq, PartialOrd)] +#[derive(Clone, Eq, Hash, Ord, PartialEq, PartialOrd)] pub struct MonitorHandle(Dispatcher); impl MonitorHandle { @@ -38,30 +39,15 @@ impl MonitorHandle { } pub fn scale_factor(&self) -> f64 { - self.0.queue(|inner| match &inner.screen { - Screen::Screen(_) => 0., - Screen::Detailed(screen) => screen.device_pixel_ratio(), - }) + self.0.queue(|inner| inner.scale_factor()) } - pub fn position(&self) -> PhysicalPosition { - self.0.queue(|inner| { - if let Screen::Detailed(screen) = &inner.screen { - PhysicalPosition::new(screen.left(), screen.top()) - } else { - PhysicalPosition::default() - } - }) + pub fn position(&self) -> Option> { + self.0.queue(|inner| inner.position()) } pub fn name(&self) -> Option { - self.0.queue(|inner| { - if let Screen::Detailed(screen) = &inner.screen { - Some(screen.label()) - } else { - None - } - }) + self.0.queue(|inner| inner.name()) } pub fn current_video_mode(&self) -> Option { @@ -73,37 +59,7 @@ impl MonitorHandle { } pub fn orientation(&self) -> OrientationData { - self.0.queue(|inner| { - let orientation = - inner.orientation.get_or_init(|| inner.screen.orientation().unchecked_into()); - let angle = orientation.angle().unwrap(); - - match orientation.type_().unwrap() { - OrientationType::LandscapePrimary => OrientationData { - orientation: Orientation::Landscape, - flipped: false, - natural: angle == 0, - }, - OrientationType::LandscapeSecondary => OrientationData { - orientation: Orientation::Landscape, - flipped: true, - natural: angle == 180, - }, - OrientationType::PortraitPrimary => OrientationData { - orientation: Orientation::Portrait, - flipped: false, - natural: angle == 0, - }, - OrientationType::PortraitSecondary => OrientationData { - orientation: Orientation::Portrait, - flipped: true, - natural: angle == 180, - }, - _ => { - unreachable!("found unrecognized orientation: {}", orientation.type_string()) - }, - } - }) + self.0.queue(|inner| inner.orientation()) } pub fn request_lock(&self, orientation_lock: OrientationLock) -> OrientationLockFuture { @@ -159,17 +115,11 @@ impl MonitorHandle { } pub fn is_internal(&self) -> Option { - self.0.queue(|inner| { - if let Screen::Detailed(screen) = &inner.screen { - Some(screen.is_internal()) - } else { - None - } - }) + self.0.queue(|inner| inner.is_internal()) } pub fn is_detailed(&self) -> bool { - self.0.queue(|inner| matches!(inner.screen, Screen::Detailed(_))) + self.0.queue(|inner| inner.is_detailed()) } pub(crate) fn detailed( @@ -190,6 +140,31 @@ impl MonitorHandle { } } +impl Debug for MonitorHandle { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + let (name, position, scale_factor, orientation, is_internal, is_detailed) = + self.0.queue(|this| { + ( + this.name(), + this.position(), + this.scale_factor(), + this.orientation(), + this.is_internal(), + this.is_detailed(), + ) + }); + + f.debug_struct("MonitorHandle") + .field("name", &name) + .field("position", &position) + .field("scale_factor", &scale_factor) + .field("orientation", &orientation) + .field("is_internal", &is_internal) + .field("is_detailed", &is_detailed) + .finish() + } +} + #[derive(Debug)] pub enum OrientationLockFuture { Future(Notified>), @@ -247,26 +222,16 @@ impl OrientationLockError { } } -#[derive(Clone, Debug, Eq, Hash, PartialEq)] +#[derive(Clone, Eq, Hash, PartialEq)] pub struct VideoModeHandle(Dispatcher); impl VideoModeHandle { - pub fn size(&self) -> PhysicalSize { - self.0.queue(|inner| { - let width = inner.screen.width().unwrap(); - let height = inner.screen.height().unwrap(); - - if let Some(Engine::Chromium) = inner.engine { - PhysicalSize::new(width, height).cast() - } else { - LogicalSize::new(width, height) - .to_physical(super::web_sys::scale_factor(&inner.window)) - } - }) + pub fn size(&self) -> Option> { + self.0.queue(|inner| inner.size()) } - pub fn bit_depth(&self) -> u16 { - self.0.queue(|inner| inner.screen.color_depth().unwrap()).try_into().unwrap() + pub fn bit_depth(&self) -> Option { + self.0.queue(|inner| inner.bit_depth()) } pub fn refresh_rate_millihertz(&self) -> Option { @@ -278,6 +243,14 @@ impl VideoModeHandle { } } +impl Debug for VideoModeHandle { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + let (size, bit_depth) = self.0.queue(|this| (this.size(), this.bit_depth())); + + f.debug_struct("MonitorHandle").field("size", &size).field("bit_depth", &bit_depth).finish() + } +} + struct Inner { window: WindowExt, engine: Option, @@ -289,6 +262,88 @@ impl Inner { fn new(window: WindowExt, engine: Option, screen: Screen) -> Self { Self { window, engine, screen, orientation: OnceCell::new() } } + + fn scale_factor(&self) -> f64 { + match &self.screen { + Screen::Screen(_) => 0., + Screen::Detailed(screen) => screen.device_pixel_ratio(), + } + } + + fn position(&self) -> Option> { + if let Screen::Detailed(screen) = &self.screen { + Some(PhysicalPosition::new(screen.left(), screen.top())) + } else { + None + } + } + + fn name(&self) -> Option { + if let Screen::Detailed(screen) = &self.screen { + Some(screen.label()) + } else { + None + } + } + + fn orientation(&self) -> OrientationData { + let orientation = + self.orientation.get_or_init(|| self.screen.orientation().unchecked_into()); + let angle = orientation.angle().unwrap(); + + match orientation.type_().unwrap() { + OrientationType::LandscapePrimary => OrientationData { + orientation: Orientation::Landscape, + flipped: false, + natural: angle == 0, + }, + OrientationType::LandscapeSecondary => OrientationData { + orientation: Orientation::Landscape, + flipped: true, + natural: angle == 180, + }, + OrientationType::PortraitPrimary => OrientationData { + orientation: Orientation::Portrait, + flipped: false, + natural: angle == 0, + }, + OrientationType::PortraitSecondary => OrientationData { + orientation: Orientation::Portrait, + flipped: true, + natural: angle == 180, + }, + _ => { + unreachable!("found unrecognized orientation: {}", orientation.type_string()) + }, + } + } + + fn is_internal(&self) -> Option { + if let Screen::Detailed(screen) = &self.screen { + Some(screen.is_internal()) + } else { + None + } + } + + fn is_detailed(&self) -> bool { + matches!(self.screen, Screen::Detailed(_)) + } + + fn size(&self) -> Option> { + let width = self.screen.width().unwrap(); + let height = self.screen.height().unwrap(); + + Some(if let Some(Engine::Chromium) = self.engine { + PhysicalSize::new(width, height).cast() + } else { + LogicalSize::new(width, height).to_physical(super::web_sys::scale_factor(&self.window)) + }) + } + + pub fn bit_depth(&self) -> Option { + Some(self.screen.color_depth().unwrap().try_into().unwrap()) + } } enum Screen { diff --git a/src/platform_impl/windows/monitor.rs b/src/platform_impl/windows/monitor.rs index d4429e4d17..125ce3706f 100644 --- a/src/platform_impl/windows/monitor.rs +++ b/src/platform_impl/windows/monitor.rs @@ -74,12 +74,12 @@ impl VideoModeHandle { } } - pub fn size(&self) -> PhysicalSize { - self.size.into() + pub fn size(&self) -> Option> { + Some(self.size.into()) } - pub fn bit_depth(&self) -> u16 { - self.bit_depth + pub fn bit_depth(&self) -> Option { + Some(self.bit_depth) } pub fn refresh_rate_millihertz(&self) -> Option { @@ -190,13 +190,13 @@ impl MonitorHandle { } #[inline] - pub fn position(&self) -> PhysicalPosition { + pub fn position(&self) -> Option> { get_monitor_info(self.0) .map(|info| { let rc_monitor = info.monitorInfo.rcMonitor; PhysicalPosition { x: rc_monitor.left, y: rc_monitor.top } }) - .unwrap_or(PhysicalPosition { x: 0, y: 0 }) + .ok() } #[inline] diff --git a/src/platform_impl/windows/window.rs b/src/platform_impl/windows/window.rs index 3bcafc3e5d..97066053da 100644 --- a/src/platform_impl/windows/window.rs +++ b/src/platform_impl/windows/window.rs @@ -819,7 +819,7 @@ impl Window { Fullscreen::Borderless(None) => monitor::current_monitor(window), }; - let position: (i32, i32) = monitor.position().into(); + let position: (i32, i32) = monitor.position().unwrap_or_default().into(); let size: (u32, u32) = monitor.size().into(); unsafe {