mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-30 06:02:00 +02:00
83 lines
2.4 KiB
Rust
83 lines
2.4 KiB
Rust
use log::error;
|
|
use serde::Serialize;
|
|
use std::sync::Mutex;
|
|
use tauri::{RunEvent, State};
|
|
use yaak_proxy::ProxyHandle;
|
|
use yaak_window::window::CreateWindowConfig;
|
|
|
|
#[derive(Serialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
struct ProxyMetadata {
|
|
name: String,
|
|
version: String,
|
|
}
|
|
|
|
#[derive(Default)]
|
|
struct ProxyState {
|
|
handle: Mutex<Option<ProxyHandle>>,
|
|
}
|
|
|
|
#[derive(Serialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
struct ProxyStartResult {
|
|
port: u16,
|
|
already_running: bool,
|
|
}
|
|
|
|
#[tauri::command]
|
|
fn proxy_metadata(app_handle: tauri::AppHandle) -> ProxyMetadata {
|
|
ProxyMetadata {
|
|
name: app_handle.package_info().name.clone(),
|
|
version: app_handle.package_info().version.to_string(),
|
|
}
|
|
}
|
|
|
|
#[tauri::command]
|
|
fn proxy_start(
|
|
state: State<'_, ProxyState>,
|
|
port: Option<u16>,
|
|
) -> Result<ProxyStartResult, String> {
|
|
let mut handle = state.handle.lock().map_err(|_| "failed to lock proxy state".to_string())?;
|
|
|
|
if let Some(existing) = handle.as_ref() {
|
|
return Ok(ProxyStartResult { port: existing.port, already_running: true });
|
|
}
|
|
|
|
let proxy_handle = yaak_proxy::start_proxy(port.unwrap_or(0))?;
|
|
let started_port = proxy_handle.port;
|
|
*handle = Some(proxy_handle);
|
|
|
|
Ok(ProxyStartResult { port: started_port, already_running: false })
|
|
}
|
|
|
|
#[tauri::command]
|
|
fn proxy_stop(state: State<'_, ProxyState>) -> Result<bool, String> {
|
|
let mut handle = state.handle.lock().map_err(|_| "failed to lock proxy state".to_string())?;
|
|
Ok(handle.take().is_some())
|
|
}
|
|
|
|
pub fn run() {
|
|
tauri::Builder::default()
|
|
.plugin(tauri_plugin_os::init())
|
|
.manage(ProxyState::default())
|
|
.invoke_handler(tauri::generate_handler![proxy_metadata, proxy_start, proxy_stop])
|
|
.build(tauri::generate_context!())
|
|
.expect("error while building yaak proxy tauri application")
|
|
.run(|app_handle, event| {
|
|
if let RunEvent::Ready = event {
|
|
let config = CreateWindowConfig {
|
|
url: "/",
|
|
label: "main",
|
|
title: "Yaak Proxy",
|
|
inner_size: Some((1000.0, 700.0)),
|
|
visible: true,
|
|
hide_titlebar: true,
|
|
..Default::default()
|
|
};
|
|
if let Err(e) = yaak_window::window::create_window(app_handle, config) {
|
|
error!("Failed to create proxy window: {e:?}");
|
|
}
|
|
}
|
|
});
|
|
}
|