From 84414e62a3d3bd88c595abdf2c12b56571869272 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Tue, 29 Dec 2020 15:57:13 +0100 Subject: [PATCH] Add new crate `eframe` which wraps egui, epi, egui_web and egui_glium --- Cargo.lock | 20 ++++++++----- eframe/Cargo.toml | 27 +++++++++++++++++ eframe/README.md | 7 +++++ eframe/src/lib.rs | 54 ++++++++++++++++++++++++++++++++++ egui_demo/Cargo.toml | 15 ++-------- egui_demo/src/lib.rs | 5 ++-- egui_demo/src/main.rs | 2 +- egui_web/src/backend.rs | 2 +- egui_web/src/lib.rs | 3 ++ epi/Cargo.toml | 2 +- epi/src/lib.rs | 2 ++ example_web/Cargo.toml | 3 +- example_web/src/example_app.rs | 1 + example_web/src/lib.rs | 7 +++-- 14 files changed, 118 insertions(+), 32 deletions(-) create mode 100644 eframe/Cargo.toml create mode 100644 eframe/README.md create mode 100644 eframe/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index d9e23665b..34675856c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -620,6 +620,16 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9ea835d29036a4087793836fa931b08837ad5e957da9e23886b29586fb9b6650" +[[package]] +name = "eframe" +version = "0.6.0" +dependencies = [ + "egui", + "egui_glium", + "egui_web", + "epi", +] + [[package]] name = "egui" version = "0.6.0" @@ -635,14 +645,9 @@ dependencies = [ name = "egui_demo" version = "0.1.0" dependencies = [ - "egui", + "eframe", "egui_demo_lib", - "egui_glium", - "egui_web", - "epi", - "js-sys", "serde", - "wasm-bindgen", ] [[package]] @@ -702,9 +707,8 @@ dependencies = [ name = "example_web" version = "0.1.0" dependencies = [ - "egui", + "eframe", "egui_web", - "epi", "image", "js-sys", "serde", diff --git a/eframe/Cargo.toml b/eframe/Cargo.toml new file mode 100644 index 000000000..b95bf89c2 --- /dev/null +++ b/eframe/Cargo.toml @@ -0,0 +1,27 @@ +[package] +name = "eframe" +version = "0.6.0" +authors = ["Emil Ernerfeldt "] +description = "Egui Framework - write GUI apps that compiles to web and/or natively" +edition = "2018" +homepage = "https://github.com/emilk/egui" +license = "MIT OR Apache-2.0" +readme = "README.md" +repository = "https://github.com/emilk/egui" +categories = ["gui", "graphics"] +keywords = ["egui", "gui", "gamedev"] +include = [ "**/*.rs", "Cargo.toml"] + +[lib] + +[dependencies] +egui = { version = "0.6.0", path = "../egui", features = ["serde"] } +epi = { version = "0.6.0", path = "../epi", features = ["serde", "serde_json"] } + +# For compiling natively: +[target.'cfg(not(target_arch = "wasm32"))'.dependencies] +egui_glium = { path = "../egui_glium" } + +# For compiling to web: +[target.'cfg(target_arch = "wasm32")'.dependencies] +egui_web = { path = "../egui_web" } diff --git a/eframe/README.md b/eframe/README.md new file mode 100644 index 000000000..c9cef9c44 --- /dev/null +++ b/eframe/README.md @@ -0,0 +1,7 @@ +# Egui Framework + +This aims to be the entry-level crate if you want to write an Egui App. + +`eframe` calls into your code (it is a framework) and supports web apps (via `egui_web`) and native apps (via `egui_glium`). + +`eframe` is a very thin crate that re-exports `egui`, `epi` and thin wrappers over the backends. diff --git a/eframe/src/lib.rs b/eframe/src/lib.rs new file mode 100644 index 000000000..63752f097 --- /dev/null +++ b/eframe/src/lib.rs @@ -0,0 +1,54 @@ +//! 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) -> 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) { + egui_glium::run(app) +} diff --git a/egui_demo/Cargo.toml b/egui_demo/Cargo.toml index eeadf9af1..eda34bd61 100644 --- a/egui_demo/Cargo.toml +++ b/egui_demo/Cargo.toml @@ -9,17 +9,6 @@ edition = "2018" crate-type = ["cdylib", "rlib"] [dependencies] -egui = { version = "0.6.0", path = "../egui", features = ["serde"] } -egui_demo_lib = { path = "../egui_demo_lib" } -epi = { version = "0.6.0", path = "../epi", features = ["serde", "serde_json"] } +eframe = { version = "0.6.0", path = "../eframe"} +egui_demo_lib = { version = "0.6.0", path = "../egui_demo_lib"} serde = { version = "1", features = ["derive"] } - -# For compiling natively: -[target.'cfg(not(target_arch = "wasm32"))'.dependencies] -egui_glium = { path = "../egui_glium" } - -# For compiling to web: -[target.'cfg(target_arch = "wasm32")'.dependencies] -egui_web = { path = "../egui_web" } -js-sys = "0.3" -wasm-bindgen = "0.2" diff --git a/egui_demo/src/lib.rs b/egui_demo/src/lib.rs index 49da9ef96..8bc38f322 100644 --- a/egui_demo/src/lib.rs +++ b/egui_demo/src/lib.rs @@ -3,7 +3,7 @@ #![warn(clippy::all)] #[cfg(target_arch = "wasm32")] -use wasm_bindgen::prelude::*; +use eframe::wasm_bindgen::{self, prelude::*}; /// This is the entry-point for all the web-assembly. /// This is called once from the HTML. @@ -13,6 +13,5 @@ use wasm_bindgen::prelude::*; #[wasm_bindgen] pub fn start(canvas_id: &str) -> Result<(), wasm_bindgen::JsValue> { let app = egui_demo_lib::DemoApp::default(); - egui_web::start(canvas_id, Box::new(app))?; - Ok(()) + eframe::start_web(canvas_id, Box::new(app)) } diff --git a/egui_demo/src/main.rs b/egui_demo/src/main.rs index dfc2a501d..fd3567136 100644 --- a/egui_demo/src/main.rs +++ b/egui_demo/src/main.rs @@ -5,5 +5,5 @@ // When compiling natively: fn main() { let app = egui_demo_lib::DemoApp::default(); - egui_glium::run(Box::new(app)); + eframe::run_native(Box::new(app)); } diff --git a/egui_web/src/backend.rs b/egui_web/src/backend.rs index b3ae6def1..b7bbfc2e3 100644 --- a/egui_web/src/backend.rs +++ b/egui_web/src/backend.rs @@ -219,7 +219,7 @@ impl AppRunner { } /// Install event listeners to register different input events -/// and starts running the given app. +/// and start running the given app. pub fn start(canvas_id: &str, app: Box) -> Result { let backend = WebBackend::new(canvas_id)?; let runner = AppRunner::new(backend, app)?; diff --git a/egui_web/src/lib.rs b/egui_web/src/lib.rs index 0084f37e6..e641a5e6e 100644 --- a/egui_web/src/lib.rs +++ b/egui_web/src/lib.rs @@ -8,6 +8,9 @@ pub mod webgl; pub use backend::*; +pub use wasm_bindgen; +pub use web_sys; + use egui::mutex::Mutex; use std::sync::Arc; use wasm_bindgen::prelude::*; diff --git a/epi/Cargo.toml b/epi/Cargo.toml index 6db6b1337..3b189b6e6 100644 --- a/epi/Cargo.toml +++ b/epi/Cargo.toml @@ -9,7 +9,7 @@ license = "MIT OR Apache-2.0" readme = "README.md" repository = "https://github.com/emilk/egui" categories = ["gui", "graphics"] -keywords = ["glium", "egui", "gui", "gamedev"] +keywords = ["egui", "gui", "gamedev"] include = [ "**/*.rs", "Cargo.toml"] [lib] diff --git a/epi/src/lib.rs b/epi/src/lib.rs index dcb371949..7ff9854cf 100644 --- a/epi/src/lib.rs +++ b/epi/src/lib.rs @@ -50,6 +50,8 @@ unused_doc_comments, )] +pub use egui; // Re-export for user convenience + // ---------------------------------------------------------------------------- /// Implement this trait to write apps that can be compiled both natively using the [`egui_glium`](https://crates.io/crates/egui_glium) crate, diff --git a/example_web/Cargo.toml b/example_web/Cargo.toml index f44e98510..f468879e6 100644 --- a/example_web/Cargo.toml +++ b/example_web/Cargo.toml @@ -9,9 +9,8 @@ edition = "2018" crate-type = ["cdylib", "rlib"] [dependencies] -egui = { path = "../egui", features = ["serde"] } +eframe = { path = "../eframe" } egui_web = { path = "../egui_web" } -epi = { version = "0.6.0", path = "../epi", features = ["serde", "serde_json"] } image = { version = "0.23", default_features = false, features = ["jpeg", "png"] } js-sys = "0.3" serde = { version = "1", features = ["derive"] } diff --git a/example_web/src/example_app.rs b/example_web/src/example_app.rs index e14ab145b..0c616a205 100644 --- a/example_web/src/example_app.rs +++ b/example_web/src/example_app.rs @@ -1,3 +1,4 @@ +use eframe::{egui, epi}; use egui_web::fetch::Response; use std::sync::mpsc::Receiver; diff --git a/example_web/src/lib.rs b/example_web/src/lib.rs index a7c5d32cc..bd075e4f7 100644 --- a/example_web/src/lib.rs +++ b/example_web/src/lib.rs @@ -4,15 +4,16 @@ mod example_app; -use wasm_bindgen::prelude::*; +#[cfg(target_arch = "wasm32")] +use eframe::wasm_bindgen::{self, 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<(), wasm_bindgen::JsValue> { let app = example_app::ExampleApp::default(); - egui_web::start(canvas_id, Box::new(app))?; - Ok(()) + eframe::start_web(canvas_id, Box::new(app)) }