mirror of
https://github.com/emilk/egui.git
synced 2026-06-26 22:53:14 -04:00
feat: add remove_string() to storage trait (#8264)
<!-- Please read the "Making a PR" section of [`CONTRIBUTING.md`](https://github.com/emilk/egui/blob/main/CONTRIBUTING.md) before opening a Pull Request! * Keep your PR:s small and focused. * The PR title is what ends up in the changelog, so make it descriptive! * If applicable, add a screenshot or gif. * If it is a non-trivial addition, consider adding a demo for it to `egui_demo_lib`, or a new example. * Do NOT open PR:s from your `master` branch, as that makes it hard for maintainers to test and add commits to your PR. * Remember to run `cargo fmt` and `cargo clippy`. * Open the PR as a draft until you have self-reviewed it and run `./scripts/check.sh`. * When you have addressed a PR comment, mark it as resolved. Please be patient! I will review your PR, but my time is limited! --> * [X] I have followed the instructions in the PR template This PR adds a `remove_string()` API to the Storage trait and also implements it in the `FileStorage` and `LocalStorage` stucts.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -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) {}
|
||||
}
|
||||
|
||||
@@ -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") {
|
||||
|
||||
Reference in New Issue
Block a user