2
2
#![ cfg_attr( not( debug_assertions) , windows_subsystem = "windows" ) ]
3
3
4
4
use tauri:: api:: dialog:: message;
5
- use tauri:: { generate_context, generate_handler} ;
5
+ use tauri:: { generate_context, generate_handler, WindowEvent } ;
6
6
7
7
mod page;
8
8
mod tests;
@@ -12,6 +12,8 @@ mod splashscreen;
12
12
13
13
use std:: process:: Command ;
14
14
use tauri:: Manager ;
15
+ use std:: sync:: Arc ;
16
+ use std:: sync:: Mutex ;
15
17
use splashscreen:: close_init;
16
18
use page:: file:: * ;
17
19
use page:: terimal:: { pwd_tauri, whoami_tauri, run_command} ;
@@ -29,24 +31,31 @@ fn main() {
29
31
Command :: new ( "../target/release/tiks" ) . status ( ) . expect ( "Failed run" ) ;
30
32
}
31
33
32
- let _python_server = Command :: new ( "python" )
34
+ let python_server = Command :: new ( "python" )
33
35
. arg ( "../py/server.py" )
34
36
. spawn ( )
35
37
. expect ( "Failed to start Python server" ) ;
36
38
39
+ let python_server = Arc :: new ( Mutex :: new ( python_server) ) ;
37
40
38
41
tauri:: Builder :: default ( )
39
42
. setup ( {
40
43
move |app| {
41
44
let main_window = app. get_window ( "main" ) . unwrap ( ) ;
42
45
43
46
// 添加更新检查逻辑
47
+ let main_window_clone = main_window. clone ( ) ;
48
+ let main_window_clone_2 = main_window_clone. clone ( ) ;
49
+
44
50
let app_handle = app. handle ( ) . clone ( ) ;
51
+
52
+ let app_handle_2 = app_handle. clone ( ) ;
53
+
45
54
tauri:: async_runtime:: spawn ( async move {
46
55
match app_handle. updater ( ) . check ( ) . await {
47
56
Ok ( update) => {
48
57
if update. is_update_available ( ) {
49
- message ( Some ( & main_window ) , "Update Available" , "A new version is available. It will be installed when you restart the application." ) ;
58
+ message ( Some ( & main_window_clone ) , "Update Available" , "A new version is available. It will be installed when you restart the application." ) ;
50
59
update. download_and_install ( ) . await . unwrap ( ) ;
51
60
}
52
61
} ,
@@ -55,6 +64,35 @@ fn main() {
55
64
}
56
65
}
57
66
} ) ;
67
+
68
+
69
+ main_window. on_window_event ( {
70
+ move |event| {
71
+ let app_handle = app_handle_2. clone ( ) ;
72
+ if let WindowEvent :: CloseRequested { api, .. } = event {
73
+ let mut server = python_server. lock ( ) . unwrap ( ) ;
74
+ server. kill ( ) . expect ( "Failed to kill Python server" ) ;
75
+ api. prevent_close ( ) ;
76
+
77
+ // 提示用户是否要更新并关闭应用
78
+ tauri:: api:: dialog:: ask (
79
+ Some ( & main_window_clone_2) ,
80
+ "Confirm Exit" ,
81
+ "Do you want to update and close the application?" ,
82
+ move |response| {
83
+ if response {
84
+ tauri:: async_runtime:: spawn ( async move {
85
+ app_handle. updater ( ) . should_install ( |_current, _latest| true ) ;
86
+ } ) ;
87
+ } else {
88
+ // 仅关闭应用而不更新
89
+ app_handle. exit ( 0 ) ;
90
+ }
91
+ } ,
92
+ ) ;
93
+ }
94
+ }
95
+ } ) ;
58
96
Ok ( ( ) )
59
97
}
60
98
} )
0 commit comments