bevy_window_manager 0.18.2

Bevy plugin for primary window restoration and multi-monitor support
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
//! Interactive example for testing window restoration and fullscreen modes.
//!
//! Run with: `cargo run --example restore_window`
//!
//! This example displays the current window state and allows switching between modes:
//! - Press `1` for exclusive fullscreen (uses selected video mode) WARNING: Exclusive fullscreen on
//!   macOS may panic on exit due to winit bugs. See: <https://github.com/rust-windowing/winit/issues/3668>
//! - Press `2` for borderless fullscreen (recommended on macOS)
//! - Press `W` or `Escape` for windowed mode
//! - Press `Up`/`Down` to cycle through available video modes
//!
//! Move and resize the window to test state persistence across restarts.

// Monitor dimensions always fit in i32
#![allow(clippy::cast_possible_wrap)]

use std::collections::HashMap;

use bevy::ecs::system::NonSendMarker;
use bevy::prelude::*;
use bevy::window::Monitor;
use bevy::window::MonitorSelection;
use bevy::window::PrimaryWindow;
use bevy::window::VideoMode;
use bevy::window::VideoModeSelection;
use bevy::window::WindowMode;
use bevy::window::WindowPosition;
use bevy::window::WindowScaleFactorChanged;
use bevy::winit::WINIT_WINDOWS;
use bevy_brp_extras::BrpExtrasPlugin;
use bevy_window_manager::CurrentMonitor;
use bevy_window_manager::Monitors;
use bevy_window_manager::WindowExt;
use bevy_window_manager::WindowManagerPlugin;
use bevy_window_manager::WindowTargetLoaded;

fn main() {
    App::new()
        .add_plugins(DefaultPlugins.set(WindowPlugin {
            primary_window: Some(Window {
                title: "Window Restore Test".into(),
                ..default()
            }),
            ..default()
        }))
        .add_plugins(WindowManagerPlugin)
        .add_plugins(BrpExtrasPlugin::default())
        .add_observer(on_window_target_loaded)
        .init_resource::<SelectedVideoModes>()
        .add_systems(Startup, setup)
        .add_systems(
            Update,
            (
                update_display,
                handle_input,
                debug_winit_monitor,
                debug_window_changed,
                debug_scale_factor_changed,
            ),
        )
        .run();
}

// --- WindowTargetLoaded Test Support ---

/// Resource inserted when `WindowTargetLoaded` event is received.
/// Queryable via BRP to verify the event fired with expected values.
#[derive(Resource, Debug, Clone, Reflect)]
#[reflect(Resource)]
struct WindowTargetLoadedReceived {
    position: Option<IVec2>,
    size:     UVec2,
    mode:     WindowMode,
}

// --- Display UI ---

#[derive(Component)]
struct MainDisplay;

const MARGIN: Val = Val::Px(20.0);

fn setup(mut commands: Commands) {
    commands.spawn(Camera2d);

    let text_font = TextFont {
        font_size: 20.0,
        ..default()
    };

    // Single text display for everything
    commands.spawn((
        Text::new(""),
        text_font,
        Node {
            position_type: PositionType::Absolute,
            top: MARGIN,
            left: MARGIN,
            ..default()
        },
        MainDisplay,
    ));
}

fn update_display(
    mut main_text: Single<&mut Text, With<MainDisplay>>,
    window_query: Single<(&Window, &CurrentMonitor), With<PrimaryWindow>>,
    monitors_res: Res<Monitors>,
    bevy_monitors: Query<(Entity, &Monitor)>,
    mut selected: ResMut<SelectedVideoModes>,
) {
    let (window, monitor) = *window_query;
    let effective_mode = window.effective_mode(&monitors_res);

    let (video_modes, refresh_rate) = get_video_modes_for_monitor(&bevy_monitors, monitor);
    let refresh_display = format_refresh_rate(window, refresh_rate);
    let active_mode_idx = find_active_video_mode_index(window, &video_modes);

    sync_selected_to_active(window, monitor, active_mode_idx, &mut selected);

    let selected_idx = selected.get(monitor.index);
    let video_modes_display =
        build_video_modes_display(&video_modes, selected_idx, active_mode_idx);

    let row1 = format_monitor_row(monitor, &refresh_display);
    let (row2, row3) = format_position_rows(window, monitor);

    main_text.0 = format!(
        "{row1}\n\
         {row2}\n\
         {row3}\n\
         \n\
         Mode:           {:?} (set value only, not dynamically updated)\n\
         Effective Mode: {:?}\n\
         \n\
         Video Modes (Up/Down to select):\n\
         {video_modes_display}\n\
         \n\
         Controls:\n\
         [1] Exclusive Fullscreen (selected mode)\n\
         [2] Borderless Fullscreen\n\
         [W/Esc] Windowed\n\
         [Q] Quit",
        window.mode, effective_mode,
    );
}

