commit 44c3b3a0377df8c1862f6a6fabbb6e774372b4e4
parent b5d4f6b594d3d9e27beb3c4cdeaa0995764b705b
Author: Sylvia Ivory <git@sivory.net>
Date: Thu, 19 Jun 2025 22:21:57 -0700
Begin wasm compatibility
Diffstat:
6 files changed, 85 insertions(+), 12 deletions(-)
diff --git a/Cargo.lock b/Cargo.lock
@@ -724,6 +724,26 @@ dependencies = [
]
[[package]]
+name = "console_error_panic_hook"
+version = "0.1.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a06aeb73f470f66dcdbf7223caeebb85984942f22f1adb2a088cf9668146bbbc"
+dependencies = [
+ "cfg-if",
+ "wasm-bindgen",
+]
+
+[[package]]
+name = "console_log"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "be8aed40e4edbf4d3b4431ab260b63fdc40f5780a4766824329ea0f1eefe3c0f"
+dependencies = [
+ "log",
+ "web-sys",
+]
+
+[[package]]
name = "core-foundation"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1312,6 +1332,8 @@ version = "0.1.0"
dependencies = [
"anyhow",
"chrono",
+ "console_error_panic_hook",
+ "console_log",
"directories",
"env_logger",
"fjordgard-unsplash",
@@ -4700,9 +4722,7 @@ dependencies = [
"bytes",
"libc",
"mio",
- "parking_lot 0.12.4",
"pin-project-lite",
- "signal-hook-registry",
"socket2",
"tokio-macros",
"windows-sys 0.52.0",
diff --git a/Cargo.toml b/Cargo.toml
@@ -13,14 +13,21 @@ edition = "2024"
anyhow = "1.0.98"
chrono = "0.4.41"
directories = "6.0.0"
-env_logger = "0.11.8"
fjordgard-unsplash = { version = "0.1.0", path = "crates/unsplash" }
fjordgard-weather = { version = "0.1.0", path = "crates/weather" }
iced = { version = "0.13.1", features = ["tokio", "canvas", "image", "svg"] }
log = "0.4.27"
-open = "5.3.2"
rfd = "0.15.3"
serde = { version = "1.0.219", features = ["derive"] }
serde_json = "1.0.140"
strum = { version = "0.27.1", features = ["derive"] }
-tokio = { version = "1.45.1", features = ["full"] }
+
+[target.'cfg(not(target_arch="wasm32"))'.dependencies]
+tokio = { version = "1.45.1", features = ["rt-multi-thread", "fs", "time"] }
+env_logger = "0.11.8"
+open = "5.3.2"
+
+[target.'cfg(target_arch="wasm32")'.dependencies]
+tokio = { version = "1.45.1", features = ["rt", "time"] }
+console_error_panic_hook = "0.1.7"
+console_log = "1.0.0"
diff --git a/src/background.rs b/src/background.rs
@@ -7,7 +7,6 @@ use iced::{
widget::{button, canvas, container, image, row, stack, text},
};
use log::{debug, error};
-use tokio::fs;
use crate::config::{BackgroundMode, Config};
@@ -109,10 +108,17 @@ impl BackgroundHandle {
match self.mode {
BackgroundMode::Local => {
- let path = self.background.clone();
+ #[cfg(not(target_arch = "wasm32"))]
+ {
+ let path = self.background.clone();
- Task::future(async move { fs::read(&path).await })
- .map(|r| Message::BackgroundRead(r.map_err(|e| e.to_string())))
+ Task::future(async move { tokio::fs::read(&path).await })
+ .map(|r| Message::BackgroundRead(r.map_err(|e| e.to_string())))
+ }
+ #[cfg(target_arch = "wasm32")]
+ {
+ Task::none()
+ }
}
BackgroundMode::Unsplash => {
if !refresh_unsplash {
@@ -272,10 +278,16 @@ impl BackgroundHandle {
}
}
Message::OpenUrl(url) => {
+ #[cfg(not(target_arch = "wasm32"))]
if let Err(e) = open::that_detached(url) {
error!("failed to open link: {e}")
}
+ #[cfg(target_arch = "wasm32")]
+ {
+ error!("open not implemented, {url}");
+ }
+
Task::none()
}
}
diff --git a/src/config.rs b/src/config.rs
@@ -1,6 +1,5 @@
use std::fs;
-use anyhow::bail;
use directories::ProjectDirs;
use serde::{Deserialize, Serialize};
@@ -65,6 +64,7 @@ impl Config {
}
}
+ #[cfg(not(target_arch = "wasm32"))]
pub async fn save(&self) -> anyhow::Result<()> {
if let Some(dir) = ProjectDirs::from("net.sivory", "", "fjordgard") {
let config_dir = dir.config_dir();
@@ -76,9 +76,14 @@ impl Config {
Ok(())
} else {
- bail!("no config directory found")
+ anyhow::bail!("no config directory found")
}
}
+
+ #[cfg(target_arch = "wasm32")]
+ pub async fn save(&self) -> anyhow::Result<()> {
+ Ok(())
+ }
}
impl Default for Config {
diff --git a/src/main.rs b/src/main.rs
@@ -197,6 +197,10 @@ impl Fjordgard {
background_task
}
}
+ #[cfg(target_arch = "wasm32")]
+ Message::Settings(settings::Message::ToBackground(msg)) => {
+ Task::done(Message::Background(msg))
+ }
Message::Settings(msg) => {
if let Some(settings) = &mut self.settings_window {
settings.update(msg).map(Message::Settings)
@@ -406,6 +410,7 @@ impl Fjordgard {
}
}
+#[cfg(not(target_arch = "wasm32"))]
fn main() -> iced::Result {
env_logger::init();
@@ -413,3 +418,11 @@ fn main() -> iced::Result {
.subscription(Fjordgard::subscription)
.run_with(Fjordgard::new)
}
+
+#[cfg(target_arch = "wasm32")]
+fn main() -> iced::Result {
+ std::panic::set_hook(Box::new(console_error_panic_hook::hook));
+ console_log::init_with_level(log::Level::Info).unwrap();
+
+ Ok(())
+}
diff --git a/src/settings.rs b/src/settings.rs
@@ -66,6 +66,9 @@ pub enum Message {
Committed,
Saved(Result<(), String>),
+
+ #[cfg(target_arch = "wasm32")]
+ ToBackground(crate::background::Message),
}
impl Settings {
@@ -212,7 +215,20 @@ impl Settings {
self.file_selector_open = false;
if let Some(file) = file {
- self.background = file.path().to_string_lossy().to_string();
+ #[cfg(not(target_arch = "wasm32"))]
+ {
+ self.background = file.path().to_string_lossy().to_string();
+ }
+ #[cfg(target_arch = "wasm32")]
+ {
+ self.background = file.file_name();
+
+ let f = file.clone();
+
+ return Task::future(async move { f.read().await }).map(|r| {
+ Message::ToBackground(crate::background::Message::BackgroundRead(Ok(r)))
+ });
+ }
}
Task::none()