feat: initial Tauri desktop shell for BatchTube v1.0
This commit is contained in:
8
src-tauri/src/lib.rs
Normal file
8
src-tauri/src/lib.rs
Normal file
@@ -0,0 +1,8 @@
|
||||
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
||||
pub fn run() {
|
||||
tauri::Builder::default()
|
||||
.plugin(tauri_plugin_shell::init())
|
||||
.invoke_handler(tauri::generate_handler![])
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
}
|
||||
73
src-tauri/src/main.rs
Normal file
73
src-tauri/src/main.rs
Normal file
@@ -0,0 +1,73 @@
|
||||
// Prevents additional console window on Windows in release
|
||||
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
||||
|
||||
use std::sync::Mutex;
|
||||
use tauri::{Manager, State};
|
||||
use tauri_plugin_shell::ShellExt;
|
||||
|
||||
struct ServerProcess(Mutex<Option<tauri_plugin_shell::process::CommandChild>>);
|
||||
|
||||
#[tauri::command]
|
||||
async fn get_server_status() -> String {
|
||||
match reqwest::get("http://localhost:8765/api/health").await {
|
||||
Ok(resp) if resp.status().is_success() => "ready".to_string(),
|
||||
_ => "starting".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
tauri::Builder::default()
|
||||
.plugin(tauri_plugin_shell::init())
|
||||
.setup(|app| {
|
||||
let handle = app.handle().clone();
|
||||
|
||||
// Spawn the Python backend sidecar
|
||||
let sidecar_command = app.shell().sidecar("batchtube-server").unwrap();
|
||||
let (mut _rx, child) = sidecar_command
|
||||
.spawn()
|
||||
.expect("Failed to spawn BatchTube server");
|
||||
|
||||
// Store process handle for cleanup
|
||||
app.manage(ServerProcess(Mutex::new(Some(child))));
|
||||
|
||||
// Wait for server to be ready, then show window
|
||||
tauri::async_runtime::spawn(async move {
|
||||
let mut attempts = 0;
|
||||
loop {
|
||||
tokio::time::sleep(tokio::time::Duration::from_millis(500)).await;
|
||||
if let Ok(resp) = reqwest::get("http://localhost:8765/api/health").await {
|
||||
if resp.status().is_success() {
|
||||
// Server ready — show the main window
|
||||
if let Some(window) = handle.get_webview_window("main") {
|
||||
window.show().unwrap();
|
||||
window.set_focus().unwrap();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
attempts += 1;
|
||||
if attempts > 30 {
|
||||
eprintln!("BatchTube server failed to start after 15 seconds");
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Ok(())
|
||||
})
|
||||
.on_window_event(|window, event| {
|
||||
if let tauri::WindowEvent::CloseRequested { .. } = event {
|
||||
// Kill the backend server on window close
|
||||
if let Some(server) = window.app_handle().try_state::<ServerProcess>() {
|
||||
if let Ok(mut guard) = server.0.lock() {
|
||||
if let Some(child) = guard.take() {
|
||||
let _ = child.kill();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
.invoke_handler(tauri::generate_handler![get_server_status])
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running BatchTube");
|
||||
}
|
||||
Reference in New Issue
Block a user