mirror of
https://github.com/emilk/egui.git
synced 2026-06-27 07:03:14 -04:00
86 lines
2.7 KiB
Rust
86 lines
2.7 KiB
Rust
//! Backend-agnostic interface for writing apps using Egui.
|
|
//!
|
|
//! Egui is a GUI library, which can be plugged in to e.g. a game engine.
|
|
//!
|
|
//! This crate provides a common interface for programming an app, using Egui,
|
|
//! so you can then easily plug it in to a backend such as `egui_web` or `egui_glium`.
|
|
//!
|
|
//! This crate is primarily used by the `egui_web` and `egui_glium` crates.
|
|
|
|
#![forbid(unsafe_code)]
|
|
#![cfg_attr(not(debug_assertions), deny(warnings))] // Forbid warnings in release builds
|
|
#![warn(clippy::all)]
|
|
|
|
pub use {egui, epi};
|
|
|
|
// ----------------------------------------------------------------------------
|
|
// When compiling for web
|
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
pub use egui_web::wasm_bindgen;
|
|
|
|
/// Install event listeners to register different input events
|
|
/// and start running the given app.
|
|
///
|
|
/// Usage:
|
|
/// ``` ignore
|
|
/// #[cfg(target_arch = "wasm32")]
|
|
/// use wasm_bindgen::prelude::*;
|
|
///
|
|
/// /// This is the entry-point for all the web-assembly.
|
|
/// /// This is called once from the HTML.
|
|
/// /// It loads the app, installs some callbacks, then returns.
|
|
/// /// You can add more callbacks like this if you want to call in to your code.
|
|
/// #[cfg(target_arch = "wasm32")]
|
|
/// #[wasm_bindgen]
|
|
/// pub fn start(canvas_id: &str) -> Result<(), eframe::wasm_bindgen::JsValue> {
|
|
/// let app = MyEguiApp::default();
|
|
/// eframe::start_web(canvas_id, Box::new(app))
|
|
/// }
|
|
/// ```
|
|
#[cfg(target_arch = "wasm32")]
|
|
pub fn start_web(canvas_id: &str, app: Box<dyn epi::App>) -> Result<(), wasm_bindgen::JsValue> {
|
|
egui_web::start(canvas_id, app)?;
|
|
Ok(())
|
|
}
|
|
|
|
// ----------------------------------------------------------------------------
|
|
// When compiling natively
|
|
|
|
/// Call from main as `eframe::run_native(Box::new(MyEguiApp::default()))`
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
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)
|
|
});
|
|
}
|
|
}
|
|
}
|