2020-12-31 13:31:11 +00:00
//! Backend-agnostic interface for writing apps using [`egui`].
2020-07-23 16:54:16 +00:00
//!
2021-09-03 19:04:43 +00:00
//! `epi` provides interfaces for window management and serialization.
2021-01-02 10:59:20 +00:00
//! An app written for `epi` can then be plugged into [`eframe`](https://docs.rs/eframe),
2021-01-17 13:48:59 +00:00
//! the egui framework crate.
2020-12-31 13:31:11 +00:00
//!
2021-01-02 10:59:20 +00:00
//! Start by looking at the [`App`] trait, and implement [`App::update`].
2020-12-29 13:15:46 +00:00
2021-06-23 07:16:39 +00:00
// Forbid warnings in release builds:
#![ cfg_attr(not(debug_assertions), deny(warnings)) ]
2020-12-29 13:15:46 +00:00
#![ forbid(unsafe_code) ]
#![ warn(
clippy ::all ,
clippy ::await_holding_lock ,
2021-05-09 12:13:09 +00:00
clippy ::char_lit_as_u8 ,
clippy ::checked_conversions ,
2020-12-29 13:15:46 +00:00
clippy ::dbg_macro ,
2021-03-23 18:53:31 +00:00
clippy ::debug_assert_with_mut_call ,
2021-10-20 14:30:39 +00:00
clippy ::disallowed_method ,
2020-12-29 13:15:46 +00:00
clippy ::doc_markdown ,
clippy ::empty_enum ,
clippy ::enum_glob_use ,
clippy ::exit ,
2021-05-09 12:13:09 +00:00
clippy ::expl_impl_clone_on_copy ,
clippy ::explicit_deref_methods ,
2021-03-23 18:53:31 +00:00
clippy ::explicit_into_iter_loop ,
2021-05-09 12:13:09 +00:00
clippy ::fallible_impl_from ,
2020-12-29 13:15:46 +00:00
clippy ::filter_map_next ,
2021-10-20 14:30:39 +00:00
clippy ::flat_map_option ,
2021-05-09 12:13:09 +00:00
clippy ::float_cmp_const ,
2020-12-29 13:15:46 +00:00
clippy ::fn_params_excessive_bools ,
2021-10-20 14:30:39 +00:00
clippy ::from_iter_instead_of_collect ,
2020-12-29 13:15:46 +00:00
clippy ::if_let_mutex ,
2021-10-20 14:30:39 +00:00
clippy ::implicit_clone ,
2020-12-29 13:15:46 +00:00
clippy ::imprecise_flops ,
clippy ::inefficient_to_string ,
2021-05-09 12:13:09 +00:00
clippy ::invalid_upcast_comparisons ,
2021-10-20 14:30:39 +00:00
clippy ::large_digit_groups ,
clippy ::large_stack_arrays ,
2021-03-23 18:53:31 +00:00
clippy ::large_types_passed_by_value ,
clippy ::let_unit_value ,
2020-12-29 13:15:46 +00:00
clippy ::linkedlist ,
clippy ::lossy_float_literal ,
clippy ::macro_use_imports ,
2021-05-09 12:13:09 +00:00
clippy ::manual_ok_or ,
2021-03-23 18:53:31 +00:00
clippy ::map_err_ignore ,
clippy ::map_flatten ,
2021-10-20 14:30:39 +00:00
clippy ::map_unwrap_or ,
2020-12-29 13:15:46 +00:00
clippy ::match_on_vec_items ,
2021-03-23 18:53:31 +00:00
clippy ::match_same_arms ,
2021-10-20 14:30:39 +00:00
clippy ::match_wild_err_arm ,
2020-12-29 13:15:46 +00:00
clippy ::match_wildcard_for_single_variants ,
clippy ::mem_forget ,
clippy ::mismatched_target_os ,
clippy ::missing_errors_doc ,
clippy ::missing_safety_doc ,
2021-05-09 12:13:09 +00:00
clippy ::mut_mut ,
clippy ::mutex_integer ,
2020-12-29 13:15:46 +00:00
clippy ::needless_borrow ,
clippy ::needless_continue ,
2021-10-20 14:30:39 +00:00
clippy ::needless_for_each ,
2020-12-29 13:15:46 +00:00
clippy ::needless_pass_by_value ,
clippy ::option_option ,
2021-05-09 12:13:09 +00:00
clippy ::path_buf_push_overwrite ,
clippy ::ptr_as_ptr ,
2021-03-23 18:53:31 +00:00
clippy ::ref_option_ref ,
2020-12-29 13:15:46 +00:00
clippy ::rest_pat_in_fully_bound_structs ,
2021-05-09 12:13:09 +00:00
clippy ::same_functions_in_if_condition ,
2021-10-20 14:30:39 +00:00
clippy ::semicolon_if_nothing_returned ,
clippy ::single_match_else ,
2021-03-23 18:53:31 +00:00
clippy ::string_add_assign ,
clippy ::string_add ,
2021-05-09 12:13:09 +00:00
clippy ::string_lit_as_bytes ,
2021-03-23 18:53:31 +00:00
clippy ::string_to_string ,
2020-12-29 13:15:46 +00:00
clippy ::todo ,
2021-05-09 12:13:09 +00:00
clippy ::trait_duplication_in_bounds ,
2020-12-29 13:15:46 +00:00
clippy ::unimplemented ,
clippy ::unnested_or_patterns ,
2021-03-23 18:53:31 +00:00
clippy ::unused_self ,
2021-05-09 12:13:09 +00:00
clippy ::useless_transmute ,
2020-12-29 13:15:46 +00:00
clippy ::verbose_file_reads ,
2021-05-09 12:13:09 +00:00
clippy ::zero_sized_map_values ,
2020-12-29 13:15:46 +00:00
future_incompatible ,
2021-03-23 18:53:31 +00:00
nonstandard_style ,
2021-12-26 08:55:13 +00:00
rust_2018_idioms ,
rustdoc ::missing_crate_level_docs
2020-12-29 13:15:46 +00:00
) ]
2021-05-09 12:00:53 +00:00
#![ allow(clippy::float_cmp) ]
2021-03-23 18:53:31 +00:00
#![ allow(clippy::manual_range_contains) ]
2021-09-08 22:16:06 +00:00
#![ warn(missing_docs) ] // Let's keep `epi` well-documented.
2020-12-29 13:15:46 +00:00
2021-10-19 19:40:55 +00:00
/// File storage which can be used by native backends.
#[ cfg(feature = " file_storage " ) ]
pub mod file_storage ;
2020-12-29 14:57:13 +00:00
pub use egui ; // Re-export for user convenience
2021-12-26 20:21:28 +00:00
use std ::sync ::{ Arc , Mutex } ;
2020-12-29 13:15:46 +00:00
// ----------------------------------------------------------------------------
2020-09-11 06:56:47 +00:00
2021-10-19 13:32:23 +00:00
/// Implement this trait to write apps that can be compiled both natively using the [`egui_glium`](https://github.com/emilk/egui/tree/master/egui_glium) crate,
/// and deployed as a web site using the [`egui_web`](https://github.com/emilk/egui/tree/master/egui_web) crate.
2020-07-23 16:54:16 +00:00
pub trait App {
2020-12-31 13:31:11 +00:00
/// Called each time the UI needs repainting, which may be many times per second.
2021-10-23 03:58:10 +00:00
///
2021-05-26 20:06:10 +00:00
/// Put your widgets into a [`egui::SidePanel`], [`egui::TopBottomPanel`], [`egui::CentralPanel`], [`egui::Window`] or [`egui::Area`].
2021-10-23 03:58:10 +00:00
///
2021-12-26 20:21:28 +00:00
/// The given [`egui::CtxRef`] is only valid for the duration of this call.
/// The [`Frame`] however can be cloned and saved.
///
/// To force a repaint, call either [`egui::Context::request_repaint`] or [`Frame::request_repaint`].
fn update ( & mut self , ctx : & egui ::CtxRef , frame : & Frame ) ;
2020-12-18 21:34:48 +00:00
2020-12-31 13:31:11 +00:00
/// Called once before the first frame.
2021-06-07 18:53:33 +00:00
///
2021-10-10 13:35:13 +00:00
/// Allows you to do setup code, e.g to call [`egui::Context::set_fonts`],
/// [`egui::Context::set_visuals`] etc.
2021-06-07 18:53:33 +00:00
///
2021-09-29 06:45:13 +00:00
/// Also allows you to restore state, if there is a storage (required the "persistence" feature).
2021-12-26 20:21:28 +00:00
fn setup ( & mut self , _ctx : & egui ::CtxRef , _frame : & Frame , _storage : Option < & dyn Storage > ) { }
2020-12-18 21:30:59 +00:00
2021-01-02 13:42:43 +00:00
/// If `true` a warm-up call to [`Self::update`] will be issued where
/// `ctx.memory().everything_is_visible()` will be set to `true`.
///
2021-10-10 13:35:13 +00:00
/// This will help pre-caching all text, preventing stutter when
/// opening a window containing new glyphs.
///
2021-01-10 10:43:01 +00:00
/// In this warm-up call, all painted shapes will be ignored.
2021-01-02 13:42:43 +00:00
fn warm_up_enabled ( & self ) -> bool {
false
}
2020-12-19 19:50:00 +00:00
/// Called on shutdown, and perhaps at regular intervals. Allows you to save state.
2021-02-28 17:53:45 +00:00
///
2021-09-29 06:45:13 +00:00
/// Only called when the "persistence" feature is enabled.
///
2021-02-28 17:53:45 +00:00
/// On web the states is stored to "Local Storage".
2021-03-31 17:51:19 +00:00
/// On native the path is picked using [`directories_next::ProjectDirs::data_dir`](https://docs.rs/directories-next/2.0.0/directories_next/struct.ProjectDirs.html#method.data_dir) which is:
/// * Linux: `/home/UserName/.local/share/APPNAME`
/// * macOS: `/Users/UserName/Library/Application Support/APPNAME`
/// * Windows: `C:\Users\UserName\AppData\Roaming\APPNAME`
///
/// where `APPNAME` is what is returned by [`Self::name()`].
2020-12-19 19:50:00 +00:00
fn save ( & mut self , _storage : & mut dyn Storage ) { }
2021-06-12 20:10:40 +00:00
/// Called once on shutdown (before or after [`Self::save`])
2020-12-31 13:31:11 +00:00
fn on_exit ( & mut self ) { }
// ---------
// Settings:
2021-10-23 03:58:10 +00:00
/// The name of your App, used for the title bar of native windows
/// and the save location of persistence (see [`Self::save`]).
2020-12-31 13:31:11 +00:00
fn name ( & self ) -> & str ;
2021-06-12 20:10:40 +00:00
/// Time between automatic calls to [`Self::save`]
2020-12-19 19:50:00 +00:00
fn auto_save_interval ( & self ) -> std ::time ::Duration {
std ::time ::Duration ::from_secs ( 30 )
}
2021-06-12 20:10:40 +00:00
/// The size limit of the web app canvas.
2021-02-12 16:57:53 +00:00
fn max_size_points ( & self ) -> egui ::Vec2 {
// Some browsers get slow with huge WebGL canvases, so we limit the size:
egui ::Vec2 ::new ( 1024.0 , 2048.0 )
}
2020-12-31 13:31:11 +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.
fn clear_color ( & self ) -> egui ::Rgba {
// NOTE: a bright gray makes the shadows of the windows look weird.
2021-03-31 18:53:13 +00:00
// We use a bit of transparency so that if the user switches on the
// `transparent()` option they get immediate results.
egui ::Color32 ::from_rgba_unmultiplied ( 12 , 12 , 12 , 180 ) . into ( )
2020-12-31 13:31:11 +00:00
}
2021-09-29 06:45:13 +00:00
/// Controls wether or not the native window position and size will be
/// persisted (only if the "persistence" feature is enabled).
fn persist_native_window ( & self ) -> bool {
true
}
/// Controls wether or not the egui memory (window positions etc) will be
/// persisted (only if the "persistence" feature is enabled).
fn persist_egui_memory ( & self ) -> bool {
true
}
2021-05-08 08:14:56 +00:00
}
2021-02-26 14:59:30 +00:00
2021-12-28 16:33:49 +00:00
/// Options controlling the behavior of a native window.
///
/// Only a single native window is currently supported.
2021-05-08 08:14:56 +00:00
#[ derive(Clone) ]
pub struct NativeOptions {
2021-05-08 08:19:47 +00:00
/// Sets whether or not the window will always be on top of other windows.
pub always_on_top : bool ,
2021-08-25 16:20:53 +00:00
/// Show window in maximized mode
pub maximized : bool ,
2021-05-08 08:19:47 +00:00
/// On desktop: add window decorations (i.e. a frame around your app)?
/// If false it will be difficult to move and resize the app.
pub decorated : bool ,
/// On Windows: enable drag and drop support.
2021-09-30 17:40:21 +00:00
/// Default is `false` to avoid issues with crates such as [`cpal`](https://github.com/RustAudio/cpal) which
/// will hang when combined with drag-and-drop.
/// See <https://github.com/rust-windowing/winit/issues/1255>.
2021-05-08 08:19:47 +00:00
pub drag_and_drop_support : bool ,
2021-02-26 14:59:30 +00:00
/// The application icon, e.g. in the Windows task bar etc.
2021-05-08 08:14:56 +00:00
pub icon_data : Option < IconData > ,
/// The initial size of the native window in points (logical pixels).
pub initial_window_size : Option < egui ::Vec2 > ,
/// Should the app window be resizable?
pub resizable : bool ,
2021-03-31 18:53:13 +00:00
/// On desktop: make the window transparent.
2021-05-08 08:14:56 +00:00
/// You control the transparency with [`App::clear_color()`].
2021-03-31 18:53:13 +00:00
/// You should avoid having a [`egui::CentralPanel`], or make sure its frame is also transparent.
2021-05-08 08:14:56 +00:00
pub transparent : bool ,
}
impl Default for NativeOptions {
fn default ( ) -> Self {
Self {
2021-05-08 08:19:47 +00:00
always_on_top : false ,
2021-08-25 16:20:53 +00:00
maximized : false ,
2021-05-08 08:19:47 +00:00
decorated : true ,
2021-08-15 15:17:16 +00:00
drag_and_drop_support : false ,
2021-05-08 08:14:56 +00:00
icon_data : None ,
initial_window_size : None ,
resizable : true ,
transparent : false ,
}
2021-04-19 20:49:28 +00:00
}
2021-02-26 14:59:30 +00:00
}
/// Image data for the icon.
2021-05-08 08:14:56 +00:00
#[ derive(Clone) ]
2021-02-26 14:59:30 +00:00
pub struct IconData {
2021-12-26 20:21:28 +00:00
/// RGBA pixels, unmultiplied.
2021-02-26 14:59:30 +00:00
pub rgba : Vec < u8 > ,
/// Image width. This should be a multiple of 4.
pub width : u32 ,
/// Image height. This should be a multiple of 4.
pub height : u32 ,
2020-07-23 16:54:16 +00:00
}
2020-12-31 13:31:11 +00:00
/// Represents the surroundings of your app.
///
/// It provides methods to inspect the surroundings (are we on the web?),
2021-09-03 19:04:43 +00:00
/// allocate textures, and change settings (e.g. window size).
2021-12-26 20:21:28 +00:00
///
/// [`Frame`] is cheap to clone and is safe to pass to other threads.
#[ derive(Clone) ]
pub struct Frame ( pub Arc < Mutex < backend ::FrameData > > ) ;
impl Frame {
/// Create a `Frame` - called by the integration.
#[ doc(hidden) ]
pub fn new ( frame_data : backend ::FrameData ) -> Self {
Self ( Arc ::new ( Mutex ::new ( frame_data ) ) )
}
/// Convenience to access the underlying `backend::FrameData`.
#[ doc(hidden) ]
#[ inline ]
pub fn lock ( & self ) -> std ::sync ::MutexGuard < '_ , backend ::FrameData > {
self . 0. lock ( ) . unwrap ( )
}
2020-12-31 13:31:11 +00:00
/// True if you are in a web environment.
pub fn is_web ( & self ) -> bool {
2021-12-26 20:21:28 +00:00
self . lock ( ) . info . web_info . is_some ( )
2020-12-31 13:31:11 +00:00
}
2020-10-24 17:23:16 +00:00
/// Information about the integration.
2021-12-26 20:21:28 +00:00
pub fn info ( & self ) -> IntegrationInfo {
self . lock ( ) . info . clone ( )
2020-12-31 13:31:11 +00:00
}
/// Signal the app to stop/exit/quit the app (only works for native apps, not web apps).
2021-08-23 19:47:00 +00:00
/// The framework will not quit immediately, but at the end of the this frame.
2021-12-26 20:21:28 +00:00
pub fn quit ( & self ) {
self . lock ( ) . output . quit = true ;
2020-12-31 13:31:11 +00:00
}
/// Set the desired inner size of the window (in egui points).
2021-12-26 20:21:28 +00:00
pub fn set_window_size ( & self , size : egui ::Vec2 ) {
self . lock ( ) . output . window_size = Some ( size ) ;
2020-12-31 13:31:11 +00:00
}
2021-10-22 22:03:17 +00:00
/// Set the desired title of the window.
2021-12-26 20:21:28 +00:00
pub fn set_window_title ( & self , title : & str ) {
self . lock ( ) . output . window_title = Some ( title . to_owned ( ) ) ;
2021-10-22 22:03:17 +00:00
}
2021-09-07 19:42:14 +00:00
/// Set whether to show window decorations (i.e. a frame around you app).
/// If false it will be difficult to move and resize the app.
2021-12-26 20:21:28 +00:00
pub fn set_decorations ( & self , decorated : bool ) {
self . lock ( ) . output . decorated = Some ( decorated ) ;
2021-09-07 19:42:14 +00:00
}
2021-09-28 15:34:58 +00:00
/// When called, the native window will follow the
/// movement of the cursor while the primary mouse button is down.
///
2021-12-26 20:21:28 +00:00
/// Does not work on the web.
pub fn drag_window ( & self ) {
self . lock ( ) . output . drag_window = true ;
2021-09-28 15:34:58 +00:00
}
2021-12-26 20:21:28 +00:00
/// This signals the [`egui`] integration that a repaint is required.
///
/// Call this e.g. when a background process finishes in an async context and/or background thread.
pub fn request_repaint ( & self ) {
self . lock ( ) . repaint_signal . request_repaint ( ) ;
}
/// for integrations only: call once per frame
pub fn take_app_output ( & self ) -> crate ::backend ::AppOutput {
let mut lock = self . lock ( ) ;
let next_id = lock . output . tex_allocation_data . next_id ;
let app_output = std ::mem ::take ( & mut lock . output ) ;
lock . output . tex_allocation_data . next_id = next_id ;
app_output
}
/// Allocate a texture. Free it again with [`Self::free_texture`].
pub fn alloc_texture ( & self , image : Image ) -> egui ::TextureId {
self . lock ( ) . output . tex_allocation_data . alloc ( image )
}
/// Free a texture that has been previously allocated with [`Self::alloc_texture`]. Idempotent.
pub fn free_texture ( & self , id : egui ::TextureId ) {
self . lock ( ) . output . tex_allocation_data . free ( id ) ;
}
}
impl TextureAllocator for Frame {
fn alloc ( & self , image : Image ) -> egui ::TextureId {
self . lock ( ) . output . tex_allocation_data . alloc ( image )
}
fn free ( & self , id : egui ::TextureId ) {
self . lock ( ) . output . tex_allocation_data . free ( id ) ;
2020-12-31 13:31:11 +00:00
}
2020-10-24 17:23:16 +00:00
}
2021-01-17 09:52:01 +00:00
/// Information about the web environment (if applicable).
2020-10-17 21:54:46 +00:00
#[ derive(Clone, Debug) ]
2020-07-23 16:54:16 +00:00
pub struct WebInfo {
2021-01-01 16:11:05 +00:00
/// e.g. "#fragment" part of "www.example.com/index.html#fragment".
/// Note that the leading `#` is included in the string.
2021-01-01 20:27:10 +00:00
/// Also known as "hash-link" or "anchor".
2020-07-23 16:54:16 +00:00
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 {
2021-10-19 19:40:55 +00:00
/// The name of the integration, e.g. `egui_web`, `egui_glium`, `egui_glow`
pub name : & 'static str ,
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
2021-06-07 18:56:18 +00:00
/// Does the system prefer dark mode (over light mode)?
/// `None` means "don't know".
pub prefer_dark_mode : Option < bool > ,
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-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-12-31 13:31:11 +00:00
/// How to allocate textures (images) to use in [`egui`].
2020-10-17 10:33:30 +00:00
pub trait TextureAllocator {
2020-12-31 13:31:11 +00:00
/// Allocate a new user texture.
2021-04-01 21:07:58 +00:00
///
/// There is no way to change a texture.
/// Instead allocate a new texture and free the previous one with [`Self::free`].
2021-12-26 20:21:28 +00:00
fn alloc ( & self , image : Image ) -> egui ::TextureId ;
2020-11-18 20:38:29 +00:00
/// Free the given texture.
2021-12-26 20:21:28 +00:00
fn free ( & self , id : egui ::TextureId ) ;
}
/// A 2D color image in RAM.
#[ derive(Clone, Default) ]
pub struct Image {
/// width, height
pub size : [ usize ; 2 ] ,
/// The pixels, row by row, from top to bottom.
pub pixels : Vec < egui ::Color32 > ,
}
impl Image {
/// Create an `Image` from flat RGBA data.
/// Panics unless `size[0] * size[1] * 4 == rgba.len()`.
/// This is usually what you want to use after having loaded an image.
pub fn from_rgba_unmultiplied ( size : [ usize ; 2 ] , rgba : & [ u8 ] ) -> Self {
assert_eq! ( size [ 0 ] * size [ 1 ] * 4 , rgba . len ( ) ) ;
let pixels = rgba
. chunks_exact ( 4 )
. map ( | p | egui ::Color32 ::from_rgba_unmultiplied ( p [ 0 ] , p [ 1 ] , p [ 2 ] , p [ 3 ] ) )
. collect ( ) ;
Self { size , pixels }
}
2020-07-23 16:54:16 +00:00
}
2021-09-05 09:00:45 +00:00
/// Abstraction for platform dependent texture reference
pub trait NativeTexture {
2021-09-08 22:16:06 +00:00
/// The native texture type.
2021-09-05 09:00:45 +00:00
type Texture ;
2021-12-28 12:22:01 +00:00
/// Bind native texture to an egui texture id.
2021-09-05 09:00:45 +00:00
fn register_native_texture ( & mut self , native : Self ::Texture ) -> egui ::TextureId ;
2021-12-28 12:22:01 +00:00
/// Change what texture the given id refers to.
2021-09-05 09:00:45 +00:00
fn replace_native_texture ( & mut self , id : egui ::TextureId , replacing : Self ::Texture ) ;
}
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-31 13:31:11 +00:00
/// Get the value for the given key.
2020-12-19 13:58:00 +00:00
fn get_string ( & self , key : & str ) -> Option < String > ;
2020-12-31 13:31:11 +00:00
/// Set the value for the given key.
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
}
2021-06-12 20:17:57 +00:00
/// Get and deserialize the [RON](https://github.com/ron-rs/ron) stored at the given key.
2021-04-05 10:09:01 +00:00
#[ cfg(feature = " ron " ) ]
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 )
2021-04-05 10:09:01 +00:00
. and_then ( | value | ron ::from_str ( & value ) . ok ( ) )
2020-07-23 16:54:16 +00:00
}
2021-04-15 07:48:06 +00:00
/// Serialize the given value as [RON](https://github.com/ron-rs/ron) and store with the given key.
2021-04-05 10:09:01 +00:00
#[ cfg(feature = " ron " ) ]
2020-07-23 16:54:16 +00:00
pub fn set_value < T : serde ::Serialize > ( storage : & mut dyn Storage , key : & str , value : & T ) {
2021-11-28 15:37:40 +00:00
storage . set_string ( key , ron ::ser ::to_string ( value ) . unwrap ( ) ) ;
2020-07-23 16:54:16 +00:00
}
2020-12-31 13:31:11 +00:00
/// [`Storage`] key used for app
2020-07-23 16:54:16 +00:00
pub const APP_KEY : & str = " app " ;
2020-12-30 19:56:50 +00:00
// ----------------------------------------------------------------------------
2020-12-31 13:31:11 +00:00
/// You only need to look here if you are writing a backend for `epi`.
pub mod backend {
2021-12-26 20:21:28 +00:00
use std ::collections ::HashMap ;
2020-12-31 13:31:11 +00:00
use super ::* ;
2021-12-26 20:21:28 +00:00
/// How to signal the [`egui`] integration that a repaint is required.
pub trait RepaintSignal : Send + Sync {
/// This signals the [`egui`] integration that a repaint is required.
///
/// Call this e.g. when a background process finishes in an async context and/or background thread.
fn request_repaint ( & self ) ;
}
2020-12-31 13:31:11 +00:00
/// The data required by [`Frame`] each frame.
2021-12-26 20:21:28 +00:00
pub struct FrameData {
2020-12-31 13:31:11 +00:00
/// Information about the integration.
pub info : IntegrationInfo ,
/// Where the app can issue commands back to the integration.
2021-12-26 20:21:28 +00:00
pub output : AppOutput ,
2020-12-31 13:31:11 +00:00
/// If you need to request a repaint from another thread, clone this and send it to that other thread.
pub repaint_signal : std ::sync ::Arc < dyn RepaintSignal > ,
}
2021-12-26 20:21:28 +00:00
/// The data needed in order to allocate and free textures/images.
#[ derive(Default) ]
#[ must_use ]
pub struct TexAllocationData {
/// We allocate texture id linearly.
pub ( crate ) next_id : u64 ,
/// New creations this frame
pub creations : HashMap < u64 , Image > ,
/// destructions this frame.
pub destructions : Vec < u64 > ,
}
impl TexAllocationData {
/// Should only be used by integrations
pub fn take ( & mut self ) -> Self {
let next_id = self . next_id ;
let ret = std ::mem ::take ( self ) ;
self . next_id = next_id ;
ret
}
/// Allocate a new texture.
pub fn alloc ( & mut self , image : Image ) -> egui ::TextureId {
let id = self . next_id ;
self . next_id + = 1 ;
self . creations . insert ( id , image ) ;
egui ::TextureId ::User ( id )
}
/// Free an existing texture.
pub fn free ( & mut self , id : egui ::TextureId ) {
if let egui ::TextureId ::User ( id ) = id {
self . destructions . push ( id ) ;
}
2020-12-31 13:31:11 +00:00
}
}
/// Action that can be taken by the user app.
2021-12-26 20:21:28 +00:00
#[ derive(Default) ]
#[ must_use ]
2020-12-31 13:31:11 +00:00
pub struct AppOutput {
/// Set to `true` to stop the app.
/// This does nothing for web apps.
pub quit : bool ,
/// Set to some size to resize the outer window (e.g. glium window) to this size.
pub window_size : Option < egui ::Vec2 > ,
2021-09-07 19:42:14 +00:00
2021-10-22 22:03:17 +00:00
/// Set to some string to rename the outer window (e.g. glium window) to this title.
pub window_title : Option < String > ,
2021-12-26 20:21:28 +00:00
/// Set to some bool to change window decorations.
2021-09-07 19:42:14 +00:00
pub decorated : Option < bool > ,
2021-09-28 15:34:58 +00:00
2021-12-26 20:21:28 +00:00
/// Set to true to drag window while primary mouse button is down.
2021-09-28 15:34:58 +00:00
pub drag_window : bool ,
2021-12-26 20:21:28 +00:00
/// A way to allocate textures (on integrations that support it).
pub tex_allocation_data : TexAllocationData ,
2020-12-31 13:31:11 +00:00
}
2020-12-30 19:56:50 +00:00
}