2022-04-30 08:44:35 +00:00
|
|
|
//! eframe - the [`egui`] framework crate
|
2020-12-29 14:57:13 +00:00
|
|
|
//!
|
2021-01-17 09:52:01 +00:00
|
|
|
//! If you are planning to write an app for web or native,
|
2022-04-30 08:44:35 +00:00
|
|
|
//! and want to use [`egui`] for everything, then `eframe` is for you!
|
2020-12-29 14:57:13 +00:00
|
|
|
//!
|
2022-04-13 14:13:24 +00:00
|
|
|
//! To get started, see the [examples](https://github.com/emilk/egui/tree/master/examples).
|
2022-04-13 09:06:13 +00:00
|
|
|
//! To learn how to set up `eframe` for web and native, go to <https://github.com/emilk/eframe_template/> and follow the instructions there!
|
2021-12-29 20:44:48 +00:00
|
|
|
//!
|
2022-04-30 08:44:35 +00:00
|
|
|
//! In short, you implement [`App`] (especially [`App::update`]) and then
|
2022-04-13 14:13:24 +00:00
|
|
|
//! call [`crate::run_native`] from your `main.rs`, and/or call `eframe::start_web` from your `lib.rs`.
|
2021-01-17 09:52:01 +00:00
|
|
|
//!
|
2021-10-23 03:58:10 +00:00
|
|
|
//! ## Usage, native:
|
|
|
|
//! ``` no_run
|
2022-03-16 14:39:48 +00:00
|
|
|
//! use eframe::egui;
|
|
|
|
//!
|
|
|
|
//! fn main() {
|
|
|
|
//! let native_options = eframe::NativeOptions::default();
|
2022-03-18 13:23:07 +00:00
|
|
|
//! eframe::run_native("My egui App", native_options, Box::new(|cc| Box::new(MyEguiApp::new(cc))));
|
2022-03-16 14:39:48 +00:00
|
|
|
//! }
|
2021-10-23 03:58:10 +00:00
|
|
|
//!
|
|
|
|
//! #[derive(Default)]
|
|
|
|
//! struct MyEguiApp {}
|
|
|
|
//!
|
2022-03-16 14:39:48 +00:00
|
|
|
//! impl MyEguiApp {
|
|
|
|
//! fn new(cc: &eframe::CreationContext<'_>) -> Self {
|
|
|
|
//! // Customize egui here with cc.egui_ctx.set_fonts and cc.egui_ctx.set_visuals.
|
|
|
|
//! // Restore app state using cc.storage (requires the "persistence" feature).
|
|
|
|
//! // Use the cc.gl (a glow::Context) to create graphics shaders and buffers that you can use
|
|
|
|
//! // for e.g. egui::PaintCallback.
|
|
|
|
//! Self::default()
|
|
|
|
//! }
|
|
|
|
//! }
|
2021-10-23 03:58:10 +00:00
|
|
|
//!
|
2022-03-16 14:39:48 +00:00
|
|
|
//! impl eframe::App for MyEguiApp {
|
2022-03-25 20:19:31 +00:00
|
|
|
//! fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
|
2021-10-23 03:58:10 +00:00
|
|
|
//! egui::CentralPanel::default().show(ctx, |ui| {
|
|
|
|
//! ui.heading("Hello World!");
|
|
|
|
//! });
|
|
|
|
//! }
|
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! ## Usage, web:
|
|
|
|
//! ``` no_run
|
|
|
|
//! #[cfg(target_arch = "wasm32")]
|
|
|
|
//! use wasm_bindgen::prelude::*;
|
|
|
|
//!
|
|
|
|
//! /// Call this once from the HTML.
|
|
|
|
//! #[cfg(target_arch = "wasm32")]
|
|
|
|
//! #[wasm_bindgen]
|
2022-10-05 18:14:18 +00:00
|
|
|
//! pub async fn start(canvas_id: &str) -> Result<AppRunnerRef, eframe::wasm_bindgen::JsValue> {
|
2022-08-20 13:06:43 +00:00
|
|
|
//! let web_options = eframe::WebOptions::default();
|
2022-10-05 18:14:18 +00:00
|
|
|
//! eframe::start_web(canvas_id, web_options, Box::new(|cc| Box::new(MyEguiApp::new(cc)))).await
|
2021-10-23 03:58:10 +00:00
|
|
|
//! }
|
|
|
|
//! ```
|
2022-06-09 13:27:22 +00:00
|
|
|
//!
|
|
|
|
//! ## Feature flags
|
2022-06-09 15:41:37 +00:00
|
|
|
#![cfg_attr(feature = "document-features", doc = document_features::document_features!())]
|
2022-06-09 13:27:22 +00:00
|
|
|
//!
|
2020-12-29 14:57:13 +00:00
|
|
|
|
2021-10-23 03:58:10 +00:00
|
|
|
#![allow(clippy::needless_doctest_main)]
|
2020-12-29 14:57:13 +00:00
|
|
|
|
2022-03-16 14:39:48 +00:00
|
|
|
// Re-export all useful libraries:
|
2022-05-12 07:02:28 +00:00
|
|
|
pub use {egui, egui::emath, egui::epaint};
|
|
|
|
|
|
|
|
#[cfg(feature = "glow")]
|
|
|
|
pub use {egui_glow, glow};
|
2022-04-30 08:44:35 +00:00
|
|
|
|
2022-05-28 15:52:36 +00:00
|
|
|
#[cfg(feature = "wgpu")]
|
|
|
|
pub use {egui_wgpu, wgpu};
|
|
|
|
|
2022-04-30 08:44:35 +00:00
|
|
|
mod epi;
|
2020-12-29 14:57:13 +00:00
|
|
|
|
2022-03-16 14:39:48 +00:00
|
|
|
// Re-export everything in `epi` so `eframe` users don't have to care about what `epi` is:
|
|
|
|
pub use epi::*;
|
2021-05-08 08:14:56 +00:00
|
|
|
|
2020-12-29 14:57:13 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// When compiling for web
|
|
|
|
|
2022-08-02 15:42:55 +00:00
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
pub mod web;
|
2022-04-30 08:44:35 +00:00
|
|
|
|
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
pub use wasm_bindgen;
|
|
|
|
|
2022-08-02 15:42:55 +00:00
|
|
|
#[cfg(target_arch = "wasm32")]
|
2022-08-02 15:37:12 +00:00
|
|
|
use web::AppRunnerRef;
|
|
|
|
|
2022-04-30 08:44:35 +00:00
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
pub use web_sys;
|
2020-12-29 14:57:13 +00:00
|
|
|
|
|
|
|
/// Install event listeners to register different input events
|
|
|
|
/// and start running the given app.
|
|
|
|
///
|
2021-01-17 09:52:01 +00:00
|
|
|
/// ``` no_run
|
2020-12-29 14:57:13 +00:00
|
|
|
/// #[cfg(target_arch = "wasm32")]
|
|
|
|
/// use wasm_bindgen::prelude::*;
|
|
|
|
///
|
|
|
|
/// /// This is the entry-point for all the web-assembly.
|
2022-08-02 15:42:55 +00:00
|
|
|
/// /// This is called from the HTML.
|
2020-12-29 14:57:13 +00:00
|
|
|
/// /// It loads the app, installs some callbacks, then returns.
|
2022-08-02 15:42:55 +00:00
|
|
|
/// /// It returns a handle to the running app that can be stopped calling `AppRunner::stop_web`.
|
2020-12-29 14:57:13 +00:00
|
|
|
/// /// You can add more callbacks like this if you want to call in to your code.
|
|
|
|
/// #[cfg(target_arch = "wasm32")]
|
|
|
|
/// #[wasm_bindgen]
|
2022-10-05 18:14:18 +00:00
|
|
|
/// pub async fn start(canvas_id: &str) -> Result<AppRunnerRef>, eframe::wasm_bindgen::JsValue> {
|
2022-06-09 15:41:59 +00:00
|
|
|
/// let web_options = eframe::WebOptions::default();
|
2022-10-05 18:14:18 +00:00
|
|
|
/// eframe::start_web(canvas_id, web_options, Box::new(|cc| Box::new(MyEguiApp::new(cc)))).await
|
2020-12-29 14:57:13 +00:00
|
|
|
/// }
|
|
|
|
/// ```
|
2022-10-30 19:55:07 +00:00
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
/// Failing to initialize WebGL graphics.
|
2020-12-29 14:57:13 +00:00
|
|
|
#[cfg(target_arch = "wasm32")]
|
2022-10-05 18:14:18 +00:00
|
|
|
pub async fn start_web(
|
2022-06-09 15:41:59 +00:00
|
|
|
canvas_id: &str,
|
|
|
|
web_options: WebOptions,
|
|
|
|
app_creator: AppCreator,
|
2022-08-02 15:37:12 +00:00
|
|
|
) -> Result<AppRunnerRef, wasm_bindgen::JsValue> {
|
2022-10-05 18:14:18 +00:00
|
|
|
let handle = web::start(canvas_id, web_options, app_creator).await?;
|
2022-08-02 15:42:55 +00:00
|
|
|
|
|
|
|
Ok(handle)
|
2020-12-29 14:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// When compiling natively
|
|
|
|
|
2022-04-29 06:17:49 +00:00
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
|
|
mod native;
|
|
|
|
|
2022-03-16 14:39:48 +00:00
|
|
|
/// This is how you start a native (desktop) app.
|
|
|
|
///
|
|
|
|
/// The first argument is name of your app, used for the title bar of the native window
|
2022-04-13 14:13:24 +00:00
|
|
|
/// and the save location of persistence (see [`App::save`]).
|
2022-03-16 14:39:48 +00:00
|
|
|
///
|
2021-12-31 14:17:55 +00:00
|
|
|
/// Call from `fn main` like this:
|
2021-10-23 03:58:10 +00:00
|
|
|
/// ``` no_run
|
2022-03-16 14:39:48 +00:00
|
|
|
/// use eframe::egui;
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// let native_options = eframe::NativeOptions::default();
|
2022-03-18 13:23:07 +00:00
|
|
|
/// eframe::run_native("MyApp", native_options, Box::new(|cc| Box::new(MyEguiApp::new(cc))));
|
2022-03-16 14:39:48 +00:00
|
|
|
/// }
|
2021-10-23 03:58:10 +00:00
|
|
|
///
|
|
|
|
/// #[derive(Default)]
|
|
|
|
/// struct MyEguiApp {}
|
|
|
|
///
|
2022-03-16 14:39:48 +00:00
|
|
|
/// impl MyEguiApp {
|
|
|
|
/// fn new(cc: &eframe::CreationContext<'_>) -> Self {
|
|
|
|
/// // Customize egui here with cc.egui_ctx.set_fonts and cc.egui_ctx.set_visuals.
|
|
|
|
/// // Restore app state using cc.storage (requires the "persistence" feature).
|
|
|
|
/// // Use the cc.gl (a glow::Context) to create graphics shaders and buffers that you can use
|
|
|
|
/// // for e.g. egui::PaintCallback.
|
|
|
|
/// Self::default()
|
|
|
|
/// }
|
|
|
|
/// }
|
2021-10-23 03:58:10 +00:00
|
|
|
///
|
2022-03-16 14:39:48 +00:00
|
|
|
/// impl eframe::App for MyEguiApp {
|
2022-03-25 20:19:31 +00:00
|
|
|
/// fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
|
2021-10-23 03:58:10 +00:00
|
|
|
/// egui::CentralPanel::default().show(ctx, |ui| {
|
|
|
|
/// ui.heading("Hello World!");
|
|
|
|
/// });
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
2020-12-29 14:57:13 +00:00
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
2022-03-21 15:54:29 +00:00
|
|
|
#[allow(clippy::needless_pass_by_value)]
|
Continue execution after closing native eframe window (#1889)
This adds `NativeOptions::run_and_return` with default value `true`.
If `true`, execution will continue after the eframe window is closed. This is a new behavior introduced in this PR.
If `false`, the app will close once the eframe window is closed. The is the old behavior.
This is `true` by default, and the `false` option is only there so we can revert if we find any bugs.
When `true`, [`winit::platform::run_return::EventLoopExtRunReturn::run_return`](https://docs.rs/winit/latest/winit/platform/run_return/trait.EventLoopExtRunReturn.html#tymethod.run_return) is used. The winit docs warns of its usage, recommending `EventLoop::run`, but 🤷
When `false`, [`winit::event_loop::EventLoop::run`](https://docs.rs/winit/latest/winit/event_loop/struct.EventLoop.html#method.run) is used.
This is a really useful feature. You can now use `eframe` to quickly open up a window and show some data or options, and then continue your program after the `eframe` window is closed
My previous attempt at this caused some problems, but my new attempt seems to be working much better, at least on my Mac.
2022-08-05 06:20:31 +00:00
|
|
|
pub fn run_native(app_name: &str, native_options: NativeOptions, app_creator: AppCreator) {
|
2022-05-12 07:02:28 +00:00
|
|
|
let renderer = native_options.renderer;
|
|
|
|
|
|
|
|
match renderer {
|
|
|
|
#[cfg(feature = "glow")]
|
|
|
|
Renderer::Glow => {
|
|
|
|
tracing::debug!("Using the glow renderer");
|
2022-08-23 12:43:22 +00:00
|
|
|
native::run::run_glow(app_name, native_options, app_creator);
|
2022-05-12 07:02:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "wgpu")]
|
|
|
|
Renderer::Wgpu => {
|
|
|
|
tracing::debug!("Using the wgpu renderer");
|
2022-08-23 12:43:22 +00:00
|
|
|
native::run::run_wgpu(app_name, native_options, app_creator);
|
2022-05-12 07:02:28 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-29 06:17:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/// Profiling macro for feature "puffin"
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
|
|
macro_rules! profile_function {
|
|
|
|
($($arg: tt)*) => {
|
|
|
|
#[cfg(feature = "puffin")]
|
|
|
|
puffin::profile_function!($($arg)*);
|
|
|
|
};
|
2021-10-19 13:32:23 +00:00
|
|
|
}
|
2022-04-29 06:17:49 +00:00
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
|
|
pub(crate) use profile_function;
|
|
|
|
|
|
|
|
/// Profiling macro for feature "puffin"
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
|
|
macro_rules! profile_scope {
|
|
|
|
($($arg: tt)*) => {
|
|
|
|
#[cfg(feature = "puffin")]
|
|
|
|
puffin::profile_scope!($($arg)*);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
|
|
pub(crate) use profile_scope;
|