bevy-website icon indicating copy to clipboard operation
bevy-website copied to clipboard

The 0.9 to 0.10 migration guide does not explain how to work with icons

Open Aultus-defora opened this issue 1 year ago • 2 comments

The guide mentiones that WindowDescriptor are no longer used but it does not explain how to add/change icon of the app

Aultus-defora avatar Mar 08 '23 18:03 Aultus-defora

This wasn't in the migration guide because it has not changed. Bevy does not have a direct API for setting a window icon yet. You'll have to access it via the windowing library Bevy uses called winit. There's a (slightly outdated, but mostly functional) example of how to do it in the cheat book here.

tim-blackbird avatar Mar 08 '23 19:03 tim-blackbird

I've got the following snippet working on my machine. Hope it helps!

fn set_window_icon(
    new_windows: Query<Entity, Added<Window>>,
    winit_windows: NonSend<WinitWindows>,
) {
    for entity in &new_windows {
        let winit_window = winit_windows.get_window(entity).unwrap();

        let (icon_rgba, icon_width, icon_height) = {
            let image = image::open("my_icon.png")
                .expect("Failed to open icon path")
                .into_rgba8();
            let (width, height) = image.dimensions();
            let rgba = image.into_raw();
            (rgba, width, height)
        };

        let icon = Icon::from_rgba(icon_rgba, icon_width, icon_height).unwrap();

        winit_window.set_window_icon(Some(icon));
    }
}

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_system(set_window_icon)
        .run();
}

Still requires adding winit(version 0.28) and image to your Cargo.toml

tim-blackbird avatar Mar 10 '23 17:03 tim-blackbird

Closing this as won't fix, since the method for setting the icon was not changed between 0.9 and 0.10.

BD103 avatar Mar 08 '24 19:03 BD103