From 9596730e52ccf5d385fa151d8bcc1eabe8ef90f3 Mon Sep 17 00:00:00 2001 From: Melody Horn Date: Sat, 4 Dec 2021 18:27:40 -0700 Subject: remember the queue position --- src/lib.rs | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e90202d..bb0fc76 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,6 +4,7 @@ use std::rc::Rc; use futures::channel::mpsc; use futures::prelude::*; +use gloo::storage::{LocalStorage, Storage as _}; use wasm_bindgen::prelude::*; use wasm_bindgen::JsCast; use wasm_bindgen_futures::*; @@ -48,7 +49,7 @@ fn update_target_marker( width: &Rc>, height: &Rc>, video: &HtmlVideoElement, - queue_position: &Rc>, + queue_position: &QueuePosition, ) { let (real_x, real_y) = queue_position.get(); let real_width = width.get() as f64; @@ -74,13 +75,38 @@ fn update_target_marker( .unwrap(); } +#[derive(Clone)] +struct QueuePosition { + inner: Rc>, +} + +impl QueuePosition { + const STORAGE_KEY: &'static str = "queue-position"; + + fn new() -> Self { + let values = LocalStorage::get(Self::STORAGE_KEY).unwrap_or((920, 520)); + Self { + inner: Rc::new(Cell::new(values)), + } + } + + fn set(&self, data: (u32, u32)) { + LocalStorage::set(Self::STORAGE_KEY, data).unwrap(); + self.inner.replace(data); + } + + fn get(&self) -> (u32, u32) { + self.inner.get() + } +} + async fn do_boot() { let width = Rc::new(Cell::new(0)); let height = Rc::new(Cell::new(0)); let streaming = Rc::new(Cell::new(false)); - let queue_position = Rc::new(Cell::new((920u32, 520u32))); + let queue_position = QueuePosition::new(); let document = gloo::utils::document(); @@ -124,7 +150,7 @@ async fn do_boot() { let fake_height = video.offset_height() as f64; let real_x = mouse_x / fake_width * real_width - QUEUE_SIZE_WIDTH as f64 / 2.0; let real_y = mouse_y / fake_height * real_height - QUEUE_SIZE_HEIGHT as f64 / 2.0; - queue_position.replace((real_x.round() as u32, real_y.round() as u32)); + queue_position.set((real_x.round() as u32, real_y.round() as u32)); update_target_marker(&target_marker, &width, &height, &video, &queue_position); spawn_local(async move { manual_update_tx.send(()).await.unwrap() }); }) -- cgit v1.2.3