11use anyhow:: Result ;
22use rand:: { Rng , distributions:: Alphanumeric } ;
3- use tauri:: { AppHandle , Emitter , State } ;
4- use tracing:: info;
3+ use tauri:: { AppHandle , Emitter , Manager , State } ;
4+ use tracing:: { debug , info} ;
55
66use crate :: {
7- DatabaseManager ,
7+ DatabaseManager , check_launch_then_exit ,
88 db:: {
99 launcher,
1010 launcher_resource:: { self , LauncherResource } ,
1111 } ,
1212 error:: OneClickLaunchError ,
13+ events:: {
14+ payloads:: { LauncherLaunched , LauncherLaunchedPayload } ,
15+ types:: { EventDispatcher , EventSystem } ,
16+ } ,
1317 open_using_default_program,
1418} ;
1519
20+ pub const LAUNCHER_LAUNCHED_EVENT : & str = "launcher_launched" ;
21+ pub const LAUNCHER_BASIC_INFO_UPDATED_EVENT : & str = "launcher_basic_info_updated" ;
22+
1623/// 创建新的启动器
1724#[ tauri:: command]
1825pub async fn craete_launcher (
@@ -22,7 +29,7 @@ pub async fn craete_launcher(
2229) -> Result < i64 , OneClickLaunchError > {
2330 let name = name. unwrap_or_else ( generate_default_launcher_name) ;
2431 let launcher_id = launcher:: create ( & db. pool , & name, None ) . await ?;
25- let _ = app. emit ( "launcher_basic_info_updated" , "" ) ;
32+ let _ = app. emit ( LAUNCHER_BASIC_INFO_UPDATED_EVENT , "" ) ;
2633 Ok ( launcher_id)
2734}
2835
@@ -50,7 +57,7 @@ pub async fn modify_launcher_name(
5057 name : String ,
5158) -> Result < ( ) , OneClickLaunchError > {
5259 launcher:: modify_launcher_name ( & db. pool , launcher_id, & name) . await ?;
53- let _ = app. emit ( "launcher_basic_info_updated" , "" ) ;
60+ let _ = app. emit ( LAUNCHER_BASIC_INFO_UPDATED_EVENT , "" ) ;
5461 Ok ( ( ) )
5562}
5663
@@ -79,7 +86,7 @@ pub async fn copy_launcher(
7986
8087 tx. commit ( ) . await ?;
8188
82- let _ = app. emit ( "launcher_basic_info_updated" , "" ) ;
89+ let _ = app. emit ( LAUNCHER_BASIC_INFO_UPDATED_EVENT , "" ) ;
8390
8491 Ok ( new_launcher_id)
8592}
@@ -147,7 +154,7 @@ pub async fn delete_launcher(
147154
148155 tx. commit ( ) . await ?;
149156
150- let _ = app. emit ( "launcher_basic_info_updated" , "" ) ;
157+ let _ = app. emit ( LAUNCHER_BASIC_INFO_UPDATED_EVENT , "" ) ;
151158
152159 Ok ( ( ) )
153160}
@@ -173,7 +180,7 @@ pub async fn modify_launcher_sort(
173180
174181 tx. commit ( ) . await ?;
175182
176- let _ = app. emit ( "launcher_basic_info_updated" , "" ) ;
183+ let _ = app. emit ( LAUNCHER_BASIC_INFO_UPDATED_EVENT , "" ) ;
177184
178185 Ok ( ( ) )
179186}
@@ -236,6 +243,13 @@ pub async fn launch(
236243
237244 launch_launcher_resources ( & app, & resources) ;
238245
246+ // app.emit(
247+ // LAUNCHER_LAUNCHED_EVENT,
248+ // LauncherLaunchedPayload { launcher_id },
249+ // )?;
250+
251+ EventDispatcher :: < LauncherLaunched > :: send_event ( & app, LauncherLaunchedPayload { launcher_id } ) ?;
252+
239253 Ok ( ( ) )
240254}
241255
@@ -249,3 +263,30 @@ pub fn launch_launcher_resources(app: &AppHandle, resources: &[LauncherResource]
249263 }
250264 }
251265}
266+
267+ pub fn de ( app : AppHandle ) {
268+ // 监听 `event-name`(无论其在什么窗口中触发)
269+ let app_handle = app; // 获取 AppHandle
270+
271+ EventSystem :: register_listener ( & app_handle. clone ( ) , LauncherLaunched , move |_payload| {
272+ let inner_app_handle = app_handle. clone ( ) ;
273+ tauri:: async_runtime:: spawn ( async move {
274+ let db = inner_app_handle. state :: < DatabaseManager > ( ) ;
275+ if let Ok ( _exit @ true ) = check_launch_then_exit ( & db. pool ) . await {
276+ debug ! ( "监听到启动器启动完成事件, 已设置启动后退出, 正在退出程序." ) ;
277+ inner_app_handle. exit ( 0 ) ;
278+ }
279+ } ) ;
280+ } ) ;
281+
282+ // let _id = app.listen(launcher_api::LAUNCHER_LAUNCHED_EVENT, move |_event| {
283+ // let inner_app_handle = app_handle.clone();
284+ // tauri::async_runtime::spawn(async move {
285+ // let db = inner_app_handle.state::<DatabaseManager>();
286+ // if let Ok(_exit @ true) = check_launch_then_exit(&db.pool).await {
287+ // debug!("监听到启动器启动完成事件, 已设置启动后退出, 正在退出程序.");
288+ // inner_app_handle.exit(0);
289+ // }
290+ // });
291+ // });
292+ }
0 commit comments