use std::fmt;
#[derive(Clone)]
pub struct MonitorHandle(pub(crate) winit::monitor::MonitorHandle);
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct VideoMode {
size: (u32, u32),
refresh_rate_millihertz: Option<u32>,
bit_depth: Option<u16>,
}
impl MonitorHandle {
pub fn name(&self) -> Option<String> {
self.0.name().map(|c| c.to_string())
}
pub fn position(&self) -> Option<(i32, i32)> {
self.0.position().map(|p| (p.x, p.y))
}
pub fn scale_factor(&self) -> f64 {
self.0.scale_factor()
}
pub fn current_video_mode(&self) -> Option<VideoMode> {
self.0.current_video_mode().map(VideoMode::from_winit)
}
pub fn video_modes(&self) -> Vec<VideoMode> {
self.0.video_modes().map(VideoMode::from_winit).collect()
}
pub fn refresh_rate_millihertz(&self) -> Option<u32> {
self.current_video_mode()?.refresh_rate_millihertz
}
pub fn size(&self) -> Option<(u32, u32)> {
self.current_video_mode().map(|m| m.size)
}
pub fn is_primary(&self, primary: &MonitorHandle) -> bool {
self.0.eq(&primary.0)
}
pub(crate) fn inner(&self) -> &winit::monitor::MonitorHandle {
&self.0
}
#[cfg(feature = "display-advanced")]
pub(crate) fn native_id(&self) -> u64 {
self.0.native_id()
}
}
impl PartialEq for MonitorHandle {
fn eq(&self, other: &Self) -> bool {
self.0.eq(&other.0)
}
}
impl Eq for MonitorHandle {}
impl fmt::Debug for MonitorHandle {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("MonitorHandle")
.field("name", &self.name())
.field("position", &self.position())
.field("scale_factor", &self.scale_factor())
.field("size", &self.size())
.finish_non_exhaustive()
}
}
impl VideoMode {
pub(crate) fn from_winit(m: winit::monitor::VideoMode) -> Self {
Self {
size: (m.size().width, m.size().height),
refresh_rate_millihertz: m.refresh_rate_millihertz().map(|r| r.get()),
bit_depth: m.bit_depth().map(|b| b.get()),
}
}
pub fn size(&self) -> (u32, u32) {
self.size
}
pub fn refresh_rate_millihertz(&self) -> Option<u32> {
self.refresh_rate_millihertz
}
pub fn refresh_rate_hz(&self) -> Option<f64> {
self.refresh_rate_millihertz.map(|mhz| mhz as f64 / 1000.0)
}
pub fn bit_depth(&self) -> Option<u16> {
self.bit_depth
}
}
impl fmt::Display for VideoMode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}x{}", self.size.0, self.size.1)?;
if let Some(mhz) = self.refresh_rate_millihertz {
write!(f, " @ {:.1} Hz", mhz as f64 / 1000.0)?;
}
if let Some(bd) = self.bit_depth {
write!(f, " ({} bpp)", bd)?;
}
Ok(())
}
}
#[cfg(feature = "display-advanced")]
pub use advanced::*;
#[cfg(feature = "display-advanced")]
mod advanced {
use std::fmt;
use thiserror::Error;
#[derive(Error, Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "devtools", derive(serde::Serialize, serde::Deserialize))]
pub enum DisplayError {
#[error("display feature not available on this platform")]
Unsupported,
#[error("display operation failed: {0}")]
Platform(String),
}
impl From<String> for DisplayError {
fn from(msg: String) -> Self {
DisplayError::Platform(msg)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Rotation {
Deg0,
Deg90,
Deg180,
Deg270,
}
impl Rotation {
fn from_degrees(d: f64) -> Option<Self> {
match d as i32 {
0 => Some(Self::Deg0),
90 => Some(Self::Deg90),
180 => Some(Self::Deg180),
270 => Some(Self::Deg270),
_ => None,
}
}
}
#[must_use]
#[allow(dead_code)]
pub struct ScreensaverInhibit(burin_platform::ScreensaverInhibit);
impl ScreensaverInhibit {
pub fn new() -> Result<Self, DisplayError> {
burin_platform::ScreensaverInhibit::new()
.map(Self)
.map_err(DisplayError::from)
}
}
impl fmt::Debug for ScreensaverInhibit {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ScreensaverInhibit").finish_non_exhaustive()
}
}
impl super::MonitorHandle {
pub fn physical_size_mm(&self) -> Option<(u32, u32)> {
burin_platform::display::physical_size_mm(self.native_id() as u64)
}
pub fn rotation(&self) -> Option<Rotation> {
burin_platform::display::rotation_degrees(self.native_id() as u64)
.and_then(Rotation::from_degrees)
}
}
}