Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attaching an icon resource to a Rust application

Tags:

rust

How do you attach an icon resource to a Rust application? I've seen how it's done in C but I'm not clear on how it works in Rust. This would be on Windows. I know that Linux and OS X work differently. If anyone has any tips on this for OS X that would be great too.

like image 494
Chris Root Avatar asked May 17 '15 20:05

Chris Root


1 Answers

Windows

An easy way to set the icon for your .exe file is with the winres crate. First, add winres as a build dependency in your Cargo.toml:

[target.'cfg(windows)'.build-dependencies]
winres = "0.1"

Then, add a build script (a file named build.rs next to your Cargo.toml):

use std::io;
#[cfg(windows)] use winres::WindowsResource;

fn main() -> io::Result<()> {
    #[cfg(windows)] {
        WindowsResource::new()
            // This path can be absolute, or relative to your crate root.
            .set_icon("assets/icon.ico")
            .compile()?;
    }
    Ok(())
}

Note that this does not update the icon shown in the taskbar or title bar. Setting that must be done via your GUI framework, e.g. iced recently added a way to configure this.

macOS

To set the icon on macOS, you need to bundle the executable into an .app. An .app is actually a directory, not a file. It looks something like this:

  • My App.app
    • Contents
      • Info.plist — This XML file includes information about your app, including the name of the binary and the location of the icon file:
        <key>CFBundleExecutable</key>
        <string>myapp</string>
        <key>CFBundleIconFile</key>
        <string>AppIcon.icns</string>
        
      • MacOS
        • myapp — The binary from target/release
      • Resources
        • AppIcon.icns

macOS apps are typically distributed as .dmg files. A release script could build the binary, bundle it into an .app, and then bundle that into a .dmg, along with a symlink to /Applications to make it easier for the user to “install” the app by moving it there.

Here are sample .dmg contents, and the corresponding release script.

like image 137
Fenhl Avatar answered Sep 17 '22 04:09

Fenhl