From 8f52604987038225ce90261f17fd8408f1a7ebe3 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 5 Feb 2020 01:40:27 +0100 Subject: Use `reqwest` and `tokio` in `pokedex` example --- examples/pokedex/Cargo.toml | 6 +++--- examples/pokedex/src/main.rs | 14 +++++++------- 2 files changed, 10 insertions(+), 10 deletions(-) (limited to 'examples') diff --git a/examples/pokedex/Cargo.toml b/examples/pokedex/Cargo.toml index 76a3a82f..b96eda91 100644 --- a/examples/pokedex/Cargo.toml +++ b/examples/pokedex/Cargo.toml @@ -6,9 +6,9 @@ edition = "2018" publish = false [dependencies] -iced = { path = "../..", features = ["image"] } -iced_futures = { path = "../../futures", features = ["async-std"] } -surf = "1.0" +iced = { path = "../..", features = ["image", "debug"] } +iced_futures = { path = "../../futures", features = ["tokio"] } +reqwest = { version = "0.10", features = ["json"] } rand = "0.7" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" diff --git a/examples/pokedex/src/main.rs b/examples/pokedex/src/main.rs index 283437b2..3c00d628 100644 --- a/examples/pokedex/src/main.rs +++ b/examples/pokedex/src/main.rs @@ -27,7 +27,7 @@ enum Message { } impl Application for Pokedex { - type Executor = iced_futures::executor::AsyncStd; + type Executor = iced_futures::executor::Tokio; type Message = Message; fn new() -> (Pokedex, Command) { @@ -175,8 +175,8 @@ impl Pokemon { let sprite = format!("https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/{}.png", id); let (entry, sprite): (Entry, _) = futures::future::try_join( - surf::get(&url).recv_json(), - surf::get(&sprite).recv_bytes(), + reqwest::get(&url).await?.json(), + reqwest::get(&sprite).await?.bytes(), ) .await?; @@ -195,7 +195,7 @@ impl Pokemon { .chars() .map(|c| if c.is_control() { ' ' } else { c }) .collect(), - image: image::Handle::from_memory(sprite), + image: image::Handle::from_memory(sprite.as_ref().to_vec()), }) } } @@ -206,9 +206,9 @@ enum Error { LanguageError, } -impl From for Error { - fn from(exception: surf::Exception) -> Error { - dbg!(&exception); +impl From for Error { + fn from(error: reqwest::Error) -> Error { + dbg!(&error); Error::APIError } -- cgit From 9df3fb13c7b0372f64ff7bd9073769833689de5b Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 5 Feb 2020 04:15:45 +0100 Subject: Make `pokedex` example work on Wasm --- examples/pokedex/Cargo.toml | 9 +++++++-- examples/pokedex/src/main.rs | 39 ++++++++++++++++++++++++++++++--------- 2 files changed, 37 insertions(+), 11 deletions(-) (limited to 'examples') diff --git a/examples/pokedex/Cargo.toml b/examples/pokedex/Cargo.toml index b96eda91..f8668be0 100644 --- a/examples/pokedex/Cargo.toml +++ b/examples/pokedex/Cargo.toml @@ -8,7 +8,12 @@ publish = false [dependencies] iced = { path = "../..", features = ["image", "debug"] } iced_futures = { path = "../../futures", features = ["tokio"] } -reqwest = { version = "0.10", features = ["json"] } -rand = "0.7" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" +rand = { version = "0.7", features = ["wasm-bindgen"] } + +[dependencies.reqwest] +version = "0.10" +git = "https://github.com/hecrj/reqwest.git" +branch = "feature/wasm-deserialize-json" +features = ["json"] diff --git a/examples/pokedex/src/main.rs b/examples/pokedex/src/main.rs index 3c00d628..13e420a9 100644 --- a/examples/pokedex/src/main.rs +++ b/examples/pokedex/src/main.rs @@ -27,7 +27,12 @@ enum Message { } impl Application for Pokedex { + #[cfg(not(target_arch = "wasm32"))] type Executor = iced_futures::executor::Tokio; + + #[cfg(target_arch = "wasm32")] + type Executor = iced_futures::executor::WasmBindgen; + type Message = Message; fn new() -> (Pokedex, Command) { @@ -166,19 +171,21 @@ impl Pokemon { } let id = { - let mut rng = rand::thread_rng(); + let mut rng = rand::rngs::OsRng::default(); rng.gen_range(0, Pokemon::TOTAL) }; - let url = format!("https://pokeapi.co/api/v2/pokemon-species/{}", id); - let sprite = format!("https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/{}.png", id); + let fetch_entry = async { + let url = + format!("https://pokeapi.co/api/v2/pokemon-species/{}", id); - let (entry, sprite): (Entry, _) = futures::future::try_join( - reqwest::get(&url).await?.json(), - reqwest::get(&sprite).await?.bytes(), - ) - .await?; + reqwest::get(&url).await?.json().await + }; + + let (entry, image): (Entry, _) = + futures::future::try_join(fetch_entry, Self::fetch_image(id)) + .await?; let description = entry .flavor_text_entries @@ -195,9 +202,23 @@ impl Pokemon { .chars() .map(|c| if c.is_control() { ' ' } else { c }) .collect(), - image: image::Handle::from_memory(sprite.as_ref().to_vec()), + image, }) } + + async fn fetch_image(id: u16) -> Result { + let url = format!("https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/{}.png", id); + + #[cfg(not(target_arch = "wasm32"))] + { + let bytes = reqwest::get(&url).await?.bytes().await?; + + Ok(image::Handle::from_memory(bytes.as_ref().to_vec())) + } + + #[cfg(target_arch = "wasm32")] + Ok(image::Handle::from_path(url)) + } } #[derive(Debug, Clone)] -- cgit From acfc815e1df7209d1e43f36e5f465b0ac44294cc Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 6 Feb 2020 01:24:04 +0100 Subject: Let checkbox breathe in `styling` example --- examples/styling/src/main.rs | 1 + 1 file changed, 1 insertion(+) (limited to 'examples') diff --git a/examples/styling/src/main.rs b/examples/styling/src/main.rs index 50095ec7..47408624 100644 --- a/examples/styling/src/main.rs +++ b/examples/styling/src/main.rs @@ -104,6 +104,7 @@ impl Sandbox for Styling { "Toggle me!", Message::CheckboxToggled, ) + .width(Length::Fill) .style(self.theme); let content = Column::new() -- cgit 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') 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/pokedex/Cargo.toml | 3 +-- examples/pokedex/src/main.rs | 7 +------ examples/todos/Cargo.toml | 3 +-- examples/todos/src/main.rs | 7 +------ 4 files changed, 4 insertions(+), 16 deletions(-) (limited to 'examples') diff --git a/examples/pokedex/Cargo.toml b/examples/pokedex/Cargo.toml index f8668be0..c1e3edb5 100644 --- a/examples/pokedex/Cargo.toml +++ b/examples/pokedex/Cargo.toml @@ -6,8 +6,7 @@ edition = "2018" publish = false [dependencies] -iced = { path = "../..", features = ["image", "debug"] } -iced_futures = { path = "../../futures", features = ["tokio"] } +iced = { path = "../..", features = ["image", "debug", "tokio"] } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" rand = { version = "0.7", features = ["wasm-bindgen"] } diff --git a/examples/pokedex/src/main.rs b/examples/pokedex/src/main.rs index 13e420a9..b0cb3c55 100644 --- a/examples/pokedex/src/main.rs +++ b/examples/pokedex/src/main.rs @@ -27,12 +27,7 @@ enum Message { } impl Application for Pokedex { - #[cfg(not(target_arch = "wasm32"))] - type Executor = iced_futures::executor::Tokio; - - #[cfg(target_arch = "wasm32")] - type Executor = iced_futures::executor::WasmBindgen; - + type Executor = iced::executor::Default; type Message = Message; fn new() -> (Pokedex, Command) { 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 f719ba3f4efebdf29cd5922d9e083f8dea96c486 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 6 Feb 2020 05:03:11 +0100 Subject: Add `font-family` to `Text` style in `iced_web` --- examples/pokedex/src/main.rs | 1 + 1 file changed, 1 insertion(+) (limited to 'examples') diff --git a/examples/pokedex/src/main.rs b/examples/pokedex/src/main.rs index b0cb3c55..4449b901 100644 --- a/examples/pokedex/src/main.rs +++ b/examples/pokedex/src/main.rs @@ -79,6 +79,7 @@ impl Application for Pokedex { fn view(&mut self) -> Element { let content = match self { Pokedex::Loading => Column::new() + .width(Length::Shrink) .push(Text::new("Searching for Pokémon...").size(40)), Pokedex::Loaded { pokemon, search } => Column::new() .max_width(500) -- cgit From 679d758627f6500de4b2220c0dba3460871b83b4 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 6 Feb 2020 05:23:05 +0100 Subject: Remove `wasm_bindgen(start)` from `tour` example --- examples/tour/Cargo.toml | 3 --- examples/tour/src/main.rs | 13 ------------- 2 files changed, 16 deletions(-) (limited to 'examples') diff --git a/examples/tour/Cargo.toml b/examples/tour/Cargo.toml index 45105c31..96749e90 100644 --- a/examples/tour/Cargo.toml +++ b/examples/tour/Cargo.toml @@ -8,6 +8,3 @@ publish = false [dependencies] iced = { path = "../..", features = ["image", "debug"] } env_logger = "0.7" - -[target.'cfg(target_arch = "wasm32")'.dependencies] -wasm-bindgen = "0.2.51" diff --git a/examples/tour/src/main.rs b/examples/tour/src/main.rs index 43c7e50f..800254ed 100644 --- a/examples/tour/src/main.rs +++ b/examples/tour/src/main.rs @@ -779,16 +779,3 @@ mod style { } } } - -// This should be gracefully handled by Iced in the future. Probably using our -// own proc macro, or maybe the whole process is streamlined by `wasm-pack` at -// some point. -#[cfg(target_arch = "wasm32")] -mod wasm { - use wasm_bindgen::prelude::*; - - #[wasm_bindgen(start)] - pub fn run() { - super::main() - } -} -- 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') 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