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 = "prompts/**/*"]
15#[include = "*.md"]
16#[exclude = "*.DS_Store"]
17pub struct Assets;
18
19impl AssetSource for Assets {
20 fn load(&self, path: &str) -> Result<Option<std::borrow::Cow<'static, [u8]>>> {
21 Self::get(path)
22 .map(|f| Some(f.data))
23 .ok_or_else(|| anyhow!("could not find asset at path \"{}\"", path))
24 }
25
26 fn list(&self, path: &str) -> Result<Vec<SharedString>> {
27 Ok(Self::iter()
28 .filter_map(|p| {
29 if p.starts_with(path) {
30 Some(p.into())
31 } else {
32 None
33 }
34 })
35 .collect())
36 }
37}
38
39impl Assets {
40 /// Populate the [`TextSystem`] of the given [`AppContext`] with all `.ttf` fonts in the `fonts` directory.
41 pub fn load_fonts(&self, cx: &AppContext) -> gpui::Result<()> {
42 let font_paths = self.list("fonts")?;
43 let mut embedded_fonts = Vec::new();
44 for font_path in font_paths {
45 if font_path.ends_with(".ttf") {
46 let font_bytes = cx
47 .asset_source()
48 .load(&font_path)?
49 .expect("Assets should never return None");
50 embedded_fonts.push(font_bytes);
51 }
52 }
53
54 cx.text_system().add_fonts(embedded_fonts)
55 }
56
57 pub fn load_test_fonts(&self, cx: &AppContext) {
58 cx.text_system()
59 .add_fonts(vec![self
60 .load("fonts/plex-mono/ZedPlexMono-Regular.ttf")
61 .unwrap()
62 .unwrap()])
63 .unwrap()
64 }
65}