gpui_component_assets/lib.rs
1//! Bundled Lucide icons shared by GPUI Kit presentation and behavior layers.
2//!
3//! [`IconName`] and [`IconNamed`] do not depend on GPUI Component. Register
4//! [`Assets`] for the default component bundle, or [`AllAssets`] for the full
5//! catalog. Applications can provide their own source for additional icons.
6
7mod icon;
8pub use icon::{IconName, IconNamed};
9
10/// Embed application assets for GPUI Component.
11///
12/// This assets provides icons svg files for [IconName].
13///
14/// ## Usage
15///
16/// ```rust,no_run
17/// use gpui_component_assets::Assets;
18///
19/// let app = gpui_platform::application().with_assets(Assets);
20/// ```
21///
22/// ## Platform Differences
23///
24/// - **Native (Desktop)**: Icons are embedded in the binary using RustEmbed
25/// - **WASM (Web)**: Icons are downloaded from CDN using web_sys::Request
26/// - This significantly reduces WASM bundle size
27/// - Icons are downloaded on-demand when first used
28/// - Downloaded icons are cached in memory
29#[cfg(not(target_family = "wasm"))]
30mod native_assets;
31
32#[cfg(target_family = "wasm")]
33mod wasm_assets;
34
35#[cfg(not(target_family = "wasm"))]
36pub use native_assets::{AllAssets, Assets};
37
38#[cfg(target_family = "wasm")]
39pub use wasm_assets::Assets;
40
41/// On WASM, the complete catalog uses the same on-demand CDN source.
42#[cfg(target_family = "wasm")]
43pub use wasm_assets::Assets as AllAssets;
44
45// Public only so exported macros can resolve dependencies in downstream crates,
46// including applications that depend solely on the gpui-kit umbrella crate.
47#[doc(hidden)]
48pub mod __private {
49 pub use crate::icon::embedded::*;
50 pub use gpui::{AssetSource, Result, SharedString};
51}
52
53/// Embed only the selected icons in a native or WebAssembly application.
54///
55/// ```ignore
56/// use gpui_kit::assets::{icon_assets, IconName};
57/// icon_assets!(AppAssets, [Search, Check]);
58/// // gpui_kit::application().with_assets(AppAssets)
59/// ```
60///
61/// Unlisted paths return `Ok(None)`. The selected SVG bytes are borrowed from
62/// static storage; loading them does not copy them or build a runtime cache.
63/// This source contains only the selected icons. Applications using components
64/// can compose it with `Assets` to retain their default icons.
65#[macro_export]
66macro_rules! icon_assets {
67 ($vis:vis $name:ident, [$($icon:ident),* $(,)?]) => {
68 #[derive(Clone, Copy, Debug, Default)]
69 $vis struct $name;
70
71 impl $crate::__private::AssetSource for $name {
72 fn load(&self, path: &str) -> $crate::__private::Result<Option<::std::borrow::Cow<'static, [u8]>>> {
73 $(if path == $crate::__private::$icon.0 {
74 return Ok(Some(::std::borrow::Cow::Borrowed($crate::__private::$icon.1)));
75 })*
76 let _ = path;
77 Ok(None)
78 }
79
80 fn list(&self, path: &str) -> $crate::__private::Result<::std::vec::Vec<$crate::__private::SharedString>> {
81 let paths: &[&str] = &[$($crate::__private::$icon.0),*];
82 Ok(paths.iter().copied().filter(|name| name.starts_with(path)).map(Into::into).collect())
83 }
84 }
85 };
86}