bevy icon indicating copy to clipboard operation
bevy copied to clipboard

window::Cursor does not support ..default() initialization pattern

Open Cpapa97 opened this issue 1 year ago • 1 comments

Bevy version

0.10.0

What you did

use bevy::{
    prelude::*,
    window::{Cursor, CursorGrabMode, PresentMode, WindowMode},
};

fn main() {
    App::new()
        .add_plugins(DefaultPlugins.set(WindowPlugin {
            primary_window: Some(Window {
                title: "Title".to_string(),
                present_mode: PresentMode::AutoNoVsync,
                mode: WindowMode::BorderlessFullscreen,
                cursor: Cursor {
                    visible: false,
                    grab_mode: CursorGrabMode::Confined,
                    ..default()
                },
                ..default()
            }),
            ..default()
        }))
    }
    .run();
}

What went wrong

This section errors because Cursor.physical_position is private even though everything else is public. This makes the ..default() initialization pattern not work because it can't set the physical_position field.

cursor: Cursor {
    visible: false,
    grab_mode: CursorGrabMode::Confined,
    ..default()
},

Additional information

Workaround:

cursor: {
    let mut cursor = Cursor::default();
    cursor.visible = false;
    cursor.grab_mode = CursorGrabMode::Confined;
    cursor
},

This workaround is quite compact and just fine honestly, though the current behavior is a bit unexpected considering how many other struct initializations work for Bevy users.

Cpapa97 avatar Mar 07 '23 06:03 Cpapa97

Checking the blame for the relevant line shows this field was intentionally made private to fix another issue, so it does not seem to be a trivial fix unfortunately.

  • https://github.com/bevyengine/bevy/pull/7297

ickk avatar Mar 07 '23 12:03 ickk