bevy/examples/window/monitor_info.rs
charlotte 3360b45153
Expose winit's MonitorHandle (#13669)
# 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>
2024-08-06 10:54:37 +00:00

102 lines
3.2 KiB
Rust

//! Displays information about available monitors (displays).
use bevy::render::camera::RenderTarget;
use bevy::window::{ExitCondition, WindowMode, WindowRef};
use bevy::{prelude::*, window::Monitor};
fn main() {
App::new()
.add_plugins(DefaultPlugins.set(WindowPlugin {
primary_window: None,
exit_condition: ExitCondition::DontExit,
..default()
}))
.add_systems(Update, (update, close_on_esc))
.run();
}
#[derive(Component)]
struct MonitorRef(Entity);
fn update(
mut commands: Commands,
monitors_added: Query<(Entity, &Monitor), Added<Monitor>>,
mut monitors_removed: RemovedComponents<Monitor>,
monitor_refs: Query<(Entity, &MonitorRef)>,
) {
for (entity, monitor) in monitors_added.iter() {
// Spawn a new window on each monitor
let name = monitor.name.clone().unwrap_or_else(|| "<no name>".into());
let size = format!("{}x{}px", monitor.physical_height, monitor.physical_width);
let hz = monitor
.refresh_rate_millihertz
.map(|x| format!("{}Hz", x as f32 / 1000.0))
.unwrap_or_else(|| "<unknown>".into());
let position = format!(
"x={} y={}",
monitor.physical_position.x, monitor.physical_position.y
);
let scale = format!("{:.2}", monitor.scale_factor);
let window = commands
.spawn((
Window {
title: name.clone(),
mode: WindowMode::Fullscreen(MonitorSelection::Entity(entity)),
position: WindowPosition::Centered(MonitorSelection::Entity(entity)),
..default()
},
MonitorRef(entity),
))
.id();
let camera = commands
.spawn(Camera2dBundle {
camera: Camera {
target: RenderTarget::Window(WindowRef::Entity(window)),
..default()
},
..default()
})
.id();
let info_text = format!(
"Monitor: {name}\nSize: {size}\nRefresh rate: {hz}\nPosition: {position}\nScale: {scale}\n\n",
);
commands.spawn((
TextBundle::from_section(info_text, default()).with_style(Style {
position_type: PositionType::Relative,
height: Val::Percent(100.0),
width: Val::Percent(100.0),
..default()
}),
TargetCamera(camera),
MonitorRef(entity),
));
}
// Remove windows for removed monitors
for monitor_entity in monitors_removed.read() {
for (ref_entity, monitor_ref) in monitor_refs.iter() {
if monitor_ref.0 == monitor_entity {
commands.entity(ref_entity).despawn_recursive();
}
}
}
}
fn close_on_esc(
mut commands: Commands,
focused_windows: Query<(Entity, &Window)>,
input: Res<ButtonInput<KeyCode>>,
) {
for (window, focus) in focused_windows.iter() {
if !focus.focused {
continue;
}
if input.just_pressed(KeyCode::Escape) {
commands.entity(window).despawn();
}
}
}