gpui-component/examples/app_assets
2025-10-23 16:36:23 +08:00
..
assets/icons examples: Add examples folder. (#1378) 2025-10-15 02:26:13 +00:00
src examples: Add examples folder. (#1378) 2025-10-15 02:26:13 +00:00
Cargo.toml Bump v0.3.0-preview2 2025-10-23 16:36:23 +08:00
README.md examples: Add examples folder. (#1378) 2025-10-15 02:26:13 +00:00

Icon assets in GPUI Component

The IconName is a enum that defined a bunch of icon names, because some internal components in GPUI Component will use them.

You can see, we have a lot of svg icon files in the assets/icons folder, but we are not embed all of the icon files in the library by default. This for keep the library size small.

So you must have your own icon files to use the Icon component in GPUI Component.

You can download the icon files from here or use your own icon files as you wish, just use the same filename as the icon name (match with the IconName defined) you want to use.

For example your assets folder:

app_root
  assets
    icons
      close.svg
      menu.svg
      ...
  src
    main.rs
  Cargo.toml

You also can just copy the svg files you want from the assets/icons folder in GPUI Component repo to your own assets folder.

How to use

You need define a Assets struct with rust-embed to register assets to GPUI application.

use anyhow::anyhow;
use gpui::*;
use rust_embed::RustEmbed;
use std::borrow::Cow;

#[derive(RustEmbed)]
#[folder = "./assets"]
#[include = "icons/**/*.svg"]
pub struct Assets;

impl AssetSource for Assets {
    fn load(&self, path: &str) -> Result<Option<Cow<'static, [u8]>>> {
        Self::get(path)
            .map(|f| Some(f.data))
            .ok_or_else(|| anyhow!("could not find asset at path \"{path}\""))
    }

    fn list(&self, path: &str) -> Result<Vec<SharedString>> {
        Ok(Self::iter()
            .filter_map(|p| p.starts_with(path).then(|| p.into()))
            .collect())
    }
}

fn main() {
    // Call with_assets to register assets
    let app = Application::new().with_assets(Assets);

    // ...
}