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:
|
||||
|
||||
Reference in New Issue
Block a user