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