1use winit::monitor::MonitorHandle;
23use bevy_ecs::{entity::Entity, resource::Resource};
45/// 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.
20pub(crate) monitors: Vec<(MonitorHandle, Entity)>,
21}
2223impl WinitMonitors {
24/// Gets the [`MonitorHandle`] at index `n`.
25pub fn nth(&self, n: usize) -> Option<MonitorHandle> {
26self.monitors.get(n).map(|(monitor, _)| monitor.clone())
27 }
2829/// Gets the [`MonitorHandle`] associated with a `Monitor` entity.
30pub fn find_entity(&self, entity: Entity) -> Option<MonitorHandle> {
31self.monitors
32 .iter()
33 .find(|(_, e)| *e == entity)
34 .map(|(monitor, _)| monitor.clone())
35 }
36}