Skip to content

Commit

Permalink
introduce windows app icons
Browse files Browse the repository at this point in the history
  • Loading branch information
PolyMeilex committed Jan 1, 2024
1 parent 9182ade commit 71244cf
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 0 deletions.
60 changes: 60 additions & 0 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions neothesia/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,7 @@ iced_widget = { git = "https://github.com/iced-rs/iced.git", rev="fc285d3e461626

[[bin]]
name = "neothesia"

[target.'cfg(target_os = "windows")'.build-dependencies]
image = "0.24"
embed-resource = "2.4.0"
27 changes: 27 additions & 0 deletions neothesia/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
fn main() {
#[cfg(target_os = "windows")]
{
use image::io::Reader as ImageReader;
use std::{
env,
fs::File,

Check warning on line 7 in neothesia/build.rs

View workflow job for this annotation

GitHub Actions / build_windows

unused imports: `Path`, `fs::File`

Check warning on line 7 in neothesia/build.rs

View workflow job for this annotation

GitHub Actions / build_windows

unused imports: `Path`, `fs::File`
path::{Path, PathBuf},
};

let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let out_ico = out_dir.join("icon.ico");
let out_manifest = out_dir.join("manifest.rc");

let img = ImageReader::open("../flatpak/com.github.polymeilex.neothesia.png")
.unwrap()
.decode()
.unwrap();

img.save(&out_ico);

Check warning on line 20 in neothesia/build.rs

View workflow job for this annotation

GitHub Actions / build_windows

unused `Result` that must be used

Check warning on line 20 in neothesia/build.rs

View workflow job for this annotation

GitHub Actions / build_windows

unused `Result` that must be used

let manifest = "neothesia_icon ICON \"icon.ico\"";
std::fs::write(&out_manifest, &manifest);

Check warning on line 23 in neothesia/build.rs

View workflow job for this annotation

GitHub Actions / build_windows

unused `Result` that must be used

Check warning on line 23 in neothesia/build.rs

View workflow job for this annotation

GitHub Actions / build_windows

unused `Result` that must be used

embed_resource::compile(&out_manifest, embed_resource::NONE);
}
}
26 changes: 26 additions & 0 deletions neothesia/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,10 @@ fn init(builder: winit::window::WindowBuilder) -> (EventLoop<NeothesiaEvent>, Ta

let window = builder.build(&event_loop).unwrap();

if let Err(err) = set_window_icon(&window) {
log::error!("Failed to load window icon: {}", err);
}

let window_state = WindowState::new(&window);
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
backends: wgpu_jumpstart::default_backends(),
Expand All @@ -308,3 +312,25 @@ fn init(builder: winit::window::WindowBuilder) -> (EventLoop<NeothesiaEvent>, Ta

(event_loop, target, surface)
}

fn set_window_icon(window: &winit::window::Window) -> Result<(), Box<dyn std::error::Error>> {
use iced_graphics::image::image_rs;
use image_rs::codecs::png::PngDecoder;
use image_rs::ImageDecoder;
use std::io::{Cursor, Read};

let icon = PngDecoder::new(Cursor::new(include_bytes!(
"../../flatpak/com.github.polymeilex.neothesia.png"
)))?;

let (w, h) = icon.dimensions();

let mut icon = icon.into_reader()?;

let mut buff = Vec::new();
icon.read_to_end(&mut buff)?;

window.set_window_icon(Some(winit::window::Icon::from_rgba(buff, w, h)?));

Ok(())
}

0 comments on commit 71244cf

Please sign in to comment.