Files
yt2press/src-tauri/src/lib.rs
T

59 lines
1.7 KiB
Rust

use tauri::Manager;
use std::{process::{Child, Command}, sync::Mutex};
struct State {
child: Mutex<Option<Child>>,
}
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
tauri::Builder::default()
.manage(State {
child: Mutex::new(None),
})
.setup(|app| {
if cfg!(debug_assertions) {
app.handle().plugin(
tauri_plugin_log::Builder::default()
.level(log::LevelFilter::Info)
.build(),
)?;
}
#[cfg(target_os = "windows")]
{
use std::{env, process::Stdio};
let cwd = env::current_dir()?;
let p = cwd.join("..").join("server.js");
println!("{:?}", p);
let child = Command::new("node")
.arg(p)
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.spawn()
.expect("spawn ws-server");
let state = app.handle().state::<State>();
*state.child.lock().unwrap() = Some(child);
}
Ok(())
})
.build(tauri::generate_context!())
.expect("error while running tauri application")
.run(|app_handle, event| match event {
// On exit handler should be avoided for mobile
#[cfg(target_os = "windows")]
tauri::RunEvent::ExitRequested { .. } => {
if let Some(mut child) = app_handle.state::<State>().child.lock().unwrap().take() {
let _ = child.kill().expect("ws-server not killed");
}
}
_ => {}
});
}