1
0
mirror of https://github.com/emilk/egui.git synced 2026-06-27 23:13:13 -04:00

Store state

This commit is contained in:
Emil Ernerfeldt
2023-04-25 11:04:58 +02:00
parent 32be36416c
commit e5d0cf68d6
3 changed files with 24 additions and 1 deletions

1
Cargo.lock generated
View File

@@ -1135,6 +1135,7 @@ dependencies = [
"eframe",
"egui_extras",
"env_logger",
"serde",
]
[[package]]

View File

@@ -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"] }

View File

@@ -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(