Skip to content

Commit

Permalink
🌐(wasm): support URL transfer data
Browse files Browse the repository at this point in the history
Use history feature to transfer data from different user and devices

Signed-off-by: Benign X <[email protected]>
  • Loading branch information
W-Mai committed Nov 18, 2024
1 parent 4020366 commit bdde589
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ log = "0.4.19"
levenshtein = "1.0.5"

dyn-clone = "1.0.16"
web-sys = "0.3.72"
web-sys = { version = "0.3.72", features = ["History"] }
serde = { version = "1.0.215", features = ["derive"] }

# native:
[target.'cfg(all(not(target_arch = "wasm32"), platform = "macos"))'.dependencies]
Expand All @@ -54,8 +55,10 @@ eframe = { version = "0.29.1", default-features = false, features = [
"__screenshot",
"default_fonts",
"glow",
"persistence"
] }
wasm-bindgen-futures = "0.4"
serde_qs = "0.13.0"

[profile.release]
opt-level = 2 # fast and small wasm
Expand Down
49 changes: 47 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ use crate::cus_component::{toggle, CodeEditor};
use crate::interfaces::{
ICodeEditor, IParser, IVisData, IVisDataGenerator, IVisualizer, ParseError,
};
use eframe::egui;
use eframe::{egui, Storage};
use egui_extras::{Size, StripBuilder};
use log::error;
use serde::{Deserialize, Serialize};
use std::time::Duration;
use std::vec;

const DEFAULT_CODE: &str = include_str!("default_code");
Expand All @@ -20,7 +22,7 @@ struct MainAppCache {
params: MainAppParams,
}

#[derive(Clone, PartialEq)]
#[derive(Clone, PartialEq, Serialize, Deserialize)]
struct MainAppParams {
vis_progress: i64,
vis_progress_max: i64,
Expand All @@ -32,6 +34,12 @@ struct MainAppParams {
trans_matrix: [[f64; 3]; 3],
}

#[derive(Clone, PartialEq, Serialize, Deserialize)]
struct TransferData {
code: String,
params: MainAppParams,
}

impl Default for MainAppParams {
fn default() -> Self {
Self {
Expand Down Expand Up @@ -71,6 +79,9 @@ impl Default for MainApp {

impl eframe::App for MainApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
#[cfg(target_arch = "wasm32")]
self.load_from_url_search();

egui::Window::new("Options")
.fixed_size([600.0, 200.0])
.default_pos(ctx.available_rect().right_top() + egui::vec2(0.0, 30.0))
Expand Down Expand Up @@ -103,6 +114,15 @@ impl eframe::App for MainApp {
});
});
}

fn save(&mut self, _storage: &mut dyn Storage) {
#[cfg(target_arch = "wasm32")]
self.save_to_url_search();
}

fn auto_save_interval(&self) -> Duration {
Duration::from_millis(30)
}
}

impl MainApp {
Expand Down Expand Up @@ -358,3 +378,28 @@ impl MainApp {
});
}
}

#[cfg(target_arch = "wasm32")]
impl MainApp {
fn load_from_url_search(&mut self) {
use eframe::web::web_location;
let location = web_location();
if let Ok(t) = serde_qs::from_str::<TransferData>(&location.query) {
self.code = AnyData::new(t.code);
self.params = t.params;
}
}

fn save_to_url_search(&mut self) {
let history = web_sys::window().unwrap().history().unwrap();
let mut t = serde_qs::to_string(&TransferData {
code: self.code.cast_ref::<String>().clone(),
params: self.params.clone(),
})
.unwrap();
t.insert(0, '?');

use eframe::wasm_bindgen::JsValue;
history.push_state_with_url(&JsValue::NULL, "", Some(&t)).unwrap()
}
}

0 comments on commit bdde589

Please sign in to comment.