bevy_window/
monitor.rs

1use alloc::{string::String, vec::Vec};
2use bevy_ecs::component::Component;
3use bevy_math::{IVec2, UVec2};
4
5#[cfg(feature = "bevy_reflect")]
6use {bevy_ecs::prelude::ReflectComponent, bevy_reflect::Reflect};
7
8#[cfg(all(feature = "serialize", feature = "bevy_reflect"))]
9use bevy_reflect::{ReflectDeserialize, ReflectSerialize};
10
11/// Represents an available monitor as reported by the user's operating system, which can be used
12/// to query information about the display, such as its size, position, and video modes.
13///
14/// Each monitor corresponds to an entity and can be used to position a monitor using
15/// [`crate::window::MonitorSelection::Entity`].
16///
17/// # Warning
18///
19/// This component is synchronized with `winit` through `bevy_winit`, but is effectively
20/// read-only as `winit` does not support changing monitor properties.
21#[derive(Component, Debug, Clone)]
22#[cfg_attr(
23    feature = "bevy_reflect",
24    derive(Reflect),
25    reflect(Component, Debug, Clone)
26)]
27#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
28#[cfg_attr(
29    all(feature = "serialize", feature = "bevy_reflect"),
30    reflect(Serialize, Deserialize)
31)]
32pub struct Monitor {
33    /// The name of the monitor
34    pub name: Option<String>,
35    /// The height of the monitor in physical pixels
36    pub physical_height: u32,
37    /// The width of the monitor in physical pixels
38    pub physical_width: u32,
39    /// The position of the monitor in physical pixels
40    pub physical_position: IVec2,
41    /// The refresh rate of the monitor in millihertz
42    pub refresh_rate_millihertz: Option<u32>,
43    /// The scale factor of the monitor
44    pub scale_factor: f64,
45    /// The video modes that the monitor supports
46    pub video_modes: Vec<VideoMode>,
47}
48
49/// A marker component for the primary monitor
50#[derive(Component, Debug, Clone)]
51#[cfg_attr(
52    feature = "bevy_reflect",
53    derive(Reflect),
54    reflect(Component, Debug, Clone)
55)]
56pub struct PrimaryMonitor;
57
58impl Monitor {
59    /// Returns the physical size of the monitor in pixels
60    pub fn physical_size(&self) -> UVec2 {
61        UVec2::new(self.physical_width, self.physical_height)
62    }
63}
64
65/// Represents a video mode that a monitor supports
66#[derive(Debug, Clone, Copy, PartialEq, Eq)]
67#[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug, Clone))]
68#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
69#[cfg_attr(
70    all(feature = "serialize", feature = "bevy_reflect"),
71    reflect(Serialize, Deserialize)
72)]
73pub struct VideoMode {
74    /// The resolution of the video mode
75    pub physical_size: UVec2,
76    /// The bit depth of the video mode
77    pub bit_depth: u16,
78    /// The refresh rate in millihertz
79    pub refresh_rate_millihertz: u32,
80}