2020-07-23 16:54:16 +00:00
|
|
|
//! Traits and helper for writing Egui apps.
|
|
|
|
//!
|
2020-08-29 13:31:06 +00:00
|
|
|
//! This module is very experimental, and you don't need to use it.
|
|
|
|
//!
|
2020-07-23 16:54:16 +00:00
|
|
|
//! Egui can be used as a library, but you can also use it as a framework to write apps in.
|
|
|
|
//! This module defined the `App` trait that can be implemented and used with the `egui_web` and `egui_glium` crates.
|
|
|
|
|
2020-09-11 06:56:47 +00:00
|
|
|
// TODO: move egui/src/app.rs to own crate, e.g. egui_framework ?
|
|
|
|
|
2020-07-23 16:54:16 +00:00
|
|
|
use crate::Ui;
|
|
|
|
|
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 {
|
|
|
|
/// Called each time the UI needs repainting, which may be many times per second.
|
2020-10-17 10:33:30 +00:00
|
|
|
fn ui(
|
|
|
|
&mut self,
|
|
|
|
ui: &mut Ui,
|
|
|
|
info: &BackendInfo,
|
|
|
|
tex_allocator: Option<&mut dyn TextureAllocator>,
|
|
|
|
) -> AppOutput;
|
2020-07-23 16:54:16 +00:00
|
|
|
|
|
|
|
/// Called once on shutdown. Allows you to save state.
|
|
|
|
fn on_exit(&mut self, _storage: &mut dyn Storage) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct WebInfo {
|
|
|
|
/// e.g. "#fragment" part of "www.example.com/index.html#fragment"
|
|
|
|
pub web_location_hash: String,
|
|
|
|
}
|
|
|
|
|
2020-10-17 10:33:30 +00:00
|
|
|
/// Information about the backend passed to the use app each frame.
|
|
|
|
pub struct BackendInfo {
|
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-09-23 06:57:23 +00:00
|
|
|
|
2020-10-17 10:33:30 +00:00
|
|
|
/// Action that can be taken by the user app.
|
|
|
|
#[derive(Clone, Copy, Debug, Default)]
|
|
|
|
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-09-11 06:56:47 +00:00
|
|
|
|
2020-10-17 10:33:30 +00:00
|
|
|
pub trait TextureAllocator {
|
2020-09-11 06:56:47 +00:00
|
|
|
/// Allocate a user texture (EXPERIMENTAL!)
|
|
|
|
fn new_texture_srgba_premultiplied(
|
|
|
|
&mut self,
|
|
|
|
size: (usize, usize),
|
|
|
|
pixels: &[crate::Srgba],
|
|
|
|
) -> crate::TextureId;
|
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 {
|
|
|
|
fn get_string(&self, key: &str) -> Option<&str>;
|
|
|
|
fn set_string(&mut self, key: &str, value: String);
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
.and_then(|value| serde_json::from_str(value).ok())
|
|
|
|
}
|
|
|
|
|
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";
|