summaryrefslogtreecommitdiffstats
path: root/examples/todos/src
diff options
context:
space:
mode:
Diffstat (limited to 'examples/todos/src')
-rw-r--r--examples/todos/src/main.rs39
1 files changed, 38 insertions, 1 deletions
diff --git a/examples/todos/src/main.rs b/examples/todos/src/main.rs
index bfae5e88..7e866b19 100644
--- a/examples/todos/src/main.rs
+++ b/examples/todos/src/main.rs
@@ -38,7 +38,7 @@ enum Message {
}
impl Application for Todos {
- type Executor = iced_futures::executor::AsyncStd;
+ type Executor = iced::executor::Default;
type Message = Message;
fn new() -> (Todos, Command<Message>) {
@@ -377,6 +377,7 @@ impl Controls {
)
.push(
Row::new()
+ .width(Length::Shrink)
.spacing(10)
.push(filter_button(
all_button,
@@ -493,6 +494,7 @@ enum SaveError {
FormatError,
}
+#[cfg(not(target_arch = "wasm32"))]
impl SavedState {
fn path() -> std::path::PathBuf {
let mut path = if let Some(project_dirs) =
@@ -555,6 +557,41 @@ impl SavedState {
}
}
+#[cfg(target_arch = "wasm32")]
+impl SavedState {
+ fn storage() -> Option<web_sys::Storage> {
+ let window = web_sys::window()?;
+
+ window.local_storage().ok()?
+ }
+
+ async fn load() -> Result<SavedState, LoadError> {
+ let storage = Self::storage().ok_or(LoadError::FileError)?;
+
+ let contents = storage
+ .get_item("state")
+ .map_err(|_| LoadError::FileError)?
+ .ok_or(LoadError::FileError)?;
+
+ serde_json::from_str(&contents).map_err(|_| LoadError::FormatError)
+ }
+
+ async fn save(self) -> Result<(), SaveError> {
+ let storage = Self::storage().ok_or(SaveError::FileError)?;
+
+ let json = serde_json::to_string_pretty(&self)
+ .map_err(|_| SaveError::FormatError)?;
+
+ storage
+ .set_item("state", &json)
+ .map_err(|_| SaveError::WriteError)?;
+
+ let _ = wasm_timer::Delay::new(std::time::Duration::from_secs(2)).await;
+
+ Ok(())
+ }
+}
+
mod style {
use iced::{button, Background, Color, Vector};