Skip to main content

gpui_m3/
assets.rs

1//! Icons embedded in the binary (24 px strokes, the same set as the reference design).
2//! They are requested as `m3/icons/<name>.svg`, so they never clash with the host app's
3//! own `icons/`; see [`crate::Icon`].
4
5use std::borrow::Cow;
6
7use gpui::{AssetSource, Result, SharedString};
8
9/// The library's asset source. To combine it with your app's, delegate
10/// `m3/…` paths to it or use [`Assets::load_icon`].
11pub struct Assets;
12
13impl Assets {
14    pub fn load_icon(path: &str) -> Option<&'static [u8]> {
15        let name = path.strip_prefix(ICON_PREFIX)?.strip_suffix(".svg")?;
16        ICONS
17            .iter()
18            .find(|(n, _)| *n == name)
19            .map(|(_, bytes)| *bytes)
20    }
21
22    /// Names of all available icons.
23    pub fn icon_names() -> impl Iterator<Item = &'static str> {
24        ICONS.iter().map(|(n, _)| *n)
25    }
26}
27
28impl AssetSource for Assets {
29    fn load(&self, path: &str) -> Result<Option<Cow<'static, [u8]>>> {
30        Ok(Self::load_icon(path).map(Cow::Borrowed))
31    }
32
33    fn list(&self, path: &str) -> Result<Vec<SharedString>> {
34        Ok(if path.starts_with("m3") {
35            ICONS
36                .iter()
37                .map(|(n, _)| format!("{ICON_PREFIX}{n}.svg").into())
38                .collect()
39        } else {
40            Vec::new()
41        })
42    }
43}
44
45/// Asset path prefix of the library icons.
46pub const ICON_PREFIX: &str = "m3/icons/";
47
48const ICONS: &[(&str, &[u8])] = &[
49    ("alert", include_bytes!("../assets/icons/alert.svg")),
50    (
51        "arrow-left",
52        include_bytes!("../assets/icons/arrow-left.svg"),
53    ),
54    ("at", include_bytes!("../assets/icons/at.svg")),
55    ("blocks", include_bytes!("../assets/icons/blocks.svg")),
56    ("bolt", include_bytes!("../assets/icons/bolt.svg")),
57    ("bookmark", include_bytes!("../assets/icons/bookmark.svg")),
58    ("box", include_bytes!("../assets/icons/box.svg")),
59    ("brain", include_bytes!("../assets/icons/brain.svg")),
60    ("branch", include_bytes!("../assets/icons/branch.svg")),
61    ("chat", include_bytes!("../assets/icons/chat.svg")),
62    ("check", include_bytes!("../assets/icons/check.svg")),
63    (
64        "chevron-down",
65        include_bytes!("../assets/icons/chevron-down.svg"),
66    ),
67    (
68        "chevron-right",
69        include_bytes!("../assets/icons/chevron-right.svg"),
70    ),
71    ("clip", include_bytes!("../assets/icons/clip.svg")),
72    ("command", include_bytes!("../assets/icons/command.svg")),
73    ("copy", include_bytes!("../assets/icons/copy.svg")),
74    ("cpu", include_bytes!("../assets/icons/cpu.svg")),
75    ("diff", include_bytes!("../assets/icons/diff.svg")),
76    ("export", include_bytes!("../assets/icons/export.svg")),
77    ("file", include_bytes!("../assets/icons/file.svg")),
78    ("files", include_bytes!("../assets/icons/files.svg")),
79    (
80        "folder-open",
81        include_bytes!("../assets/icons/folder-open.svg"),
82    ),
83    (
84        "folder-plus",
85        include_bytes!("../assets/icons/folder-plus.svg"),
86    ),
87    ("folder", include_bytes!("../assets/icons/folder.svg")),
88    ("gauge", include_bytes!("../assets/icons/gauge.svg")),
89    ("gear", include_bytes!("../assets/icons/gear.svg")),
90    ("history", include_bytes!("../assets/icons/history.svg")),
91    ("hook", include_bytes!("../assets/icons/hook.svg")),
92    ("import", include_bytes!("../assets/icons/import.svg")),
93    ("layers", include_bytes!("../assets/icons/layers.svg")),
94    ("maximize", include_bytes!("../assets/icons/maximize.svg")),
95    ("monitor", include_bytes!("../assets/icons/monitor.svg")),
96    ("moon", include_bytes!("../assets/icons/moon.svg")),
97    ("more", include_bytes!("../assets/icons/more.svg")),
98    ("palette", include_bytes!("../assets/icons/palette.svg")),
99    (
100        "panel-bottom",
101        include_bytes!("../assets/icons/panel-bottom.svg"),
102    ),
103    (
104        "panel-left",
105        include_bytes!("../assets/icons/panel-left.svg"),
106    ),
107    (
108        "panel-right",
109        include_bytes!("../assets/icons/panel-right.svg"),
110    ),
111    ("pen", include_bytes!("../assets/icons/pen.svg")),
112    ("plug", include_bytes!("../assets/icons/plug.svg")),
113    ("plus", include_bytes!("../assets/icons/plus.svg")),
114    ("refresh", include_bytes!("../assets/icons/refresh.svg")),
115    ("search", include_bytes!("../assets/icons/search.svg")),
116    ("shield", include_bytes!("../assets/icons/shield.svg")),
117    ("slash", include_bytes!("../assets/icons/slash.svg")),
118    (
119        "smartphone",
120        include_bytes!("../assets/icons/smartphone.svg"),
121    ),
122    ("spark", include_bytes!("../assets/icons/spark.svg")),
123    ("star", include_bytes!("../assets/icons/star.svg")),
124    ("star-fill", include_bytes!("../assets/icons/star-fill.svg")),
125    ("stop", include_bytes!("../assets/icons/stop.svg")),
126    ("sun", include_bytes!("../assets/icons/sun.svg")),
127    ("terminal", include_bytes!("../assets/icons/terminal.svg")),
128    ("trash", include_bytes!("../assets/icons/trash.svg")),
129    ("up", include_bytes!("../assets/icons/up.svg")),
130    ("x", include_bytes!("../assets/icons/x.svg")),
131];