2019-02-06 02:30:33 +11:00
|
|
|
//! Types useful for interacting with a user's monitors.
|
|
|
|
//!
|
2022-06-12 02:57:19 +10:00
|
|
|
//! If you want to get basic information about a monitor, you can use the
|
|
|
|
//! [`MonitorHandle`] type. This is retrieved from one of the following
|
|
|
|
//! methods, which return an iterator of [`MonitorHandle`]:
|
|
|
|
//! - [`EventLoopWindowTarget::available_monitors`](crate::event_loop::EventLoopWindowTarget::available_monitors).
|
|
|
|
//! - [`Window::available_monitors`](crate::window::Window::available_monitors).
|
2019-06-22 01:33:15 +10:00
|
|
|
use crate::{
|
|
|
|
dpi::{PhysicalPosition, PhysicalSize},
|
|
|
|
platform_impl,
|
|
|
|
};
|
2019-02-06 02:30:33 +11:00
|
|
|
|
2019-06-13 04:07:25 +10:00
|
|
|
/// Describes a fullscreen video mode of a monitor.
|
|
|
|
///
|
2022-06-12 02:57:19 +10:00
|
|
|
/// Can be acquired with [`MonitorHandle::video_modes`].
|
2019-10-04 06:19:10 +10:00
|
|
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
2019-06-13 04:07:25 +10:00
|
|
|
pub struct VideoMode {
|
2019-07-30 04:16:14 +10:00
|
|
|
pub(crate) video_mode: platform_impl::VideoMode,
|
|
|
|
}
|
|
|
|
|
2019-10-04 06:19:10 +10:00
|
|
|
impl std::fmt::Debug for VideoMode {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
self.video_mode.fmt(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-30 04:16:14 +10:00
|
|
|
impl PartialOrd for VideoMode {
|
|
|
|
fn partial_cmp(&self, other: &VideoMode) -> Option<std::cmp::Ordering> {
|
|
|
|
Some(self.cmp(other))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Ord for VideoMode {
|
|
|
|
fn cmp(&self, other: &VideoMode) -> std::cmp::Ordering {
|
|
|
|
self.monitor().cmp(&other.monitor()).then(
|
2023-05-16 12:11:43 +10:00
|
|
|
self.size()
|
|
|
|
.cmp(&other.size())
|
2019-07-30 04:16:14 +10:00
|
|
|
.then(
|
2022-07-08 20:25:56 +10:00
|
|
|
self.refresh_rate_millihertz()
|
|
|
|
.cmp(&other.refresh_rate_millihertz())
|
2019-07-30 04:16:14 +10:00
|
|
|
.then(self.bit_depth().cmp(&other.bit_depth())),
|
|
|
|
)
|
|
|
|
.reverse(),
|
|
|
|
)
|
|
|
|
}
|
2019-06-13 04:07:25 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
impl VideoMode {
|
|
|
|
/// Returns the resolution of this video mode.
|
2019-07-30 04:16:14 +10:00
|
|
|
#[inline]
|
2020-01-04 17:33:07 +11:00
|
|
|
pub fn size(&self) -> PhysicalSize<u32> {
|
2019-07-30 04:16:14 +10:00
|
|
|
self.video_mode.size()
|
2019-06-13 04:07:25 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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
|
|
|
|
///
|
2023-01-06 00:58:08 +11:00
|
|
|
/// - **Wayland / Orbital:** Always returns 32.
|
2019-06-13 04:07:25 +10:00
|
|
|
/// - **iOS:** Always returns 32.
|
2019-07-30 04:16:14 +10:00
|
|
|
#[inline]
|
2019-06-13 04:07:25 +10:00
|
|
|
pub fn bit_depth(&self) -> u16 {
|
2019-07-30 04:16:14 +10:00
|
|
|
self.video_mode.bit_depth()
|
2019-06-13 04:07:25 +10:00
|
|
|
}
|
|
|
|
|
2022-07-08 20:25:56 +10:00
|
|
|
/// Returns the refresh rate of this video mode in mHz.
|
2019-07-30 04:16:14 +10:00
|
|
|
#[inline]
|
2022-07-08 20:25:56 +10:00
|
|
|
pub fn refresh_rate_millihertz(&self) -> u32 {
|
|
|
|
self.video_mode.refresh_rate_millihertz()
|
2019-07-30 04:16:14 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the monitor that this video mode is valid for. Each monitor has
|
|
|
|
/// a separate set of valid video modes.
|
|
|
|
#[inline]
|
|
|
|
pub fn monitor(&self) -> MonitorHandle {
|
2022-09-21 18:04:28 +10:00
|
|
|
MonitorHandle {
|
|
|
|
inner: self.video_mode.monitor(),
|
|
|
|
}
|
2019-07-30 04:16:14 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Display for VideoMode {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
write!(
|
|
|
|
f,
|
2022-07-08 20:25:56 +10:00
|
|
|
"{}x{} @ {} mHz ({} bpp)",
|
2019-07-30 04:16:14 +10:00
|
|
|
self.size().width,
|
|
|
|
self.size().height,
|
2022-07-08 20:25:56 +10:00
|
|
|
self.refresh_rate_millihertz(),
|
2019-07-30 04:16:14 +10:00
|
|
|
self.bit_depth()
|
|
|
|
)
|
2019-06-13 04:07:25 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-06 02:30:33 +11:00
|
|
|
/// Handle to a monitor.
|
|
|
|
///
|
|
|
|
/// Allows you to retrieve information about a given monitor and can be used in [`Window`] creation.
|
|
|
|
///
|
2019-11-12 10:05:59 +11:00
|
|
|
/// [`Window`]: crate::window::Window
|
2019-07-30 04:16:14 +10:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
2019-02-06 02:30:33 +11:00
|
|
|
pub struct MonitorHandle {
|
2019-06-22 01:33:15 +10:00
|
|
|
pub(crate) inner: platform_impl::MonitorHandle,
|
2019-02-06 02:30:33 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
impl MonitorHandle {
|
|
|
|
/// Returns a human-readable name of the monitor.
|
|
|
|
///
|
|
|
|
/// Returns `None` if the monitor doesn't exist anymore.
|
2019-09-25 09:33:32 +10:00
|
|
|
///
|
|
|
|
/// ## Platform-specific
|
|
|
|
///
|
|
|
|
/// - **Web:** Always returns None
|
2019-02-06 02:30:33 +11:00
|
|
|
#[inline]
|
2019-05-30 11:29:54 +10:00
|
|
|
pub fn name(&self) -> Option<String> {
|
|
|
|
self.inner.name()
|
2019-02-06 02:30:33 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the monitor's resolution.
|
2019-09-25 09:33:32 +10:00
|
|
|
///
|
|
|
|
/// ## Platform-specific
|
|
|
|
///
|
|
|
|
/// - **Web:** Always returns (0,0)
|
2019-02-06 02:30:33 +11:00
|
|
|
#[inline]
|
2020-01-04 17:33:07 +11:00
|
|
|
pub fn size(&self) -> PhysicalSize<u32> {
|
2019-06-13 16:33:44 +10:00
|
|
|
self.inner.size()
|
2019-02-06 02:30:33 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the top-left corner position of the monitor relative to the larger full
|
|
|
|
/// screen area.
|
2019-09-25 09:33:32 +10:00
|
|
|
///
|
|
|
|
/// ## Platform-specific
|
|
|
|
///
|
|
|
|
/// - **Web:** Always returns (0,0)
|
2019-02-06 02:30:33 +11:00
|
|
|
#[inline]
|
2020-01-04 17:33:07 +11:00
|
|
|
pub fn position(&self) -> PhysicalPosition<i32> {
|
2019-05-30 11:29:54 +10:00
|
|
|
self.inner.position()
|
2019-02-06 02:30:33 +11:00
|
|
|
}
|
|
|
|
|
2022-07-08 20:25:56 +10:00
|
|
|
/// The monitor refresh rate used by the system.
|
|
|
|
///
|
2022-11-29 21:28:46 +11:00
|
|
|
/// Return `Some` if succeed, or `None` if failed, which usually happens when the monitor
|
|
|
|
/// the window is on is removed.
|
|
|
|
///
|
2022-07-08 20:25:56 +10:00
|
|
|
/// When using exclusive fullscreen, the refresh rate of the [`VideoMode`] that was used to
|
|
|
|
/// enter fullscreen should be used instead.
|
|
|
|
#[inline]
|
|
|
|
pub fn refresh_rate_millihertz(&self) -> Option<u32> {
|
|
|
|
self.inner.refresh_rate_millihertz()
|
|
|
|
}
|
|
|
|
|
2020-01-04 06:52:27 +11:00
|
|
|
/// Returns the scale factor that can be used to map logical pixels to physical pixels, and vice versa.
|
2019-02-06 02:30:33 +11:00
|
|
|
///
|
2019-11-12 10:05:59 +11:00
|
|
|
/// See the [`dpi`](crate::dpi) module for more information.
|
2019-02-06 02:30:33 +11:00
|
|
|
///
|
|
|
|
/// ## Platform-specific
|
|
|
|
///
|
2020-01-04 06:52:27 +11:00
|
|
|
/// - **X11:** Can be overridden using the `WINIT_X11_SCALE_FACTOR` environment variable.
|
2019-02-06 02:30:33 +11:00
|
|
|
/// - **Android:** Always returns 1.0.
|
2019-09-25 09:33:32 +10:00
|
|
|
/// - **Web:** Always returns 1.0
|
2019-02-06 02:30:33 +11:00
|
|
|
#[inline]
|
2020-01-04 06:52:27 +11:00
|
|
|
pub fn scale_factor(&self) -> f64 {
|
|
|
|
self.inner.scale_factor()
|
2019-02-06 02:30:33 +11:00
|
|
|
}
|
2019-06-13 04:07:25 +10:00
|
|
|
|
|
|
|
/// Returns all fullscreen video modes supported by this monitor.
|
2019-09-25 09:33:32 +10:00
|
|
|
///
|
|
|
|
/// ## Platform-specific
|
|
|
|
///
|
|
|
|
/// - **Web:** Always returns an empty iterator
|
2019-06-13 04:07:25 +10:00
|
|
|
#[inline]
|
|
|
|
pub fn video_modes(&self) -> impl Iterator<Item = VideoMode> {
|
2022-09-21 18:04:28 +10:00
|
|
|
self.inner
|
|
|
|
.video_modes()
|
|
|
|
.map(|video_mode| VideoMode { video_mode })
|
2019-06-13 04:07:25 +10:00
|
|
|
}
|
2019-02-06 02:30:33 +11:00
|
|
|
}
|