
# Objective Adds a new `Monitor` component representing a winit `MonitorHandle` that can be used to spawn new windows and check for system monitor information. Closes #12955. ## Solution For every winit event, check available monitors and spawn them into the world as components. ## Testing TODO: - [x] Test plugging in and unplugging monitor during app runtime - [x] Test spawning a window on a second monitor by entity id - [ ] Since this touches winit, test all platforms --- ## Changelog - Adds a new `Monitor` component that can be queried for information about available system monitors. ## Migration Guide - `WindowMode` variants now take a `MonitorSelection`, which can be set to `MonitorSelection::Primary` to mirror the old behavior. --------- Co-authored-by: Pascal Hertleif <pascal@technocreatives.com> Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com> Co-authored-by: Pascal Hertleif <killercup@gmail.com>
36 lines
1.3 KiB
Rust
36 lines
1.3 KiB
Rust
use winit::monitor::MonitorHandle;
|
|
|
|
use bevy_ecs::entity::Entity;
|
|
use bevy_ecs::system::Resource;
|
|
|
|
/// Stores [`winit`] monitors and their corresponding entities
|
|
///
|
|
/// # Known Issues
|
|
///
|
|
/// On some platforms, physically disconnecting a monitor might result in a
|
|
/// panic in [`winit`]'s loop. This will lead to a crash in the bevy app. See
|
|
/// [13669] for investigations and discussions.
|
|
///
|
|
/// [13669]: https://github.com/bevyengine/bevy/pull/13669
|
|
#[derive(Resource, Debug, Default)]
|
|
pub struct WinitMonitors {
|
|
/// Stores [`winit`] monitors and their corresponding entities
|
|
// We can't use a `BtreeMap` here because clippy complains about using `MonitorHandle` as a key
|
|
// on some platforms. Using a `Vec` is fine because we don't expect to have a large number of
|
|
// monitors and avoids having to audit the code for `MonitorHandle` equality.
|
|
pub(crate) monitors: Vec<(MonitorHandle, Entity)>,
|
|
}
|
|
|
|
impl WinitMonitors {
|
|
pub fn nth(&self, n: usize) -> Option<MonitorHandle> {
|
|
self.monitors.get(n).map(|(monitor, _)| monitor.clone())
|
|
}
|
|
|
|
pub fn find_entity(&self, entity: Entity) -> Option<MonitorHandle> {
|
|
self.monitors
|
|
.iter()
|
|
.find(|(_, e)| *e == entity)
|
|
.map(|(monitor, _)| monitor.clone())
|
|
}
|
|
}
|