@@ -12,8 +12,6 @@ impl ActivePids {
1212 }
1313}
1414
15- // ── Windows-only process helpers (no extra crates needed) ─────────────────
16-
1715#[ cfg( windows) ]
1816use std:: os:: windows:: process:: CommandExt ;
1917#[ cfg( windows) ]
@@ -50,12 +48,9 @@ fn is_pid_running(pid: u32) -> bool {
5048 . output ( )
5149 . map ( |o| String :: from_utf8_lossy ( & o. stdout ) . to_string ( ) )
5250 . unwrap_or_default ( ) ;
53- // Output has a data row if running; "INFO: No tasks..." if not
5451 out. contains ( '"' )
5552}
5653
57- // ── Session-end helper: update DB + emit + restore window ─────────────────
58-
5954fn finish_session ( app : & AppHandle , game_id : & str , elapsed_mins : i64 ) {
6055 let now = Utc :: now ( ) . to_rfc3339 ( ) ;
6156 {
@@ -69,18 +64,14 @@ fn finish_session(app: &AppHandle, game_id: &str, elapsed_mins: i64) {
6964 }
7065 }
7166 let _ = app. emit ( "game-session-ended" , game_id) ;
72- // Restore window after game exits
7367 if let Some ( win) = app. get_webview_window ( "main" ) {
7468 let _ = win. unminimize ( ) ;
7569 let _ = win. set_focus ( ) ;
7670 }
7771}
7872
79- // ── Commands ──────────────────────────────────────────────────────────────
80-
8173#[ tauri:: command]
8274pub fn launch_game ( app : AppHandle , state : State < DbState > , id : String ) -> Result < ( ) , String > {
83- // Phase 1: gather data, release mutex
8475 let ( exe_path, minimize) = {
8576 let conn = state. 0 . lock ( ) . map_err ( |e| e. to_string ( ) ) ?;
8677 let game = queries:: get_game_by_id ( & conn, & id)
@@ -92,12 +83,10 @@ pub fn launch_game(app: AppHandle, state: State<DbState>, id: String) -> Result<
9283 ( exe, min)
9384 } ;
9485
95- // Phase 2: spawn process
9686 let child = std:: process:: Command :: new ( & exe_path)
9787 . spawn ( )
9888 . map_err ( |e| format ! ( "Failed to launch: {}" , e) ) ?;
9989
100- // Record last_played immediately
10190 let now = Utc :: now ( ) . to_rfc3339 ( ) ;
10291 {
10392 let conn = state. 0 . lock ( ) . map_err ( |e| e. to_string ( ) ) ?;
@@ -110,16 +99,13 @@ pub fn launch_game(app: AppHandle, state: State<DbState>, id: String) -> Result<
11099 if minimize {
111100 let app_min = app. clone ( ) ;
112101 std:: thread:: spawn ( move || {
113- // Short delay so the game window can appear and grab focus first,
114- // preventing Windows from immediately re-focusing ZGameLib
115102 std:: thread:: sleep ( std:: time:: Duration :: from_millis ( 400 ) ) ;
116103 if let Some ( win) = app_min. get_webview_window ( "main" ) {
117104 let _ = win. minimize ( ) ;
118105 }
119106 } ) ;
120107 }
121108
122- // Phase 3: background thread — wait for exit, record playtime
123109 let app_clone = app. clone ( ) ;
124110 let game_id = id. clone ( ) ;
125111 let start = std:: time:: Instant :: now ( ) ;
@@ -142,7 +128,6 @@ pub fn launch_steam_game(
142128 app_id : String ,
143129 game_id : String ,
144130) -> Result < ( ) , String > {
145- // Get exe_path for process monitoring + minimize setting
146131 let ( exe_path, minimize) = {
147132 let conn = state. 0 . lock ( ) . map_err ( |e| e. to_string ( ) ) ?;
148133 let exe = queries:: get_game_by_id ( & conn, & game_id)
@@ -153,11 +138,9 @@ pub fn launch_steam_game(
153138 ( exe, min)
154139 } ;
155140
156- // Launch via Steam URI
157141 open:: that ( format ! ( "steam://run/{}" , app_id) )
158142 . map_err ( |e| format ! ( "Failed to launch Steam game: {}" , e) ) ?;
159143
160- // Record last_played immediately
161144 let now = Utc :: now ( ) . to_rfc3339 ( ) ;
162145 {
163146 let conn = state. 0 . lock ( ) . map_err ( |e| e. to_string ( ) ) ?;
@@ -177,7 +160,6 @@ pub fn launch_steam_game(
177160 } ) ;
178161 }
179162
180- // Background: find game process by exe name, wait for exit, record playtime
181163 if let Some ( exe) = exe_path {
182164 let app_clone = app. clone ( ) ;
183165 let gid = game_id. clone ( ) ;
@@ -195,7 +177,6 @@ pub fn launch_steam_game(
195177
196178 let start = std:: time:: Instant :: now ( ) ;
197179
198- // Wait up to 120 s for process to appear (Steam can be slow to launch)
199180 let mut pid: Option < u32 > = None ;
200181 for _ in 0 ..120 {
201182 if let Some ( p) = find_pid_by_exe_name ( & exe_name) {
@@ -212,7 +193,6 @@ pub fn launch_steam_game(
212193 return ;
213194 }
214195 }
215- // Poll every 5 s until the process disappears
216196 loop {
217197 std:: thread:: sleep ( std:: time:: Duration :: from_secs ( 5 ) ) ;
218198 if !is_pid_running ( pid) { break ; }
@@ -236,7 +216,6 @@ pub fn launch_epic_game(
236216 app_name : String ,
237217 game_id : String ,
238218) -> Result < ( ) , String > {
239- // Get exe_path for process monitoring + minimize setting
240219 let ( exe_path, minimize) = {
241220 let conn = state. 0 . lock ( ) . map_err ( |e| e. to_string ( ) ) ?;
242221 let exe = queries:: get_game_by_id ( & conn, & game_id)
@@ -247,15 +226,13 @@ pub fn launch_epic_game(
247226 ( exe, min)
248227 } ;
249228
250- // Launch via Epic Games Launcher URI
251229 let uri = format ! (
252230 "com.epicgames.launcher://apps/{}?action=launch&silent=true" ,
253231 app_name
254232 ) ;
255233 open:: that ( & uri)
256234 . map_err ( |e| format ! ( "Failed to launch Epic game: {}" , e) ) ?;
257235
258- // Record last_played immediately
259236 let now = Utc :: now ( ) . to_rfc3339 ( ) ;
260237 {
261238 let conn = state. 0 . lock ( ) . map_err ( |e| e. to_string ( ) ) ?;
@@ -275,7 +252,6 @@ pub fn launch_epic_game(
275252 } ) ;
276253 }
277254
278- // Background: find game process by exe name, wait for exit, record playtime
279255 if let Some ( exe) = exe_path {
280256 let app_clone = app. clone ( ) ;
281257 let gid = game_id. clone ( ) ;
@@ -293,7 +269,6 @@ pub fn launch_epic_game(
293269
294270 let start = std:: time:: Instant :: now ( ) ;
295271
296- // Wait up to 180 s for process to appear (Epic launcher can be slow)
297272 let mut pid: Option < u32 > = None ;
298273 for _ in 0 ..180 {
299274 if let Some ( p) = find_pid_by_exe_name ( & exe_name) {
@@ -310,7 +285,6 @@ pub fn launch_epic_game(
310285 return ;
311286 }
312287 }
313- // Poll every 5 s until the process disappears
314288 loop {
315289 std:: thread:: sleep ( std:: time:: Duration :: from_secs ( 5 ) ) ;
316290 if !is_pid_running ( pid) { break ; }
0 commit comments