# Objective Fix https://github.com/bevyengine/bevy/issues/4530 - Make it easier to open/close/modify windows by setting them up as `Entity`s with a `Window` component. - Make multiple windows very simple to set up. (just add a `Window` component to an entity and it should open) ## Solution - Move all properties of window descriptor to ~components~ a component. - Replace `WindowId` with `Entity`. - ~Use change detection for components to update backend rather than events/commands. (The `CursorMoved`/`WindowResized`/... events are kept for user convenience.~ Check each field individually to see what we need to update, events are still kept for user convenience. --- ## Changelog - `WindowDescriptor` renamed to `Window`. - Width/height consolidated into a `WindowResolution` component. - Requesting maximization/minimization is done on the [`Window::state`] field. - `WindowId` is now `Entity`. ## Migration Guide - Replace `WindowDescriptor` with `Window`. - Change `width` and `height` fields in a `WindowResolution`, either by doing ```rust WindowResolution::new(width, height) // Explicitly // or using From<_> for tuples for convenience (1920., 1080.).into() ``` - Replace any `WindowCommand` code to just modify the `Window`'s fields directly and creating/closing windows is now by spawning/despawning an entity with a `Window` component like so: ```rust let window = commands.spawn(Window { ... }).id(); // open window commands.entity(window).despawn(); // close window ``` ## Unresolved - ~How do we tell when a window is minimized by a user?~ ~Currently using the `Resize(0, 0)` as an indicator of minimization.~ No longer attempting to tell given how finnicky this was across platforms, now the user can only request that a window be maximized/minimized. ## Future work - Move `exit_on_close` functionality out from windowing and into app(?) - https://github.com/bevyengine/bevy/issues/5621 - https://github.com/bevyengine/bevy/issues/7099 - https://github.com/bevyengine/bevy/issues/7098 Co-authored-by: Carter Anderson <mcanders1@gmail.com>
84 lines
2.4 KiB
Rust
84 lines
2.4 KiB
Rust
use crate::WinitWindows;
|
|
use bevy_app::{App, Plugin};
|
|
use bevy_ecs::prelude::*;
|
|
use crossbeam_channel::{Receiver, Sender};
|
|
use wasm_bindgen::JsCast;
|
|
use winit::dpi::LogicalSize;
|
|
|
|
pub(crate) struct CanvasParentResizePlugin;
|
|
|
|
impl Plugin for CanvasParentResizePlugin {
|
|
fn build(&self, app: &mut App) {
|
|
app.init_resource::<CanvasParentResizeEventChannel>()
|
|
.add_system(canvas_parent_resize_event_handler);
|
|
}
|
|
}
|
|
|
|
struct ResizeEvent {
|
|
size: LogicalSize<f32>,
|
|
window: Entity,
|
|
}
|
|
|
|
#[derive(Resource)]
|
|
pub(crate) struct CanvasParentResizeEventChannel {
|
|
sender: Sender<ResizeEvent>,
|
|
receiver: Receiver<ResizeEvent>,
|
|
}
|
|
|
|
fn canvas_parent_resize_event_handler(
|
|
winit_windows: NonSend<WinitWindows>,
|
|
resize_events: Res<CanvasParentResizeEventChannel>,
|
|
) {
|
|
for event in resize_events.receiver.try_iter() {
|
|
if let Some(window) = winit_windows.get_window(event.window) {
|
|
window.set_inner_size(event.size);
|
|
}
|
|
}
|
|
}
|
|
|
|
fn get_size(selector: &str) -> Option<LogicalSize<f32>> {
|
|
let win = web_sys::window().unwrap();
|
|
let doc = win.document().unwrap();
|
|
let element = doc.query_selector(selector).ok()??;
|
|
let parent_element = element.parent_element()?;
|
|
let rect = parent_element.get_bounding_client_rect();
|
|
return Some(winit::dpi::LogicalSize::new(
|
|
rect.width() as f32,
|
|
rect.height() as f32,
|
|
));
|
|
}
|
|
|
|
pub(crate) const WINIT_CANVAS_SELECTOR: &str = "canvas[data-raw-handle]";
|
|
|
|
impl Default for CanvasParentResizeEventChannel {
|
|
fn default() -> Self {
|
|
let (sender, receiver) = crossbeam_channel::unbounded();
|
|
return Self { sender, receiver };
|
|
}
|
|
}
|
|
|
|
impl CanvasParentResizeEventChannel {
|
|
pub(crate) fn listen_to_selector(&self, window: Entity, selector: &str) {
|
|
let sender = self.sender.clone();
|
|
let owned_selector = selector.to_string();
|
|
let resize = move || {
|
|
if let Some(size) = get_size(&owned_selector) {
|
|
sender.send(ResizeEvent { size, window }).unwrap();
|
|
}
|
|
};
|
|
|
|
// ensure resize happens on startup
|
|
resize();
|
|
|
|
let closure = wasm_bindgen::closure::Closure::wrap(Box::new(move |_: web_sys::Event| {
|
|
resize();
|
|
}) as Box<dyn FnMut(_)>);
|
|
let window = web_sys::window().unwrap();
|
|
|
|
window
|
|
.add_event_listener_with_callback("resize", closure.as_ref().unchecked_ref())
|
|
.unwrap();
|
|
closure.forget();
|
|
}
|
|
}
|