diff --git a/src-tauri/src/core/mod.rs b/src-tauri/src/core/mod.rs index 10548bd..ac01442 100644 --- a/src-tauri/src/core/mod.rs +++ b/src-tauri/src/core/mod.rs @@ -93,7 +93,7 @@ impl Core { if silent_start.unwrap_or(false) { let window = self.window.lock(); window.as_ref().map(|win| { - win.close().unwrap(); + win.hide().unwrap(); }); } @@ -382,7 +382,13 @@ impl Core { result.error.map(|err| log::error!("{err}")); }); - window.emit("script-handler", payload).unwrap(); + let verge = self.verge.lock(); + let silent_start = verge.enable_silent_start.clone(); + if silent_start.unwrap_or(false) { + window.emit("script-handler-close", payload).unwrap(); + } else { + window.emit("script-handler", payload).unwrap(); + } Ok(()) } diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 93401ea..19bbc41 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -182,7 +182,7 @@ fn main() -> std::io::Result<()> { .build(tauri::generate_context!()) .expect("error while running tauri application") .run(|app_handle, e| match e { - tauri::RunEvent::ExitRequested { .. } => { + tauri::RunEvent::ExitRequested { api, .. } => { api.prevent_exit(); } tauri::RunEvent::Exit => { diff --git a/src/services/enhance.ts b/src/services/enhance.ts index a1ebf5a..803ee2e 100644 --- a/src/services/enhance.ts +++ b/src/services/enhance.ts @@ -1,4 +1,5 @@ -import { emit, listen } from "@tauri-apps/api/event"; +import { emit, listen, Event } from "@tauri-apps/api/event"; +import { appWindow } from "@tauri-apps/api/window"; import { CmdType } from "./types"; import ignoreCase from "../utils/ignore-case"; @@ -124,21 +125,29 @@ class Enhance { return this.resultMap.get(uid); } + async enhanceHandler(event: Event) { + const payload = event.payload as CmdType.EnhancedPayload; + + const result = await this.runner(payload).catch((err: any) => ({ + data: null, + status: "error", + error: err.message, + })); + + emit(payload.callback, JSON.stringify(result)).catch(console.error); + } // setup the handler setup() { if (this.isSetup) return; this.isSetup = true; listen("script-handler", async (event) => { - const payload = event.payload as CmdType.EnhancedPayload; + await this.enhanceHandler(event); + }); - const result = await this.runner(payload).catch((err: any) => ({ - data: null, - status: "error", - error: err.message, - })); - - emit(payload.callback, JSON.stringify(result)).catch(console.error); + listen("script-handler-close", async (event) => { + await this.enhanceHandler(event); + appWindow.close(); }); }