diff --git a/crates/eframe/src/epi.rs b/crates/eframe/src/epi.rs index d6bdbd62b..7de55736a 100644 --- a/crates/eframe/src/epi.rs +++ b/crates/eframe/src/epi.rs @@ -942,6 +942,9 @@ pub trait Storage { /// Set the value for the given key. fn set_string(&mut self, key: &str, value: String); + /// Remove a given key. + fn remove_string(&mut self, key: &str); + /// write-to-disk or similar fn flush(&mut self); } diff --git a/crates/eframe/src/native/file_storage.rs b/crates/eframe/src/native/file_storage.rs index 13e22fa75..947554a00 100644 --- a/crates/eframe/src/native/file_storage.rs +++ b/crates/eframe/src/native/file_storage.rs @@ -159,6 +159,11 @@ impl crate::Storage for FileStorage { } } + fn remove_string(&mut self, key: &str) { + self.kv.remove(key); + self.dirty = true; + } + fn flush(&mut self) { if self.dirty { profiling::scope!("FileStorage::flush"); diff --git a/crates/eframe/src/web/app_runner.rs b/crates/eframe/src/web/app_runner.rs index 5388bf023..8d3f6c9d7 100644 --- a/crates/eframe/src/web/app_runner.rs +++ b/crates/eframe/src/web/app_runner.rs @@ -431,5 +431,9 @@ impl epi::Storage for LocalStorage { super::storage::local_storage_set(key, &value); } + fn remove_string(&mut self, key: &str) { + super::storage::local_storage_remove(key); + } + fn flush(&mut self) {} } diff --git a/crates/eframe/src/web/storage.rs b/crates/eframe/src/web/storage.rs index baa3ad83b..cf9d90dbb 100644 --- a/crates/eframe/src/web/storage.rs +++ b/crates/eframe/src/web/storage.rs @@ -24,6 +24,23 @@ pub fn local_storage_set(key: &str, value: &str) { } } +/// Remove data from local storage. +pub fn local_storage_remove(key: &str) { + match local_storage() { + Some(storage) => { + if let Err(err) = storage.remove_item(key) { + log::warn!( + "local_storage_remove failed: key={key}, err={}", + crate::web::string_from_js_value(&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") {