Skip to content

Commit

Permalink
Set window icon on desktop platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
mystal committed Jun 1, 2024
1 parent cbf1b89 commit 501f575
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ features = [
version = "0.13"
features = ["serialize"]

# Dependencies to set window icon. Versions should match bevy's.
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
image = { version = "0.24", default-features = false, features = ["png"] }
winit = "0.29"

[target.'cfg(target_arch = "wasm32")'.dependencies]
# Show panics in the browser console: https://bevy-cheatbook.github.io/platforms/wasm/panic-console.html
console_error_panic_hook = "0.1"
Expand Down
3 changes: 2 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* [x] Loading infrastructure
* [x] Animated sprites
* Mostly working, seems to be a bug with bevy_aseprite
* [ ] Set window icon: https://bevy-cheatbook.github.io/window/icon.html
* [x] Set window icon: https://bevy-cheatbook.github.io/window/icon.html
* [ ] Set exe/app icon
* On Linux
* On Windows: https://bevy-cheatbook.github.io/platforms/windows.html#creating-an-icon-for-your-app
Expand Down Expand Up @@ -46,6 +46,7 @@
* [x] Update to bevy 0.13
* [x] Remove input events when egui should consume them:
https://github.com/mvlabat/bevy_egui/issues/47#issuecomment-1922695612
* [ ] Move window_state.ron to a local config/data file.
* Pixel-perfect rendering
* [x] Remove OS window scaling.
* [x] Allow scaling window by integer values. Default to 2x
Expand Down
35 changes: 34 additions & 1 deletion src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ impl Plugin for WindowPlugin {
.add_systems(PostUpdate, update_window_state);
#[cfg(not(target_arch = "wasm32"))]
{
app.add_systems(Last, save_window_state_on_exit.run_if(on_event::<AppExit>()));
app
.add_systems(Startup, window_icon::set_window_icon)
.add_systems(Last, save_window_state_on_exit.run_if(on_event::<AppExit>()));

app
.insert_resource(LogFpsTimer::default())
Expand All @@ -78,6 +80,37 @@ impl Plugin for WindowPlugin {
}
}

#[cfg(not(target_arch = "wasm32"))]
mod window_icon {
use bevy::prelude::*;
use bevy::winit::WinitWindows;
use winit::window::Icon;

pub fn set_window_icon(
// Have to use `NonSend` here
windows: NonSend<WinitWindows>,
) {
// Taken from: https://bevy-cheatbook.github.io/window/icon.html
// Use the `image` crate to load our icon data from a png file.
// NOTE: This is not a very bevy-native solution, but it will do.
let (icon_rgba, icon_width, icon_height) = {
let image = image::open("assets/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)
.expect("Could not create Icon from icon data");

// Do it for all windows.
for window in windows.windows.values() {
window.set_window_icon(Some(icon.clone()));
}
}
}

pub fn update_window_state(
mut window_state: ResMut<WindowState>,
window_q: Query<&Window, (With<PrimaryWindow>, Changed<Window>)>,
Expand Down

0 comments on commit 501f575

Please sign in to comment.