47 lines
1.8 KiB
Rust
47 lines
1.8 KiB
Rust
// SonicForge DAW — desktop shell: spawn/terminate daw_engine.exe sidecar.
|
|
use tauri::Manager;
|
|
use tauri_plugin_shell::process::CommandChild;
|
|
use tauri_plugin_shell::ShellExt;
|
|
use std::sync::Mutex;
|
|
|
|
struct EngineProcess(Mutex<Option<CommandChild>>);
|
|
|
|
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
|
pub fn run() {
|
|
tauri::Builder::default()
|
|
.plugin(tauri_plugin_shell::init())
|
|
.setup(|app| {
|
|
// 1. Spawn sidecar daw_engine.exe (PyInstaller bundle)
|
|
let sidecar_command = app
|
|
.shell()
|
|
.sidecar("daw_engine")
|
|
.expect("sidecar daw_engine not found — run build_windows.ps1 first");
|
|
let (_rx, child) = sidecar_command
|
|
.env("SF_PARENT_PID", std::process::id().to_string())
|
|
.spawn()
|
|
.expect("Failed to spawn daw_engine sidecar");
|
|
|
|
app.manage(EngineProcess(Mutex::new(Some(child))));
|
|
println!("Python Background Engine started (localhost:8000, auto-fallback 8000-8010)");
|
|
Ok(())
|
|
})
|
|
.on_window_event(|window, event| {
|
|
// 2. Terminate sidecar khi DAW window dong — tranh orphan process
|
|
if let tauri::WindowEvent::Destroyed = event {
|
|
// Lay child ra khoi lock, guard drop ngay tai day (trach E0597)
|
|
let child = window
|
|
.state::<EngineProcess>()
|
|
.0
|
|
.lock()
|
|
.ok()
|
|
.and_then(|mut lock| lock.take());
|
|
if let Some(child) = child {
|
|
let _ = child.kill();
|
|
println!("daw_engine sidecar terminated.");
|
|
}
|
|
}
|
|
})
|
|
.run(tauri::generate_context!())
|
|
.expect("error while running tauri application");
|
|
}
|