mirror of
https://github.com/emilk/egui.git
synced 2026-06-27 23:13:13 -04:00
Store state
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -1135,6 +1135,7 @@ dependencies = [
|
||||
"eframe",
|
||||
"egui_extras",
|
||||
"env_logger",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
@@ -10,7 +10,9 @@ publish = false
|
||||
|
||||
[dependencies]
|
||||
eframe = { path = "../../crates/eframe", features = [
|
||||
"persistence",
|
||||
"__screenshot", # __screenshot is so we can dump a screenshot using EFRAME_SCREENSHOT_TO
|
||||
] }
|
||||
egui_extras = { path = "../../crates/egui_extras" }
|
||||
env_logger = "0.10"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
|
||||
@@ -11,9 +11,22 @@ fn main() -> Result<(), eframe::Error> {
|
||||
initial_window_size: Some(egui::vec2(800.0, 600.0)),
|
||||
..Default::default()
|
||||
};
|
||||
eframe::run_native("Dock", options, Box::new(|_cc| Box::<MyApp>::default()))
|
||||
eframe::run_native(
|
||||
"Dock",
|
||||
options,
|
||||
Box::new(|cc| {
|
||||
let mut app = MyApp::default();
|
||||
if let Some(storage) = cc.storage {
|
||||
if let Some(state) = eframe::get_value(storage, eframe::APP_KEY) {
|
||||
app = state;
|
||||
}
|
||||
}
|
||||
Box::new(app)
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize, serde::Serialize)]
|
||||
pub struct View {
|
||||
title: String,
|
||||
color: Color32,
|
||||
@@ -68,8 +81,11 @@ impl dock::Behavior<View> for DockBehavior {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize, serde::Serialize)]
|
||||
struct MyApp {
|
||||
dock: dock::Dock<View>,
|
||||
|
||||
#[serde(skip, default)]
|
||||
behavior: DockBehavior,
|
||||
}
|
||||
|
||||
@@ -132,6 +148,10 @@ impl eframe::App for MyApp {
|
||||
self.dock.ui(&mut self.behavior, ui);
|
||||
});
|
||||
}
|
||||
|
||||
fn save(&mut self, storage: &mut dyn eframe::Storage) {
|
||||
eframe::set_value(storage, eframe::APP_KEY, &self);
|
||||
}
|
||||
}
|
||||
|
||||
fn tree_ui(
|
||||
|
||||
Reference in New Issue
Block a user