Skip to content

Commit c531e11

Browse files
authored
Refactor memory data (#836)
This refactors the widget state storage introduced by @optozorax in emilk/egui#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.
1 parent a3ddf4c commit c531e11

File tree

1 file changed

+26
-22
lines changed

1 file changed

+26
-22
lines changed

egui/src/widgets/plot/mod.rs

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ struct PlotMemory {
3030
min_auto_bounds: Bounds,
3131
}
3232

33+
impl PlotMemory {
34+
pub fn load(ctx: &Context, id: Id) -> Option<Self> {
35+
ctx.memory().data.get_persisted(id)
36+
}
37+
38+
pub fn store(self, ctx: &Context, id: Id) {
39+
ctx.memory().data.insert_persisted(id, self);
40+
}
41+
}
42+
3343
// ----------------------------------------------------------------------------
3444

3545
/// A 2D plot, e.g. a graph of a function.
@@ -342,17 +352,13 @@ impl Widget for Plot {
342352
} = self;
343353

344354
let plot_id = ui.make_persistent_id(id_source);
345-
let mut memory = ui
346-
.memory()
347-
.id_data
348-
.get_mut_or_insert_with(plot_id, || PlotMemory {
349-
bounds: min_auto_bounds,
350-
auto_bounds: !min_auto_bounds.is_valid(),
351-
hovered_entry: None,
352-
hidden_items: Default::default(),
353-
min_auto_bounds,
354-
})
355-
.clone();
355+
let mut memory = PlotMemory::load(ui.ctx(), plot_id).unwrap_or_else(|| PlotMemory {
356+
bounds: min_auto_bounds,
357+
auto_bounds: !min_auto_bounds.is_valid(),
358+
hovered_entry: None,
359+
hidden_items: Default::default(),
360+
min_auto_bounds,
361+
});
356362

357363
// If the min bounds changed, recalculate everything.
358364
if min_auto_bounds != memory.min_auto_bounds {
@@ -363,7 +369,7 @@ impl Widget for Plot {
363369
min_auto_bounds,
364370
..memory
365371
};
366-
ui.memory().id_data.insert(plot_id, memory.clone());
372+
memory.clone().store(ui.ctx(), plot_id);
367373
}
368374

369375
let PlotMemory {
@@ -511,16 +517,14 @@ impl Widget for Plot {
511517
hovered_entry = legend.get_hovered_entry_name();
512518
}
513519

514-
ui.memory().id_data.insert(
515-
plot_id,
516-
PlotMemory {
517-
bounds,
518-
auto_bounds,
519-
hovered_entry,
520-
hidden_items,
521-
min_auto_bounds,
522-
},
523-
);
520+
let memory = PlotMemory {
521+
bounds,
522+
auto_bounds,
523+
hovered_entry,
524+
hidden_items,
525+
min_auto_bounds,
526+
};
527+
memory.store(ui.ctx(), plot_id);
524528

525529
if show_x || show_y {
526530
response.on_hover_cursor(CursorIcon::Crosshair)

0 commit comments

Comments
 (0)