From e8316b208705910958152b2ef6c4c5d7110b4e6c Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 6 Feb 2020 03:06:39 +0100 Subject: Allow `todos` example to compile to `wasm32` --- examples/todos/Cargo.toml | 4 +++- examples/todos/src/main.rs | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) (limited to 'examples/todos') diff --git a/examples/todos/Cargo.toml b/examples/todos/Cargo.toml index 21acd5d6..cfb8e97d 100644 --- a/examples/todos/Cargo.toml +++ b/examples/todos/Cargo.toml @@ -8,9 +8,11 @@ publish = false [dependencies] iced = { path = "../.." } iced_futures = { path = "../../futures", features = ["async-std"] } -async-std = "1.0" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" + +[target.'cfg(not(target_arch = "wasm32"))'.dependencies] +async-std = "1.0" directories = "2.0" [package.metadata.deb] diff --git a/examples/todos/src/main.rs b/examples/todos/src/main.rs index bfae5e88..8262b537 100644 --- a/examples/todos/src/main.rs +++ b/examples/todos/src/main.rs @@ -38,7 +38,12 @@ enum Message { } impl Application for Todos { + #[cfg(not(target_arch = "wasm32"))] type Executor = iced_futures::executor::AsyncStd; + + #[cfg(target_arch = "wasm32")] + type Executor = iced_futures::executor::WasmBindgen; + type Message = Message; fn new() -> (Todos, Command) { @@ -377,6 +382,7 @@ impl Controls { ) .push( Row::new() + .width(Length::Shrink) .spacing(10) .push(filter_button( all_button, @@ -493,6 +499,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 +562,18 @@ impl SavedState { } } +// TODO +#[cfg(target_arch = "wasm32")] +impl SavedState { + async fn load() -> Result { + Err(LoadError::FileError) + } + + async fn save(self) -> Result<(), SaveError> { + Err(SaveError::FileError) + } +} + mod style { use iced::{button, Background, Color, Vector}; -- cgit From ad500441afc355f0b8ca2a463248d350d74f0f20 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 6 Feb 2020 03:56:21 +0100 Subject: Allow switching `executor::Default` with features --- examples/todos/Cargo.toml | 3 +-- examples/todos/src/main.rs | 7 +------ 2 files changed, 2 insertions(+), 8 deletions(-) (limited to 'examples/todos') diff --git a/examples/todos/Cargo.toml b/examples/todos/Cargo.toml index cfb8e97d..c905fc38 100644 --- a/examples/todos/Cargo.toml +++ b/examples/todos/Cargo.toml @@ -6,8 +6,7 @@ edition = "2018" publish = false [dependencies] -iced = { path = "../.." } -iced_futures = { path = "../../futures", features = ["async-std"] } +iced = { path = "../..", features = ["async-std"] } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" diff --git a/examples/todos/src/main.rs b/examples/todos/src/main.rs index 8262b537..a4009874 100644 --- a/examples/todos/src/main.rs +++ b/examples/todos/src/main.rs @@ -38,12 +38,7 @@ enum Message { } impl Application for Todos { - #[cfg(not(target_arch = "wasm32"))] - type Executor = iced_futures::executor::AsyncStd; - - #[cfg(target_arch = "wasm32")] - type Executor = iced_futures::executor::WasmBindgen; - + type Executor = iced::executor::Default; type Message = Message; fn new() -> (Todos, Command) { -- cgit From 36e617ae70cc7a86ce998cbd61f6aa702bb42933 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 6 Feb 2020 05:56:23 +0100 Subject: Implement local storage for `todos` example in Wasm --- examples/todos/Cargo.toml | 4 ++++ examples/todos/src/main.rs | 29 ++++++++++++++++++++++++++--- 2 files changed, 30 insertions(+), 3 deletions(-) (limited to 'examples/todos') diff --git a/examples/todos/Cargo.toml b/examples/todos/Cargo.toml index c905fc38..f945cde5 100644 --- a/examples/todos/Cargo.toml +++ b/examples/todos/Cargo.toml @@ -14,6 +14,10 @@ serde_json = "1.0" async-std = "1.0" directories = "2.0" +[target.'cfg(target_arch = "wasm32")'.dependencies] +web-sys = { version = "0.3", features = ["Window", "Storage"] } +wasm-timer = "0.2" + [package.metadata.deb] assets = [ ["target/release/todos", "usr/bin/iced-todos", "755"], diff --git a/examples/todos/src/main.rs b/examples/todos/src/main.rs index a4009874..7e866b19 100644 --- a/examples/todos/src/main.rs +++ b/examples/todos/src/main.rs @@ -557,15 +557,38 @@ impl SavedState { } } -// TODO #[cfg(target_arch = "wasm32")] impl SavedState { + fn storage() -> Option { + let window = web_sys::window()?; + + window.local_storage().ok()? + } + async fn load() -> Result { - Err(LoadError::FileError) + 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> { - Err(SaveError::FileError) + 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(()) } } -- cgit