tauri2-guide/moxie-app/src-tauri/src/lib.rs
2026-05-30 10:44:44 -07:00

54 lines
1.8 KiB
Rust

use tauri::Manager;
use tauri::{AppHandle, WebviewUrl, WebviewWindowBuilder};
// 1. Declare the new server module file 👇
mod server;
#[tauri::command]
async fn greet(app_handle: AppHandle, name: String) -> Result<String, String> {
let window_label = "remote-target-window";
let target_url = match name.parse() {
Ok(url) => WebviewUrl::External(url),
Err(_) => {
return Err(format!("Error: '{}' is not a valid URL format.", name));
}
};
if let Some(existing_window) = app_handle.get_webview_window(window_label) {
let _ = existing_window.set_focus();
return Ok(format!("Focused existing window for {}", name));
}
let app_clone = app_handle.clone();
let url_clone = target_url.clone();
let script_to_inject = include_str!("../extWebview.js");
app_handle.run_on_main_thread(move || {
let _new_window = WebviewWindowBuilder::new(&app_clone, window_label, url_clone)
.title("Remote Session")
.inner_size(1024.0, 768.0)
.resizable(true)
.focused(true)
.initialization_script(script_to_inject)
.build();
}).map_err(|e| format!("Failed to dispatch to main thread: {}", e))?;
Ok(format!("Dispatched window spawn for: {}", name))
}
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
tauri::Builder::default()
.plugin(tauri_plugin_opener::init())
.setup(|_app| {
// 2. Call the server function using module referencing 👇
tauri::async_runtime::spawn(server::start_websocket_server());
Ok(())
})
.invoke_handler(tauri::generate_handler![greet])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}