Crate bevy_window_manager

Crate bevy_window_manager 

Source
Expand description

Window management plugin for Bevy.

This plugin provides multi-monitor support, window state persistence, and utilities for managing window position and size across application sessions.

§The Problem

On macOS with multiple monitors that have different scale factors (e.g., a Retina display at scale 2.0 and an external monitor at scale 1.0), Bevy’s window positioning has issues:

  1. Window.position is unreliable at startup: When a window is created, Window.position is Automatic (not At(pos)), even though winit has placed the window at a specific physical position.

  2. Scale factor conversion in changed_windows: When you modify Window.resolution, Bevy’s changed_windows system applies scale factor conversion if scale_factor != cached_scale_factor. This corrupts the size when moving windows between monitors with different scale factors.

  3. Timing of scale factor updates: The CachedWindow is updated after winit events are processed, but our systems run before we receive the ScaleFactorChanged event.

§The Solution

This plugin uses winit directly to capture the actual window position at startup, compensates for scale factor conversions, and properly restores windows across monitors.

§Usage

use bevy::prelude::*;
use bevy_window_manager::WindowManagerPlugin;

App::new()
    .add_plugins(DefaultPlugins)
    .add_plugins(WindowManagerPlugin)
    .run();

The plugin automatically hides the window during startup and shows it after positioning is complete, preventing any visual flash at the default position.

See examples/custom_app_name.rs for how to override the app_name used in the path (default is to choose executable name).

See examples/custom_path.rs for how to override the full path to the state file.

Structs§

CurrentMonitor
Component storing the current monitor for a window.
MonitorInfo
Information about a single monitor.
Monitors
Sorted monitor list, updated when monitors change.
WindowManagerPlugin
The main plugin. See module docs for usage.
WindowTargetLoaded
Event fired when window target state is loaded from the save file.

Traits§

WindowExt
Extension trait for Window providing monitor-aware methods.