From ea38f610584d505f175c02fbf2291547d8102613 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kav=C3=ADk?= Date: Sat, 1 Jun 2024 19:39:19 +0200 Subject: [PATCH] platform --- Cargo.lock | 5 ++ Cargo.toml | 1 + frontend/Cargo.toml | 5 ++ frontend/src/controls_panel.rs | 9 ++- frontend/src/main.rs | 4 +- frontend/src/platform.rs | 33 +++++++++++ frontend/src/platform/browser.rs | 58 +++++++++++++++++++ .../{tauri_bridge.rs => platform/tauri.rs} | 12 ++-- frontend/src/waveform_panel.rs | 6 +- shared/Cargo.toml | 3 + shared/src/lib.rs | 2 +- {src-tauri => shared}/src/wellen_helpers.rs | 0 src-tauri/Cargo.toml | 1 + src-tauri/src/lib.rs | 3 +- 14 files changed, 123 insertions(+), 19 deletions(-) create mode 100644 frontend/src/platform.rs create mode 100644 frontend/src/platform/browser.rs rename frontend/src/{tauri_bridge.rs => platform/tauri.rs} (72%) rename {src-tauri => shared}/src/wellen_helpers.rs (100%) diff --git a/Cargo.lock b/Cargo.lock index 54b2d6d..89dd31c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1182,6 +1182,7 @@ version = "0.1.0" dependencies = [ "serde", "serde_json", + "shared", "tauri", "tauri-build", "tauri-plugin-window-state", @@ -1263,6 +1264,7 @@ dependencies = [ name = "frontend" version = "0.1.0" dependencies = [ + "shared", "wasm-bindgen-test", "wellen", "zoon", @@ -3753,6 +3755,9 @@ dependencies = [ [[package]] name = "shared" version = "0.1.0" +dependencies = [ + "wellen", +] [[package]] name = "signal-hook-registry" diff --git a/Cargo.toml b/Cargo.toml index edd297b..f7fe8b2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,7 @@ readme = "../README.md" publish = false [workspace.dependencies] +shared = { path = "./shared" } # wellen = { version = "0.9.9", features = ["serde1"] } # wellen = { path = "../wellen/wellen", features = ["serde1"] } wellen = { git = "https://github.com/MartinKavik/wellen", features = ["serde1"], branch = "new_pub_types" } diff --git a/frontend/Cargo.toml b/frontend/Cargo.toml index 4c8a1a1..206f980 100644 --- a/frontend/Cargo.toml +++ b/frontend/Cargo.toml @@ -11,6 +11,11 @@ publish.workspace = true wasm-bindgen-test = "0.3.19" [dependencies] +shared.workspace = true zoon.workspace = true wellen.workspace = true +[features] +platform_browser = [] +platform_tauri = [] + diff --git a/frontend/src/controls_panel.rs b/frontend/src/controls_panel.rs index 2e8f723..3a33f27 100644 --- a/frontend/src/controls_panel.rs +++ b/frontend/src/controls_panel.rs @@ -1,5 +1,4 @@ -use crate::tauri_bridge; -use crate::{HierarchyAndTimeTable, Layout}; +use crate::{platform, HierarchyAndTimeTable, Layout}; use futures_util::join; use std::mem; use std::ops::Not; @@ -143,10 +142,10 @@ impl ControlsPanel { drop(hierarchy_and_time_table_lock); let hierarchy_and_time_table = hierarchy_and_time_table.clone(); Task::start(async move { - tauri_bridge::load_waveform(test_file_name).await; + platform::load_waveform(test_file_name).await; let (hierarchy, time_table) = join!( - tauri_bridge::get_hierarchy(), - tauri_bridge::get_time_table() + platform::get_hierarchy(), + platform::get_time_table() ); hierarchy_and_time_table.set(Some((Rc::new(hierarchy), Rc::new(time_table)))) }) diff --git a/frontend/src/main.rs b/frontend/src/main.rs index 94629ac..de4b4c1 100644 --- a/frontend/src/main.rs +++ b/frontend/src/main.rs @@ -1,7 +1,7 @@ use std::rc::Rc; use zoon::*; -mod tauri_bridge; +mod platform; mod controls_panel; use controls_panel::ControlsPanel; @@ -23,7 +23,7 @@ fn main() { Task::start(async { // https://github.com/tauri-apps/tauri/issues/5170 Timer::sleep(100).await; - tauri_bridge::show_window().await; + platform::show_window().await; }); } diff --git a/frontend/src/platform.rs b/frontend/src/platform.rs new file mode 100644 index 0000000..465c9f9 --- /dev/null +++ b/frontend/src/platform.rs @@ -0,0 +1,33 @@ +#[cfg(feature = "platform_tauri")] +mod tauri; +#[cfg(feature = "platform_tauri")] +use tauri as platform; + +#[cfg(feature = "platform_browser")] +mod browser; +#[cfg(feature = "platform_browser")] +use browser as platform; + +pub async fn show_window() { + platform::show_window().await +} + +pub async fn load_waveform(test_file_name: &'static str) { + platform::load_waveform(test_file_name).await +} + +pub async fn get_hierarchy() -> wellen::Hierarchy { + platform::get_hierarchy().await +} + +pub async fn get_time_table() -> wellen::TimeTable { + platform::get_time_table().await +} + +pub async fn load_and_get_signal(signal_ref: wellen::SignalRef) -> wellen::Signal { + platform::load_and_get_signal(signal_ref).await +} + +pub async fn unload_signal(signal_ref: wellen::SignalRef) { + platform::unload_signal(signal_ref).await +} diff --git a/frontend/src/platform/browser.rs b/frontend/src/platform/browser.rs new file mode 100644 index 0000000..1245fae --- /dev/null +++ b/frontend/src/platform/browser.rs @@ -0,0 +1,58 @@ +use zoon::*; +use std::sync::Mutex; +use wellen::simple::Waveform; +use shared::wellen_helpers; + +#[derive(Default)] +struct Store { + waveform: Mutex>, +} + +static STORE: Lazy = lazy::default(); + +pub(super) async fn show_window() {} + +pub(super) async fn load_waveform(test_file_name: &'static str) { + static SIMPLE_VCD: &'static [u8; 311] = include_bytes!("../../../test_files/simple.vcd"); + // static WAVE_27_FST: &'static [u8; 28860652] = include_bytes!("../../../test_files/wave_27.fst"); + let chosen_file = match test_file_name { + "simple.vcd" => SIMPLE_VCD.to_vec(), + // "wave_27.fst" => WAVE_27_FST.to_vec(), + test_file_name => todo!("add {test_file_name} to the `test_files` folder"), + }; + let waveform = wellen_helpers::read_from_bytes(chosen_file); + let Ok(waveform) = waveform else { + panic!("VCD file reading failed") + }; + *STORE.waveform.lock().unwrap_throw() = Some(waveform); +} + +pub(super) async fn get_hierarchy() -> wellen::Hierarchy { + let waveform = STORE.waveform.lock().unwrap_throw(); + let hierarchy = waveform.as_ref().unwrap_throw().hierarchy(); + // @TODO Wrap `hierarchy` in `Waveform` with `Rc/Arc` or add the method `take` / `clone` or refactor? + serde_json::from_value(serde_json::to_value(hierarchy).unwrap_throw()).unwrap_throw() +} + +pub(super) async fn get_time_table() -> wellen::TimeTable { + let waveform = STORE.waveform.lock().unwrap_throw(); + let time_table = waveform.as_ref().unwrap_throw().time_table(); + // @TODO Wrap `time_table` in `Waveform` with `Rc/Arc` or add the method `take` / `clone` or refactor? + serde_json::from_value(serde_json::to_value(time_table).unwrap_throw()).unwrap_throw() +} + +pub(super) async fn load_and_get_signal(signal_ref: wellen::SignalRef) -> wellen::Signal { + let mut waveform_lock = STORE.waveform.lock().unwrap_throw(); + let waveform = waveform_lock.as_mut().unwrap_throw(); + // @TODO maybe run it in a thread to not block the main one and then + waveform.load_signals(&[signal_ref]); + let signal = waveform.get_signal(signal_ref).unwrap_throw(); + // @TODO `clone` / `Rc/Arc` / refactor? + serde_json::from_value(serde_json::to_value(signal).unwrap_throw()).unwrap_throw() +} + +pub(super) async fn unload_signal(signal_ref: wellen::SignalRef) { + let mut waveform_lock = STORE.waveform.lock().unwrap_throw(); + let waveform = waveform_lock.as_mut().unwrap_throw(); + waveform.unload_signals(&[signal_ref]); +} diff --git a/frontend/src/tauri_bridge.rs b/frontend/src/platform/tauri.rs similarity index 72% rename from frontend/src/tauri_bridge.rs rename to frontend/src/platform/tauri.rs index 4358c01..0e803e8 100644 --- a/frontend/src/tauri_bridge.rs +++ b/frontend/src/platform/tauri.rs @@ -1,27 +1,27 @@ use zoon::*; -pub async fn show_window() { +pub(super) async fn show_window() { tauri_glue::show_window().await } -pub async fn load_waveform(test_file_name: &'static str) { +pub(super) async fn load_waveform(test_file_name: &'static str) { tauri_glue::load_waveform(test_file_name).await } -pub async fn get_hierarchy() -> wellen::Hierarchy { +pub(super) async fn get_hierarchy() -> wellen::Hierarchy { serde_wasm_bindgen::from_value(tauri_glue::get_hierarchy().await).unwrap_throw() } -pub async fn get_time_table() -> wellen::TimeTable { +pub(super) async fn get_time_table() -> wellen::TimeTable { serde_wasm_bindgen::from_value(tauri_glue::get_time_table().await).unwrap_throw() } -pub async fn load_and_get_signal(signal_ref: wellen::SignalRef) -> wellen::Signal { +pub(super) async fn load_and_get_signal(signal_ref: wellen::SignalRef) -> wellen::Signal { serde_wasm_bindgen::from_value(tauri_glue::load_and_get_signal(signal_ref.index()).await) .unwrap_throw() } -pub async fn unload_signal(signal_ref: wellen::SignalRef) { +pub(super) async fn unload_signal(signal_ref: wellen::SignalRef) { tauri_glue::unload_signal(signal_ref.index()).await } diff --git a/frontend/src/waveform_panel.rs b/frontend/src/waveform_panel.rs index ad090a0..80b2b39 100644 --- a/frontend/src/waveform_panel.rs +++ b/frontend/src/waveform_panel.rs @@ -1,4 +1,4 @@ -use crate::{tauri_bridge, HierarchyAndTimeTable}; +use crate::{platform, HierarchyAndTimeTable}; use wellen::GetItem; use zoon::{eprintln, *}; @@ -61,7 +61,7 @@ impl WaveformPanel { selected_var_refs.signal_vec().delay_remove(clone!((hierarchy_and_time_table) move |var_ref| { clone!((var_ref, hierarchy_and_time_table) async move { if let Some(hierarchy_and_time_table) = hierarchy_and_time_table.get_cloned() { - tauri_bridge::unload_signal(hierarchy_and_time_table.0.get(var_ref).signal_ref()).await; + platform::unload_signal(hierarchy_and_time_table.0.get(var_ref).signal_ref()).await; } }) })).for_each(clone!((controller, hierarchy_and_time_table) move |vec_diff| { @@ -117,7 +117,7 @@ impl WaveformPanel { let var = hierarchy.get(var_ref); let signal_ref = var.signal_ref(); - let signal = tauri_bridge::load_and_get_signal(signal_ref).await; + let signal = platform::load_and_get_signal(signal_ref).await; let timescale = hierarchy.timescale(); // @TODO remove diff --git a/shared/Cargo.toml b/shared/Cargo.toml index 8f63f9b..545e49d 100644 --- a/shared/Cargo.toml +++ b/shared/Cargo.toml @@ -6,3 +6,6 @@ repository.workspace = true authors.workspace = true readme.workspace = true publish.workspace = true + +[dependencies] +wellen.workspace = true diff --git a/shared/src/lib.rs b/shared/src/lib.rs index 8b13789..89b0b3b 100644 --- a/shared/src/lib.rs +++ b/shared/src/lib.rs @@ -1 +1 @@ - +pub mod wellen_helpers; diff --git a/src-tauri/src/wellen_helpers.rs b/shared/src/wellen_helpers.rs similarity index 100% rename from src-tauri/src/wellen_helpers.rs rename to shared/src/wellen_helpers.rs diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index fd54463..6a7573d 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -16,6 +16,7 @@ crate-type = ["staticlib", "cdylib", "rlib"] tauri-build = { version = "=2.0.0-beta.15", features = [] } [dependencies] +shared.workspace = true wellen.workspace = true serde_json = "1.0" serde = { version = "1.0", features = ["derive"] } diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 107b55e..f316644 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -1,8 +1,7 @@ use std::rc::Rc; use std::sync::Mutex; use wellen::simple::Waveform; - -mod wellen_helpers; +use shared::wellen_helpers; #[derive(Default)] struct Store {