1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-30 05:10:03 -04:00

Add http fetch to eframe and implement it in egui_glium using ureq

This commit is contained in:
Emil Ernerfeldt
2020-12-30 20:56:50 +01:00
parent 6d9cdafbc9
commit 9db1b8dbf9
13 changed files with 488 additions and 36 deletions

View File

@@ -52,3 +52,34 @@ pub fn start_web(canvas_id: &str, app: Box<dyn epi::App>) -> Result<(), wasm_bin
pub fn run_native(app: Box<dyn epi::App>) {
egui_glium::run(app)
}
// ----------------------------------------------------------------------------
pub mod http {
pub use epi::http::*;
/// Do a HTTP request and call the callback when done.
pub fn fetch(
request: Request,
on_done: impl 'static + Send + FnOnce(Result<Response, String>),
) {
fetch_dyn(request, Box::new(on_done))
}
fn fetch_dyn(request: Request, on_done: Box<dyn FnOnce(Result<Response, String>) + Send>) {
#[cfg(target_arch = "wasm32")]
{
egui_web::spawn_future(async move {
let result = egui_web::http::fetch_async(&request).await;
on_done(result)
});
}
#[cfg(not(target_arch = "wasm32"))]
{
std::thread::spawn(move || {
let result = egui_glium::http::fetch_blocking(&request);
on_done(result)
});
}
}
}