/// Get video modes and refresh rate for the monitor matching the given position.
fn get_video_modes_for_monitor<'a>(
    bevy_monitors: &'a Query<(Entity, &Monitor)>,
    monitor: &CurrentMonitor,
) -> (Vec<&'a VideoMode>, Option<u32>) {
    bevy_monitors
        .iter()
        .find(|(_, m)| m.physical_position == monitor.position)
        .map(|(_, m)| {
            (
                m.video_modes.iter().collect(),
                m.refresh_rate_millihertz.map(|r| r / 1000),
            )
        })
        .unwrap_or_default()
}

/// Format refresh rate - use video mode rate in exclusive fullscreen, otherwise monitor rate.
fn format_refresh_rate(window: &Window, monitor_refresh: Option<u32>) -> String {
    let active_refresh = match &window.mode {
        WindowMode::Fullscreen(_, VideoModeSelection::Specific(mode)) => {
            Some(mode.refresh_rate_millihertz / 1000)
        },
        _ => monitor_refresh,
    };
    active_refresh.map_or_else(|| "N/A".into(), |hz| format!("{hz}Hz"))
}

/// Find the index of the currently active video mode if in exclusive fullscreen.
fn find_active_video_mode_index(window: &Window, video_modes: &[&VideoMode]) -> Option<usize> {
    match &window.mode {
        WindowMode::Fullscreen(_, VideoModeSelection::Specific(active)) => {
            video_modes.iter().position(|m| {
                m.physical_size == active.physical_size
                    && m.refresh_rate_millihertz == active.refresh_rate_millihertz
            })
        },
        _ => None,
    }
}

/// Sync selected video mode index to active mode when mode changes.
fn sync_selected_to_active(
    window: &Window,
    monitor: &CurrentMonitor,
    active_mode_idx: Option<usize>,
    selected: &mut SelectedVideoModes,
) {
    if let WindowMode::Fullscreen(_, VideoModeSelection::Specific(active)) = &window.mode {
        let current_mode = (active.physical_size, active.refresh_rate_millihertz);
        if selected.last_sync != Some(current_mode)
            && let Some(active_idx) = active_mode_idx
        {
            selected.set(monitor.index, active_idx);
            selected.last_sync = Some(current_mode);
        }
    } else {
        selected.last_sync = None;
    }
}

/// Get platform suffix for Linux (Wayland or X11).
///
/// Not const on Linux due to `std::env::var` check; clippy false positive on other platforms.
#[cfg_attr(not(target_os = "linux"), allow(clippy::missing_const_for_fn))]
fn platform_suffix() -> &'static str {
    #[cfg(target_os = "linux")]
    {
        if std::env::var("WAYLAND_DISPLAY")
            .map(|v| !v.is_empty())
            .unwrap_or(false)
        {
            " (Wayland)"
        } else {
            " (X11)"
        }
    }
    #[cfg(not(target_os = "linux"))]
    {
        ""
    }
}

/// Format the first row with monitor info.
fn format_monitor_row(monitor: &CurrentMonitor, refresh_display: &str) -> String {
    let primary_marker = if monitor.index == 0 {
        " Primary Monitor -"
    } else {
        " -"
    };
    format!(
        "Monitor: {}{primary_marker} Scale: {} - Refresh Rate: {refresh_display}{}",
        monitor.index,
        monitor.scale,
        platform_suffix()
    )
}

/// Format rows 2 and 3 with monitor and window position/size.
fn format_position_rows(window: &Window, monitor: &CurrentMonitor) -> (String, String) {
    let (monitor_pos, window_pos) = match window.position {
        WindowPosition::At(pos) => format_aligned_pair(
            (monitor.position.x, monitor.position.y),
            (pos.x, pos.y),
            Delimiter::Parens,
        ),
        WindowPosition::Automatic => {
            let monitor_pos = format!("({}, {})", monitor.position.x, monitor.position.y);
            (monitor_pos, "Automatic".to_string())
        },
        WindowPosition::Centered(mon_sel) => {
            let monitor_pos = format!("({}, {})", monitor.position.x, monitor.position.y);
            (monitor_pos, format!("Centered({mon_sel:?})"))
        },
    };

    let (monitor_size, window_size) = format_aligned_pair(
        (monitor.size.x as i32, monitor.size.y as i32),
        (
            window.physical_width() as i32,
            window.physical_height() as i32,
        ),
        Delimiter::None,
    );

    let row2 = format!("Monitor Position: {monitor_pos} - Size: {monitor_size}");
    let row3 = format!("Window  Position: {window_pos} - Size: {window_size}");
    (row2, row3)
}

