From 4fe8f913713bb54697047ad87c0beefb20a98113 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Fri, 3 Apr 2026 17:09:21 +0200 Subject: [PATCH] Stateful launch --- src-tauri/src/commands.rs | 27 ++++++++++++++++++++------ src/components/PlaySection.tsx | 35 +++++++++++++++------------------- 2 files changed, 36 insertions(+), 26 deletions(-) diff --git a/src-tauri/src/commands.rs b/src-tauri/src/commands.rs index 9c6aab7..3fc9f18 100644 --- a/src-tauri/src/commands.rs +++ b/src-tauri/src/commands.rs @@ -1,4 +1,4 @@ -use tauri::State; +use tauri::{AppHandle, Emitter, State}; use crate::{GameVersion, LauncherSettings, Mod, SharedState}; @@ -49,11 +49,26 @@ pub fn update_settings(state: State, new_settings: LauncherSettings } #[tauri::command] -pub fn launch_game(version_id: String) -> Result { - println!("Launching Saturn Client with version {}", version_id); +pub fn launch_game(app: AppHandle, version_id: String) -> Result<(), String> { + std::thread::spawn(move || { + app.emit("is-launching", "true").unwrap(); + app.emit("launch-status", "Preparing...").unwrap(); - // Replace this with real launcher logic - std::thread::sleep(std::time::Duration::from_secs(2)); + std::thread::sleep(std::time::Duration::from_millis(500)); + app.emit("launch-status", "Verifying assets...").unwrap(); - Ok("Game launched successfully!".into()) + std::thread::sleep(std::time::Duration::from_millis(700)); + app.emit("launch-status", "Applying Saturn optimizations...") + .unwrap(); + + std::thread::sleep(std::time::Duration::from_millis(700)); + app.emit("launch-status", "Starting game...").unwrap(); + + std::thread::sleep(std::time::Duration::from_millis(1000)); + app.emit("launch-status", "Game launched!").unwrap(); + + app.emit("is-launching", "false").unwrap(); + }); + + Ok(()) } diff --git a/src/components/PlaySection.tsx b/src/components/PlaySection.tsx index aeb7221..99cc575 100644 --- a/src/components/PlaySection.tsx +++ b/src/components/PlaySection.tsx @@ -1,7 +1,8 @@ -import React, { useState } from "react"; +import React, { useEffect, useState } from "react"; import { Play, Loader2 } from "lucide-react"; import { launchGame, type GameVersion } from "../services/LauncherService"; import Bg from "@/assets/bg.png"; +import { listen } from "@tauri-apps/api/event"; interface PlaySectionProps { version: GameVersion; @@ -11,26 +12,20 @@ const PlaySection: React.FC = ({ version }) => { const [isLaunching, setIsLaunching] = useState(false); const [launchStatus, setLaunchStatus] = useState(""); - const handleLaunch = async () => { - setIsLaunching(true); - setLaunchStatus("Preparing..."); + useEffect(() => { + const unlistenLaunchStatus = listen("launch-status", (event) => { + setLaunchStatus(event.payload); + }); - // Simulate launch steps - setTimeout(() => setLaunchStatus("Verifying assets..."), 500); - setTimeout(() => setLaunchStatus("Applying Saturn optimizations..."), 1200); + const unlistenIsLaunching = listen("is-launching", (event) => { + setIsLaunching(event.payload === "true"); + }); - const result = await launchGame(version.id); - - if (result.success) { - setLaunchStatus("Game running"); - // In a real app, we might minimize or close the launcher here - } - - setTimeout(() => { - setIsLaunching(false); - setLaunchStatus(""); - }, 3000); - }; + return () => { + unlistenLaunchStatus.then((f) => f()); + unlistenIsLaunching.then((f) => f()); + }; + }, []); return (
@@ -47,7 +42,7 @@ const PlaySection: React.FC = ({ version }) => {