-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathstorage.rs
More file actions
54 lines (48 loc) · 1.51 KB
/
Copy pathstorage.rs
File metadata and controls
54 lines (48 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
fn local_storage() -> Option<web_sys::Storage> {
web_sys::window()?.local_storage().ok()?
}
/// Read data from local storage.
pub fn local_storage_get(key: &str) -> Option<String> {
local_storage().map(|storage| storage.get_item(key).ok())??
}
/// Write data to local storage.
pub fn local_storage_set(key: &str, value: &str) {
match local_storage() {
Some(storage) => {
if let Err(err) = storage.set_item(key, value) {
log::warn!("local_storage_set failed: key={key}, err={err:?}");
}
}
None => {
log::warn!("local_storage unavailable");
}
}
}
#[cfg(feature = "persistence")]
pub(crate) fn load_memory(ctx: &egui::Context) {
if let Some(memory_string) = local_storage_get("egui_memory_ron") {
match ron::from_str(&memory_string) {
Ok(memory) => {
ctx.memory_mut(|m| *m = memory);
}
Err(err) => {
log::warn!("Failed to parse memory RON: {err}");
}
}
}
}
#[cfg(not(feature = "persistence"))]
pub(crate) fn load_memory(_: &egui::Context) {}
#[cfg(feature = "persistence")]
pub(crate) fn save_memory(ctx: &egui::Context) {
match ctx.memory(ron::to_string) {
Ok(ron) => {
local_storage_set("egui_memory_ron", &ron);
}
Err(err) => {
log::warn!("Failed to serialize memory as RON: {err}");
}
}
}
#[cfg(not(feature = "persistence"))]
pub(crate) fn save_memory(_: &egui::Context) {}