Persist window state across runs
This commit is contained in:
+50
-2
@@ -1,6 +1,6 @@
|
||||
use std::{
|
||||
fmt::{Display, UpperHex},
|
||||
ops::{Bound, RangeBounds},
|
||||
ops::{Bound, Deref, DerefMut, RangeBounds},
|
||||
str::FromStr,
|
||||
};
|
||||
|
||||
@@ -8,7 +8,7 @@ use atoi::FromRadix16;
|
||||
use egui::{
|
||||
Align, Color32, CornerRadius, CursorIcon, Event, Frame, Key, Layout, Margin, Rect, Response,
|
||||
RichText, Sense, Shape, Stroke, StrokeKind, TextEdit, Ui, UiBuilder, Vec2, Widget, WidgetText,
|
||||
ecolor::HexColor,
|
||||
ecolor::HexColor, util::id_type_map::SerializableAny,
|
||||
};
|
||||
use num_traits::{CheckedAdd, CheckedSub, One};
|
||||
|
||||
@@ -409,3 +409,51 @@ impl ResponseExt for Response {
|
||||
self.clicked() || self.dragged()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct UiData<T> {
|
||||
current: T,
|
||||
prev: T,
|
||||
loaded: bool,
|
||||
}
|
||||
|
||||
impl<T> UiData<T>
|
||||
where
|
||||
T: Default + PartialEq + SerializableAny,
|
||||
{
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
current: T::default(),
|
||||
prev: T::default(),
|
||||
loaded: false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn load(&mut self, ui: &Ui) {
|
||||
if !self.loaded {
|
||||
self.current = ui
|
||||
.data_mut(|d| d.get_persisted(ui.id()))
|
||||
.unwrap_or_default();
|
||||
self.loaded = true;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn save(&mut self, ui: &Ui) {
|
||||
if self.current != self.prev {
|
||||
ui.data_mut(|d| d.insert_persisted(ui.id(), self.current.clone()));
|
||||
self.prev = self.current.clone();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Deref for UiData<T> {
|
||||
type Target = T;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.current
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> DerefMut for UiData<T> {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.current
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user