diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 5a8fc865..37dc5492 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -3,16 +3,24 @@ use tauri::Manager; -// 放一个常数,用来判断是否注册deep link -static mut DEEP_LINK_REGISTERED: bool = false; +// 放一个常数,用来判断应用是否初始化 +static mut APP_INITIALIZED: bool = false; #[tauri::command] -async fn register_deep_link(app_handle: tauri::AppHandle) { +async fn init_app(app_handle: tauri::AppHandle) { unsafe { - if DEEP_LINK_REGISTERED { + if APP_INITIALIZED { return; } } + app_handle.emit_all("initApp", ()).unwrap(); + unsafe { + APP_INITIALIZED = true; + } +} + +#[tauri::command] +async fn register_deep_link(app_handle: tauri::AppHandle) { tauri_plugin_deep_link::register( "teyvatguide", move |request| { @@ -21,9 +29,6 @@ async fn register_deep_link(app_handle: tauri::AppHandle) { }, ) .unwrap(); - unsafe { - DEEP_LINK_REGISTERED = true; - } } fn main() { @@ -48,7 +53,7 @@ fn main() { } }) .plugin(tauri_plugin_sql::Builder::default().build()) - .invoke_handler(tauri::generate_handler![register_deep_link]) + .invoke_handler(tauri::generate_handler![register_deep_link, init_app]) .setup(|_app| { let _window = _app.get_window("TeyvatGuide").unwrap(); #[cfg(debug_assertions)] // only include this code on debug builds diff --git a/src/App.vue b/src/App.vue index 45563572..24075b0b 100644 --- a/src/App.vue +++ b/src/App.vue @@ -37,12 +37,9 @@ onBeforeMount(async () => { isMain.value = win.label === "TeyvatGuide"; if (isMain.value) { const title = "Teyvat Guide v" + (await app.getVersion()) + " Beta"; - await tauri.invoke("register_deep_link"); - await getDeepLink(); await win.setTitle(title); - await emojiLoad(); - await checkAppLoad(); - await checkUserLoad(); + await listenOnInit(); + await tauri.invoke("init_app"); } }); @@ -63,6 +60,17 @@ async function listenOnTheme(): Promise { }); } +// 启动后只执行一次的监听 +async function listenOnInit(): Promise { + await event.listen("initApp", async () => { + await tauri.invoke("register_deep_link"); + await getDeepLink(); + await emojiLoad(); + await checkAppLoad(); + await checkUserLoad(); + }); +} + async function emojiLoad(): Promise { const res = await getEmojis(); if ("retcode" in res) {