changed electron to tauri v2

This commit is contained in:
2026-08-20 20:59:46 -07:00
parent a1d61a23ff
commit 4754801fe3
72 changed files with 11783 additions and 5228 deletions

131
src-tauri/src/main.rs Normal file
View File

@@ -0,0 +1,131 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
use std::path::PathBuf;
use std::process::{Child, Command, Stdio};
use std::sync::Mutex;
use tauri::{Manager, State, WebviewUrl, WebviewWindowBuilder};
struct NodeServices(Mutex<Option<Child>>);
#[derive(serde::Deserialize)]
struct BackendMessage {
event: String,
data: serde_json::Value,
}
fn get_project_root() -> PathBuf {
let mut dir = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
if dir.ends_with("src-tauri") {
dir.pop();
}
dir
}
const LINK_HANDLER_SCRIPT: &str = r#"
window.open = function(url) {
if (!url) return null;
if (url.startsWith('http://localhost') || url.startsWith('/') || url.startsWith(window.location.origin)) {
window.location.href = url;
} else if (window.__TAURI__ && window.__TAURI__.opener) {
window.__TAURI__.opener.openUrl(url);
} else if (window.__TAURI_INTERNALS__) {
window.__TAURI_INTERNALS__.invoke('plugin:opener|open_url', { rule: { url: url } });
}
return null;
};
document.addEventListener('click', (e) => {
const a = e.target.closest('a');
if (!a || !a.href) return;
const isLocal = a.href.startsWith('http://localhost') || a.href.startsWith('tauri://') || a.href.startsWith(window.location.origin) || a.href.startsWith('/');
if (a.target === '_blank') {
e.preventDefault();
if (isLocal) {
window.location.href = a.href;
} else if (window.__TAURI__ && window.__TAURI__.opener) {
window.__TAURI__.opener.openUrl(a.href);
} else if (window.__TAURI_INTERNALS__) {
window.__TAURI_INTERNALS__.invoke('plugin:opener|open_url', { rule: { url: a.href } });
}
}
}, true);
"#;
fn main() {
tauri::Builder::default()
.plugin(tauri_plugin_shell::init())
.plugin(tauri_plugin_process::init())
.plugin(tauri_plugin_opener::init())
.manage(NodeServices(Mutex::new(None)))
.setup(|app| {
let app_data = app.path().app_data_dir().unwrap_or_default();
let app_data_str = app_data.to_string_lossy().to_string();
let window = WebviewWindowBuilder::new(app, "main", WebviewUrl::App("index.html".into()))
.title("Vyla Home")
.inner_size(1280.0, 800.0)
.decorations(true)
.resizable(true)
.fullscreen(false)
.initialization_script(LINK_HANDLER_SCRIPT)
.build()?;
let root_dir = get_project_root();
let main_js = root_dir.join("main.js");
let mut cmd = Command::new("node");
cmd.arg(&main_js)
.current_dir(&root_dir)
.env("APPDATA_PATH", &app_data_str)
.stdout(Stdio::piped())
.stderr(Stdio::inherit());
#[cfg(windows)]
{
use std::os::windows::process::CommandExt;
const CREATE_NO_WINDOW: u32 = 0x08000000;
cmd.creation_flags(CREATE_NO_WINDOW);
}
if let Ok(mut child) = cmd.spawn() {
if let Some(stdout) = child.stdout.take() {
let w = window.clone();
std::thread::spawn(move || {
use std::io::{BufRead, BufReader};
let reader = BufReader::new(stdout);
for line in reader.lines().flatten() {
println!("{}", line);
if let Ok(msg) = serde_json::from_str::<BackendMessage>(&line) {
if msg.event == "ready" {
if let Some(url) = msg.data.get("url").and_then(|u| u.as_str()) {
let _ = w.eval(&format!("window.location.href = '{}';", url));
}
}
}
}
});
}
let state: State<NodeServices> = app.state();
if let Ok(mut lock) = state.0.lock() {
*lock = Some(child);
};
}
Ok(())
})
.on_window_event(|window, event| {
if let tauri::WindowEvent::Destroyed = event {
let state: State<NodeServices> = window.state();
if let Ok(mut lock) = state.0.lock() {
if let Some(mut child) = lock.take() {
let _ = child.kill();
}
};
}
})
.run(tauri::generate_context!())
.expect("error while running tauri application");
}