@@ -16,6 +16,7 @@ import {
16
16
} from './core/mihomoApi'
17
17
18
18
export let mainWindow : BrowserWindow | null = null
19
+ export let destroyTimer : NodeJS . Timeout | null = null
19
20
20
21
const gotTheLock = app . requestSingleInstanceLock ( )
21
22
@@ -25,26 +26,43 @@ if (!gotTheLock) {
25
26
const initPromise = init ( )
26
27
27
28
app . on ( 'second-instance' , async ( _event , commandline ) => {
28
- mainWindow ?. show ( )
29
- mainWindow ?. focusOnWebView ( )
29
+ if ( ! mainWindow ) {
30
+ if ( destroyTimer ) {
31
+ clearTimeout ( destroyTimer )
32
+ }
33
+ createWindow ( true )
34
+ } else {
35
+ mainWindow ?. show ( )
36
+ mainWindow ?. focusOnWebView ( )
37
+ }
38
+
30
39
const url = commandline . pop ( )
31
40
if ( url ) {
32
41
await handleDeepLink ( url )
33
42
}
34
43
} )
35
44
36
45
app . on ( 'open-url' , async ( _event , url ) => {
37
- mainWindow ?. show ( )
38
- mainWindow ?. focusOnWebView ( )
46
+ if ( ! mainWindow ) {
47
+ if ( destroyTimer ) {
48
+ clearTimeout ( destroyTimer )
49
+ }
50
+ createWindow ( true )
51
+ } else {
52
+ mainWindow ?. show ( )
53
+ mainWindow ?. focusOnWebView ( )
54
+ }
55
+
39
56
await handleDeepLink ( url )
40
57
} )
41
58
// Quit when all windows are closed, except on macOS. There, it's common
42
59
// for applications and their menu bar to stay active until the user quits
43
60
// explicitly with Cmd + Q.
44
- app . on ( 'window-all-closed' , ( ) => {
45
- if ( process . platform !== 'darwin' ) {
46
- app . quit ( )
47
- }
61
+ app . on ( 'window-all-closed' , ( e ) => {
62
+ e . preventDefault ( )
63
+ // if (process.platform !== 'darwin') {
64
+ // app.quit()
65
+ // }
48
66
} )
49
67
50
68
app . on ( 'before-quit' , ( ) => {
@@ -78,7 +96,12 @@ app.whenReady().then(async () => {
78
96
app . on ( 'activate' , function ( ) {
79
97
// On macOS it's common to re-create a window in the app when the
80
98
// dock icon is clicked and there are no other windows open.
81
- if ( BrowserWindow . getAllWindows ( ) . length === 0 ) createWindow ( )
99
+ if ( BrowserWindow . getAllWindows ( ) . length === 0 ) {
100
+ if ( destroyTimer ) {
101
+ clearTimeout ( destroyTimer )
102
+ }
103
+ createWindow ( true )
104
+ }
82
105
} )
83
106
} )
84
107
@@ -106,7 +129,7 @@ async function handleDeepLink(url: string): Promise<void> {
106
129
}
107
130
}
108
131
109
- function createWindow ( ) : void {
132
+ export function createWindow ( show = false ) : void {
110
133
Menu . setApplicationMenu ( null )
111
134
// Create the browser window.
112
135
mainWindow = new BrowserWindow ( {
@@ -125,7 +148,7 @@ function createWindow(): void {
125
148
} )
126
149
mainWindow . on ( 'ready-to-show' , async ( ) => {
127
150
const { silentStart } = await getAppConfig ( )
128
- if ( ! silentStart ) {
151
+ if ( ! silentStart || show ) {
129
152
mainWindow ?. show ( )
130
153
mainWindow ?. focusOnWebView ( )
131
154
}
@@ -145,7 +168,14 @@ function createWindow(): void {
145
168
stopMihomoMemory ( )
146
169
event . preventDefault ( )
147
170
mainWindow ?. hide ( )
148
- mainWindow ?. webContents . reload ( )
171
+ if ( destroyTimer ) {
172
+ clearTimeout ( destroyTimer )
173
+ }
174
+ destroyTimer = setTimeout ( ( ) => {
175
+ mainWindow ?. destroy ( )
176
+ mainWindow = null
177
+ } , 300000 )
178
+ // mainWindow?.webContents.reload()
149
179
} )
150
180
151
181
mainWindow . webContents . setWindowOpenHandler ( ( details ) => {
0 commit comments