Managing App State
Hold and update UI data.
State on the App Struct
In egui your application state is plain Rust data living on the struct that implements eframe::App. There is no special store or framework-owned model.
Fields hold everything: form inputs, selections, loaded data, flags. Each frame, update reads and mutates these fields directly through &mut self.
struct App {
query: String,
results: Vec<Item>,
selected: Option<usize>,
loading: bool,
}Deriving Default
eframe builds your app in a closure passed to run_native. The simplest pattern is #[derive(Default)] and constructing with App::default().
For non-default initial values, implement Default by hand or add a constructor that takes the CreationContext for setup like fonts or persistence.
impl App {
fn new(cc: &eframe::CreationContext) -> Self {
let mut app = Self::default();
app.dark = cc.egui_ctx.style().visuals.dark_mode;
app
}
}All lessons in this course
- Immediate-Mode UI Basics
- Widgets and Layout
- Managing App State
- Packaging a Desktop App