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.
2
3use anyhow::Context as _;
4use gpui::{App, AssetSource, Result, SharedString};
5use rust_embed::RustEmbed;
6
7#[derive(RustEmbed)]
8#[folder = "../../assets"]
9#[include = "fonts/**/*"]
10#[include = "icons/**/*"]
11#[include = "images/**/*"]
12#[include = "themes/**/*"]
13#[exclude = "themes/src/*"]
14#[include = "sounds/**/*"]
15#[include = "prompts/**/*"]
16#[include = "*.md"]
17#[exclude = "*.DS_Store"]
18pub struct Assets;
19
20impl AssetSource for Assets {
21 fn load(&self, path: &str) -> Result<Option<std::borrow::Cow<'static, [u8]>>> {
22 Self::get(path)
23 .map(|f| Some(f.data))
24 .with_context(|| format!("loading asset at path {path:?}"))
25 }
26
27 fn list(&self, path: &str) -> Result<Vec<SharedString>> {
28 Ok(Self::iter()
29 .filter_map(|p| {
30 if p.starts_with(path) {
31 Some(p.into())
32 } else {
33 None
34 }
35 })
36 .collect())
37 }
38}
39
40impl Assets {
41 /// Populate the [`TextSystem`] of the given [`AppContext`] with all `.ttf` fonts in the `fonts` directory.
42 pub fn load_fonts(&self, cx: &App) -> anyhow::Result<()> {
43 let font_paths = self.list("fonts")?;
44 let mut embedded_fonts = Vec::new();
45 for font_path in font_paths {
46 if font_path.ends_with(".ttf") {
47 let font_bytes = cx
48 .asset_source()
49 .load(&font_path)?
50 .expect("Assets should never return None");
51 embedded_fonts.push(font_bytes);
52 }
53 }
54
55 cx.text_system().add_fonts(embedded_fonts)
56 }
57
58 pub fn load_test_fonts(&self, cx: &App) {
59 cx.text_system()
60 .add_fonts(vec![
61 self.load("fonts/lilex/Lilex-Regular.ttf").unwrap().unwrap(),
62 ])
63 .unwrap()
64 }
65}