use super::AppViews;
use bevy::ecs::message::MessageWriter;
use bevy::prelude::*;
use bevy::window::{PrimaryWindow, Window, WindowResized};
pub struct AppViewPlugin;
impl Plugin for AppViewPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Last, update_window_size);
}
}
fn update_window_size(
app_views: Option<NonSend<AppViews>>,
mut windows: Query<(Entity, &mut Window), With<PrimaryWindow>>,
mut resize_events: MessageWriter<WindowResized>,
) {
let Some(app_views) = app_views else { return };
if let Some(view_window) = app_views.first_view() {
let (width, height) = view_window.view.logical_resolution();
let scale = view_window.view.scale_factor();
for (entity, mut window) in windows.iter_mut() {
let current_width = window.resolution.width();
let current_height = window.resolution.height();
if (current_width - width).abs() > 1.0 || (current_height - height).abs() > 1.0 {
window.resolution.set(width, height);
window.resolution.set_scale_factor(scale);
resize_events.write(WindowResized {
window: entity,
width,
height,
});
}
}
}
}