pub trait App {
fn update(&mut self, ctx: &Context, frame: &mut Frame);
fn save(&mut self, _storage: &mut dyn Storage) { ... }
fn on_close_event(&mut self) -> bool { ... }
fn on_exit(&mut self, _gl: Option<&Context>) { ... }
fn auto_save_interval(&self) -> Duration { ... }
fn max_size_points(&self) -> Vec2 { ... }
fn clear_color(&self, _visuals: &Visuals) -> Rgba { ... }
fn persist_native_window(&self) -> bool { ... }
fn persist_egui_memory(&self) -> bool { ... }
fn warm_up_enabled(&self) -> bool { ... }
fn post_rendering(&mut self, _window_size_px: [u32; 2], _frame: &Frame) { ... }
}
Expand description
Implement this trait to write apps that can be compiled for both web/wasm and desktop/native using eframe
.
Required Methods
Called each time the UI needs repainting, which may be many times per second.
Put your widgets into a egui::SidePanel
, egui::TopBottomPanel
, egui::CentralPanel
, egui::Window
or egui::Area
.
The egui::Context
can be cloned and saved if you like.
To force a repaint, call egui::Context::request_repaint
at any time (e.g. from another thread).
Provided Methods
Called on shutdown, and perhaps at regular intervals. Allows you to save state.
Only called when the “persistence” feature is enabled.
On web the state is stored to “Local Storage”.
On native the path is picked using directories_next::ProjectDirs::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 given to eframe::run_native
.
fn on_close_event(&mut self) -> bool
fn on_close_event(&mut self) -> bool
Called when the user attempts to close the desktop window and/or quit the application.
By returning false
the closing will be aborted. To continue the closing return true
.
A scenario where this method will be run is after pressing the close button on a native window, which allows you to ask the user whether they want to do something before exiting. See the example at https://github.com/emilk/egui/blob/master/examples/confirm_exit/ for practical usage.
It will not be called on the web or when the window is forcefully closed.
Called once on shutdown, after Self::save
.
If you need to abort an exit use Self::on_close_event
.
To get a glow
context you need to compile with the glow
feature flag,
and run eframe with the glow backend.
fn auto_save_interval(&self) -> Duration
fn auto_save_interval(&self) -> Duration
Time between automatic calls to Self::save
fn max_size_points(&self) -> Vec2
fn max_size_points(&self) -> Vec2
The size limit of the web app canvas.
By default the max size is [egui::Vec2::INFINITY
], i.e. unlimited.
A large canvas can lead to bad frame rates on some older browsers on some platforms (see https://bugzilla.mozilla.org/show_bug.cgi?id=1010527#c0).
fn clear_color(&self, _visuals: &Visuals) -> Rgba
fn clear_color(&self, _visuals: &Visuals) -> Rgba
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 persist_native_window(&self) -> bool
fn persist_native_window(&self) -> bool
Controls whether or not the native window position and size will be persisted (only if the “persistence” feature is enabled).
fn persist_egui_memory(&self) -> bool
fn persist_egui_memory(&self) -> bool
Controls whether or not the egui memory (window positions etc) will be persisted (only if the “persistence” feature is enabled).
fn warm_up_enabled(&self) -> bool
fn warm_up_enabled(&self) -> bool
If true
a warm-up call to Self::update
will be issued where
ctx.memory().everything_is_visible()
will be set to true
.
This can help pre-caching resources loaded by different parts of the UI, preventing stutter later on.
In this warm-up call, all painted shapes will be ignored.
The default is false
, and it is unlikely you will want to change this.