Skip to main content

wgpui_kit_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 crate embeds Lucide SVGs for `wgpui-component`'s `IconName`.
13///
14/// ## Usage
15///
16/// ```rust,no_run
17/// use wgpui::*;
18/// use wgpui_kit_assets::Assets;
19///
20/// let app = Application::new().with_assets(Assets);
21/// ```
22///
23/// ## Platform Differences
24///
25/// - **Native (Desktop)**: Icons are embedded in the binary using RustEmbed
26/// - **WebAssembly adapter**: Reqwest downloads icons from the configured endpoint.
27///   - The default empty endpoint requests relative `/assets/icons/` paths.
28///   - Icons are requested on demand and cached in memory; no CDN is built in.
29///   - Framework/application WebAssembly acceptance is a separate platform gate.
30#[cfg(not(target_family = "wasm"))]
31mod native_assets;
32
33#[cfg(target_family = "wasm")]
34mod wasm_assets;
35
36#[cfg(not(target_family = "wasm"))]
37pub use native_assets::{AllAssets, Assets};
38
39#[cfg(target_family = "wasm")]
40pub use wasm_assets::Assets;
41
42/// On WebAssembly, the complete catalog uses the same application-hosted source.
43/// This asset adapter alone does not establish full framework WebAssembly support.
44#[cfg(target_family = "wasm")]
45pub use wasm_assets::Assets as AllAssets;
46
47// Public only so exported macros can resolve dependencies in downstream crates,
48// including applications that depend solely on the WGPUI Kit umbrella crate.
49#[doc(hidden)]
50pub mod __private {
51    pub use crate::icon::embedded::*;
52    pub use wgpui::{AssetSource, Result, SharedString};
53}
54
55/// Embed only the selected icons in a native or WebAssembly application.
56///
57/// ```ignore
58/// use wgpui_kit::assets::{icon_assets, IconName};
59/// icon_assets!(AppAssets, [Search, Check]);
60/// // wgpui_kit::application().with_assets(AppAssets)
61/// ```
62///
63/// Unlisted paths return `Ok(None)`. The selected SVG bytes are borrowed from
64/// static storage; loading them does not copy them or build a runtime cache.
65/// This source contains only the selected icons. Applications using components
66/// can compose it with `Assets` to retain their default icons.
67#[macro_export]
68macro_rules! icon_assets {
69    ($vis:vis $name:ident, [$($icon:ident),* $(,)?]) => {
70        #[derive(Clone, Copy, Debug, Default)]
71        $vis struct $name;
72
73        impl $crate::__private::AssetSource for $name {
74            fn load(&self, path: &str) -> $crate::__private::Result<Option<::std::borrow::Cow<'static, [u8]>>> {
75                $(if path == $crate::__private::$icon.0 {
76                    return Ok(Some(::std::borrow::Cow::Borrowed($crate::__private::$icon.1)));
77                })*
78                let _ = path;
79                Ok(None)
80            }
81
82            fn list(&self, path: &str) -> $crate::__private::Result<::std::vec::Vec<$crate::__private::SharedString>> {
83                let paths: &[&str] = &[$($crate::__private::$icon.0),*];
84                Ok(paths.iter().copied().filter(|name| name.starts_with(path)).map(Into::into).collect())
85            }
86        }
87    };
88}