Implement Storage trait for egui_web:s local storage bindings
This commit is contained in:
parent
7ff5d4726b
commit
99808d2df8
3 changed files with 19 additions and 5 deletions
|
@ -108,7 +108,7 @@ pub trait RepaintSignal: Send {
|
|||
/// On the web this is backed by [local storage](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage).
|
||||
/// On desktop this is backed by the file system.
|
||||
pub trait Storage {
|
||||
fn get_string(&self, key: &str) -> Option<&str>;
|
||||
fn get_string(&self, key: &str) -> Option<String>;
|
||||
fn set_string(&mut self, key: &str, value: String);
|
||||
|
||||
/// write-to-disk or similar
|
||||
|
@ -120,7 +120,7 @@ pub trait Storage {
|
|||
pub struct DummyStorage {}
|
||||
|
||||
impl Storage for DummyStorage {
|
||||
fn get_string(&self, _key: &str) -> Option<&str> {
|
||||
fn get_string(&self, _key: &str) -> Option<String> {
|
||||
None
|
||||
}
|
||||
fn set_string(&mut self, _key: &str, _value: String) {}
|
||||
|
@ -131,7 +131,7 @@ impl Storage for DummyStorage {
|
|||
pub fn get_value<T: serde::de::DeserializeOwned>(storage: &dyn Storage, key: &str) -> Option<T> {
|
||||
storage
|
||||
.get_string(key)
|
||||
.and_then(|value| serde_json::from_str(value).ok())
|
||||
.and_then(|value| serde_json::from_str(&value).ok())
|
||||
}
|
||||
|
||||
#[cfg(feature = "serde_json")]
|
||||
|
|
|
@ -25,8 +25,8 @@ impl FileStorage {
|
|||
}
|
||||
|
||||
impl egui::app::Storage for FileStorage {
|
||||
fn get_string(&self, key: &str) -> Option<&str> {
|
||||
self.kv.get(key).map(String::as_str)
|
||||
fn get_string(&self, key: &str) -> Option<String> {
|
||||
self.kv.get(key).cloned()
|
||||
}
|
||||
|
||||
fn set_string(&mut self, key: &str, value: String) {
|
||||
|
|
|
@ -172,6 +172,20 @@ pub fn save_memory(ctx: &egui::Context) {
|
|||
}
|
||||
}
|
||||
|
||||
pub struct LocalStorage {}
|
||||
|
||||
impl egui::app::Storage for LocalStorage {
|
||||
fn get_string(&self, key: &str) -> Option<String> {
|
||||
local_storage_get(key)
|
||||
}
|
||||
fn set_string(&mut self, key: &str, value: String) {
|
||||
local_storage_set(key, &value);
|
||||
}
|
||||
fn flush(&mut self) {}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
pub fn handle_output(output: &egui::Output) {
|
||||
let egui::Output {
|
||||
cursor_icon,
|
||||
|
|
Loading…
Reference in a new issue