// --- Input Handling ---

fn handle_input(
    keys: Res<ButtonInput<KeyCode>>,
    mut window_query: Single<(&mut Window, &CurrentMonitor), With<PrimaryWindow>>,
    bevy_monitors: Query<(Entity, &Monitor)>,
    mut selected: ResMut<SelectedVideoModes>,
) {
    let (window, monitor) = &mut *window_query;

    // Get video modes for current monitor by matching position
    let video_modes: Vec<VideoMode> = bevy_monitors
        .iter()
        .find(|(_, m)| m.physical_position == monitor.position)
        .map(|(_, m)| m.video_modes.clone())
        .unwrap_or_default();

    // Navigate video modes (per monitor)
    let current_idx = selected.get(monitor.index);
    if keys.just_pressed(KeyCode::ArrowUp) && current_idx > 0 {
        selected.set(monitor.index, current_idx - 1);
    }
    if keys.just_pressed(KeyCode::ArrowDown) && current_idx < video_modes.len().saturating_sub(1) {
        selected.set(monitor.index, current_idx + 1);
    }

    if keys.just_pressed(KeyCode::Digit1) {
        let selected_idx = selected
            .get(monitor.index)
            .min(video_modes.len().saturating_sub(1));
        let video_mode_selection = video_modes
            .get(selected_idx)
            .map_or(VideoModeSelection::Current, |mode| {
                VideoModeSelection::Specific(*mode)
            });

        window.mode =
            WindowMode::Fullscreen(MonitorSelection::Index(monitor.index), video_mode_selection);
    }
    if keys.just_pressed(KeyCode::Digit2) {
        window.mode = WindowMode::BorderlessFullscreen(MonitorSelection::Index(monitor.index));
    }
    if keys.just_pressed(KeyCode::KeyW) || keys.just_pressed(KeyCode::Escape) {
        window.mode = WindowMode::Windowed;
    }
    if keys.just_pressed(KeyCode::KeyQ) {
        std::process::exit(0);
    }
}

// --- Video Mode Selection State ---

/// Tracks the selected video mode index per monitor for exclusive fullscreen.
#[derive(Resource, Default)]
struct SelectedVideoModes {
    /// Selected index per monitor (keyed by monitor index).
    indices:   HashMap<usize, usize>,
    /// Track last synced mode to avoid overriding user selection.
    last_sync: Option<(UVec2, u32)>,
}

impl SelectedVideoModes {
    fn get(&self, monitor_index: usize) -> usize {
        self.indices.get(&monitor_index).copied().unwrap_or(0)
    }

    fn set(&mut self, monitor_index: usize, index: usize) {
        self.indices.insert(monitor_index, index);
    }
}

// --- Formatting Helpers ---

enum Delimiter {
    Parens,
    None,
}

/// Formats two pairs of values (monitor and window) with right-aligned numbers.
/// Returns (`monitor_str`, `window_str`) with matching widths.
fn format_aligned_pair(
    monitor_vals: (i32, i32),
    window_vals: (i32, i32),
    delimiter: Delimiter,
) -> (String, String) {
    let (m1, m2) = monitor_vals;
    let (w1, w2) = window_vals;

    // Find max width for each column
    let width1 = m1.to_string().len().max(w1.to_string().len());
    let width2 = m2.to_string().len().max(w2.to_string().len());

    match delimiter {
        Delimiter::Parens => (
            format!("({m1:>width1$}, {m2:>width2$})"),
            format!("({w1:>width1$}, {w2:>width2$})"),
        ),
        Delimiter::None => (
            format!("{m1:>width1$}x{m2:>width2$}"),
            format!("{w1:>width1$}x{w2:>width2$}"),
        ),
    }
}

