[ui] change framework from Tauri Leptos to Tauri Vanilla (Typescript) and React
This commit is contained in:
134
crates/lanspread-tauri-deno-ts/src-tauri/src/lib.rs
Normal file
134
crates/lanspread-tauri-deno-ts/src-tauri/src/lib.rs
Normal file
@ -0,0 +1,134 @@
|
||||
use std::net::SocketAddr;
|
||||
|
||||
use lanspread_client::{ClientCommand, ClientEvent};
|
||||
use lanspread_mdns::{discover_service, LANSPREAD_INSTANCE_NAME, LANSPREAD_SERVICE_TYPE};
|
||||
use tauri::{AppHandle, Emitter as _, Manager};
|
||||
use tokio::sync::{mpsc::UnboundedSender, Mutex};
|
||||
|
||||
// Learn more about Tauri commands at https://tauri.app/develop/calling-rust/
|
||||
|
||||
struct LanSpreadState {
|
||||
server_addr: Mutex<Option<SocketAddr>>,
|
||||
client_ctrl: UnboundedSender<ClientCommand>,
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn request_games(state: tauri::State<LanSpreadState>) {
|
||||
log::debug!("request_games");
|
||||
|
||||
if let Err(e) = state.inner().client_ctrl.send(ClientCommand::ListGames) {
|
||||
log::error!("Failed to send message to client: {e:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn run_game_backend(id: String, state: tauri::State<LanSpreadState>) -> String {
|
||||
log::error!("Running game with id {id}");
|
||||
|
||||
// let result = Command::new(r#"C:\Users\ddidderr\scoop\apps\mpv\0.39.0\mpv.exe"#).spawn();
|
||||
|
||||
if let Err(e) = state.inner().client_ctrl.send(ClientCommand::GetGame(id)) {
|
||||
log::error!("Failed to send message to client: {e:?}");
|
||||
}
|
||||
|
||||
"TODO".to_string()
|
||||
|
||||
// if result.is_ok() {
|
||||
// "Ok".to_string()
|
||||
// } else {
|
||||
// "Failed to run game".to_string()
|
||||
// }
|
||||
}
|
||||
|
||||
async fn find_server(app: AppHandle) {
|
||||
log::info!("Looking for server...");
|
||||
|
||||
loop {
|
||||
match discover_service(LANSPREAD_SERVICE_TYPE, Some(LANSPREAD_INSTANCE_NAME)) {
|
||||
Ok(server_addr) => {
|
||||
log::info!("Found server at {server_addr}");
|
||||
let state: tauri::State<LanSpreadState> = app.state();
|
||||
{
|
||||
// mutex scope
|
||||
let mut addr = state.server_addr.lock().await;
|
||||
*addr = Some(server_addr);
|
||||
}
|
||||
state
|
||||
.client_ctrl
|
||||
.send(ClientCommand::ServerAddr(server_addr))
|
||||
.unwrap();
|
||||
request_games(state);
|
||||
break;
|
||||
}
|
||||
Err(e) => {
|
||||
log::warn!("Failed to find server: {e} - retrying...");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
||||
pub fn run() {
|
||||
let tauri_logger_builder = tauri_plugin_log::Builder::new()
|
||||
.clear_targets()
|
||||
.target(tauri_plugin_log::Target::new(
|
||||
tauri_plugin_log::TargetKind::Stdout,
|
||||
))
|
||||
.level(log::LevelFilter::Info)
|
||||
.level_for("lanspread_client", log::LevelFilter::Trace)
|
||||
.level_for("lanspread_tauri_leptos_lib", log::LevelFilter::Debug)
|
||||
.level_for("mdns_sd::service_daemon", log::LevelFilter::Off);
|
||||
|
||||
// channel to pass commands to the client
|
||||
let (tx_client_control, rx_client_control) =
|
||||
tokio::sync::mpsc::unbounded_channel::<ClientCommand>();
|
||||
|
||||
// channel to receive events from the client
|
||||
let (tx_client_event, mut rx_client_event) =
|
||||
tokio::sync::mpsc::unbounded_channel::<ClientEvent>();
|
||||
|
||||
let lanspread_state = LanSpreadState {
|
||||
server_addr: Mutex::new(None),
|
||||
client_ctrl: tx_client_control,
|
||||
};
|
||||
|
||||
tauri::Builder::default()
|
||||
.plugin(tauri_logger_builder.build())
|
||||
.plugin(tauri_plugin_shell::init())
|
||||
.invoke_handler(tauri::generate_handler![run_game_backend, request_games])
|
||||
.manage(lanspread_state)
|
||||
.setup(|app| {
|
||||
let app_handle = app.handle().clone();
|
||||
// discover server
|
||||
tauri::async_runtime::spawn(async move { find_server(app_handle).await });
|
||||
tauri::async_runtime::spawn(async move {
|
||||
lanspread_client::run(rx_client_control, tx_client_event).await
|
||||
});
|
||||
|
||||
let app_handle = app.handle().clone();
|
||||
tauri::async_runtime::spawn(async move {
|
||||
while let Some(event) = rx_client_event.recv().await {
|
||||
match event {
|
||||
ClientEvent::ListGames(games) => {
|
||||
log::debug!("Received client event: ListGames");
|
||||
|
||||
for game in &games {
|
||||
log::trace!("client event ListGames iter: {game:?}");
|
||||
}
|
||||
|
||||
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
|
||||
if let Err(e) = app_handle.emit("games-list-updated", Some(games)) {
|
||||
log::error!("Failed to emit games-list-updated event: {e}");
|
||||
} else {
|
||||
log::info!("Emitted games-list-updated event");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Ok(())
|
||||
})
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
}
|
Reference in New Issue
Block a user