1// This crate was essentially pulled out verbatim from main `zed` crate to avoid having to run RustEmbed macro whenever zed has to be rebuilt. It saves a second or two on an incremental build.
2use anyhow::anyhow;
3
4use gpui::{AppContext, AssetSource, Result, SharedString};
5use rust_embed::RustEmbed;
6
7#[derive(RustEmbed)]
8#[folder = "../../assets"]
9#[include = "fonts/**/*"]
10#[include = "icons/**/*"]
11#[include = "themes/**/*"]
12#[exclude = "themes/src/*"]
13#[include = "sounds/**/*"]
14#[include = "*.md"]
15#[exclude = "*.DS_Store"]
16pub struct Assets;
17
18impl AssetSource for Assets {
19 fn load(&self, path: &str) -> Result<Option<std::borrow::Cow<'static, [u8]>>> {
20 Self::get(path)
21 .map(|f| Some(f.data))
22 .ok_or_else(|| anyhow!("could not find asset at path \"{}\"", path))
23 }
24
25 fn list(&self, path: &str) -> Result<Vec<SharedString>> {
26 Ok(Self::iter()
27 .filter_map(|p| {
28 if p.starts_with(path) {
29 Some(p.into())
30 } else {
31 None
32 }
33 })
34 .collect())
35 }
36}
37
38impl Assets {
39 /// Populate the [`TextSystem`] of the given [`AppContext`] with all `.ttf` fonts in the `fonts` directory.
40 pub fn load_fonts(&self, cx: &AppContext) -> gpui::Result<()> {
41 let font_paths = self.list("fonts")?;
42 let mut embedded_fonts = Vec::new();
43 for font_path in font_paths {
44 if font_path.ends_with(".ttf") {
45 let font_bytes = cx
46 .asset_source()
47 .load(&font_path)?
48 .expect("Assets should never return None");
49 embedded_fonts.push(font_bytes);
50 }
51 }
52
53 cx.text_system().add_fonts(embedded_fonts)
54 }
55}