2020-12-29 13:15:46 +00:00
|
|
|
//! Backend-agnostic interface for writing apps using Egui.
|
2020-07-23 16:54:16 +00:00
|
|
|
//!
|
2020-12-29 13:15:46 +00:00
|
|
|
//! Egui is a GUI library, which can be plugged in to e.g. a game engine.
|
2020-08-29 13:31:06 +00:00
|
|
|
//!
|
2020-12-29 13:15:46 +00:00
|
|
|
//! 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.
|
|
|
|
|
|
|
|
#![cfg_attr(not(debug_assertions), deny(warnings))] // Forbid warnings in release builds
|
|
|
|
#![forbid(unsafe_code)]
|
|
|
|
#![warn(
|
|
|
|
clippy::all,
|
|
|
|
clippy::await_holding_lock,
|
|
|
|
clippy::dbg_macro,
|
|
|
|
clippy::doc_markdown,
|
|
|
|
clippy::empty_enum,
|
|
|
|
clippy::enum_glob_use,
|
|
|
|
clippy::exit,
|
|
|
|
clippy::filter_map_next,
|
|
|
|
clippy::fn_params_excessive_bools,
|
|
|
|
clippy::if_let_mutex,
|
|
|
|
clippy::imprecise_flops,
|
|
|
|
clippy::inefficient_to_string,
|
|
|
|
clippy::linkedlist,
|
|
|
|
clippy::lossy_float_literal,
|
|
|
|
clippy::macro_use_imports,
|
|
|
|
clippy::match_on_vec_items,
|
|
|
|
clippy::match_wildcard_for_single_variants,
|
|
|
|
clippy::mem_forget,
|
|
|
|
clippy::mismatched_target_os,
|
|
|
|
clippy::missing_errors_doc,
|
|
|
|
clippy::missing_safety_doc,
|
|
|
|
clippy::needless_borrow,
|
|
|
|
clippy::needless_continue,
|
|
|
|
clippy::needless_pass_by_value,
|
|
|
|
clippy::option_option,
|
|
|
|
clippy::pub_enum_variant_names,
|
|
|
|
clippy::rest_pat_in_fully_bound_structs,
|
|
|
|
clippy::todo,
|
|
|
|
clippy::unimplemented,
|
|
|
|
clippy::unnested_or_patterns,
|
|
|
|
clippy::verbose_file_reads,
|
|
|
|
future_incompatible,
|
|
|
|
missing_crate_level_docs,
|
|
|
|
missing_doc_code_examples,
|
|
|
|
// missing_docs,
|
|
|
|
nonstandard_style,
|
|
|
|
rust_2018_idioms,
|
|
|
|
unused_doc_comments,
|
|
|
|
)]
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2020-09-11 06:56:47 +00:00
|
|
|
|
2020-08-21 16:53:43 +00:00
|
|
|
/// Implement this trait to write apps that can be compiled both natively using the [`egui_glium`](https://crates.io/crates/egui_glium) crate,
|
|
|
|
/// and deployed as a web site using the [`egui_web`](https://crates.io/crates/egui_web) crate.
|
2020-07-23 16:54:16 +00:00
|
|
|
pub trait App {
|
2020-12-18 21:30:59 +00:00
|
|
|
/// The name of your App.
|
|
|
|
fn name(&self) -> &str;
|
2020-12-18 21:34:48 +00:00
|
|
|
|
2020-12-29 00:13:14 +00:00
|
|
|
/// Background color for the app, e.g. what is sent to `gl.clearColor`.
|
|
|
|
/// This is the background of your windows if you don't set a central panel.
|
2020-12-29 13:15:46 +00:00
|
|
|
fn clear_color(&self) -> egui::Rgba {
|
2020-12-29 00:13:14 +00:00
|
|
|
// NOTE: a bright gray makes the shadows of the windows look weird.
|
2020-12-29 13:15:46 +00:00
|
|
|
egui::Srgba::from_rgb(12, 12, 12).into()
|
2020-12-18 21:30:59 +00:00
|
|
|
}
|
|
|
|
|
2020-12-19 19:50:00 +00:00
|
|
|
/// Called once on start. Allows you to restore state.
|
|
|
|
fn load(&mut self, _storage: &dyn Storage) {}
|
|
|
|
|
|
|
|
/// Called on shutdown, and perhaps at regular intervals. Allows you to save state.
|
|
|
|
fn save(&mut self, _storage: &mut dyn Storage) {}
|
|
|
|
|
|
|
|
/// Time between automatic calls to `save()`
|
|
|
|
fn auto_save_interval(&self) -> std::time::Duration {
|
|
|
|
std::time::Duration::from_secs(30)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Called once on shutdown (before or after `save()`)
|
|
|
|
fn on_exit(&mut self) {}
|
|
|
|
|
2020-10-31 17:03:13 +00:00
|
|
|
/// Called once before the first frame.
|
|
|
|
/// Allows you to do setup code and to call `ctx.set_fonts()`.
|
|
|
|
/// Optional.
|
2020-12-29 13:15:46 +00:00
|
|
|
fn setup(&mut self, _ctx: &egui::CtxRef) {}
|
2020-10-31 17:03:13 +00:00
|
|
|
|
2020-12-22 14:20:38 +00:00
|
|
|
/// Returns true if this app window should be resizable.
|
|
|
|
fn is_resizable(&self) -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2020-07-23 16:54:16 +00:00
|
|
|
/// Called each time the UI needs repainting, which may be many times per second.
|
2020-10-24 16:37:20 +00:00
|
|
|
/// Put your widgets into a `SidePanel`, `TopPanel`, `CentralPanel`, `Window` or `Area`.
|
2020-12-29 13:15:46 +00:00
|
|
|
fn ui(&mut self, ctx: &egui::CtxRef, integration_context: &mut IntegrationContext<'_>);
|
2020-07-23 16:54:16 +00:00
|
|
|
}
|
|
|
|
|
2020-10-24 17:23:16 +00:00
|
|
|
pub struct IntegrationContext<'a> {
|
|
|
|
/// Information about the integration.
|
|
|
|
pub info: IntegrationInfo,
|
|
|
|
/// A way to allocate textures (on integrations that support it).
|
|
|
|
pub tex_allocator: Option<&'a mut dyn TextureAllocator>,
|
|
|
|
/// Where the app can issue commands back to the integration.
|
|
|
|
pub output: AppOutput,
|
2020-12-27 09:49:26 +00:00
|
|
|
/// If you need to request a repaint from another thread, clone this and send it to that other thread.
|
2020-11-20 19:35:16 +00:00
|
|
|
pub repaint_signal: std::sync::Arc<dyn RepaintSignal>,
|
2020-10-24 17:23:16 +00:00
|
|
|
}
|
|
|
|
|
2020-10-17 21:54:46 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2020-07-23 16:54:16 +00:00
|
|
|
pub struct WebInfo {
|
|
|
|
/// e.g. "#fragment" part of "www.example.com/index.html#fragment"
|
|
|
|
pub web_location_hash: String,
|
|
|
|
}
|
|
|
|
|
2020-10-24 17:23:16 +00:00
|
|
|
/// Information about the integration passed to the use app each frame.
|
2020-10-17 21:54:46 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2020-10-24 17:23:16 +00:00
|
|
|
pub struct IntegrationInfo {
|
2020-07-23 16:54:16 +00:00
|
|
|
/// If the app is running in a Web context, this returns information about the environment.
|
2020-10-17 10:33:30 +00:00
|
|
|
pub web_info: Option<WebInfo>,
|
2020-07-23 16:54:16 +00:00
|
|
|
|
2020-10-01 20:25:44 +00:00
|
|
|
/// Seconds of cpu usage (in seconds) of UI code on the previous frame.
|
2020-10-17 10:33:30 +00:00
|
|
|
/// `None` if this is the first frame.
|
|
|
|
pub cpu_usage: Option<f32>,
|
2020-07-23 16:54:16 +00:00
|
|
|
|
2020-09-23 06:57:23 +00:00
|
|
|
/// Local time. Used for the clock in the demo app.
|
2020-10-17 10:33:30 +00:00
|
|
|
/// Set to `None` if you don't know.
|
|
|
|
pub seconds_since_midnight: Option<f64>,
|
2020-10-17 21:54:46 +00:00
|
|
|
|
|
|
|
/// The OS native pixels-per-point
|
|
|
|
pub native_pixels_per_point: Option<f32>,
|
2020-10-17 10:33:30 +00:00
|
|
|
}
|
2020-09-23 06:57:23 +00:00
|
|
|
|
2020-10-17 10:33:30 +00:00
|
|
|
/// Action that can be taken by the user app.
|
2020-10-17 21:54:46 +00:00
|
|
|
#[derive(Clone, Copy, Debug, Default, PartialEq)]
|
2020-10-17 10:33:30 +00:00
|
|
|
pub struct AppOutput {
|
|
|
|
/// Set to `true` to stop the app.
|
2020-08-29 13:31:06 +00:00
|
|
|
/// This does nothing for web apps.
|
2020-10-17 10:33:30 +00:00
|
|
|
pub quit: bool,
|
2020-10-17 21:54:46 +00:00
|
|
|
|
2020-10-24 16:45:31 +00:00
|
|
|
/// Set to some size to resize the outer window (e.g. glium window) to this size.
|
2020-12-29 13:15:46 +00:00
|
|
|
pub window_size: Option<egui::Vec2>,
|
2020-10-24 16:45:31 +00:00
|
|
|
|
2020-10-17 21:54:46 +00:00
|
|
|
/// If the app sets this, change the `pixels_per_point` of Egui to this next frame.
|
|
|
|
pub pixels_per_point: Option<f32>,
|
2020-10-17 10:33:30 +00:00
|
|
|
}
|
2020-09-11 06:56:47 +00:00
|
|
|
|
2020-10-17 10:33:30 +00:00
|
|
|
pub trait TextureAllocator {
|
2020-11-18 20:38:29 +00:00
|
|
|
/// A.locate a new user texture.
|
2020-12-29 13:15:46 +00:00
|
|
|
fn alloc(&mut self) -> egui::TextureId;
|
2020-11-18 20:38:29 +00:00
|
|
|
|
|
|
|
/// Set or change the pixels of a user texture.
|
|
|
|
fn set_srgba_premultiplied(
|
2020-09-11 06:56:47 +00:00
|
|
|
&mut self,
|
2020-12-29 13:15:46 +00:00
|
|
|
id: egui::TextureId,
|
2020-09-11 06:56:47 +00:00
|
|
|
size: (usize, usize),
|
2020-12-29 13:15:46 +00:00
|
|
|
srgba_pixels: &[egui::Srgba],
|
2020-11-18 20:38:29 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
/// Free the given texture.
|
2020-12-29 13:15:46 +00:00
|
|
|
fn free(&mut self, id: egui::TextureId);
|
2020-07-23 16:54:16 +00:00
|
|
|
}
|
|
|
|
|
2020-12-27 09:49:26 +00:00
|
|
|
pub trait RepaintSignal: Send + Sync {
|
2020-11-20 19:35:16 +00:00
|
|
|
/// This signals the Egui integration that a repaint is required.
|
|
|
|
/// This is meant to be called when a background process finishes in an async context and/or background thread.
|
|
|
|
fn request_repaint(&self);
|
|
|
|
}
|
|
|
|
|
2020-12-29 13:15:46 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2020-07-23 16:54:16 +00:00
|
|
|
/// A place where you can store custom data in a way that persists when you restart the app.
|
|
|
|
///
|
|
|
|
/// On the web this is backed by [local storage](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage).
|
|
|
|
/// On desktop this is backed by the file system.
|
|
|
|
pub trait Storage {
|
2020-12-19 13:58:00 +00:00
|
|
|
fn get_string(&self, key: &str) -> Option<String>;
|
2020-07-23 16:54:16 +00:00
|
|
|
fn set_string(&mut self, key: &str, value: String);
|
2020-10-19 18:25:05 +00:00
|
|
|
|
|
|
|
/// write-to-disk or similar
|
|
|
|
fn flush(&mut self);
|
2020-07-23 16:54:16 +00:00
|
|
|
}
|
|
|
|
|
2020-10-19 18:12:14 +00:00
|
|
|
/// Stores nothing.
|
|
|
|
#[derive(Clone, Default)]
|
|
|
|
pub struct DummyStorage {}
|
|
|
|
|
|
|
|
impl Storage for DummyStorage {
|
2020-12-19 13:58:00 +00:00
|
|
|
fn get_string(&self, _key: &str) -> Option<String> {
|
2020-10-19 18:12:14 +00:00
|
|
|
None
|
|
|
|
}
|
|
|
|
fn set_string(&mut self, _key: &str, _value: String) {}
|
2020-10-19 18:25:05 +00:00
|
|
|
fn flush(&mut self) {}
|
2020-10-19 18:12:14 +00:00
|
|
|
}
|
|
|
|
|
2020-08-09 15:34:26 +00:00
|
|
|
#[cfg(feature = "serde_json")]
|
2020-07-23 16:54:16 +00:00
|
|
|
pub fn get_value<T: serde::de::DeserializeOwned>(storage: &dyn Storage, key: &str) -> Option<T> {
|
|
|
|
storage
|
|
|
|
.get_string(key)
|
2020-12-19 13:58:00 +00:00
|
|
|
.and_then(|value| serde_json::from_str(&value).ok())
|
2020-07-23 16:54:16 +00:00
|
|
|
}
|
|
|
|
|
2020-08-09 15:34:26 +00:00
|
|
|
#[cfg(feature = "serde_json")]
|
2020-07-23 16:54:16 +00:00
|
|
|
pub fn set_value<T: serde::Serialize>(storage: &mut dyn Storage, key: &str, value: &T) {
|
|
|
|
storage.set_string(key, serde_json::to_string(value).unwrap());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// storage key used for app
|
|
|
|
pub const APP_KEY: &str = "app";
|