| 1 | use std::env;
|
| 2 | use std::fs::File;
|
| 3 | use std::io::BufWriter;
|
| 4 | use std::path::Path;
|
| 5 |
|
| 6 | fn main() {
|
| 7 | println!("cargo:rerun-if-changed=build.rs");
|
| 8 | println!("cargo:rerun-if-changed=pos-logo.png");
|
| 9 |
|
| 10 | if env::var("CARGO_CFG_TARGET_OS").as_deref() != Ok("windows") {
|
| 11 | return;
|
| 12 | }
|
| 13 |
|
| 14 | let out_dir = env::var("OUT_DIR").expect("OUT_DIR");
|
| 15 | let ico_path = Path::new(&out_dir).join("pos-logo.ico");
|
| 16 | write_ico("pos-logo.png", &ico_path);
|
| 17 |
|
| 18 | winres::WindowsResource::new()
|
| 19 | .set_icon(ico_path.to_string_lossy().as_ref())
|
| 20 | .compile()
|
| 21 | .expect("failed to embed Windows executable icon");
|
| 22 | }
|
| 23 |
|
| 24 | fn write_ico(png_path: &str, out_path: &Path) {
|
| 25 | let png_bytes = std::fs::read(png_path).unwrap_or_else(|e| {
|
| 26 | panic!("failed to read {png_path}: {e}");
|
| 27 | });
|
| 28 | let image = image::load_from_memory(&png_bytes)
|
| 29 | .unwrap_or_else(|e| panic!("failed to decode {png_path}: {e}"))
|
| 30 | .to_rgba8();
|
| 31 |
|
| 32 | let mut icon_dir = ico::IconDir::new(ico::ResourceType::Icon);
|
| 33 | for size in [16u32, 24, 32, 48, 64, 128, 256] {
|
| 34 | let rgba = if image.width() == size && image.height() == size {
|
| 35 | image.clone().into_raw()
|
| 36 | } else {
|
| 37 | image::imageops::resize(
|
| 38 | &image,
|
| 39 | size,
|
| 40 | size,
|
| 41 | image::imageops::FilterType::Triangle,
|
| 42 | )
|
| 43 | .into_raw()
|
| 44 | };
|
| 45 | let icon_image = ico::IconImage::from_rgba_data(size, size, rgba);
|
| 46 | let entry = ico::IconDirEntry::encode(&icon_image)
|
| 47 | .unwrap_or_else(|e| panic!("failed to encode {size}x{size} icon: {e}"));
|
| 48 | icon_dir.add_entry(entry);
|
| 49 | }
|
| 50 |
|
| 51 | let file = File::create(out_path).unwrap_or_else(|e| panic!("failed to create {out_path:?}: {e}"));
|
| 52 | let mut writer = BufWriter::new(file);
|
| 53 | icon_dir
|
| 54 | .write(&mut writer)
|
| 55 | .unwrap_or_else(|e| panic!("failed to write {out_path:?}: {e}"));
|
| 56 | }
|