Skip to main content

bevy_wicon/
lib.rs

1use bevy::ecs::system::NonSendMarker;
2use bevy::prelude::*;
3
4#[derive(Debug, Default, Clone)]
5pub struct WindowIconPlugin {
6    path: String,
7}
8
9#[derive(Resource)]
10struct WindowIconResource {
11    path: String,
12}
13
14impl Plugin for WindowIconPlugin {
15    fn build(&self, app: &mut App) {
16        app.insert_resource(WindowIconResource {
17            path: self.path.clone(),
18        });
19        app.add_systems(Startup, set);
20    }
21}
22
23impl WindowIconPlugin {
24    pub fn with_path(path: &str) -> Self {
25        Self {
26            path: path.to_string(),
27        }
28    }
29}
30
31fn set(window_icon_resource: Res<WindowIconResource>, _: NonSendMarker) -> Result {
32    let icon = image::open(window_icon_resource.path.as_str())?.into_rgba8();
33    bevy::winit::WINIT_WINDOWS.with_borrow_mut(|winit_windows| -> Result {
34        if winit_windows.windows.is_empty() {
35            return Ok(());
36        }
37        for window in winit_windows.windows.values() {
38            window.set_window_icon(Some(winit::window::Icon::from_rgba(
39                icon.clone().into_raw(),
40                icon.width(),
41                icon.height(),
42            )?));
43        }
44        Ok(())
45    })
46}