#icons #file #retrieve #cross-platform #file-path #provider #macos

file_icon_provider

Cross-platform Rust library to retrieve file icons on Windows, MacOS and Linux

8 unstable releases (3 breaking)

0.4.0 Jan 19, 2025
0.3.1 Dec 2, 2024
0.2.1 Nov 30, 2024
0.1.2 Nov 25, 2024

#338 in Images

Download history 213/week @ 2024-11-20 452/week @ 2024-11-27 121/week @ 2024-12-04 103/week @ 2024-12-11 173/week @ 2024-12-18 147/week @ 2024-12-25 108/week @ 2025-01-01 77/week @ 2025-01-08 172/week @ 2025-01-15 151/week @ 2025-01-22 57/week @ 2025-01-29

470 downloads per month
Used in tauri-plugin-fs-pro

MIT license

37KB
251 lines

File Icon Provider

version Documentation

File Icon Provider is a cross-platform Rust library designed to simplify the retrieval of file icons on Windows, MacOS and Linux (Gnome).

Use the get_file_icon function to retrieve the icon for a specific file path.

Examples

//! Extract and save the system icon associated with any file.
//!
//! Usage: cargo run --example save_icon <source_file> <output_name>
//! Example: cargo run --example save_icon document.pdf icon.png

use file_icon_provider::get_file_icon;
use clap::Parser;
use image::{DynamicImage, RgbaImage};
use std::path::PathBuf;

#[derive(Parser)]
#[command(version, about = "Retrieve and save the icon associated with any file.", long_about = None)]
struct Cli {
    /// The file we want to extract the icon.
    file_path: PathBuf,
    /// The file path of the extracted image.
    output_path: PathBuf,
}

fn main() {
    let cli = Cli::parse();
    let icon = get_file_icon(cli.file_path, 32).expect("Failed to get icon");
    let image = RgbaImage::from_raw(icon.width, icon.height, icon.pixels)
        .map(DynamicImage::ImageRgba8)
        .expect("Failed to convert Icon to Image");

    match image.save_with_format(&cli.output_path, image::ImageFormat::Png) {
        Err(error) => {
            println!("Failed to save the image: {}", error);
        }
        _ => println!("Saved image: '{}'", cli.output_path.display()),
    }
}

Examples are available in the examples directory.

Linux Support

Linux support is limited, and the get_file_icon function must be called from the main thread. Contributions to improve compatibility are welcome.

Installation

On Linux you need to install theses packages:

sudo apt install libgtk-4-dev libgtk-3-dev libatk1.0-dev

Dependencies

~0–41MB
~616K SLoC