Skip to content

Commit

Permalink
set window icon
Browse files Browse the repository at this point in the history
  • Loading branch information
kayhhh committed Sep 19, 2024
1 parent 4fd7635 commit 4ce1dd5
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 1 deletion.
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.

4 changes: 3 additions & 1 deletion crates/unavi-app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
winres = "0.1.12"
2 changes: 2 additions & 0 deletions crates/unavi-app/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ pub async fn start(db: Surreal<Db>, 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());
Expand Down
33 changes: 33 additions & 0 deletions crates/unavi-app/src/native/icon.rs
Original file line number Diff line number Diff line change
@@ -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<WinitWindows>) {
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<Icon> {
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)
}
1 change: 1 addition & 0 deletions crates/unavi-app/src/native/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod db;
mod dirs;
pub mod icon;
pub mod update;

0 comments on commit 4ce1dd5

Please sign in to comment.