Skip to main content

bevy_window/
monitor.rs

1use alloc::{string::String, vec::Vec};
2use bevy_ecs::{component::Component, entity::Entity};
3use bevy_math::{IVec2, UVec2};
4
5#[cfg(all(feature = "serialize", feature = "bevy_reflect"))]
6use bevy_reflect::{ReflectDeserialize, ReflectSerialize};
7#[cfg(feature = "bevy_reflect")]
8use {bevy_ecs::prelude::ReflectComponent, bevy_reflect::Reflect};
9
10/// Represents an available monitor as reported by the user's operating system, which can be used
11/// to query information about the display, such as its size, position, and video modes.
12///
13/// Each monitor corresponds to an entity and can be used to position a monitor using
14/// [`MonitorSelection::Entity`](`crate::window::MonitorSelection::Entity`).
15///
16/// # Warning
17///
18/// This component is synchronized with `winit` through `bevy_winit`, but is effectively
19/// read-only as `winit` does not support changing monitor properties.
20#[derive(Component, Debug, Clone)]
21#[require(HasWindows)]
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
58/// A relationship for all Windows on a specific Monitor.
59#[derive(Component, Debug, Default)]
60#[relationship_target(relationship = crate::window::OnMonitor, linked_spawn)]
61pub struct HasWindows(Vec<Entity>);
62
63impl Monitor {
64    /// Returns the physical size of the monitor in pixels
65    pub fn physical_size(&self) -> UVec2 {
66        UVec2::new(self.physical_width, self.physical_height)
67    }
68}
69
70/// Represents a video mode that a monitor supports
71#[derive(Debug, Clone, Copy, PartialEq, Eq)]
72#[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug, Clone))]
73#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
74#[cfg_attr(
75    all(feature = "serialize", feature = "bevy_reflect"),
76    reflect(Serialize, Deserialize)
77)]
78pub struct VideoMode {
79    /// The resolution of the video mode
80    pub physical_size: UVec2,
81    /// The bit depth of the video mode
82    pub bit_depth: u16,
83    /// The refresh rate in millihertz
84    pub refresh_rate_millihertz: u32,
85}