Add localStorage accessor for storing/retrieving volume between sessions, script execution

This commit is contained in:
2024-04-23 20:21:31 -05:00
parent 0d9f9099a4
commit 3131d6ee52
3 changed files with 80 additions and 11 deletions

54
src/store.rs Normal file
View File

@@ -0,0 +1,54 @@
pub struct Store;
#[cfg(not(target_os = "emscripten"))]
impl Store {
pub fn volume(&self) -> Option<u32> {
None
}
pub fn set_volume(&self, volume: u32) {
// Do nothing
}
}
#[cfg(target_os = "emscripten")]
extern "C" {
pub fn emscripten_run_script(script: *const libc::c_char);
pub fn emscripten_run_script_string(script: *const libc::c_char) -> *mut libc::c_char;
}
#[cfg(target_os = "emscripten")]
impl Store {
fn run_script(script: &str) {
use std::ffi::CString;
let script = CString::new(script).unwrap();
unsafe {
emscripten_run_script(script.as_ptr());
}
}
fn run_script_string(script: &str) -> String {
use std::ffi::{CStr, CString};
let script = CString::new(script).unwrap();
unsafe {
let ptr = emscripten_run_script_string(script.as_ptr());
let c_str = CStr::from_ptr(ptr);
String::from(c_str.to_str().unwrap())
}
}
pub fn volume(&self) -> Option<u32> {
// Use local storage to try and read volume
let script = "localStorage.getItem('volume')";
let response = Store::run_script_string(&script);
response.parse::<u32>().ok()
}
pub fn set_volume(&self, volume: u32) {
// Use local storage to set volume
let script = format!("localStorage.setItem('volume', '{}')", volume);
Store::run_script_string(&script);
}
}