FastWave2.0/src-tauri/src/lib.rs

163 lines
5.4 KiB
Rust
Raw Normal View History

2024-05-28 15:18:56 +00:00
use std::sync::Mutex;
2024-06-03 17:11:22 +00:00
use tauri_plugin_dialog::DialogExt;
2024-05-27 19:24:46 +00:00
use wellen::simple::Waveform;
2024-06-03 17:11:22 +00:00
type Filename = String;
2024-05-27 19:24:46 +00:00
#[derive(Default)]
struct Store {
waveform: Mutex<Option<Waveform>>,
}
#[tauri::command(rename_all = "snake_case")]
2024-06-03 17:11:22 +00:00
async fn show_window(window: tauri::Window) {
2024-05-27 19:24:46 +00:00
window.show().unwrap();
}
#[tauri::command(rename_all = "snake_case")]
2024-06-03 17:11:22 +00:00
async fn pick_and_load_waveform(
store: tauri::State<'_, Store>,
app: tauri::AppHandle,
) -> Result<Option<Filename>, ()> {
let Some(file_response) = app.dialog().file().blocking_pick_file() else {
return Ok(None);
2024-05-28 10:57:51 +00:00
};
2024-06-03 17:11:22 +00:00
let file_path = file_response.path.as_os_str().to_str().unwrap();
// @TODO `read` should accept `Path` instead of `&str`
let waveform = wellen::simple::read(file_path);
2024-05-27 19:24:46 +00:00
let Ok(waveform) = waveform else {
2024-06-03 17:11:22 +00:00
panic!("Waveform file reading failed")
2024-05-27 19:24:46 +00:00
};
*store.waveform.lock().unwrap() = Some(waveform);
2024-06-03 17:11:22 +00:00
Ok(Some(file_response.name.unwrap()))
2024-05-27 19:24:46 +00:00
}
#[tauri::command(rename_all = "snake_case")]
2024-06-03 17:11:22 +00:00
async fn get_hierarchy(store: tauri::State<'_, Store>) -> Result<serde_json::Value, ()> {
2024-05-27 19:24:46 +00:00
let waveform = store.waveform.lock().unwrap();
let hierarchy = waveform.as_ref().unwrap().hierarchy();
2024-06-03 17:11:22 +00:00
Ok(serde_json::to_value(hierarchy).unwrap())
2024-05-27 19:24:46 +00:00
}
#[tauri::command(rename_all = "snake_case")]
async fn load_signal_and_get_timeline(
2024-06-06 20:04:57 +00:00
signal_ref_index: usize,
screen_width: u32,
2024-06-06 20:45:20 +00:00
block_height: u32,
2024-06-06 20:04:57 +00:00
store: tauri::State<'_, Store>,
) -> Result<serde_json::Value, ()> {
2024-06-07 21:18:06 +00:00
// @TODO run (all?) in a blocking thread?
2024-06-06 20:04:57 +00:00
let signal_ref = wellen::SignalRef::from_index(signal_ref_index).unwrap();
let mut waveform_lock = store.waveform.lock().unwrap();
let waveform = waveform_lock.as_mut().unwrap();
waveform.load_signals_multi_threaded(&[signal_ref]);
let signal = waveform.get_signal(signal_ref).unwrap();
2024-06-06 20:45:20 +00:00
let time_table = waveform.time_table();
let timeline = signal_to_timeline(signal, time_table, screen_width, block_height);
2024-06-06 20:04:57 +00:00
Ok(serde_json::to_value(timeline).unwrap())
}
2024-05-27 19:24:46 +00:00
#[tauri::command(rename_all = "snake_case")]
2024-06-03 17:11:22 +00:00
async fn unload_signal(signal_ref_index: usize, store: tauri::State<'_, Store>) -> Result<(), ()> {
2024-05-27 19:24:46 +00:00
let signal_ref = wellen::SignalRef::from_index(signal_ref_index).unwrap();
let mut waveform_lock = store.waveform.lock().unwrap();
let waveform = waveform_lock.as_mut().unwrap();
waveform.unload_signals(&[signal_ref]);
2024-06-03 17:11:22 +00:00
Ok(())
2024-05-27 19:24:46 +00:00
}
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
// https://github.com/tauri-apps/tauri/issues/8462
#[cfg(target_os = "linux")]
std::env::set_var("WEBKIT_DISABLE_DMABUF_RENDERER", "1");
tauri::Builder::default()
.manage(Store::default())
.plugin(tauri_plugin_window_state::Builder::default().build())
2024-06-03 17:11:22 +00:00
.plugin(tauri_plugin_dialog::init())
2024-05-27 19:24:46 +00:00
// Npte: Add all handlers to `frontend/src/tauri_bridge.rs`
.invoke_handler(tauri::generate_handler![
show_window,
2024-06-03 17:11:22 +00:00
pick_and_load_waveform,
2024-05-27 19:24:46 +00:00
get_hierarchy,
load_signal_and_get_timeline,
2024-05-27 19:24:46 +00:00
unload_signal,
])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
2024-06-06 20:45:20 +00:00
fn signal_to_timeline(
2024-06-06 23:44:09 +00:00
signal: &wellen::Signal,
2024-06-06 20:45:20 +00:00
time_table: &[wellen::Time],
screen_width: u32,
block_height: u32,
) -> shared::Timeline {
const MIN_BLOCK_WIDTH: u32 = 3;
const LETTER_WIDTH: u32 = 15;
const LETTER_HEIGHT: u32 = 21;
const LABEL_X_PADDING: u32 = 10;
let Some(last_time) = time_table.last().copied() else {
2024-06-06 23:44:09 +00:00
return shared::Timeline::default();
2024-06-06 20:45:20 +00:00
};
let last_time = last_time as f64;
let screen_width = screen_width as f64;
let mut x_value_pairs = signal
.iter_changes()
.map(|(index, value)| {
let index = index as usize;
let time = time_table[index] as f64;
let x = time / last_time * screen_width;
(x, value)
})
.peekable();
2024-06-07 23:11:18 +00:00
// @TODO parallelize?
2024-06-06 20:45:20 +00:00
let mut blocks = Vec::new();
while let Some((block_x, value)) = x_value_pairs.next() {
let next_block_x = if let Some((next_block_x, _)) = x_value_pairs.peek() {
*next_block_x
} else {
screen_width
};
let block_width = (next_block_x - block_x) as u32;
if block_width < MIN_BLOCK_WIDTH {
continue;
2024-06-06 23:44:09 +00:00
}
2024-06-06 20:45:20 +00:00
// @TODO dynamic formatter
2024-06-07 23:11:18 +00:00
// @TODO optimize it by not using `.to_string` if possible
let value = value.to_string();
let ones_and_zeros = value.chars().rev().map(|char| char.to_digit(2).unwrap()).collect::<Vec<_>>();
let mut base = convert_base::Convert::new(2, 16);
let output = base.convert::<u32, u32>(&ones_and_zeros);
let value: String = output.into_iter().map(|number| char::from_digit(number, 16).unwrap()).collect();
2024-06-06 20:45:20 +00:00
let value_width = value.chars().count() as u32 * LETTER_WIDTH;
let label = if (value_width + (2 * LABEL_X_PADDING)) <= block_width {
Some(shared::TimeLineBlockLabel {
text: value,
x: (block_width - value_width) / 2,
y: (block_height - LETTER_HEIGHT) / 2,
})
} else {
None
};
let block = shared::TimelineBlock {
x: block_x as u32,
width: block_width,
height: block_height,
2024-06-06 23:44:09 +00:00
label,
2024-06-06 20:45:20 +00:00
};
blocks.push(block);
}
shared::Timeline { blocks }
}