From 4ce1dd52137b33d6c698f4be2b5a3fc82ad563ca Mon Sep 17 00:00:00 2001 From: Lean Date: Thu, 19 Sep 2024 16:56:58 -0400 Subject: [PATCH] set window icon --- Cargo.lock | 2 ++ crates/unavi-app/Cargo.toml | 4 +++- crates/unavi-app/src/lib.rs | 2 ++ crates/unavi-app/src/native/icon.rs | 33 +++++++++++++++++++++++++++++ crates/unavi-app/src/native/mod.rs | 1 + 5 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 crates/unavi-app/src/native/icon.rs diff --git a/Cargo.lock b/Cargo.lock index c20d7c41a..3b8e0b232 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -12028,6 +12028,7 @@ dependencies = [ "clap", "directories", "dwn", + "image 0.25.2", "reqwest 0.12.7", "self_update", "surrealdb", @@ -12043,6 +12044,7 @@ dependencies = [ "wasm-bindgen", "wasm-bindgen-futures", "web-sys", + "winit", "winres", "zip", ] diff --git a/crates/unavi-app/Cargo.toml b/crates/unavi-app/Cargo.toml index 8f34e9fe7..3dc0cc2d0 100644 --- a/crates/unavi-app/Cargo.toml +++ b/crates/unavi-app/Cargo.toml @@ -40,13 +40,15 @@ web-sys = { workspace = true, features = [ [target.'cfg(not(target_family = "wasm"))'.dependencies] bevy_oxr = "0.3.0" +image = { version = "0.25.2", default-features = false, features = ["png"] } reqwest.workspace = true self_update = "0.41.0" surrealdb = { workspace = true, features = ["kv-mem", "kv-surrealkv"] } tempfile = "3.12.0" tokio.workspace = true +winit = { version = "0.30.5", default-features = false } zip = { version = "2.2.0", default-features = false, features = ["deflate"] } [build-dependencies] unavi-constants = { path = "../unavi-constants" } -winres = "0.1.12" \ No newline at end of file +winres = "0.1.12" diff --git a/crates/unavi-app/src/lib.rs b/crates/unavi-app/src/lib.rs index 5af1afde4..9b7c74c2f 100644 --- a/crates/unavi-app/src/lib.rs +++ b/crates/unavi-app/src/lib.rs @@ -107,6 +107,8 @@ pub async fn start(db: Surreal, opts: StartOptions) { )); app.add_systems(Startup, unavi_system::spawn_unavi_system); + #[cfg(not(target_family = "wasm"))] + app.add_systems(Startup, native::icon::set_window_icon); if opts.debug_physics { app.add_plugins(PhysicsDebugPlugin::default()); diff --git a/crates/unavi-app/src/native/icon.rs b/crates/unavi-app/src/native/icon.rs new file mode 100644 index 000000000..5a12d8771 --- /dev/null +++ b/crates/unavi-app/src/native/icon.rs @@ -0,0 +1,33 @@ +use std::path::PathBuf; + +use anyhow::Result; +use bevy::{prelude::*, winit::WinitWindows}; +use winit::window::Icon; + +pub fn set_window_icon(windows: NonSend) { + if let Ok(icon) = try_get_icon() { + for window in windows.windows.values() { + window.set_window_icon(Some(icon.clone())); + } + } +} + +/// Try to get the icon from the assets directory. +/// Will likely fail to find the file during devlopment, but +/// should work correctly in the distributed release. +fn try_get_icon() -> Result { + let (icon_rgba, icon_width, icon_height) = { + let image = image::open( + std::env::current_exe()? + .parent() + .unwrap() + .join(PathBuf::from_iter(["assets", "images", "logo.png"])), + )? + .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)?; + Ok(icon) +} diff --git a/crates/unavi-app/src/native/mod.rs b/crates/unavi-app/src/native/mod.rs index 1713ddce6..95bd348e9 100644 --- a/crates/unavi-app/src/native/mod.rs +++ b/crates/unavi-app/src/native/mod.rs @@ -1,3 +1,4 @@ pub mod db; mod dirs; +pub mod icon; pub mod update;