mirror of
https://github.com/emilk/egui.git
synced 2026-08-30 21:30:03 -04:00
Refactor memory data (#836)
This refactors the widget state storage introduced by @optozorax in https://github.com/emilk/egui/pull/257 * Unify the four buckets (`data`, `data_temp`, `id_data` and `id_data_temp`) into a single `data`. * Less complexity, and also less chance of error (storing in one bucket, reading from another). * Store data by `Id` and `TypeId`. * Users can thus reuse the same `Id` to store many types. * Uses a simple xor of id and typeid, which is fast and good since both id and typeid are already high-entropy hashes. * Use different suffixes on the functions to pick if you want the data persisted or not (`get_temp`, `insert_persisted`, etc). * Writing with one suffix and reading with the other works. * To store state not bound to a specific `Id` (i.e. only based on type), use the new `Id::null` as the key.
This commit is contained in:
@@ -27,7 +27,7 @@ pub fn password_ui(ui: &mut egui::Ui, text: &mut String) -> egui::Response {
|
||||
// You can read more about available `Memory` functions in the documentation of `egui::Memory`
|
||||
// struct and `egui::any` module.
|
||||
// You should get state by value, not by reference to avoid borrowing of `Memory`.
|
||||
let mut plaintext = *ui.memory().id_data_temp.get_or_default::<State>(id);
|
||||
let mut plaintext = ui.memory().data.get_temp::<State>(id).unwrap_or_default();
|
||||
|
||||
// 4. Process ui, change a local copy of the state
|
||||
// We want TextEdit to fill entire space, and have button after that, so in that case we can
|
||||
@@ -51,7 +51,7 @@ pub fn password_ui(ui: &mut egui::Ui, text: &mut String) -> egui::Response {
|
||||
});
|
||||
|
||||
// 5. Insert changed state back
|
||||
ui.memory().id_data_temp.insert(id, plaintext);
|
||||
ui.memory().data.insert_temp(id, plaintext);
|
||||
|
||||
// All done! Return the interaction response so the user can check what happened
|
||||
// (hovered, clicked, …) and maybe show a tooltip:
|
||||
|
||||
@@ -146,21 +146,27 @@ impl CodeTheme {
|
||||
|
||||
pub fn from_memory(ctx: &egui::Context) -> Self {
|
||||
if ctx.style().visuals.dark_mode {
|
||||
*ctx.memory()
|
||||
.id_data
|
||||
.get_or_insert_with(egui::Id::new("dark"), CodeTheme::dark)
|
||||
ctx.memory()
|
||||
.data
|
||||
.get_persisted(egui::Id::new("dark"))
|
||||
.unwrap_or_else(CodeTheme::dark)
|
||||
} else {
|
||||
*ctx.memory()
|
||||
.id_data
|
||||
.get_or_insert_with(egui::Id::new("light"), CodeTheme::light)
|
||||
ctx.memory()
|
||||
.data
|
||||
.get_persisted(egui::Id::new("light"))
|
||||
.unwrap_or_else(CodeTheme::light)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn store_in_memory(&self, ctx: &egui::Context) {
|
||||
if self.dark_mode {
|
||||
ctx.memory().id_data.insert(egui::Id::new("dark"), *self);
|
||||
ctx.memory()
|
||||
.data
|
||||
.insert_persisted(egui::Id::new("dark"), *self);
|
||||
} else {
|
||||
ctx.memory().id_data.insert(egui::Id::new("light"), *self);
|
||||
ctx.memory()
|
||||
.data
|
||||
.insert_persisted(egui::Id::new("light"), *self);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -229,7 +235,11 @@ impl CodeTheme {
|
||||
|
||||
pub fn ui(&mut self, ui: &mut egui::Ui) {
|
||||
ui.horizontal_top(|ui| {
|
||||
let mut selected_tt: TokenType = *ui.memory().data.get_or(TokenType::Comment);
|
||||
let selected_id = egui::Id::null();
|
||||
let mut selected_tt: TokenType = *ui
|
||||
.memory()
|
||||
.data
|
||||
.get_persisted_mut_or(selected_id, TokenType::Comment);
|
||||
|
||||
ui.vertical(|ui| {
|
||||
ui.set_width(150.0);
|
||||
@@ -271,7 +281,7 @@ impl CodeTheme {
|
||||
|
||||
ui.add_space(16.0);
|
||||
|
||||
ui.memory().data.insert(selected_tt);
|
||||
ui.memory().data.insert_persisted(selected_id, selected_tt);
|
||||
|
||||
egui::Frame::group(ui.style())
|
||||
.margin(egui::Vec2::splat(2.0))
|
||||
|
||||
Reference in New Issue
Block a user