Skip to main content

bevy_winit/
winit_monitors.rs

1use winit::monitor::MonitorHandle;
2
3use bevy_ecs::{entity::Entity, resource::Resource};
4
5/// Stores [`winit`] monitors and their corresponding entities
6///
7/// # Known Issues
8///
9/// On some platforms, physically disconnecting a monitor might result in a
10/// panic in [`winit`]'s loop. This will lead to a crash in the bevy app. See
11/// [13669] for investigations and discussions.
12///
13/// [13669]: https://github.com/bevyengine/bevy/pull/13669
14#[derive(impl bevy_ecs::resource::Resource for WinitMonitors where
    Self: ::core::marker::Send + ::core::marker::Sync + 'static {}Resource, #[automatically_derived]
impl ::core::fmt::Debug for WinitMonitors {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field1_finish(f, "WinitMonitors",
            "monitors", &&self.monitors)
    }
}Debug, #[automatically_derived]
impl ::core::default::Default for WinitMonitors {
    #[inline]
    fn default() -> WinitMonitors {
        WinitMonitors { monitors: ::core::default::Default::default() }
    }
}Default)]
15pub struct WinitMonitors {
16    /// Stores [`winit`] monitors and their corresponding entities
17    // We can't use a `BtreeMap` here because clippy complains about using `MonitorHandle` as a key
18    // on some platforms. Using a `Vec` is fine because we don't expect to have a large number of
19    // monitors and avoids having to audit the code for `MonitorHandle` equality.
20    pub(crate) monitors: Vec<(MonitorHandle, Entity)>,
21}
22
23impl WinitMonitors {
24    /// Gets the [`MonitorHandle`] at index `n`.
25    pub fn nth(&self, n: usize) -> Option<MonitorHandle> {
26        self.monitors.get(n).map(|(monitor, _)| monitor.clone())
27    }
28
29    /// Gets the [`MonitorHandle`] associated with a `Monitor` entity.
30    pub fn find_entity(&self, entity: Entity) -> Option<MonitorHandle> {
31        self.monitors
32            .iter()
33            .find(|(_, e)| *e == entity)
34            .map(|(monitor, _)| monitor.clone())
35    }
36}