Skip to content

Commit c566150

Browse files
roobscoobalii
andcommitted
Embedded assets
Co-authored-by: Alistair Smith <hi@alistair.sh>
1 parent 3e95351 commit c566150

File tree

3 files changed

+28
-32
lines changed

3 files changed

+28
-32
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ui/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ tokio = { version = "1.41.1", features = ["full"] }
2727
components = { package = "ui", git = "https://github.com/longbridgeapp/gpui-component", version = "0.1.0" }
2828
log = "0.4.22"
2929
random-string = "1.1.0"
30+
rust-embed = "8.5.0"
3031

3132
[features]
3233
default = ["gpui/x11"]

src/ui/src/main.rs

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,19 @@ use std::{fs, path::PathBuf, sync::Arc};
77
use app_state::AppState;
88
use components::theme::Theme;
99
use gpui::*;
10+
use http_client::anyhow;
1011

11-
struct Assets {
12-
base: PathBuf,
13-
}
12+
#[derive(rust_embed::RustEmbed)]
13+
#[folder = "../../assets"]
14+
struct Assets;
1415

1516
impl AssetSource for Assets {
1617
fn load(&self, path: &str) -> Result<Option<std::borrow::Cow<'static, [u8]>>> {
17-
fs::read(self.base.join(path)).map(|data| Some(std::borrow::Cow::Owned(data))).map_err(|e| e.into())
18+
Self::get(path).map(|f| Some(f.data)).ok_or_else(|| anyhow!("could not find asset at path \"{}\"", path))
1819
}
1920

2021
fn list(&self, path: &str) -> Result<Vec<SharedString>> {
21-
fs::read_dir(self.base.join(path))
22-
.map(|entries| entries.filter_map(|entry| entry.ok().and_then(|entry| entry.file_name().into_string().ok()).map(SharedString::from)).collect())
23-
.map_err(|e| e.into())
22+
Ok(Self::iter().filter_map(|p| if p.starts_with(path) { Some(p.into()) } else { None }).collect())
2423
}
2524
}
2625

@@ -40,31 +39,26 @@ async fn main() {
4039

4140
let app_state = Arc::new(AppState {});
4241

43-
App::new()
44-
.with_assets(Assets {
45-
base: PathBuf::from("assets"),
46-
})
47-
.with_http_client(Arc::new(reqwest_client::ReqwestClient::new()))
48-
.run(move |cx: &mut AppContext| {
49-
AppState::set_global(Arc::downgrade(&app_state), cx);
50-
51-
if let Err(e) = init(app_state.clone(), cx) {
52-
log::error!("{}", e);
53-
return;
54-
}
55-
56-
Theme::sync_system_appearance(cx);
57-
58-
let opts = WindowOptions {
59-
window_decorations: Some(WindowDecorations::Client),
60-
titlebar: Some(TitlebarOptions {
61-
appears_transparent: true,
62-
title: Some(SharedString::new_static("scope")),
63-
..Default::default()
64-
}),
42+
App::new().with_assets(Assets).with_http_client(Arc::new(reqwest_client::ReqwestClient::new())).run(move |cx: &mut AppContext| {
43+
AppState::set_global(Arc::downgrade(&app_state), cx);
44+
45+
if let Err(e) = init(app_state.clone(), cx) {
46+
log::error!("{}", e);
47+
return;
48+
}
49+
50+
Theme::sync_system_appearance(cx);
51+
52+
let opts = WindowOptions {
53+
window_decorations: Some(WindowDecorations::Client),
54+
titlebar: Some(TitlebarOptions {
55+
appears_transparent: true,
56+
title: Some(SharedString::new_static("scope")),
6557
..Default::default()
66-
};
58+
}),
59+
..Default::default()
60+
};
6761

68-
cx.open_window(opts, |cx| cx.new_view(|cx| crate::app::App::new(cx))).unwrap();
69-
});
62+
cx.open_window(opts, |cx| cx.new_view(|cx| crate::app::App::new(cx))).unwrap();
63+
});
7064
}

0 commit comments

Comments
 (0)