/// Builds the video modes display string showing a scrollable window of modes.
fn build_video_modes_display(
    video_modes: &[&VideoMode],
    selected_idx: usize,
    active_mode_idx: Option<usize>,
) -> String {
    if video_modes.is_empty() {
        return "  (no video modes available)".into();
    }

    let selected_idx = selected_idx.min(video_modes.len().saturating_sub(1));
    let len = video_modes.len();

    // Determine the visible window start position
    let start = if len <= 5 {
        // Show all modes if 5 or fewer
        0
    } else {
        // Center on active mode (slot 3 of 5) if it exists, otherwise center on selected
        let center_target = active_mode_idx.unwrap_or(selected_idx);

        // But always ensure selected is visible by adjusting if needed
        let ideal_start = center_target.saturating_sub(2);
        let ideal_end = ideal_start + 5;

        // Check if selected would be outside the ideal window
        if selected_idx < ideal_start {
            // Selected is above the window, scroll up to show it
            selected_idx.saturating_sub(2)
        } else if selected_idx >= ideal_end {
            // Selected is below the window, scroll down to show it
            (selected_idx + 3).saturating_sub(5)
        } else {
            // Selected is visible, use the ideal centering on active
            ideal_start
        }
        .min(len.saturating_sub(5))
    };
    let end = (start + 5).min(len);

    video_modes[start..end]
        .iter()
        .enumerate()
        .map(|(i, mode)| {
            let actual_idx = start + i;
            let left_marker = if actual_idx == selected_idx { ">" } else { " " };
            let right_marker = if Some(actual_idx) == active_mode_idx {
                " <- active"
            } else {
                ""
            };
            format!(
                "  {left_marker} {}x{} @ {}Hz{right_marker}",
                mode.physical_size.x,
                mode.physical_size.y,
                mode.refresh_rate_millihertz / 1000
            )
        })
        .collect::<Vec<_>>()
        .join("\n")
}

// --- Debug: Winit Monitor Detection ---

/// Debug system that runs every frame and logs winit-detected monitor changes.
fn debug_winit_monitor(
    window: Single<Entity, With<PrimaryWindow>>,
    monitors: Res<Monitors>,
    mut cached_monitor: Local<Option<usize>>,
    _non_send: NonSendMarker,
) {
    let window_entity = *window;

    let winit_monitor_index: Option<usize> = WINIT_WINDOWS.with(|ww| {
        let ww = ww.borrow();
        ww.get_window(window_entity).and_then(|winit_window| {
            winit_window.current_monitor().and_then(|current_monitor| {
                let pos = current_monitor.position();
                monitors.at(pos.x, pos.y).map(|mon| mon.index)
            })
        })
    });

    if *cached_monitor != winit_monitor_index {
        info!(
            "[debug_winit_monitor] Monitor changed: {:?} -> {:?}",
            *cached_monitor, winit_monitor_index
        );
        *cached_monitor = winit_monitor_index;
    }
}

/// Cached state for detecting what changed in Window component.
#[derive(Default)]
struct CachedWindowDebug {
    position: Option<WindowPosition>,
    width:    u32,
    height:   u32,
    mode:     Option<WindowMode>,
    focused:  bool,
}

/// Debug system that logs when Changed<Window> fires and what changed.
fn debug_window_changed(
    window: Single<&Window, (With<PrimaryWindow>, Changed<Window>)>,
    mut cached: Local<CachedWindowDebug>,
) {
    let w = *window;

    let position_changed = cached.position.as_ref() != Some(&w.position);
    let size_changed = cached.width != w.physical_width() || cached.height != w.physical_height();
    let mode_changed = cached.mode.as_ref() != Some(&w.mode);
    let focused_changed = cached.focused != w.focused;

    let mut changes = Vec::new();
    if position_changed {
        changes.push(format!(
            "position: {:?} -> {:?}",
            cached.position, w.position
        ));
    }
    if size_changed {
        changes.push(format!(
            "size: {}x{} -> {}x{}",
            cached.width,
            cached.height,
            w.physical_width(),
            w.physical_height()
        ));
    }
    if mode_changed {
        changes.push(format!("mode: {:?} -> {:?}", cached.mode, w.mode));
    }
    if focused_changed {
        changes.push(format!("focused: {} -> {}", cached.focused, w.focused));
    }

    if !changes.is_empty() {
        info!("[debug_window_changed] {}", changes.join(", "));
    }

    // Update cache
    cached.position = Some(w.position);
    cached.width = w.physical_width();
    cached.height = w.physical_height();
    cached.mode = Some(w.mode);
    cached.focused = w.focused;
}

/// Debug system that logs when `WindowScaleFactorChanged` messages are received.
fn debug_scale_factor_changed(mut messages: MessageReader<WindowScaleFactorChanged>) {
    for msg in messages.read() {
        info!(
            "[debug_scale_factor_changed] WindowScaleFactorChanged received: scale_factor={}",
            msg.scale_factor
        );
    }
}

/// Observer that logs when `WindowTargetLoaded` event is received and inserts a queryable resource.
fn on_window_target_loaded(trigger: On<WindowTargetLoaded>, mut commands: Commands) {
    let event = trigger.event();
    info!(
        "[on_window_target_loaded] WindowTargetLoaded received: entity={:?} position={:?} size={} mode={:?}",
        event.entity, event.position, event.size, event.mode
    );
    commands.insert_resource(WindowTargetLoadedReceived {
        position: event.position,
        size:     event.size,
        mode:     event.mode,
    });
}