From faa1832c1efa5ed66929d907afe9111517b6e175 Mon Sep 17 00:00:00 2001 From: BTMuli Date: Mon, 9 Oct 2023 17:52:28 +0800 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20=E5=90=AF=E5=8A=A8?= =?UTF-8?q?=E5=90=8E=E5=8F=AA=E6=89=A7=E8=A1=8C=E4=B8=80=E6=AC=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src-tauri/src/main.rs | 21 +++++++++++++-------- src/App.vue | 18 +++++++++++++----- 2 files changed, 26 insertions(+), 13 deletions(-) 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) {