mirror of
https://github.com/emilk/egui.git
synced 2026-09-01 22:30:03 -04:00
Store state
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -1135,6 +1135,7 @@ dependencies = [
|
|||||||
"eframe",
|
"eframe",
|
||||||
"egui_extras",
|
"egui_extras",
|
||||||
"env_logger",
|
"env_logger",
|
||||||
|
"serde",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|||||||
@@ -10,7 +10,9 @@ publish = false
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
eframe = { path = "../../crates/eframe", features = [
|
eframe = { path = "../../crates/eframe", features = [
|
||||||
|
"persistence",
|
||||||
"__screenshot", # __screenshot is so we can dump a screenshot using EFRAME_SCREENSHOT_TO
|
"__screenshot", # __screenshot is so we can dump a screenshot using EFRAME_SCREENSHOT_TO
|
||||||
] }
|
] }
|
||||||
egui_extras = { path = "../../crates/egui_extras" }
|
egui_extras = { path = "../../crates/egui_extras" }
|
||||||
env_logger = "0.10"
|
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)),
|
initial_window_size: Some(egui::vec2(800.0, 600.0)),
|
||||||
..Default::default()
|
..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 {
|
pub struct View {
|
||||||
title: String,
|
title: String,
|
||||||
color: Color32,
|
color: Color32,
|
||||||
@@ -68,8 +81,11 @@ impl dock::Behavior<View> for DockBehavior {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(serde::Deserialize, serde::Serialize)]
|
||||||
struct MyApp {
|
struct MyApp {
|
||||||
dock: dock::Dock<View>,
|
dock: dock::Dock<View>,
|
||||||
|
|
||||||
|
#[serde(skip, default)]
|
||||||
behavior: DockBehavior,
|
behavior: DockBehavior,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -132,6 +148,10 @@ impl eframe::App for MyApp {
|
|||||||
self.dock.ui(&mut self.behavior, ui);
|
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(
|
fn tree_ui(
|
||||||
|
|||||||
Reference in New Issue
Block a user